mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-02-06 15:04:58 +00:00
Update webloader.js
This commit is contained in:
parent
469c1d41aa
commit
1b91503d7e
74
webloader.js
74
webloader.js
|
@ -1,40 +1,42 @@
|
|||
/*
|
||||
* webloader.js
|
||||
*/
|
||||
|
||||
var FILE = require('lib/file');
|
||||
|
||||
// https://stackoverflow.com/questions/597268/element-prototype-in-ie7
|
||||
if (!window.Element) {
|
||||
Element = function() {};
|
||||
|
||||
var __createElement = document.createElement;
|
||||
document.createElement = function(tagName) {
|
||||
var element = __createElement(tagName);
|
||||
for (var key in Element.prototype) {
|
||||
element[key] = Element.prototype[key];
|
||||
if (element == null) {
|
||||
return null;
|
||||
}
|
||||
for (var key in Element.prototype)
|
||||
element[key] = Element.prototype[key];
|
||||
return element;
|
||||
}
|
||||
|
||||
var __getElementById = document.getElementById;
|
||||
document.getElementById = function(id) {
|
||||
var element = __getElementById(id);
|
||||
for (var key in Element.prototype) {
|
||||
element[key] = Element.prototype[key];
|
||||
if (element == null) {
|
||||
return null;
|
||||
}
|
||||
for (var key in Element.prototype)
|
||||
element[key] = Element.prototype[key];
|
||||
return element;
|
||||
}
|
||||
}
|
||||
|
||||
// https://gist.github.com/jonathantneal/3748027
|
||||
if (!window.addEventListener) {
|
||||
(function(WindowPrototype, DocumentPrototype, ElementPrototype, registry) {
|
||||
DocumentPrototype.head = (function() {
|
||||
return DocumentPrototype.getElementsByTagName("head")[0];
|
||||
})();
|
||||
|
||||
var addEventListener = function(type, listener) {
|
||||
var inject = function(obj, registry) {
|
||||
obj.addEventListener = function(type, listener) {
|
||||
var target = this;
|
||||
|
||||
registry.unshift([target, type, listener, function(event) {
|
||||
|
@ -52,32 +54,26 @@ if (!window.addEventListener) {
|
|||
|
||||
this.attachEvent("on" + type, registry[0][3]);
|
||||
};
|
||||
WindowPrototype.addEventListener = addEventListener;
|
||||
DocumentPrototype.addEventListener = addEventListener;
|
||||
ElementPrototype.ElementPrototype = addEventListener;
|
||||
|
||||
var removeEventListener = function(type, listener) {
|
||||
obj.removeEventListener = function(type, listener) {
|
||||
for (var index = 0, register; register = registry[index]; ++index) {
|
||||
if (register[0] == this && register[1] == type && register[2] == listener) {
|
||||
return this.detachEvent("on" + type, registry.splice(index, 1)[0][3]);
|
||||
}
|
||||
}
|
||||
};
|
||||
WindowPrototype.removeEventListener = removeEventListener;
|
||||
DocumentPrototype.removeEventListener = removeEventListener;
|
||||
ElementPrototype.removeEventListener = removeEventListener;
|
||||
|
||||
var dispatchEvent = function(eventObject) {
|
||||
obj.dispatchEvent = function(eventObject) {
|
||||
return this.fireEvent("on" + eventObject.type, eventObject);
|
||||
};
|
||||
WindowPrototype.dispatchEvent = dispatchEvent;
|
||||
DocumentPrototype.dispatchEvent = dispatchEvent;
|
||||
ElementPrototype.dispatchEvent = dispatchEvent;
|
||||
};
|
||||
|
||||
inject(WindowPrototype, registry);
|
||||
inject(DocumentPrototype, registry);
|
||||
inject(ElementPrototype, registry);
|
||||
})(window, document, Element.prototype, []);
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/questions/10964966/detect-ie-version-prior-to-v9-in-javascript
|
||||
// https://stackoverflow.com/a/18249612
|
||||
var IEVersion = (function() {
|
||||
var undef,
|
||||
v = 3,
|
||||
|
@ -94,8 +90,6 @@ var IEVersion = (function() {
|
|||
|
||||
return {
|
||||
setWindowDraggable: function() {
|
||||
// https://stackoverflow.com/questions/21493777/how-to-make-a-tab-to-move-the-window-of-an-hta-application-in-vbscript
|
||||
// https://stackoverflow.com/a/21497175
|
||||
var grip = document.getElementById('app'),
|
||||
oX, oY,
|
||||
mouseDown = function(e) {
|
||||
|
@ -117,20 +111,25 @@ return {
|
|||
window.removeEventListener('mousemove', mouseMove);
|
||||
window.removeEventListener('mouseup', mouseUp);
|
||||
};
|
||||
|
||||
grip.addEventListener('mousedown', mouseDown);
|
||||
grip.addEventListener('mousemove', gripMouseMove);
|
||||
},
|
||||
getIEVersion: function() {
|
||||
return IEVersion;
|
||||
},
|
||||
addScript: function(url, callback, test) {
|
||||
var _callback = function(el) {
|
||||
addScript: function(url, callback, test, ttl) {
|
||||
var _callback = function(el, ttl) {
|
||||
setTimeout(function() {
|
||||
var result = test(el);
|
||||
if (typeof(result) !== "undefined") {
|
||||
callback(el);
|
||||
} else {
|
||||
_callback(el);
|
||||
if (ttl > 0) {
|
||||
_callback(el, ttl - 50);
|
||||
} else {
|
||||
console.log("failed load " + url);
|
||||
}
|
||||
}
|
||||
}, 50);
|
||||
};
|
||||
|
@ -142,37 +141,24 @@ return {
|
|||
document.head.appendChild(el);
|
||||
|
||||
if (typeof(test) === "function") {
|
||||
_callback();
|
||||
// Time-To-Live: default value is 30 seconds
|
||||
ttl = (typeof(ttl) == "number" ? ttl : 30000);
|
||||
_callback(el, ttl);
|
||||
} else if (typeof(callback) === "function") {
|
||||
el.onload = callback(el);
|
||||
}
|
||||
|
||||
return el;
|
||||
},
|
||||
addStylesheet: function(url, callback, test) {
|
||||
var _callback = function(el) {
|
||||
setTimeout(function() {
|
||||
var result = test(el);
|
||||
if(typeof(result) !== "undefined") {
|
||||
callback(el);
|
||||
} else {
|
||||
_callback(el);
|
||||
}
|
||||
}, 50);
|
||||
};
|
||||
|
||||
addStylesheet: function(url, callback) {
|
||||
var el = document.createElement("link");
|
||||
el.href = url;
|
||||
el.rel = "stylesheet";
|
||||
el.type = "text/css";
|
||||
document.head.appendChild(el);
|
||||
|
||||
if(typeof(test) === "function") {
|
||||
_callback();
|
||||
} else if(typeof(callback) === "function") {
|
||||
if (typeof(callback) === "function") {
|
||||
el.onload = callback(el);
|
||||
}
|
||||
|
||||
return el;
|
||||
},
|
||||
main: function() {
|
||||
|
|
Loading…
Reference in New Issue
Block a user