2014-09-25 04:15:41 -04:00
|
|
|
define(['fandango', 'websocket', 'aes', 'SHA1'], function (fandango, websocket, aes, SHA1) {
|
2014-09-18 12:21:07 -04:00
|
|
|
var exports = {
|
|
|
|
selector: 0,
|
|
|
|
utilities: {},
|
|
|
|
prototype: {}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Shortcuts
|
|
|
|
utils = exports.utilities,
|
|
|
|
proto = exports.prototype,
|
|
|
|
|
2014-09-21 19:04:52 -04:00
|
|
|
each = fandango.each,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Regex for matching NaN.
|
|
|
|
*
|
|
|
|
* @property reNaN
|
|
|
|
* @type {Regex}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
reDigits = /^\d+$/;
|
2014-09-25 04:15:41 -04:00
|
|
|
|
2014-09-18 12:21:07 -04:00
|
|
|
// The DOM selector engine
|
|
|
|
exports.selector = function (selector) {
|
|
|
|
var match,
|
|
|
|
matches = [];
|
|
|
|
|
|
|
|
if (selector === document) {
|
|
|
|
matches.push(document);
|
|
|
|
} else {
|
|
|
|
selector = selector.slice(1);
|
|
|
|
|
2014-09-24 11:20:20 -04:00
|
|
|
if ((match = document.getElementById(selector))) {
|
2014-09-18 12:21:07 -04:00
|
|
|
matches.push(match);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must ALWAYS return a native array: []
|
|
|
|
return matches;
|
|
|
|
};
|
|
|
|
|
2014-09-25 04:15:41 -04:00
|
|
|
// Namespace SHA1
|
|
|
|
utils.SHA1 = function (string) {
|
|
|
|
return SHA1(string).toString();
|
2014-09-18 12:21:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Namespace encode
|
|
|
|
utils.AES = {
|
|
|
|
decrypt: function (string, fgh) {
|
|
|
|
return aes.decrypt(string, fgh).toString(CryptoJS.enc.Utf8);
|
|
|
|
},
|
2014-09-23 08:11:48 -04:00
|
|
|
|
2014-09-18 12:21:07 -04:00
|
|
|
encrypt: function (string, fgh) {
|
|
|
|
return aes.encrypt(string, fgh).toString();
|
2014-09-23 08:11:48 -04:00
|
|
|
}
|
2014-09-18 12:21:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Namespace websocket
|
|
|
|
utils.Websocket = {
|
|
|
|
connect: websocket.connect,
|
|
|
|
on: websocket.on
|
|
|
|
};
|
|
|
|
|
|
|
|
utils.ssplit = function (string, seperator) {
|
|
|
|
var components = string.split(seperator);
|
|
|
|
return [components.shift(), components.join(seperator)];
|
|
|
|
};
|
|
|
|
|
|
|
|
utils.activeElement = function () {
|
|
|
|
try { return document.activeElement; } catch (e) {}
|
2014-09-24 11:20:20 -04:00
|
|
|
};
|
2014-09-18 12:21:07 -04:00
|
|
|
|
2014-09-21 19:04:52 -04:00
|
|
|
/**
|
|
|
|
* Removes all characters but 0 - 9 from given string.
|
|
|
|
*
|
|
|
|
* @method digits
|
|
|
|
* @param {String} str The string to sanitize
|
|
|
|
* @return {String} The sanitized string
|
|
|
|
* @example
|
|
|
|
* $.digits('foo8bar'); // `8`
|
|
|
|
* $.digits('->#5*duckM4N!!!111'); // `54111`
|
|
|
|
*/
|
|
|
|
utils.isDigits = function(value) {
|
|
|
|
return reDigits.test(value);
|
|
|
|
};
|
|
|
|
|
2014-09-18 12:21:07 -04:00
|
|
|
/**
|
2014-09-21 15:09:08 -04:00
|
|
|
* A very simple templating function.
|
2014-09-18 12:21:07 -04:00
|
|
|
* @param {} str [description]
|
|
|
|
* @param {[type]} map [description]
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
|
|
|
utils.template = function (str, map) {
|
|
|
|
return str && str.replace(/{(\w+)}/gi, function(outer, inner) {
|
|
|
|
return map.hasOwnProperty(inner) ? map[inner] : outer /* '' */;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-09-21 19:04:52 -04:00
|
|
|
utils.getJSON = function (path, onSuccess, onError) {
|
|
|
|
var data, request = new XMLHttpRequest();
|
|
|
|
request.open('GET', path, true);
|
|
|
|
|
|
|
|
request.onreadystatechange = function() {
|
|
|
|
if (this.readyState === 4) {
|
|
|
|
if (this.status >= 200 && this.status < 400) {
|
|
|
|
try {
|
|
|
|
onSuccess && onSuccess(JSON.parse(this.responseText));
|
|
|
|
} catch (e) {
|
|
|
|
onError && onError();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
onError && onError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
request.send();
|
|
|
|
request = null;
|
|
|
|
};
|
|
|
|
|
2014-09-20 09:34:38 -04:00
|
|
|
// Part of this is originating from mustasche.js
|
|
|
|
// Code: https://github.com/janl/mustache.js/blob/master/mustache.js#L43
|
|
|
|
// License: https://github.com/janl/mustache.js/blob/master/LICENSE
|
2014-09-18 12:21:07 -04:00
|
|
|
utils.escapeHtml = (function () {
|
|
|
|
var pattern = /[&<>"'\/]/g,
|
|
|
|
entities = {
|
|
|
|
'&': '&',
|
|
|
|
'<': '<',
|
|
|
|
'>': '>',
|
|
|
|
'"': '"',
|
|
|
|
"'": ''',
|
|
|
|
'/': '/'
|
|
|
|
};
|
|
|
|
|
|
|
|
return function (string) {
|
|
|
|
return String(string).replace(pattern, function (s) {
|
|
|
|
return entities[s];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
|
|
|
// Extremely naive implementations of .html() and .append()
|
|
|
|
proto.html = function (string) {
|
|
|
|
each(this, function (element) {
|
|
|
|
element.innerHTML = string;
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
proto.append = function (string) {
|
2014-09-23 08:11:48 -04:00
|
|
|
for (var i = 0, len = this.length; i < len; i++) {
|
|
|
|
this[0].innerHTML += string;
|
|
|
|
}
|
2014-09-18 12:21:07 -04:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2016-01-02 13:13:25 -05:00
|
|
|
proto.first = function () {
|
|
|
|
return this[0];
|
|
|
|
};
|
|
|
|
|
2014-09-23 08:11:48 -04:00
|
|
|
// Naive implementations of .on()
|
2014-09-18 12:21:07 -04:00
|
|
|
proto.on = function (eventName, callback) {
|
2014-09-23 08:11:48 -04:00
|
|
|
for (var i = 0, len = this.length; i < len; i++) {
|
|
|
|
if (this[0].addEventListener) {
|
|
|
|
this[0].addEventListener(eventName, callback, false);
|
|
|
|
} else if (this[0].attachEvent) {
|
|
|
|
this[0].attachEvent('on' + eventName, callback);
|
|
|
|
}
|
|
|
|
}
|
2014-09-18 12:21:07 -04:00
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
proto.focus = function () {
|
|
|
|
this[0].focus();
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
})
|