Minor adjustments

This commit is contained in:
Hexagon 2017-02-08 21:24:58 +01:00
parent 1cf7a8f4c6
commit fd8e8a9915
5 changed files with 180 additions and 182 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,194 +1,38 @@
define(['websocket', 'crypto-js/aes', 'crypto-js/sha1', 'crypto-js/enc-utf8'], function (websocket, aes, sha1, utf8) { define(['$.utils', '$.proto'], function (utils, proto) {
var exports = { // Create a custom edition of Array, extended with $.proto
selector: 0, var ElementArray = function () {};
utilities: {}, ElementArray.prototype = new Array;
prototype: {} ElementArray.constructor = Array;
}, for(var key in proto) ElementArray.prototype[key] = proto[key];
// Shortcuts // Create to actual dollar function
utils = exports.utilities, function Dollar (selector) {
proto = exports.prototype,
/**
* Regex for matching NaN.
*
* @property reNaN
* @type {Regex}
* @private
*/
reDigits = /^\d+$/;
// The DOM selector engine
exports.selector = function (selector) {
var match, var match,
matches = []; matches = new ElementArray();
if (selector !== undefined) {
if (selector === document) { if (selector === document) {
matches.push(document); matches.push(document);
} else if (selector === window) {
matches.push(window);
} else { } else {
selector = selector.slice(1); if ((match = document.querySelectorAll(selector))) {
for( var i=0; i < match.length; i++) {
if ((match = document.getElementById(selector))) { matches.push(match[i]);
matches.push(match); }
}
} }
} }
return matches; return matches;
};
// Namespace SHA1
utils.SHA1 = function (string) {
return sha1(string).toString();
};
// Namespace encode
utils.AES = {
decrypt: function (string, fgh) {
return aes.decrypt(string, fgh).toString(utf8);
},
encrypt: function (string, fgh) {
return aes.encrypt(string, fgh).toString();
}
};
// Namespace websocket
utils.io = websocket;
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) {}
};
/**
* 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);
};
/**
* A very simple templating function.
* @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 /* '' */;
});
};
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;
};
// 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
utils.escapeHtml = (function () {
var pattern = /[&<>"'\/]/g,
entities = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
'/': '&#x2F;'
};
return function (string) {
return String(string).replace(pattern, function (s) {
return entities[s];
});
};
}());
// Extremely naive implementations of .html() and .append()
proto.html = function (string) {
this.forEach(function (element) {
element.innerHTML = string;
});
return this;
};
proto.append = function (string) {
this.forEach(function (element) {
element.innerHTML += string;
});
return this;
};
proto.first = function () {
return this[0];
};
// Naive implementations of .on()
proto.on = function (eventName, callback) {
this.forEach(function (element) {
if (element.addEventListener) {
element.addEventListener(eventName, callback, false);
} else if (element.attachEvent) {
element.attachEvent('on' + eventName, callback);
}
});
return this;
};
proto.focus = function () {
// It doesn't make sense to focus all matched elements. So we settle for the first one
if(this[0]) {
this[0].focus();
}
return this;
};
// Prepare the dollar function
function dollar (selector) {
// Get array of matches given by selector
var matches = exports.selector(selector);
// Extend the result array with the functions from exports.prototype
matches.__proto__ = Object.assign(matches.__proto__, exports.prototype);
// Return matches, with extended functions (.on, .html, .first etc.)
return matches;
} }
// Extend the dollar function with the functions of exports.utilities (.isDigits etc.) // Add utils to Dollar
dollar.__proto__ = exports.utilities; for(var key in utils) Dollar[key] = utils[key];
return dollar; return Dollar;
}); });

47
public/js/lib/$.proto.js Normal file
View File

@ -0,0 +1,47 @@
define(function () {
var
exports = {};
// Extremely naive implementations of .html() and .append()
exports.html = function (string) {
this.forEach(function (element) {
element.innerHTML = string;
});
return this;
};
exports.append = function (string) {
this.forEach(function (element) {
element.innerHTML += string;
});
return this;
};
exports.first = function () {
return this[0];
};
// Naive implementations of .on()
exports.on = function (eventName, callback) {
this.forEach(function (element) {
if (element.addEventListener) {
element.addEventListener(eventName, callback, false);
} else if (element.attachEvent) {
element.attachEvent('on' + eventName, callback);
}
});
return this;
};
exports.focus = function () {
// It doesn't make sense to focus all matched elements. So we settle for the first one
if(this[0]) {
this[0].focus();
}
return this;
};
return exports;
});

107
public/js/lib/$.utils.js Normal file
View File

@ -0,0 +1,107 @@
define(['websocket','crypto-js/aes', 'crypto-js/sha1', 'crypto-js/enc-utf8'],function (websocket, aes, sha1, utf8) {
var
exports = {},
reDigits = /^\d+$/;
// Namespace websocket
exports.io = websocket;
// Namespace SHA1
exports.SHA1 = function (string) {
return sha1(string).toString();
};
// Namespace encode
exports.AES = {
decrypt: function (string, fgh) {
return aes.decrypt(string, fgh).toString(utf8);
},
encrypt: function (string, fgh) {
return aes.encrypt(string, fgh).toString();
}
};
exports.ssplit = function (string, seperator) {
var components = string.split(seperator);
return [components.shift(), components.join(seperator)];
};
exports.activeElement = function () {
try { return document.activeElement; } catch (e) {}
};
/**
* 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`
*/
exports.isDigits = function(value) {
return reDigits.test(value);
};
/**
* A very simple templating function.
* @param {} str [description]
* @param {[type]} map [description]
* @return {[type]} [description]
*/
exports.template = function (str, map) {
return str && str.replace(/{(\w+)}/gi, function(outer, inner) {
return map.hasOwnProperty(inner) ? map[inner] : outer /* '' */;
});
};
exports.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;
};
// 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
exports.escapeHtml = (function () {
var pattern = /[&<>"'\/]/g,
entities = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
'/': '&#x2F;'
};
return function (string) {
return String(string).replace(pattern, function (s) {
return entities[s];
});
};
}());
return exports;
});