mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-08 20:56:04 +00:00
Add method Browser.waitUntil
This commit is contained in:
parent
05df3518ac
commit
533521239f
120
lib/browser.js
120
lib/browser.js
|
@ -1,6 +1,7 @@
|
||||||
// browser.js
|
// browser.js
|
||||||
// Namhyeon Go <abuse@catswords.net>
|
// Namhyeon Go <abuse@catswords.net>
|
||||||
// https://github.com/gnh1201/welsonjs
|
// https://github.com/gnh1201/welsonjs
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// only less than IE 9
|
// only less than IE 9
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -110,9 +111,9 @@ if (!window.addEventListener) {
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// exports.getIEVersion()
|
// getIEVersion()
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
exports.getIEVersion = function() {
|
function getIEVersion() {
|
||||||
var undef,
|
var undef,
|
||||||
v = 3,
|
v = 3,
|
||||||
div = document.createElement('div'),
|
div = document.createElement('div'),
|
||||||
|
@ -127,50 +128,64 @@ exports.getIEVersion = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// exports.addScript()
|
// exports.waitUntil()
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
exports.addScript = function(url, callback, test, ttl) {
|
function waitUntil(f, test, ttl) {
|
||||||
var _callback = function(el, ttl) {
|
if (typeof f !== "function") return;
|
||||||
|
if (typeof test !== "function") return;
|
||||||
|
var ttl = (typeof ttl === "number" ? ttl : 30000); // TTL(Time-To-Live)
|
||||||
|
var started_time = Date.now();
|
||||||
|
|
||||||
|
var _test = function(el, callback, ttl) {
|
||||||
var result = test(el);
|
var result = test(el);
|
||||||
if (typeof(result) !== "undefined") {
|
if (typeof result !== "undefined" && !!result) {
|
||||||
callback(el);
|
callback(el);
|
||||||
} else {
|
} else {
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
if (ttl > 0) {
|
var current_time = Date.now();
|
||||||
_callback(el, ttl - 1);
|
if (current_time - started_time < ttl) {
|
||||||
|
_test(el, callback, ttl);
|
||||||
} else {
|
} else {
|
||||||
console.log("failed load " + url);
|
console.warn("Failed to load:", url);
|
||||||
}
|
}
|
||||||
}, 1);
|
}, 1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
f(_test, ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////
|
||||||
|
// addScript()
|
||||||
|
////////////////////////////////////////////////////////////////////////
|
||||||
|
function addScript(url, callback, test, ttl) {
|
||||||
|
if (typeof test !== "function") {
|
||||||
|
test = function(el, callback, ttl) {};
|
||||||
|
}
|
||||||
|
var ttl = (typeof ttl === "number" ? ttl : 30000); // TTL(Time-To-Live)
|
||||||
|
|
||||||
var el = document.createElement("script");
|
var el = document.createElement("script");
|
||||||
el.src = url;
|
el.src = url;
|
||||||
el.type = "text/javascript";
|
el.type = "text/javascript";
|
||||||
el.charset = "utf-8";
|
el.charset = "utf-8";
|
||||||
document.head.appendChild(el);
|
document.head.appendChild(el);
|
||||||
|
|
||||||
if (typeof(test) === "function" && typeof(callback) === "function") {
|
if (typeof test === "function") {
|
||||||
// Time-To-Live: default value is 30 seconds
|
test(el, callback, ttl);
|
||||||
ttl = (typeof(ttl) == "number" ? ttl : 30000);
|
|
||||||
el.onload = _callback(el, ttl);
|
|
||||||
} else if (typeof(callback) === "function") {
|
|
||||||
el.onload = callback(el);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return el;
|
return el;
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// exports.addResources()
|
// addResources()
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
exports.addResources = function(resources, callback) {
|
function addResources(resources, callback) {
|
||||||
resources.forEach(function(resource) {
|
resources.forEach(function(resource) {
|
||||||
if (["javascript", "text/javascript"].indexOf(resource.type) > -1) {
|
if (["javascript", "text/javascript"].indexOf(resource.type) > -1) {
|
||||||
exports.addScript(resource.url, callback);
|
addScript(resource.url, callback);
|
||||||
} else if (["stylesheet", "text/css"].indexOf(resource.type) > -1) {
|
} else if (["stylesheet", "text/css"].indexOf(resource.type) > -1) {
|
||||||
exports.addStylesheet(resource.url, callback);
|
addStylesheet(resource.url, callback);
|
||||||
} else {
|
} else {
|
||||||
console.warn("Not supported resource type");
|
console.warn("Not supported resource type");
|
||||||
}
|
}
|
||||||
|
@ -178,9 +193,9 @@ exports.addResources = function(resources, callback) {
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// exports.addStylesheet()
|
// addStylesheet()
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
exports.addStylesheet = function(url, callback) {
|
function addStylesheet(url, callback) {
|
||||||
var el = document.createElement("link");
|
var el = document.createElement("link");
|
||||||
el.href = url;
|
el.href = url;
|
||||||
el.rel = "stylesheet";
|
el.rel = "stylesheet";
|
||||||
|
@ -194,64 +209,79 @@ exports.addStylesheet = function(url, callback) {
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// exports.setContent()
|
// setContent()
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
exports.setContent = function(content) {
|
function setContent(content) {
|
||||||
document.getElementById("app").innerHTML = content;
|
document.getElementById("app").innerHTML = content;
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// exports.start()
|
// start()
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
exports.start = function(callback) {
|
function start(callback) {
|
||||||
var IEVersion = exports.getIEVersion();
|
var msie = getIEVersion();
|
||||||
|
|
||||||
// load jQuery and cross browsing libraries
|
// load jQuery and cross browsing libraries
|
||||||
if (IEVersion == 8) {
|
if (msie == 8) {
|
||||||
exports.addScript("app/assets/css/jquery/ie8-0.8.1.min.js");
|
addScript("app/assets/css/jquery/ie8-0.8.1.min.js");
|
||||||
}
|
}
|
||||||
exports.addScript("app/assets/js/html5shiv-printshiv-3.7.3.min.js");
|
addScript("app/assets/js/html5shiv-printshiv-3.7.3.min.js");
|
||||||
if (IEVersion < 9) {
|
if (msie < 9) {
|
||||||
exports.addScript("app/assets/js/respond-1.4.2.edited.js");
|
addScript("app/assets/js/respond-1.4.2.edited.js");
|
||||||
exports.addScript("app/assets/js/selectivizr-1.0.2.edited.js");
|
addScript("app/assets/js/selectivizr-1.0.2.edited.js");
|
||||||
exports.addScript("app/assets/js/excanvas.arv-565afad.js");
|
addScript("app/assets/js/excanvas.arv-565afad.js");
|
||||||
exports.addScript("app/assets/js/jquery-1.11.3.min.js", callback, function(el) {
|
|
||||||
|
waitUntil(function(test, ttl) {
|
||||||
|
addScript("app/assets/js/jquery-1.11.3.min.js", callback, test, ttl);
|
||||||
|
}, function(el) {
|
||||||
return window.jQuery;
|
return window.jQuery;
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
exports.addScript("app/assets/js/jquery-3.5.1.min.js", callback, function(el) {
|
waitUntil(function(test, ttl) {
|
||||||
|
addScript("app/assets/js/jquery-3.5.1.min.js", callback, test, ttl);
|
||||||
|
}, function(el) {
|
||||||
return window.jQuery;
|
return window.jQuery;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
exports.addScript("app/assets/js/jquery.html5-placeholder-shim.jcampbell1-5a87f05.js");
|
addScript("app/assets/js/jquery.html5-placeholder-shim.jcampbell1-5a87f05.js");
|
||||||
|
|
||||||
// load Modernizr (2.8.3)
|
// load Modernizr (2.8.3)
|
||||||
exports.addScript("app/assets/js/modernizr-2.8.3.min.js");
|
addScript("app/assets/js/modernizr-2.8.3.min.js");
|
||||||
|
|
||||||
// load jQuery UI (1.12.1)
|
// load jQuery UI (1.12.1)
|
||||||
exports.addScript("app/assets/js/jquery-ui-1.21.1.min.js");
|
addScript("app/assets/js/jquery-ui-1.21.1.min.js");
|
||||||
|
|
||||||
// load jsRender (1.0.8)
|
// load jsRender (1.0.8)
|
||||||
exports.addScript("app/assets/js/jsrender-1.0.8.min.js");
|
addScript("app/assets/js/jsrender-1.0.8.min.js");
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// exports.reload()
|
// reload()
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
exports.reload = function() {
|
function reload() {
|
||||||
if (typeof window !== "undefined") {
|
if (typeof window !== "undefined") {
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// exports.close()
|
// close()
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
exports.close = function() {
|
function close() {
|
||||||
exit(0);
|
exit(0);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.VERSIONINFO = "Browser Library (browser.js) version 0.1.2";
|
exports.getIEVersion = getIEVersion;
|
||||||
|
exports.waitUntil = waitUntil;
|
||||||
|
exports.addScript = addScript;
|
||||||
|
exports.addResources = addResources;
|
||||||
|
exports.addStylesheet = addStylesheet;
|
||||||
|
exports.setContent = setContent;
|
||||||
|
exports.start = start;
|
||||||
|
exports.reload = reload;
|
||||||
|
exports.close = close;
|
||||||
|
|
||||||
|
exports.VERSIONINFO = "Browser Library (browser.js) version 0.1.3";
|
||||||
exports.AUTHOR = "abuse@catswords.net";
|
exports.AUTHOR = "abuse@catswords.net";
|
||||||
exports.global = global;
|
exports.global = global;
|
||||||
exports.require = global.require;
|
exports.require = global.require;
|
||||||
|
|
25
lib/file.js
25
lib/file.js
|
@ -1,10 +1,6 @@
|
||||||
//////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
// file.js
|
// file.js
|
||||||
//
|
|
||||||
// Common routines. Defines LIB object which contains the API, as well as
|
|
||||||
// a global console.log function.
|
|
||||||
|
|
||||||
// with the PIPE based IPC (lib/pipe-ipc.js)
|
// with the PIPE based IPC (lib/pipe-ipc.js)
|
||||||
//
|
//
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -12,10 +8,6 @@
|
||||||
var LIB = require("lib/std");
|
var LIB = require("lib/std");
|
||||||
var PipeIPC = require("lib/pipe-ipc");
|
var PipeIPC = require("lib/pipe-ipc");
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Private APIs / Utility functions
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
// fileExists
|
// fileExists
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -154,9 +146,17 @@ function deleteFile(FN) {
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
function includeFile(FN) {
|
function includeFile(FN) {
|
||||||
eval(readFile(FN));
|
try {
|
||||||
|
eval(readFile(FN));
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e.message, "in", FN);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// appendFile
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
function appendFile(FN, content, charset) {
|
function appendFile(FN, content, charset) {
|
||||||
var result = false;
|
var result = false;
|
||||||
var pipe = PipeIPC.connect(PipeIPC.CRC32(FN));
|
var pipe = PipeIPC.connect(PipeIPC.CRC32(FN));
|
||||||
|
@ -168,6 +168,10 @@ function appendFile(FN, content, charset) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// rotateFile
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
function rotateFile(FN, content, numOfLines, charset) {
|
function rotateFile(FN, content, numOfLines, charset) {
|
||||||
var result = false;
|
var result = false;
|
||||||
var pipe = PipeIPC.connect(PipeIPC.CRC32(FN));
|
var pipe = PipeIPC.connect(PipeIPC.CRC32(FN));
|
||||||
|
@ -196,6 +200,7 @@ exports.rotateFile = rotateFile;
|
||||||
|
|
||||||
exports.CdoCharset = PipeIPC.CdoCharset;
|
exports.CdoCharset = PipeIPC.CdoCharset;
|
||||||
|
|
||||||
exports.VERSIONINFO = "File Library (file.js) version 0.2.10";
|
exports.VERSIONINFO = "File IO Library (file.js) version 0.2.11";
|
||||||
|
exports.AUTHOR = "abuse@catswords.net";
|
||||||
exports.global = global;
|
exports.global = global;
|
||||||
exports.require = global.require;
|
exports.require = global.require;
|
||||||
|
|
35
webloader.js
35
webloader.js
|
@ -1,7 +1,6 @@
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// Webloader
|
// Webloader
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
var CONFIG = require("lib/config");
|
|
||||||
var FILE = require("lib/file");
|
var FILE = require("lib/file");
|
||||||
var Browser = require("lib/browser");
|
var Browser = require("lib/browser");
|
||||||
|
|
||||||
|
@ -39,7 +38,7 @@ global.console._echo = function(args, type) {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (typeof(window.jQuery.toast) !== "undefined") {
|
if (typeof window.jQuery.toast !== "undefined") {
|
||||||
window.jQuery.toast({
|
window.jQuery.toast({
|
||||||
heading: heading,
|
heading: heading,
|
||||||
text: message,
|
text: message,
|
||||||
|
@ -127,24 +126,26 @@ exports.main = function(args) {
|
||||||
Browser.start(function(el) {
|
Browser.start(function(el) {
|
||||||
jQuery.support.cors = true;
|
jQuery.support.cors = true;
|
||||||
|
|
||||||
Browser.addScript("app/assets/js/jquery.toast-1.3.2.min.js", function(el) {
|
Browser.waitUntil(function(test, ttl) {
|
||||||
var messages = global.console._messages;
|
Browser.addScript("app/assets/js/jquery.toast-1.3.2.min.js", function(el) {
|
||||||
if (messages.length > 0) {
|
var messages = global.console._messages;
|
||||||
// print messages
|
if (messages.length > 0) {
|
||||||
for (var i in messages) {
|
// print messages
|
||||||
console.log(messages[i]);
|
for (var i in messages) {
|
||||||
|
console.log(messages[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// start the app
|
||||||
|
Browser.addScript("app/assets/js/jquery.form-4.3.0.min.js");
|
||||||
|
Browser.addScript("app/index.js");
|
||||||
|
|
||||||
|
// hide loading image
|
||||||
|
document.getElementById("loading").style.display = "none";
|
||||||
}
|
}
|
||||||
|
}, test, ttl);
|
||||||
// start this app
|
|
||||||
Browser.addScript("app/assets/js/jquery.form-4.3.0.min.js");
|
|
||||||
Browser.addScript("app/index.js");
|
|
||||||
|
|
||||||
// hide loading image
|
|
||||||
document.getElementById("loading").style.display = "none";
|
|
||||||
}
|
|
||||||
}, function(el) {
|
}, function(el) {
|
||||||
return window.jQuery.toast;
|
return window.jQuery.toast;
|
||||||
});
|
}, 30000);
|
||||||
});
|
});
|
||||||
|
|
||||||
// hook drag event
|
// hook drag event
|
||||||
|
|
Loading…
Reference in New Issue
Block a user