Skip to content

Commit

Permalink
use defer
Browse files Browse the repository at this point in the history
 * fix to use defer if possible
 * replace $().ready(...) with $(function(){...}) and use jquery layzyload hack
  • Loading branch information
wkpark committed Nov 2, 2022
1 parent 7f67c4f commit d3d0315
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 22 deletions.
7 changes: 5 additions & 2 deletions local/ac.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function search(e, ui) {
location.href = url_prefix + _qp + encodeURIComponent(ui.item.value).replace(/%2F/g,'/');
}

$().ready(function() {
function attachAutocomplete() {
if (init_inputform('go','ac_goto'))
$('#ac_goto').autocomplete({
source: titleindex, delay: 100,
Expand All @@ -59,7 +59,10 @@ $().ready(function() {
location.href = url_prefix + _qp + encodeURIComponent(ui.item.value).replace(/%2F/g,'/');
}
});
});
}

if (window.addEventListener) window.addEventListener("load",attachAutocomplete,false);
else if (window.attachEvent) window.attachEvent("onload",attachAutocomplete);
})();

// vim:et:sts=4:sw=4:
1 change: 1 addition & 0 deletions local/js/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ readLanguage = function(domain) {
var js = document.createElement('script');
js.type = 'text/javascript';
js.src = _url_prefix + '/local/js/locale/' + lang.substr(0,2) + '/' + domain + '.js';
js.defer = true;
js.onreadystatechange = function () {
if (this.readyState == 'complete') {
loadLanguage();
Expand Down
2 changes: 1 addition & 1 deletion local/mobile.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ function init_images() {
}

// onload
if (jQuery)
if (typeof jQuery != 'undefined')
$(document).ready(function() { init_images(); });
else
if (window.addEventListener)
Expand Down
12 changes: 7 additions & 5 deletions plugin/Clip.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,6 @@ function showImage(url) {
};
}
if (url) {
showImage(url);
}
function postBase64(base64, name) {
var postdata = 'action=clip&name=' + name + '&value=' + encodeURIComponent(base64);
HTTPPost("$url_save", postdata, function(ret) {
Expand All @@ -168,7 +164,12 @@ function postBase64(base64, name) {
});
}
$(document).ready(function() {
$(function() {
if (url) {
showImage(url);
}
$("#clipPlugin").on('paste', function(e) {
var clipData = (e.clipboardData || e.originalEvent.clipboardData)
if (!clipData)
Expand Down Expand Up @@ -197,6 +198,7 @@ function postBase64(base64, name) {
/*]]>*/
</script>\n
JS;
$formatter->jqReady = true;

$png_url="$imgpath.png";

Expand Down
2 changes: 1 addition & 1 deletion plugin/JME.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function macro_JME($formatter,$value) {
$id = $cid + 0;

return <<<APPLET
<script type="text/javascript" language="javascript" src="$pubpath/jsme/jsme.nocache.js"></script>
<script defer type="text/javascript" language="javascript" src="$pubpath/jsme/jsme.nocache.js"></script>
<script type='text/javascript'>
/*<![CDATA[*/
var jsmeApplet$id = null;
Expand Down
4 changes: 3 additions & 1 deletion plugin/JSUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function abortHandler(event) {
console.log("Upload Aborted");
}
$(document).ready(function() {
$(function() {
$('#jsuploadform :file').on('change', function(){
for (i=0; i<this.files.length; i++) {
uploadFile(this.files[i]);
Expand All @@ -200,6 +200,8 @@ function abortHandler(event) {
/*]]>*/
</script>
EOF;
$formatter->jqReady = true;

$submit_btn='<span id="spanButtonPlaceHolder"><input type="file" name="upload[]" multiple /></span>';
$cancel_btn='';

Expand Down
5 changes: 3 additions & 2 deletions plugin/Keywords.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ function macro_Keywords($formatter,$value,$options=array()) {
$out = preg_replace('/&amp;/',urlencode('&'),$out)."</ul>";

$_prefix=qualifiedUrl("$DBInfo->url_prefix/applets/TagCloud");
$formatter->register_javascripts(array("$_prefix/jquery.tagcanvas.js"));
$formatter->register_javascripts("<script type='text/javascript' defer src='$_prefix/jquery.tagcanvas.js'></script>");
$formatter->jqReady = true;
return <<<JS
<script type="text/javascript">
/*<![CDATA[*/
Expand All @@ -390,7 +391,7 @@ function macro_Keywords($formatter,$value,$options=array()) {
lock,
shape = 'sphere';
$(document).ready(function(){
$(function(){
TagCanvas.textFont = 'Trebuchet MS, Helvetica, sans-serif';
TagCanvas.textColour = '#4800ff';
TagCanvas.textHeight = 25;
Expand Down
5 changes: 3 additions & 2 deletions plugin/RecentChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ function macro_RecentChanges($formatter,$value='',$options='') {

$btn[]=$formatter->link_tag($formatter->page_urlname,"?$arg=...",'...',
'onClick="return daysago(this)"');
$script="<script type='text/javascript' src='$DBInfo->url_prefix/local/rc.js' ></script>";
$script="<script defer type='text/javascript' src='$DBInfo->url_prefix/local/rc.js' ></script>";
$btnlist=$msg[0].' <ul><li>'.implode("</li>\n<li>",$btn).
'</li></ul> '.$msg[1];
$btnlist=$script."<div class='rc-button'>\n".$btnlist."</div>\n";
Expand Down Expand Up @@ -1263,7 +1263,7 @@ function update_bookmark(time) {
$js = <<<JS
<script type='text/javascript'>
/*<![CDATA[*/
(function() {
$(function() {
var url = "$url";
HTTPGet(url, function(txt) {
var rc = document.getElementById("rc$rc_id");
Expand All @@ -1278,6 +1278,7 @@ function update_bookmark(time) {
/*]]>*/
</script>
JS;
$formatter->jqReady = true;
$rc_id++;
} else if ($use_js and $rctype == 'board') {
$js.= <<<JS
Expand Down
6 changes: 3 additions & 3 deletions plugin/processor/jsmol.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ function initJmol() {
//console: "none", // default will be jmolApplet0_infodiv, but you can designate another div here or "none"
}
$(document).ready(function() {
$(function() {
initJmol();
$("#appdiv").html(Jmol.getAppletHtml("jmolApplet$id", Info))
})
Expand All @@ -253,8 +253,8 @@ function initJmol() {
/*>*/
</script>\n
JS;

$formatter->register_javascripts('<script type="text/javascript" src="'.$pubpath.'/jsmol/JSmol.min.nojq.js"></script>');
$formatter->jqReady = true;
$formatter->register_javascripts('<script defer type="text/javascript" src="'.$pubpath.'/jsmol/JSmol.min.nojq.js"></script>');

return <<<APP
<div class="jmolControl">
Expand Down
34 changes: 33 additions & 1 deletion wiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function kbd_handler($prefix = '') {
FrontPage= "$Config[frontpage]";
/*]]>*/
</script>
<script type="text/javascript" src="$Config[kbd_script]"></script>\n
<script defer type="text/javascript" src="$Config[kbd_script]"></script>\n
EOS;
}

Expand Down Expand Up @@ -1911,6 +1911,34 @@ function __construct($page="",$options=array()) {
# recursive footnote regex
$this->footrule='\[\*[^\[\]]*((?:[^\[\]]++|\[(?13)\])*)\]';

register_shutdown_function(array(&$this,'close_javascripts'));
}

function close_javascripts() {
if (!empty($this->jqReady))
echo <<<EOJS
<script type="text/javascript">
/*<![CDATA[*/
(function(){
var i = 0;
function _jqFinalize() {
if (typeof(jQuery) != 'undefined') {
$(function() {
$.each(___MWJQQ___, function(i, fn) { fn(); });
});
} else {
if (++i < 3) {
console.log("jq retry.. #" + i);
setTimeout(_jqFinalize, 100);
}
}
}
_jqFinalize();
})();
/*]]>*/
</script>\n
EOJS;
$this->jqReady = false;
}

/**
Expand Down Expand Up @@ -4832,6 +4860,10 @@ function send_header($header="",$options=array()) {
<script type="text/javascript">
/*<![CDATA[*/
_url_prefix="$DBInfo->url_prefix";
/* https://stackoverflow.com/a/21013975/1696120 */
___MWJQQ___ = [];
$ = function(fn) { ___MWJQQ___.push(fn); };
/*]]>*/
</script>
JSHEAD;
Expand Down
10 changes: 6 additions & 4 deletions wikilib.php
Original file line number Diff line number Diff line change
Expand Up @@ -4337,12 +4337,13 @@ function macro_RandomPage($formatter, $value = '', $params = array()) {
$myid = sprintf("randomPage%02d", $id);
$id++;
$url = $formatter->link_url('', "?action=randompage/ajax&value=".$number);
$formatter->jqReady = true;
return <<<EOF
<div id='$myid'>
</div>
<script type='text/javascript'>
/*<![CDATA[*/
(function () {
$(function () {
HTTPGet("$url", function(msg) {
var ret;
if (msg != null && (ret = eval(msg))) {
Expand All @@ -4356,7 +4357,7 @@ function macro_RandomPage($formatter, $value = '', $params = array()) {
div.appendChild(ul);
}
});
})();
});
/*]]>*/
</script>
EOF;
Expand Down Expand Up @@ -5159,7 +5160,7 @@ function macro_PageCount($formatter, $value = '', $options = array()) {
$js = <<<JS
<script type='text/javascript'>
/*<![CDATA[*/
(function() {
$(function() {
var url = "$url";
var mode = "$mode";
HTTPGet(url, function(txt) {
Expand All @@ -5172,10 +5173,11 @@ function macro_PageCount($formatter, $value = '', $options = array()) {
out = ret['redirect'];
rc.innerHTML = out;
});
})();
});
/*]]>*/
</script>
JS;
$formatter->jqReady = true;
}

$redirects = 0;
Expand Down

0 comments on commit d3d0315

Please sign in to comment.