mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-09 13:16:05 +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
|
* webloader.js
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var FILE = require('lib/file');
|
var FILE = require('lib/file');
|
||||||
|
|
||||||
// https://stackoverflow.com/questions/597268/element-prototype-in-ie7
|
|
||||||
if (!window.Element) {
|
if (!window.Element) {
|
||||||
Element = function() {};
|
Element = function() {};
|
||||||
|
|
||||||
var __createElement = document.createElement;
|
var __createElement = document.createElement;
|
||||||
document.createElement = function(tagName) {
|
document.createElement = function(tagName) {
|
||||||
var element = __createElement(tagName);
|
var element = __createElement(tagName);
|
||||||
for (var key in Element.prototype) {
|
if (element == null) {
|
||||||
element[key] = Element.prototype[key];
|
return null;
|
||||||
}
|
}
|
||||||
|
for (var key in Element.prototype)
|
||||||
|
element[key] = Element.prototype[key];
|
||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
var __getElementById = document.getElementById;
|
var __getElementById = document.getElementById;
|
||||||
document.getElementById = function(id) {
|
document.getElementById = function(id) {
|
||||||
var element = __getElementById(id);
|
var element = __getElementById(id);
|
||||||
for (var key in Element.prototype) {
|
if (element == null) {
|
||||||
element[key] = Element.prototype[key];
|
return null;
|
||||||
}
|
}
|
||||||
|
for (var key in Element.prototype)
|
||||||
|
element[key] = Element.prototype[key];
|
||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://gist.github.com/jonathantneal/3748027
|
|
||||||
if (!window.addEventListener) {
|
if (!window.addEventListener) {
|
||||||
(function(WindowPrototype, DocumentPrototype, ElementPrototype, registry) {
|
(function(WindowPrototype, DocumentPrototype, ElementPrototype, registry) {
|
||||||
DocumentPrototype.head = (function() {
|
DocumentPrototype.head = (function() {
|
||||||
return DocumentPrototype.getElementsByTagName("head")[0];
|
return DocumentPrototype.getElementsByTagName("head")[0];
|
||||||
})();
|
})();
|
||||||
|
|
||||||
var addEventListener = function(type, listener) {
|
var inject = function(obj, registry) {
|
||||||
|
obj.addEventListener = function(type, listener) {
|
||||||
var target = this;
|
var target = this;
|
||||||
|
|
||||||
registry.unshift([target, type, listener, function(event) {
|
registry.unshift([target, type, listener, function(event) {
|
||||||
|
@ -52,32 +54,26 @@ if (!window.addEventListener) {
|
||||||
|
|
||||||
this.attachEvent("on" + type, registry[0][3]);
|
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) {
|
for (var index = 0, register; register = registry[index]; ++index) {
|
||||||
if (register[0] == this && register[1] == type && register[2] == listener) {
|
if (register[0] == this && register[1] == type && register[2] == listener) {
|
||||||
return this.detachEvent("on" + type, registry.splice(index, 1)[0][3]);
|
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);
|
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, []);
|
})(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 IEVersion = (function() {
|
||||||
var undef,
|
var undef,
|
||||||
v = 3,
|
v = 3,
|
||||||
|
@ -94,8 +90,6 @@ var IEVersion = (function() {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
setWindowDraggable: function() {
|
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'),
|
var grip = document.getElementById('app'),
|
||||||
oX, oY,
|
oX, oY,
|
||||||
mouseDown = function(e) {
|
mouseDown = function(e) {
|
||||||
|
@ -117,20 +111,25 @@ return {
|
||||||
window.removeEventListener('mousemove', mouseMove);
|
window.removeEventListener('mousemove', mouseMove);
|
||||||
window.removeEventListener('mouseup', mouseUp);
|
window.removeEventListener('mouseup', mouseUp);
|
||||||
};
|
};
|
||||||
|
|
||||||
grip.addEventListener('mousedown', mouseDown);
|
grip.addEventListener('mousedown', mouseDown);
|
||||||
grip.addEventListener('mousemove', gripMouseMove);
|
grip.addEventListener('mousemove', gripMouseMove);
|
||||||
},
|
},
|
||||||
getIEVersion: function() {
|
getIEVersion: function() {
|
||||||
return IEVersion;
|
return IEVersion;
|
||||||
},
|
},
|
||||||
addScript: function(url, callback, test) {
|
addScript: function(url, callback, test, ttl) {
|
||||||
var _callback = function(el) {
|
var _callback = function(el, ttl) {
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
var result = test(el);
|
var result = test(el);
|
||||||
if (typeof(result) !== "undefined") {
|
if (typeof(result) !== "undefined") {
|
||||||
callback(el);
|
callback(el);
|
||||||
} else {
|
} else {
|
||||||
_callback(el);
|
if (ttl > 0) {
|
||||||
|
_callback(el, ttl - 50);
|
||||||
|
} else {
|
||||||
|
console.log("failed load " + url);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, 50);
|
}, 50);
|
||||||
};
|
};
|
||||||
|
@ -142,37 +141,24 @@ return {
|
||||||
document.head.appendChild(el);
|
document.head.appendChild(el);
|
||||||
|
|
||||||
if (typeof(test) === "function") {
|
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") {
|
} else if (typeof(callback) === "function") {
|
||||||
el.onload = callback(el);
|
el.onload = callback(el);
|
||||||
}
|
}
|
||||||
|
|
||||||
return el;
|
return el;
|
||||||
},
|
},
|
||||||
addStylesheet: function(url, callback, test) {
|
addStylesheet: function(url, callback) {
|
||||||
var _callback = function(el) {
|
|
||||||
setTimeout(function() {
|
|
||||||
var result = test(el);
|
|
||||||
if(typeof(result) !== "undefined") {
|
|
||||||
callback(el);
|
|
||||||
} else {
|
|
||||||
_callback(el);
|
|
||||||
}
|
|
||||||
}, 50);
|
|
||||||
};
|
|
||||||
|
|
||||||
var el = document.createElement("link");
|
var el = document.createElement("link");
|
||||||
el.href = url;
|
el.href = url;
|
||||||
el.rel = "stylesheet";
|
el.rel = "stylesheet";
|
||||||
el.type = "text/css";
|
el.type = "text/css";
|
||||||
document.head.appendChild(el);
|
document.head.appendChild(el);
|
||||||
|
if (typeof(callback) === "function") {
|
||||||
if(typeof(test) === "function") {
|
|
||||||
_callback();
|
|
||||||
} else if(typeof(callback) === "function") {
|
|
||||||
el.onload = callback(el);
|
el.onload = callback(el);
|
||||||
}
|
}
|
||||||
|
|
||||||
return el;
|
return el;
|
||||||
},
|
},
|
||||||
main: function() {
|
main: function() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user