Created
July 5, 2012 15:32
-
-
Save wesleyhales/3054384 to your computer and use it in GitHub Desktop.
temp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (window, undefined) { | |
"use strict"; | |
/** | |
* @class asynchronous resource loader | |
*/ | |
var CNNLoader = (function () { | |
var init = function (options) { | |
options = options || {}; | |
}, | |
loader = {}, | |
instance, | |
loaderURLCache = {}, | |
XMLHttpRequest = window.XMLHttpRequest, | |
localStorage = window.localStorage; | |
loader.start = function (options) { | |
options = options || {}; | |
//init all one time setup, polyfills and instances | |
document.head = document.head || document.getElementsByTagName('head')[0]; | |
if(!document.querySelectorAll){document.querySelectorAll = querySelectorAll;} | |
if(!document.querySelector){document.querySelector = querySelector;} | |
if(!document.getElementsByClassName){document.getElementsByClassName = getElementsByClassName;} | |
if (XMLHttpRequest === undefined) { | |
XMLHttpRequest = function (){ | |
try { | |
return new ActiveXObject("Msxml2.XMLHTTP.6.0"); | |
} catch (e1) { | |
} | |
try { | |
return new ActiveXObject("Msxml2.XMLHTTP.3.0"); | |
} catch (e2) { | |
} | |
try { | |
return new ActiveXObject("Msxml2.XMLHTTP"); | |
} catch (e3) { | |
} | |
throw new Error("This browser does not support XMLHttpRequest."); | |
}; | |
XMLHttpRequest.UNSENT = 0; | |
XMLHttpRequest.OPENED = 1; | |
XMLHttpRequest.HEADERS_RECEIVED = 2; | |
XMLHttpRequest.LOADING = 3; | |
XMLHttpRequest.DONE = 4; | |
} | |
/** | |
* @class backwards-compatible polyfill for HTML5 localStorage | |
*/ | |
if (localStorage === undefined) { | |
localStorage = function () { | |
/** | |
* creates or updates a key<->value pair in the local store | |
*/ | |
this.setItem = function (key, value) { | |
_createCookie(key, value, 3000); | |
}; | |
/** | |
* retrieves a value from the local store by key. | |
*/ | |
this.getItem = function (key) { | |
return (_readCookie(key)); | |
}; | |
var _createCookie = function (name, value, days) { | |
var date, expires = ""; | |
if (days) { | |
date = new Date(); | |
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); | |
expires = "; expires=" + date.toGMTString(); | |
} | |
document.cookie = name + "=" + value + expires + "; path=/"; | |
}; | |
var _readCookie = function (name) { | |
var result = "", | |
nameEQ = name + "=", | |
ca = document.cookie.split(';'), | |
i, c; | |
for (i = 0; i < ca.length; i++) { | |
c = ca[i]; | |
while (c.charAt(0) === ' ') { | |
c = c.substring(1, c.length); | |
} | |
result = c.indexOf(nameEQ) === 0 ? c.substring(nameEQ.length, c.length) : ""; | |
} | |
return (result); | |
}; | |
}; | |
} | |
//[has|add|remove|toggle]Class | |
if (typeof(Element) !== "undefined") { | |
/** | |
* Determine if an element as a specified class | |
*/ | |
if (!Element.hasClass){ | |
Element.prototype.hasClass = function (c) { | |
return new RegExp('(^| )' + c + '( |$)', 'g').test(this.className); | |
}; | |
} | |
/** | |
* add the specified class to the element | |
*/ | |
if (!Element.addClass){ | |
Element.prototype.addClass = function (c) { | |
c = c.split(' '); | |
for (var i = 0; i < c.length; i++){ | |
if (!this.hasClass(c[i])){ | |
this.className = this.className !== '' ? (this.className + ' ' + c[i]) : c[i]; | |
} | |
} | |
return this; | |
}; | |
} | |
/** | |
* remove the specified class from the element | |
*/ | |
if (!Element.removeClass){ | |
Element.prototype.removeClass = function (c) { | |
c = c.split(' '); | |
for (var i = 0; i < c.length; i++){ | |
this.className = this.className.replace(new RegExp('\\b' + c[i] + '\\b( )?', 'g'), ''); | |
} | |
return this; | |
}; | |
} | |
/** | |
* toggle the specified class on the element | |
*/ | |
if (!Element.toggleClass) { | |
Element.prototype.toggleClass = function (c) { | |
return !this.hasClass(c) ? this.addClass(c) : this.removeClass(c); | |
}; | |
} | |
} | |
return CNNLoader; | |
}; | |
function createTag(d, c, e) { | |
var b = document.createElement(d); | |
Object.keys(c).forEach(function (f) { | |
b[f] = c[f]; | |
}); | |
b.onload = b.onreadystatechange = e; | |
document.getElementsByTagName('body')[0].appendChild(b); | |
return b; | |
} | |
function createId(url) { | |
var id = url, | |
liSlash = url.lastIndexOf('/') + 1, | |
liDot = url.lastIndexOf('.'); | |
id = url.substring(liSlash, liDot).replace(/\W/, '_'); | |
return id; | |
} | |
function getType(url) { | |
return (/\.([^\.]+)$/.exec(url)[1].toLowerCase()); | |
} | |
/** | |
* Convenience method to load a resource URL into the current context | |
* @return a promise object that completes when the ajax request completes | |
* @see ajax.request | |
*/ | |
loader.load = function (url) { | |
var type = getType(url), promise; | |
if (url in loaderURLCache) { | |
console.log(url, "in cache"); | |
return loaderURLCache[url]; | |
} else { | |
console.log(url, "NOT in cache"); | |
switch (type) { | |
case 'js': | |
promise = this.js(url); | |
break; | |
case 'css': | |
promise = this.css(url); | |
break; | |
case 'html': | |
promise = this.tmpl(url); | |
break; | |
case 'tmpl': | |
promise = this.tmpl(url); | |
break; | |
default: | |
promise = this.html(url); | |
break; | |
} | |
} | |
loaderURLCache[url] = promise; | |
promise.then(function (err, res) { | |
if (!err) { | |
log.warn("loaded", url); | |
} | |
}); | |
return promise; | |
}; | |
/** | |
* load a JavaScript resource URL into the current context | |
* @return a promise object that completes when the ajax request completes | |
* @see load | |
*/ | |
loader.js = function (url) { | |
var p = new Promise(), id = createId(url), | |
tag = createTag("SCRIPT", { | |
async:true, | |
src:url, | |
id:id | |
}, function () { | |
if (!(this.readyState && this.readyState !== "complete" && this.readyState !== "loaded")) { | |
p.done(null, url + " loaded"); | |
} | |
} | |
); | |
return p; | |
}; | |
/** | |
* load a CSS resource URL into the current context | |
* @return a promise object that completes when the ajax request completes | |
* @see load | |
*/ | |
loader.css = function (url) { | |
var p = new Promise(), id = createId(url); | |
var tag = createTag("LINK", { | |
rel:"stylesheet", | |
href:url, | |
type:'text/css', | |
media:'all', | |
id:id | |
}, function () { | |
/* Safari does not trigger readyState events for LINK tags */ | |
/*if (!(this.readyState && this.readyState !== "complete" && this.readyState !== "loaded")) { | |
p.done(null, url + " loaded"); | |
}*/ | |
} | |
); | |
p.done(null, url + " loaded"); | |
return p; | |
}; | |
/** | |
* load an HTML resource URL into the current context by appending it to the body | |
* @return a promise object that completes when the ajax request completes | |
* @see load | |
*/ | |
loader.html = function (url) { | |
var p = new Promise(), id = CNNLoader.createId(url); | |
ajax.get(url).then(function (error, response) { | |
if (error) { | |
p.done(error, response); | |
} else { | |
document.body.innerHTML = response + document.body.innerHTML; | |
p.done(null, url + " loaded"); | |
} | |
}); | |
return p; | |
}; | |
/** | |
* load a Knockout template resource URL into the current context by injecting it into | |
* a script tag of type "text/html" | |
* @return a promise object that completes when the ajax request completes | |
* @see load | |
*/ | |
loader.tmpl = function (url) { | |
var p = new Promise(), | |
id = createId(url) + '-template'; | |
this.ajax.get(url).then(function (error, response) { | |
if (error) { | |
p.done(error, response); | |
} else { | |
var tag = createTag("SCRIPT", { | |
type:'text/html', | |
src:url, | |
id:id | |
}, function () { | |
console.log(this.readyState); | |
if (!(this.readyState && this.readyState !== "complete" && this.readyState !== "loaded")) { | |
p.done(null, url + " loaded"); | |
} | |
p.done(null, url + " loaded"); | |
} | |
); | |
tag.text = response; | |
(function () { | |
p.done(null, url + " loaded"); | |
})(); | |
} | |
}); | |
return p; | |
}; | |
loader.ajax = { | |
_encode:function(data) { | |
var result = ""; | |
if (typeof data === "string") { | |
result = data; | |
} else { | |
var e = encodeURIComponent; | |
for (var k in data) { | |
if (data.hasOwnProperty(k)) { | |
result += '&' + e(k) + '=' + e(data[k]); | |
} | |
} | |
} | |
return result; | |
}, | |
_ajax:function(method, url, data, headers) { | |
var p = new Promise(), | |
xhr, | |
payload; | |
try { | |
data = data || {}; | |
headers = headers || {}; | |
try { | |
xhr = new XMLHttpRequest(); | |
} catch (e) { | |
p.done(-1, "could not instantiate XMLHttpRequest"); | |
return p; | |
} | |
payload = this._encode(data); | |
if (method === 'GET' && payload) { | |
url += '?' + payload; | |
payload = null; | |
} | |
xhr.open(method, url); | |
xhr.setRequestHeader('Content-type', | |
'application/x-www-form-urlencoded'); | |
for (var h in headers) { | |
if (headers.hasOwnProperty(h)) { | |
xhr.setRequestHeader(h, headers[h]); | |
} | |
} | |
xhr.onreadystatechange = function () { | |
if (xhr.readyState === XMLHttpRequest.DONE) { | |
if (xhr.status === 200) { | |
p.done(null, xhr.responseText); | |
} else { | |
p.done(xhr.status, xhr.statusText); | |
} | |
} | |
}; | |
xhr.send(payload); | |
} catch (err) { | |
alert(err); | |
} | |
return p; | |
}, | |
/** | |
* generic AJAX request method | |
* @return a promise object that completes when the ajax request completes | |
*/ | |
request : function (method, url, data, headers) { | |
return this._ajax(method, url, data, headers); | |
}, | |
/** | |
* GET AJAX request method | |
* shorthand for request("GET", url, data, headers) | |
* @return a promise object that completes when the ajax request completes | |
* @see request | |
*/ | |
get : function (url, data, headers) { | |
return this._ajax("GET", url, data, headers); | |
}, | |
/** | |
* POST AJAX request method | |
* shorthand for request("POST", url, data, headers) | |
* @return a promise object that completes when the ajax request completes | |
* @see request | |
*/ | |
post : function (url, data, headers) { | |
return this._ajax("POST", url, data, headers); | |
}, | |
/** | |
* PUT AJAX request method | |
* shorthand for request("PUT", url, data, headers) | |
* @return a promise object that completes when the ajax request completes | |
* @see request | |
*/ | |
put : function (url, data, headers) { | |
return this._ajax("PUT", url, data, headers); | |
}, | |
/** | |
* DELETE AJAX request method | |
* shorthand for request("DELETE", url, data, headers) | |
* @return a promise object that completes when the ajax request completes | |
* @see request | |
*/ | |
del : function (url, data, headers) { | |
return this._ajax("DELETE", url, data, headers); | |
} | |
}; | |
/** | |
* Selectors API Level 1 ({@link http://www.w3.org/TR/selectors-api/}) | |
* @returns a NodeList containing Elements matching the given selector | |
*/ | |
function querySelectorAll(selector) { | |
return new Sizzle(selector, this); | |
} | |
/** | |
* Selectors API Level 1 ({@link http://www.w3.org/TR/selectors-api/}) | |
* @returns a single Element matching the given selector | |
*/ | |
function querySelector(selector) { | |
return (document.querySelectorAll.call(this, selector)[0] || null); | |
} | |
/** | |
* Selectors API Level 1 ({@link http://www.w3.org/TR/selectors-api/}) | |
* @returns a NodeList containing Elements with the given class name | |
*/ | |
function getElementsByClassName(selector) { | |
var classNames; | |
classNames = String(classNames).replace(/^|\s+/g, '.'); | |
return document.querySelectorAll(classNames); | |
} | |
return loader; | |
}()); | |
window.Loader = CNNLoader.start(); | |
})(this); | |
//sizzle js, for IE7 selector support | |
/** | |
* @ignore | |
*/ | |
(function (S, r) { | |
var e = S.document, j = e.documentElement, V = "sizcache" + (Math.random() + "").replace(".", ""), p = 0, d = Object.prototype.toString, C = Array.prototype.concat, D = "undefined", m = false, h = true, N = /^#([\w\-]+$)|^(\w+$)|^\.([\w\-]+$)/, M = /^(\w*)(?:#([\w\-]+))?(?:\.([\w\-]+))?$/, B = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g, T = /^[+~]$/, A = /\\(?!\\)/g, W = /\W/, g = /^\w/, u = /\D/, f = /(-?)(\d*)(?:n([+\-]?\d*))?/, y = /^\+|\s*/g, x = /h\d/i, O = /input|select|textarea|button/i, n = /[\t\n\f\r]/g, t = "(?:[-\\w]|[^\\x00-\\xa0]|\\\\.)", J = {ID:new RegExp("#(" + t + "+)"), CLASS:new RegExp("\\.(" + t + "+)"), NAME:new RegExp("\\[name=['\"]*(" + t + "+)['\"]*\\]"), TAG:new RegExp("^(" + t.replace("[-", "[-\\*") + "+)"), ATTR:new RegExp("\\[\\s*(" + t + "+)\\s*(?:(\\S?=)\\s*(?:(['\"])(.*?)\\3|(#?" + t + "*)|)|)\\s*\\]"), PSEUDO:new RegExp(":(" + t + "+)(?:\\((['\"]?)((?:\\([^\\)]+\\)|[^\\(\\)]*)+)\\2\\))?"), CHILD:/:(only|nth|last|first)-child(?:\(\s*(even|odd|(?:[+\-]?\d+|(?:[+\-]?\d*)?n\s*(?:[+\-]\s*\d+)?))\s*\))?/, POS:/:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^\-]|$)/}, P = J.POS, Q = (function () { | |
var Y, Z = function (ab, aa) { | |
return"\\" + (aa - 0 + 1) | |
}, X = {}; | |
for (Y in J) { | |
J[Y] = new RegExp(J[Y].source + (/(?![^\[]*\])(?![^\(]*\))/.source)); | |
X[Y] = new RegExp(/(^(?:.|\r|\n)*?)/.source + J[Y].source.replace(/\\(\d+)/g, Z)) | |
} | |
J.globalPOS = P; | |
return X | |
})(), l = {}, F = {}, E = {}, L = function (X) { | |
var Y = false, aa = e.createElement("div"); | |
try { | |
Y = X(aa) | |
} catch (Z) { | |
} | |
aa = null; | |
return Y | |
}, w = L(function (Y) { | |
Y.innerHTML = "<select></select>"; | |
var X = typeof Y.lastChild.getAttribute("multiple"); | |
return X !== "boolean" && X !== "string" | |
}), i = L(function (Z) { | |
var X = true, Y = "script" + (new Date()).getTime(); | |
Z.innerHTML = "<a name ='" + Y + "'/>"; | |
j.insertBefore(Z, j.firstChild); | |
if (e.getElementById(Y)) { | |
X = false | |
} | |
j.removeChild(Z); | |
return X | |
}), c = L(function (X) { | |
X.appendChild(e.createComment("")); | |
return X.getElementsByTagName("*").length === 0 | |
}), I = L(function (X) { | |
X.innerHTML = "<a href='#'></a>"; | |
return X.firstChild && typeof X.firstChild.getAttribute !== D && X.firstChild.getAttribute("href") === "#" | |
}), H = L(function (X) { | |
X.innerHTML = "<div class='test e'></div><div class='test'></div>"; | |
if (!X.getElementsByClassName || X.getElementsByClassName("e").length === 0) { | |
return false | |
} | |
X.lastChild.className = "e"; | |
return X.getElementsByClassName("e").length !== 1 | |
}); | |
var R = function (ab, X, ad, af) { | |
ad = ad || []; | |
X = X || e; | |
var ae, Y, aa, Z, ac = X.nodeType; | |
if (ac !== 1 && ac !== 9) { | |
return[] | |
} | |
if (!ab || typeof ab !== "string") { | |
return ad | |
} | |
aa = v(X); | |
if (!aa && !af) { | |
if ((ae = N.exec(ab))) { | |
if ((Z = ae[1])) { | |
if (ac === 9) { | |
Y = X.getElementById(Z); | |
if (Y && Y.parentNode) { | |
if (Y.id === Z) { | |
return s([Y], ad) | |
} | |
} else { | |
return s([], ad) | |
} | |
} else { | |
if (X.ownerDocument && (Y = X.ownerDocument.getElementById(Z)) && G(X, Y) && Y.id === Z) { | |
return s([Y], ad) | |
} | |
} | |
} else { | |
if (ae[2]) { | |
return s(X.getElementsByTagName(ab), ad) | |
} else { | |
if (H && (Z = ae[3]) && X.getElementsByClassName) { | |
return s(X.getElementsByClassName(Z), ad) | |
} | |
} | |
} | |
} | |
} | |
return U(ab, X, ad, af, aa) | |
}; | |
var U = function (al, aa, ah, ac, ad) { | |
var aj, ag, aq, Z, ar, ab, af, ai, am, an, ap, at, ae, ak, X = aa, Y = true, ao = al; | |
if ((ai = l[al]) === r) { | |
ai = []; | |
do { | |
B.exec(""); | |
aj = B.exec(ao); | |
if (aj) { | |
ao = aj[3]; | |
ai.push(aj[1]); | |
if (aj[2]) { | |
Z = aj[3]; | |
break | |
} | |
} | |
} while (aj); | |
l[al] = ai && ai.slice(0); | |
F[al] = Z | |
} else { | |
ai = ai.slice(0); | |
Z = F[al] | |
} | |
if (ai.length > 1 && P.exec(al)) { | |
if (ai.length === 2 && K.relative[ai[0]]) { | |
ag = z(ai[0] + ai[1], aa, ac, ad) | |
} else { | |
ag = K.relative[ai[0]] ? [aa] : R(ai.shift(), aa); | |
while (ai.length) { | |
al = ai.shift(); | |
if (K.relative[al]) { | |
al += ai.shift() | |
} | |
ag = z(al, ag, ac, ad) | |
} | |
} | |
} else { | |
if (!ac && ai.length > 1 && aa.nodeType === 9 && !ad && J.ID.test(ai[0]) && !J.ID.test(ai[ai.length - 1])) { | |
ar = R.find(ai.shift(), aa, ad); | |
aa = ar.expr ? R.filter(ar.expr, ar.set)[0] : ar.set[0] | |
} | |
if (aa) { | |
ar = ac ? {expr:ai.pop(), set:s(ac)} : R.find(ai.pop(), (ai.length >= 1 && T.test(ai[0]) && aa.parentNode) || aa, ad); | |
ag = ar.expr ? R.filter(ar.expr, ar.set) : ar.set; | |
if (ai.length > 0) { | |
aq = s(ag); | |
am = 0; | |
an = aq.length; | |
ae = []; | |
for (; am < an; am++) { | |
ae[am] = am | |
} | |
} else { | |
Y = false | |
} | |
while (ai.length) { | |
ab = ai.pop(); | |
if (K.relative[ab]) { | |
af = ai.pop() | |
} else { | |
af = ab; | |
ab = "" | |
} | |
if (af == null) { | |
af = aa | |
} | |
K.relative[ab](aq, ae, af, ad); | |
aq = C.apply([], aq); | |
ae = C.apply([], ae) | |
} | |
} else { | |
aq = ai = [] | |
} | |
} | |
if (!aq) { | |
aq = ag | |
} | |
if (!aq) { | |
R.error(ab || al) | |
} | |
if (d.call(aq) === "[object Array]") { | |
if (!Y) { | |
ah.push.apply(ah, aq) | |
} else { | |
at = aa && aa.nodeType === 1; | |
ag = s(ag); | |
for (am = 0; (ap = aq[am]) != null; am++) { | |
if (ap === true || (ap.nodeType === 1 && (!at || G(aa, ap)))) { | |
ak = ae ? ae[am] : am; | |
if (ag[ak]) { | |
ah.push(ag[ak]); | |
ag[ak] = false | |
} | |
} | |
} | |
} | |
} else { | |
s(aq, ah) | |
} | |
if (Z) { | |
U(Z, X, ah, ac, ad); | |
o(ah) | |
} | |
return ah | |
}; | |
R.matches = function (X, Y) { | |
return R(X, null, null, Y) | |
}; | |
R.matchesSelector = function (X, Y) { | |
return R(Y, null, null, [X]).length > 0 | |
}; | |
R.find = function (af, X, Z) { | |
var ae, aa, ac, ab, ad, Y; | |
if (!af) { | |
return[] | |
} | |
for (aa = 0, ac = K.order.length; aa < ac; aa++) { | |
ad = K.order[aa]; | |
if ((ab = Q[ad].exec(af))) { | |
Y = ab[1]; | |
ab.splice(1, 1); | |
if (Y.substr(Y.length - 1) !== "\\") { | |
ab[1] = (ab[1] || "").replace(A, ""); | |
ae = K.find[ad](ab, X, Z); | |
if (ae != null) { | |
af = af.replace(J[ad], ""); | |
break | |
} | |
} | |
} | |
} | |
if (!ae) { | |
ae = K.find.TAG([0, "*"], X) | |
} | |
return{set:ae, expr:af} | |
}; | |
R.filter = function (ai, ah, al, ab) { | |
var X, ag, an, ac, Y, aa, ak, ad, aj, ae = E[ai], Z = ai, am = [], af = ah && ah[0] && v(ah[0]); | |
if (!ae) { | |
ae = M.exec(ai); | |
if (ae) { | |
ae[1] = (ae[1] || "").toLowerCase(); | |
ae[3] = ae[3] && (" " + ae[3] + " "); | |
E[ai] = ae | |
} | |
} | |
if (ae && !af) { | |
for (ad = 0; (ac = ah[ad]) != null; ad++) { | |
if (ac) { | |
ak = ac.attributes || {}; | |
an = (!ae[1] || (ac.nodeName && ac.nodeName.toLowerCase() === ae[1])) && (!ae[2] || (ak.id || {}).value === ae[2]) && (!ae[3] || ~(" " + ((ak["class"] || {}).value || "").replace(n, " ") + " ").indexOf(ae[3])); | |
aj = ab ^ an; | |
if (al && !aj) { | |
ah[ad] = false | |
} else { | |
if (aj) { | |
am.push(ac) | |
} | |
} | |
} | |
} | |
if (!al) { | |
ah = am | |
} | |
return ah | |
} | |
while (ai && ah.length) { | |
for (ag in K.filter) { | |
if ((ae = Q[ag].exec(ai)) && ae[2]) { | |
Y = K.filter[ag]; | |
aa = ae[1]; | |
X = false; | |
ae.splice(1, 1); | |
if (aa.substr(aa.length - 1) === "\\") { | |
continue | |
} | |
if (ah === am) { | |
am = [] | |
} | |
if (K.preFilter[ag]) { | |
ae = K.preFilter[ag](ae, ah, al, am, ab, af); | |
if (!ae) { | |
X = an = true | |
} else { | |
if (ae === true) { | |
continue | |
} | |
} | |
} | |
if (ae) { | |
for (ad = 0; (ac = ah[ad]) != null; ad++) { | |
if (ac) { | |
an = Y(ac, ae, ad, ah); | |
aj = ab ^ an; | |
if (al && an != null) { | |
if (aj) { | |
X = true | |
} else { | |
ah[ad] = false | |
} | |
} else { | |
if (aj) { | |
am.push(ac); | |
X = true | |
} | |
} | |
} | |
} | |
} | |
if (an !== r) { | |
if (!al) { | |
ah = am | |
} | |
ai = ai.replace(J[ag], ""); | |
if (!X) { | |
return[] | |
} | |
break | |
} | |
} | |
} | |
if (ai === Z) { | |
if (X == null) { | |
R.error(ai) | |
} else { | |
break | |
} | |
} | |
Z = ai | |
} | |
return ah | |
}; | |
R.attr = function (Z, Y) { | |
if (K.attrHandle[Y]) { | |
return K.attrHandle[Y](Z) | |
} | |
if (w || v(Z)) { | |
return Z.getAttribute(Y) | |
} | |
var X = (Z.attributes || {})[Y]; | |
return X && X.specified ? X.value : null | |
}; | |
R.error = function (X) { | |
throw new Error("Syntax error, unrecognized expression: " + X) | |
}; | |
if (e.querySelectorAll) { | |
(function () { | |
var ad, af = U, X = "__sizzle__", ag = /[^\\],/g, ab = /^\s*[+~]/, Z = /'/g, ac = /\=\s*([^'"\]]*)\s*\]/g, Y = [], ae = [], aa = j.matchesSelector || j.mozMatchesSelector || j.webkitMatchesSelector || j.oMatchesSelector || j.msMatchesSelector; | |
L(function (ah) { | |
ah.innerHTML = "<select><option selected></option></select>"; | |
if (!ah.querySelectorAll("[selected]").length) { | |
Y.push("\\[[\\x20\\t\\n\\r\\f]*(?:checked|disabled|ismap|multiple|readonly|selected|value)") | |
} | |
if (!ah.querySelectorAll(":checked").length) { | |
Y.push(":checked") | |
} | |
}); | |
L(function (ah) { | |
ah.innerHTML = "<p class=''></p>"; | |
if (ah.querySelectorAll("[class^='']").length) { | |
Y.push("[*^$]=[\\x20\\t\\n\\r\\f]*(?:\"\"|'')") | |
} | |
ah.innerHTML = "<input type='hidden'>"; | |
if (!ah.querySelectorAll(":enabled").length) { | |
Y.push(":enabled", ":disabled") | |
} | |
}); | |
Y = Y.length && new RegExp(Y.join("|")); | |
U = function (an, ai, ap, aq, am) { | |
if (!aq && !am && (!Y || !Y.test(an))) { | |
if (ai.nodeType === 9) { | |
try { | |
return s(ai.querySelectorAll(an), ap) | |
} catch (al) { | |
} | |
} else { | |
if (ai.nodeType === 1 && ai.nodeName.toLowerCase() !== "object") { | |
var ao, aj = ai, ak = ai.getAttribute("id"), ah = ak || X, at = ai.parentNode, ar = ab.test(an); | |
if (!ak) { | |
ai.setAttribute("id", ah) | |
} else { | |
ah = ah.replace(Z, "\\$&") | |
} | |
if (ar && at) { | |
ai = at | |
} | |
try { | |
if (!ar || at) { | |
ah = "[id='" + ah + "'] "; | |
ao = ah + an.replace(ag, "$&" + ah); | |
return s(ai.querySelectorAll(ao), ap) | |
} | |
} catch (al) { | |
} finally { | |
if (!ak) { | |
aj.removeAttribute("id") | |
} | |
} | |
} | |
} | |
} | |
return af(an, ai, ap, aq, am) | |
}; | |
if (aa) { | |
L(function (ai) { | |
ad = aa.call(ai, "div"); | |
try { | |
aa.call(ai, "[test!='']:sizzle"); | |
ae.push(K.match.PSEUDO) | |
} catch (ah) { | |
} | |
}); | |
ae = ae.length && new RegExp(ae.join("|")); | |
R.matchesSelector = function (ai, ak) { | |
ak = ak.replace(ac, "='$1']"); | |
if (!v(ai) && (!ae || ae.test(ak)) && (!Y || !Y.test(ak))) { | |
try { | |
var ah = aa.call(ai, ak); | |
if (ah || !ad || ai.document && ai.document.nodeType !== 11) { | |
return ah | |
} | |
} catch (aj) { | |
} | |
} | |
return R(ak, null, null, [ai]).length > 0 | |
} | |
} | |
})() | |
} | |
function s(aa, Z) { | |
Z = Z || []; | |
var Y = 0, X = aa.length; | |
if (typeof X === "number") { | |
for (; Y < X; Y++) { | |
Z.push(aa[Y]) | |
} | |
} else { | |
for (; aa[Y]; Y++) { | |
Z.push(aa[Y]) | |
} | |
} | |
return Z | |
} | |
var v = R.isXML = function (X) { | |
var Y = (X ? X.ownerDocument || X : 0).documentElement; | |
return Y ? Y.nodeName !== "HTML" : false | |
}; | |
var G = R.contains = j.compareDocumentPosition ? function (Y, X) { | |
return !!(Y.compareDocumentPosition(X) & 16) | |
} : j.contains ? function (Y, X) { | |
return Y !== X && (Y.contains ? Y.contains(X) : false) | |
} : function (Y, X) { | |
while ((X = X.parentNode)) { | |
if (X === Y) { | |
return true | |
} | |
} | |
return false | |
}; | |
var a = R.getText = function (ab) { | |
var Z, aa, X = ab.nodeType, Y = ""; | |
if (X) { | |
if (X === 1 || X === 9 || X === 11) { | |
if (typeof ab.textContent === "string") { | |
return ab.textContent | |
} else { | |
for (ab = ab.firstChild; ab; ab = ab.nextSibling) { | |
Y += a(ab) | |
} | |
} | |
} else { | |
if (X === 3 || X === 4) { | |
return ab.nodeValue | |
} | |
} | |
} else { | |
for (Z = 0; (aa = ab[Z]); Z++) { | |
if (aa.nodeType !== 8) { | |
Y += a(aa) | |
} | |
} | |
} | |
return Y | |
}; | |
function k(ab, an, ak, X, af) { | |
var aa, am, al, ae, Z, Y, ac, ai, ad = 0, ah = an.length, ag = typeof X === "string", aj = ++p; | |
if (ag && !W.test(X)) { | |
X = X.toLowerCase(); | |
am = true | |
} | |
for (; ad < ah; ad++) { | |
if ((aa = an[ad])) { | |
ae = []; | |
Z = 0; | |
aa = aa[ab]; | |
while (aa) { | |
if (aa[V] === aj && aa.sizLevelIndex === Z) { | |
Y = an[aa.sizset]; | |
ae = ae.length ? Y.length ? ae.concat(Y) : ae : Y; | |
break | |
} | |
al = aa.nodeType === 1; | |
if (al && !af) { | |
aa[V] = aj; | |
aa.sizset = ad; | |
aa.sizLevelIndex = Z | |
} | |
if (am) { | |
if (aa.nodeName.toLowerCase() === X) { | |
ae.push(aa) | |
} | |
} else { | |
if (al) { | |
if (!ag) { | |
if (aa === X) { | |
ae = true; | |
break | |
} | |
} else { | |
if (R.filter(X, [aa]).length > 0) { | |
ae.push(aa) | |
} | |
} | |
} | |
} | |
aa = aa[ab]; | |
Z++ | |
} | |
if ((ai = ae.length)) { | |
an[ad] = ae; | |
if (ai > 1) { | |
ak[ad] = []; | |
ac = 0; | |
for (; ac < ai; ac++) { | |
ak[ad].push(ad) | |
} | |
} | |
} else { | |
an[ad] = typeof ae === "boolean" ? ae : false | |
} | |
} | |
} | |
} | |
function z(Z, X, ad, Y) { | |
var ac, af = [], ab = "", ag = X.nodeType ? [X] : X, aa = 0, ae = ag.length; | |
while ((ac = J.PSEUDO.exec(Z))) { | |
ab += ac[0]; | |
Z = Z.replace(J.PSEUDO, "") | |
} | |
if (K.relative[Z]) { | |
Z += "*" | |
} | |
for (; aa < ae; aa++) { | |
U(Z, ag[aa], af, ad, Y) | |
} | |
return R.filter(ab, af) | |
} | |
var K = R.selectors = {match:J, leftMatch:Q, order:["ID", "NAME", "TAG"], attrHandle:{}, relative:{"+":function (ae, ad, X) { | |
var Y, Z = 0, ab = ae.length, aa = typeof X === "string", af = aa && !W.test(X), ac = aa && !af; | |
if (af) { | |
X = X.toLowerCase() | |
} | |
for (; Z < ab; Z++) { | |
if ((Y = ae[Z])) { | |
while ((Y = Y.previousSibling) && Y.nodeType !== 1) { | |
} | |
ae[Z] = ac || Y && Y.nodeName.toLowerCase() === X ? Y || false : Y === X | |
} | |
} | |
if (ac) { | |
R.filter(X, ae, true) | |
} | |
}, ">":function (ae, Z, Y) { | |
var ad, aa = 0, X = ae.length, ac = typeof Y === "string"; | |
if (ac && !W.test(Y)) { | |
Y = Y.toLowerCase(); | |
for (; aa < X; aa++) { | |
if ((ad = ae[aa])) { | |
var ab = ad.parentNode; | |
ae[aa] = ab.nodeName.toLowerCase() === Y ? ab : false | |
} | |
} | |
} else { | |
for (; aa < X; aa++) { | |
if ((ad = ae[aa])) { | |
ae[aa] = ac ? ad.parentNode : ad.parentNode === Y | |
} | |
} | |
if (ac) { | |
R.filter(Y, ae, true) | |
} | |
} | |
}, "":function (aa, Z, Y, X) { | |
k("parentNode", aa, Z, Y, X) | |
}, "~":function (aa, Z, Y, X) { | |
k("previousSibling", aa, Z, Y, X) | |
}}, find:{ID:i ? function (Z, aa, Y) { | |
if (typeof aa.getElementById !== D && !Y) { | |
var X = aa.getElementById(Z[1]); | |
return X && X.parentNode ? [X] : [] | |
} | |
} : function (Z, aa, Y) { | |
if (typeof aa.getElementById !== D && !Y) { | |
var X = aa.getElementById(Z[1]); | |
return X ? X.id === Z[1] || typeof X.getAttributeNode !== D && X.getAttributeNode("id").value === Z[1] ? [X] : r : [] | |
} | |
}, NAME:function (Z, ac) { | |
if (typeof ac.getElementsByName !== D) { | |
var Y = [], ab = ac.getElementsByName(Z[1]), aa = 0, X = ab.length; | |
for (; aa < X; aa++) { | |
if (ab[aa].getAttribute("name") === Z[1]) { | |
Y.push(ab[aa]) | |
} | |
} | |
return Y.length === 0 ? null : Y | |
} | |
}, TAG:c ? function (X, Y) { | |
if (typeof Y.getElementsByTagName !== D) { | |
return Y.getElementsByTagName(X[1]) | |
} | |
} : function (X, ab) { | |
var aa = ab.getElementsByTagName(X[1]); | |
if (X[1] === "*") { | |
var Z = [], Y = 0; | |
for (; aa[Y]; Y++) { | |
if (aa[Y].nodeType === 1) { | |
Z.push(aa[Y]) | |
} | |
} | |
aa = Z | |
} | |
return aa | |
}}, preFilter:{CLASS:function (ab, Y, Z, X, ae, aa) { | |
var ad, ac = 0; | |
ab = " " + ab[1].replace(A, "") + " "; | |
if (aa) { | |
return ab | |
} | |
for (; (ad = Y[ac]) != null; ac++) { | |
if (ad) { | |
if (ae ^ (ad.className && ~(" " + ad.className + " ").replace(n, " ").indexOf(ab))) { | |
if (!Z) { | |
X.push(ad) | |
} | |
} else { | |
if (Z) { | |
Y[ac] = false | |
} | |
} | |
} | |
} | |
return false | |
}, ID:function (X) { | |
return X[1].replace(A, "") | |
}, TAG:function (X) { | |
return X[1].replace(A, "").toLowerCase() | |
}, CHILD:function (X) { | |
if (X[1] === "nth") { | |
if (!X[2]) { | |
R.error(X[0]) | |
} | |
X[2] = X[2].replace(y, ""); | |
var Y = f.exec(X[2] === "even" && "2n" || X[2] === "odd" && "2n+1" || !u.test(X[2]) && "0n+" + X[2] || X[2]); | |
X[2] = (Y[1] + (Y[2] || 1)) - 0; | |
X[3] = Y[3] - 0 | |
} else { | |
if (X[2]) { | |
R.error(X[0]) | |
} | |
} | |
X[0] = ++p; | |
return X | |
}, ATTR:function (X) { | |
X[1] = X[1].replace(A, ""); | |
X[4] = (X[4] || X[5] || "").replace(A, ""); | |
if (X[2] === "~=") { | |
X[4] = " " + X[4] + " " | |
} | |
return X | |
}, PSEUDO:function (ac, Y, Z, X, ad, ab) { | |
if (ac[1] === "not") { | |
if ((B.exec(ac[3]) || "").length > 1 || g.test(ac[3])) { | |
ac[3] = U(ac[3], e, [], Y, ab) | |
} else { | |
var aa = R.filter(ac[3], Y, Z, !ad); | |
if (!Z) { | |
X.push.apply(X, aa) | |
} | |
return false | |
} | |
} else { | |
if (J.POS.test(ac[0]) || J.CHILD.test(ac[0])) { | |
return true | |
} | |
} | |
return ac | |
}, POS:function (X) { | |
X.unshift(true); | |
return X | |
}}, filters:{enabled:function (X) { | |
return X.disabled === false | |
}, disabled:function (X) { | |
return X.disabled === true | |
}, checked:function (X) { | |
var Y = X.nodeName.toLowerCase(); | |
return(Y === "input" && !!X.checked) || (Y === "option" && !!X.selected) | |
}, selected:function (X) { | |
if (X.parentNode) { | |
X.parentNode.selectedIndex | |
} | |
return X.selected === true | |
}, parent:function (X) { | |
return !!X.firstChild | |
}, empty:function (X) { | |
return !X.firstChild | |
}, has:function (Z, Y, X) { | |
return !!R(X[3], Z).length | |
}, header:function (X) { | |
return x.test(X.nodeName) | |
}, text:function (Z) { | |
var X = Z.getAttribute("type"), Y = Z.type; | |
return Z.nodeName.toLowerCase() === "input" && "text" === Y && (X === null || X.toLowerCase() === Y) | |
}, radio:function (X) { | |
return X.nodeName.toLowerCase() === "input" && "radio" === X.type | |
}, checkbox:function (X) { | |
return X.nodeName.toLowerCase() === "input" && "checkbox" === X.type | |
}, file:function (X) { | |
return X.nodeName.toLowerCase() === "input" && "file" === X.type | |
}, password:function (X) { | |
return X.nodeName.toLowerCase() === "input" && "password" === X.type | |
}, submit:function (Y) { | |
var X = Y.nodeName.toLowerCase(); | |
return(X === "input" || X === "button") && "submit" === Y.type | |
}, image:function (X) { | |
return X.nodeName.toLowerCase() === "input" && "image" === X.type | |
}, reset:function (Y) { | |
var X = Y.nodeName.toLowerCase(); | |
return(X === "input" || X === "button") && "reset" === Y.type | |
}, button:function (Y) { | |
var X = Y.nodeName.toLowerCase(); | |
return X === "input" && "button" === Y.type || X === "button" | |
}, input:function (X) { | |
return O.test(X.nodeName) | |
}, focus:function (X) { | |
var Y = X.ownerDocument; | |
return X === Y.activeElement && (!Y.hasFocus || Y.hasFocus()) && !!(X.type || X.href) | |
}, active:function (X) { | |
return X === X.ownerDocument.activeElement | |
}, contains:function (Z, Y, X) { | |
return ~(Z.textContent || Z.innerText || a(Z)).indexOf(X[3]) | |
}}, setFilters:{first:function (Y, X) { | |
return X === 0 | |
}, last:function (Z, Y, X, aa) { | |
return Y === aa.length - 1 | |
}, even:function (Y, X) { | |
return X % 2 === 0 | |
}, odd:function (Y, X) { | |
return X % 2 === 1 | |
}, lt:function (Z, Y, X) { | |
return Y < X[3] - 0 | |
}, gt:function (Z, Y, X) { | |
return Y > X[3] - 0 | |
}, nth:function (Z, Y, X) { | |
return X[3] - 0 === Y | |
}, eq:function (Z, Y, X) { | |
return X[3] - 0 === Y | |
}}, filter:{PSEUDO:function (Z, ad, ac, af) { | |
var X = ad[1], Y = K.filters[X]; | |
if (Y) { | |
return Y(Z, ac, ad, af) | |
} else { | |
if (X === "not") { | |
var aa = ad[3], ab = 0, ae = aa.length; | |
for (; ab < ae; ab++) { | |
if (aa[ab] === Z) { | |
return false | |
} | |
} | |
return true | |
} else { | |
R.error(X) | |
} | |
} | |
}, CHILD:function (Y, aa) { | |
var Z, ag, ac, af, ab, ae, ad = aa[1], X = Y; | |
switch (ad) { | |
case"only": | |
case"first": | |
while ((X = X.previousSibling)) { | |
if (X.nodeType === 1) { | |
return false | |
} | |
} | |
if (ad === "first") { | |
return true | |
} | |
X = Y; | |
case"last": | |
while ((X = X.nextSibling)) { | |
if (X.nodeType === 1) { | |
return false | |
} | |
} | |
return true; | |
case"nth": | |
Z = aa[2]; | |
ag = aa[3]; | |
if (Z === 1 && ag === 0) { | |
return true | |
} | |
ac = aa[0]; | |
af = Y.parentNode; | |
if (af && (af[V] !== ac || !Y.sizset)) { | |
ab = 0; | |
for (X = af.firstChild; X; X = X.nextSibling) { | |
if (X.nodeType === 1) { | |
X.sizset = ++ab; | |
if (X === Y) { | |
break | |
} | |
} | |
} | |
af[V] = ac | |
} | |
ae = Y.sizset - ag; | |
if (Z === 0) { | |
return ae === 0 | |
} else { | |
return(ae % Z === 0 && ae / Z >= 0) | |
} | |
} | |
}, ID:i ? function (Y, X) { | |
return Y.nodeType === 1 && Y.getAttribute("id") === X | |
} : function (Z, X) { | |
var Y = typeof Z.getAttributeNode !== D && Z.getAttributeNode("id"); | |
return Z.nodeType === 1 && Y && Y.value === X | |
}, TAG:function (Y, X) { | |
return(X === "*" && Y.nodeType === 1) || Y.nodeName && Y.nodeName.toLowerCase() === X | |
}, CLASS:function (Y, X) { | |
return ~(" " + (Y.className || Y.getAttribute("class")) + " ").indexOf(X) | |
}, ATTR:function (ac, aa) { | |
var Z = aa[1], X = R.attr(ac, Z), ad = X + "", ab = aa[2], Y = aa[4]; | |
return X == null ? ab === "!=" : !ab ? X != null : ab === "=" ? ad === Y : ab === "*=" ? ~ad.indexOf(Y) : ab === "~=" ? ~(" " + ad + " ").indexOf(Y) : !Y ? ad && X !== false : ab === "!=" ? ad !== Y : ab === "^=" ? ad.indexOf(Y) === 0 : ab === "$=" ? ad.substr(ad.length - Y.length) === Y : ab === "|=" ? ad === Y || ad.substr(0, Y.length + 1) === Y + "-" : false | |
}, POS:function (ab, Y, Z, ac) { | |
var X = Y[2], aa = K.setFilters[X]; | |
if (aa) { | |
return aa(ab, Z, Y, ac) | |
} | |
}}}; | |
if (!I) { | |
K.attrHandle = {href:function (X) { | |
return X.getAttribute("href", 2) | |
}, type:function (X) { | |
return X.getAttribute("type") | |
}} | |
} | |
if (H) { | |
K.order.splice(1, 0, "CLASS"); | |
K.find.CLASS = function (Y, Z, X) { | |
if (typeof Z.getElementsByClassName !== D && !X) { | |
return Z.getElementsByClassName(Y[1]) | |
} | |
} | |
} | |
[0, 0].sort(function () { | |
h = false; | |
return 0 | |
}); | |
var q, b; | |
if (j.compareDocumentPosition) { | |
q = function (Y, X) { | |
if (Y === X) { | |
m = true; | |
return 0 | |
} | |
if (!Y.compareDocumentPosition || !X.compareDocumentPosition) { | |
return Y.compareDocumentPosition ? -1 : 1 | |
} | |
return Y.compareDocumentPosition(X) & 4 ? -1 : 1 | |
} | |
} else { | |
q = function (af, ae) { | |
if (af === ae) { | |
m = true; | |
return 0 | |
} else { | |
if (af.sourceIndex && ae.sourceIndex) { | |
return af.sourceIndex - ae.sourceIndex | |
} | |
} | |
var ac, Y, Z = [], X = [], ab = af.parentNode, ad = ae.parentNode, ag = ab; | |
if (ab === ad) { | |
return b(af, ae) | |
} else { | |
if (!ab) { | |
return -1 | |
} else { | |
if (!ad) { | |
return 1 | |
} | |
} | |
} | |
while (ag) { | |
Z.unshift(ag); | |
ag = ag.parentNode | |
} | |
ag = ad; | |
while (ag) { | |
X.unshift(ag); | |
ag = ag.parentNode | |
} | |
ac = Z.length; | |
Y = X.length; | |
for (var aa = 0; aa < ac && aa < Y; aa++) { | |
if (Z[aa] !== X[aa]) { | |
return b(Z[aa], X[aa]) | |
} | |
} | |
return aa === ac ? b(af, X[aa], -1) : b(Z[aa], ae, 1) | |
}; | |
b = function (Y, X, Z) { | |
if (Y === X) { | |
return Z | |
} | |
var aa = Y.nextSibling; | |
while (aa) { | |
if (aa === X) { | |
return -1 | |
} | |
aa = aa.nextSibling | |
} | |
return 1 | |
} | |
} | |
var o = R.uniqueSort = function (Y) { | |
if (q) { | |
m = h; | |
Y.sort(q); | |
if (m) { | |
for (var X = 1; X < Y.length; X++) { | |
if (Y[X] === Y[X - 1]) { | |
Y.splice(X--, 1) | |
} | |
} | |
} | |
} | |
return Y | |
}; | |
S.Sizzle = R | |
})(window); | |
/** | |
* @class Minimal implimentation of the Promise/A API | |
*/ | |
function Promise() { | |
this.callbacks = []; | |
} | |
(function (container) { | |
function bind(obj, context) { | |
return function () { | |
return obj.apply(context, arguments); | |
}; | |
} | |
/** | |
* executes the callback in the given context after the promise has completed | |
*/ | |
Promise.prototype.then = function (callback, context) { | |
var f = bind(callback, context); | |
if (this.complete) { | |
f(this.error, this.result); | |
} else { | |
this.callbacks.push(f); | |
} | |
//return this; | |
}; | |
/** | |
* fulfills a promise with error or result (or both). | |
*/ | |
Promise.prototype.done = function (error, result) { | |
this.complete = true; | |
this.error = error; | |
this.result = result; | |
for (var i = 0; i < this.callbacks.length; i++) { | |
this.callbacks[i](error, result); | |
} | |
this.callbacks = []; | |
}; | |
/** | |
* executes a list of callbacks in parallel | |
* @return a promise object that completes when the last promise argument completes | |
*/ | |
Promise.join = function (promises) { | |
var total = promises.length; | |
var completed = 0; | |
var p = new Promise(); | |
var errors = []; | |
var results = []; | |
function notifier(i) { | |
return function (error, result) { | |
completed += 1; | |
errors[i] = error; | |
results[i] = result; | |
if (completed === total) { | |
p.done(errors, results); | |
} | |
}; | |
} | |
for (var i = 0; i < total; i++) { | |
promises[i]().then(notifier(i)); | |
} | |
return p; | |
} | |
/** | |
* executes a list of callbacks in sequence | |
* @return a promise object that completes when the last promise argument completes | |
*/ | |
Promise.chain = function (promises, error, result) { | |
var p = new Promise(); | |
if (promises.length === 0) { | |
p.done(error, result); | |
} else { | |
promises[0](error, result).then(function (res, err) { | |
promises.splice(0, 1); | |
Promise.chain(promises, res, err).then(function (r, e) { | |
p.done(r, e); | |
}); | |
}); | |
} | |
return p; | |
} | |
container.Promise = Promise; | |
})(this); | |
if (!Object.keys) { | |
/** | |
* ES5 15.2.3.14 Object.keys ( O ) | |
* https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys | |
*/ | |
Object.keys = function (c) { | |
if (c !== Object(c)) { | |
throw new TypeError("Object.keys called on non-object") | |
} | |
var a = [], | |
b; | |
for (b in c) { | |
if (Object.prototype.hasOwnProperty.call(c, b)) { | |
a.push(b) | |
} | |
} | |
return a | |
} | |
} | |
if (!Object.toArray) { | |
/** | |
* Convert an array-like object to an actual array (such as NodeList). | |
*/ | |
Object.toArray = function (array) { | |
var result = [], i; | |
for (i = 0; i < array.length; i += 1) { | |
result.push(array[i]); | |
} | |
return result; | |
} | |
} | |
if (!Array.isArray) { | |
/** | |
* ES5 15.4.3.2 | |
* @see http://es5.github.com/#x15.4.3.2 | |
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray | |
*/ | |
Array.isArray = function isArray(obj) { | |
//return obj.toString() == "[object Array]"; | |
return (obj && typeof(obj) === 'object' && 'length' in obj); | |
}; | |
} | |
if (!Array.prototype.indexOf) { | |
/** | |
* ES5 15.4.4.14 | |
* @see http://es5.github.com/#x15.4.4.14 | |
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf | |
*/ | |
Array.prototype.indexOf = function indexOf(sought /*, fromIndex */) { | |
var self = this, | |
length = self.length >>> 0; | |
if (!length) { | |
return -1; | |
} | |
var i = 0; | |
if (arguments.length > 1) { | |
i = toInteger(arguments[1]); | |
} | |
// handle negative indices | |
i = i >= 0 ? i : Math.max(0, length + i); | |
for (; i < length; i++) { | |
if (i in self && self[i] === sought) { | |
return i; | |
} | |
} | |
return -1; | |
}; | |
} | |
if (!Array.prototype.forEach) { | |
/** ES5 15.4.4.18 | |
* @see http://es5.github.com/#x15.4.4.18 | |
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/forEach | |
*/ | |
Array.prototype.forEach = function forEach(a) { | |
var b = this, | |
d = arguments[1], | |
c = -1, | |
e = b.length >>> 0; | |
if (typeof(a) === "undefined") { | |
throw new TypeError() | |
} | |
while (++c < e) { | |
if (c in b) { | |
a.call(d, b[c], c, b) | |
} | |
} | |
} | |
} | |
if (!Array.prototype.reduce2) { | |
/** | |
* ES5 15.4.4.18 | |
* @see http://es5.github.com/#x15.4.4.18 | |
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/forEach | |
*/ | |
Array.prototype.reduce2 = function reduce2(fun /*, initial*/) { | |
var self = this, | |
length = self.length >>> 0; | |
// If no callback function or if callback is not a callable function | |
if (!fun) { | |
throw new TypeError(fun + " is not a function"); | |
} | |
// no value to return if no initial value and an empty array | |
if (!length && arguments.length == 1) { | |
throw new TypeError('reduce of empty array with no initial value'); | |
} | |
var i = 0; | |
var result; | |
if (arguments.length >= 2) { | |
result = arguments[1]; | |
} else { | |
do { | |
if (i in self) { | |
result = self[i++]; | |
break; | |
} | |
// if array contains no values, no initial value to return | |
if (++i >= length) { | |
throw new TypeError('reduce of empty array with no initial value'); | |
} | |
} while (true); | |
} | |
for (; i < length; i++) { | |
if (i in self) { | |
result = fun.call(void 0, result, self[i], i, self); | |
} | |
} | |
return result; | |
}; | |
} | |
/** | |
* @class Minimal implimentation of the Promise/A API | |
*/ | |
//function Promise() { | |
// this.callbacks = []; | |
//} | |
//(function (container) { | |
// | |
// container.Promise = Promise; | |
//})(this); | |
/** | |
* @class simple AJAX abstraction layer | |
*/ | |
/** | |
* @class simple event handling abstraction | |
*/ | |
var Emitter = function () { | |
}; | |
(function (container) { | |
/** | |
* bind to an event on an object | |
*/ | |
Emitter.prototype.bind = function (event, fn) { | |
var events = this.events = this.events || {}, | |
parts = event.split(/\s+/), | |
i = 0, | |
num = parts.length, | |
part; | |
for (; i < num; i++) { | |
events[(part = parts[i])] = events[part] || []; | |
events[part].push(fn); | |
} | |
} | |
/** | |
* bind to an event on an object and unbind after the first execution | |
*/ | |
Emitter.prototype.one = function (event, fn) { | |
this.bind(event, function fnc() { | |
fn.apply(this, Object.toArray(arguments)); | |
this.unbind(event, fnc); | |
}); | |
} | |
/** | |
* remove a function bound to a particular event | |
*/ | |
Emitter.prototype.unbind = function (event, fn) { | |
var events = this.events, eventName, i, parts; | |
if (!events) return; | |
parts = event.split(/\s+/); | |
for (i = 0, num = parts.length; i < num; i++) { | |
if ((eventName = parts[i]) in events !== false) { | |
events[eventName].splice(events[eventName].indexOf(fn), 1); | |
} | |
} | |
} | |
/** | |
* fire an event | |
*/ | |
Emitter.prototype.trigger = function (event) { | |
var events = this.events, i, args; | |
if (!events || event in events === false) return; | |
args = Object.toArray(arguments, 1); | |
for (i = events[event].length - 1; i >= 0; i--) { | |
events[event][i].apply(this, args); | |
} | |
} | |
/** | |
* Add the Emitter methods directly to a target object. | |
* NOTE: this could cause conflicts with knockout model objects. | |
*/ | |
Emitter.mixin = function (destObject) { | |
var props = ['bind', 'one', 'unbind', 'trigger']; | |
for (var i = 0; i < props.length; i++) { | |
//destObject.prototype[props[i]] = Emitter.prototype[props[i]]; | |
destObject[props[i]] = Emitter.prototype[props[i]]; | |
} | |
} | |
//container.Emitter = Emitter;// || Emitter(); | |
//Emitter.mixin(window.Element); | |
//Emitter.mixin(Object); | |
})(this); | |
/** | |
* @class onDOMReady polyfill | |
*/ | |
(function (win) { | |
var timer, doc = win.document, ready = false, setup = false, stack = []; | |
function onStateChange(e) { | |
e = e || win.event; | |
if (e && e.type && (/DOMContentLoaded|load/).test(e.type)) { | |
fireDOMReady() | |
} else if (doc.readyState) { | |
if ((/loaded|complete/).test(doc.readyState)) { | |
fireDOMReady() | |
} else if (doc.documentElement.doScroll) { | |
try { | |
ready || doc.documentElement.doScroll('left') | |
} catch (e) { | |
return | |
} | |
fireDOMReady() | |
} | |
} | |
} | |
; | |
function fireDOMReady() { | |
if (!ready) { | |
ready = true; | |
for (var i = 0, len = stack.length; i < len; i++) { | |
stack[i][0].call(stack[i][1]) | |
} | |
if (doc.removeEventListener)doc.removeEventListener("DOMContentLoaded", onStateChange, false); | |
clearInterval(timer); | |
doc.onreadystatechange = win.onload = timer = null | |
} | |
} | |
; | |
win.onDOMReady = function (fn, ctx) { | |
ctx = ctx || win; | |
if (ready) { | |
fn.call(ctx); | |
return | |
} | |
if (!setup) { | |
setup = true; | |
if (doc.addEventListener)doc.addEventListener("DOMContentLoaded", onStateChange, false); | |
timer = setInterval(onStateChange, 5); | |
doc.onreadystatechange = win.onload = onStateChange | |
} | |
stack.push([fn, ctx]) | |
} | |
})(this); | |
/** | |
* default logger | |
*/ | |
window.log = (function () { | |
var i = this, b = Array.prototype.slice, d = i.console, h = {}, f, g, m = 9, c = ["error", "warn", "info", "debug", "log"], l = "assert clear count dir dirxml exception group groupCollapsed groupEnd profile profileEnd table time timeEnd trace".split(" "), j = l.length, a = []; | |
while (--j >= 0) { | |
(function (n) { | |
h[n] = function () { | |
m !== 0 && d && d[n] && d[n].apply(d, arguments) | |
} | |
})(l[j]) | |
} | |
j = c.length; | |
while (--j >= 0) { | |
(function (n, o) { | |
h[o] = function () { | |
var q = b.call(arguments), p = [o].concat(q); | |
a.push(p); | |
e(p); | |
if (!d || !k(n)) { | |
return | |
} | |
d.firebug ? d[o].apply(i, q) : d[o] ? d[o](q) : d.log(q) | |
} | |
})(j, c[j]) | |
} | |
function e(n) { | |
if (f && (g || !d || !d.log)) { | |
f.apply(i, n) | |
} | |
} | |
h.setLevel = function (n) { | |
m = typeof n === "number" ? n : 9 | |
}; | |
function k(n) { | |
return m > 0 ? m > n : c.length + m <= n | |
} | |
h.setCallback = function () { | |
var o = b.call(arguments), n = a.length, p = n; | |
f = o.shift() || null; | |
g = typeof o[0] === "boolean" ? o.shift() : false; | |
p -= typeof o[0] === "number" ? o.shift() : n; | |
while (p < n) { | |
e(a[p++]) | |
} | |
}; | |
return h | |
})(); | |
/** | |
* This is the main method for loading a widget. | |
*/ | |
var loadWidget = function (type, version, dependencies, perElementCallback) { | |
var elements = document.querySelectorAll(".cnn_widget[type='" + type + "'][version='" + version + "']"), | |
templateNames = ((elements && elements.length > 0) ? elements[0].getAttribute('templates') : 'template'), | |
templateNameArray = templateNames.split(/\s*,\s*/), | |
resources = templateNameArray.reduce2(function (result, current, index, array) { | |
result.push(widgetURI + '/templates/' + current + '.css'); | |
result.push(widgetURI + '/templates/' + current + '.html'); | |
return result; | |
}, dependencies); | |
/* load our stuff in a closure, so we don't leak anything to the global namespace */ | |
if (elements && elements.length > 0) { | |
var promises = resources.reduce2(function (result, current, index, array) { | |
result.push(function (error, result) { | |
return Loader.load(current) | |
}); | |
return result; | |
}, []); | |
Promise.chain(promises).then(function (error, result) { | |
/* for each element passed into the closure, instantiate an instance of the model, initialize it, and bind it to the element */ | |
for (var i = 0, count = elements.length; i < count; i++) { | |
(function (target) { | |
(perElementCallback) && perElementCallback(target); | |
})(elements[i]); | |
} | |
}); | |
} | |
}; | |
/* base URI for all widgets */ | |
var baseURI = '/.element/widget'; | |
function initialize(baseURI) { | |
/* all elements that match our widget criteria */ | |
var elements = Object.toArray(document.querySelectorAll('.cnn_widget')), dependencies = []; | |
('JSON' in window) || (dependencies.push(baseURI + '/json2.js')); | |
/* reduce our elements into a list of URLs */ | |
var widgets = elements.reduce2(function (result, current, index, array) { | |
var type = current.getAttribute('type'), | |
version = current.getAttribute('version'), | |
widgetURI = baseURI + '/' + type + '/' + version, | |
uri = widgetURI + '/main.js'; | |
//uri=type+'/main'; | |
if (type !== null && version !== null && widgetURI !== baseURI && result.indexOf(uri) === -1) { | |
result.push(uri); | |
} | |
return result; | |
}, dependencies); | |
var promises = widgets.reduce2(function (result, current, index, array) { | |
result.push(function (error, result) { | |
return Loader.load(current) | |
}); | |
return result; | |
}, []); | |
Promise.chain(promises).then(function (error, result) { | |
log.info("all scripts loaded.", error, result); | |
}); | |
} | |
onDOMReady(function () { | |
/** Where the magic happens... */ | |
initialize(baseURI); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment