initial commit

This commit is contained in:
Blue 2018-08-05 00:46:25 +03:00 committed by Юрий Губич
commit 4b60ece582
327 changed files with 28286 additions and 0 deletions

View file

@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 2.8.12)
configure_file(class.js class.js)
configure_file(subscribable.js subscribable.js)
configure_file(globalMethods.js globalMethods.js)

52
libjs/utils/class.js Normal file
View file

@ -0,0 +1,52 @@
"use strict";
var Class = function() {};
Class.inherit = function(proto) {
var superClass = this;
var Base = function() {};
var subclass = proto && (proto.constructor !== Object) ? proto.constructor : function() {
superClass.apply(this, arguments)
};
Base.prototype = this.prototype;
var fn = new Base();
for (var key in proto) {
if (proto.hasOwnProperty(key)) {
fn[key] = proto[key];
}
}
for (var method in this) {
if (this.hasOwnProperty(method)) {
subclass[method] = this[method];
}
}
fn.constructor = subclass;
subclass.prototype = subclass.fn = fn;
return subclass;
}
module.exports = Class.inherit({
className: "Class",
constructor: function() {
if (!(this instanceof Class)) {
throw new SyntaxError("You didn't call \"new\" operator");
}
Class.call(this);
this._uncyclic = [];
},
destructor: function() {
for (var i = 0; i < this._uncyclic.length; ++i) {
this._uncyclic[i].call(this);
}
for (var key in this) {
this[key] = undefined;
delete this[key];
}
}
});

View file

@ -0,0 +1,69 @@
"use strict";
global.W = {
extend: function () {
var lTarget = arguments[0] || {};
var lIndex = 1;
var lLength = arguments.length;
var lDeep = false;
var lOptions, lName, lSrc, lCopy, lCopyIsArray, lClone;
// Handle a deep copy situation
if (typeof lTarget === "boolean") {
lDeep = lTarget;
lTarget = arguments[1] || {};
// skip the boolean and the target
lIndex = 2;
}
// Handle case when target is a string or something (possible in deep
// copy)
if (typeof lTarget !== "object" && typeof lTarget != "function") {
lTarget = {};
}
if (lLength === lIndex) {
lTarget = this;
--lIndex;
}
for (; lIndex < lLength; lIndex++) {
// Only deal with non-null/undefined values
if ((lOptions = arguments[lIndex]) != undefined) {
// Extend the base object
for (lName in lOptions) {
lSrc = lTarget[lName];
lCopy = lOptions[lName];
// Prevent never-ending loop
if (lTarget === lCopy) {
continue;
}
// Recurse if we're merging plain objects or arrays
if (lDeep && lCopy && (Object.isObject(lCopy) || (lCopyIsArray = Array.isArray(lCopy)))) {
if (lCopyIsArray) {
lCopyIsArray = false;
lClone = lSrc && Array.isArray(lSrc) ? lSrc : [];
} else {
lClone = lSrc && Object.isObject(lSrc) ? lSrc : {};
}
// Never move original objects, clone them
lTarget[lName] = Object.extend(lDeep, lClone, lCopy);
// Don't bring in undefined values
} else {
if (lCopy !== undefined) {
lTarget[lName] = lCopy;
}
}
}
}
}
// Return the modified object
return lTarget;
}
};

View file

@ -0,0 +1,94 @@
"use strict";
var Class = require("./class");
var Subscribable = Class.inherit({
"className": "Subscribable",
"constructor": function() {
Class.fn.constructor.call(this);
this._events = Object.create(null);
},
"destructor": function() {
this.off();
Class.fn.destructor.call(this);
},
"on": function(name, handler, context) {
var handlers = this._events[name];
if (typeof name !== "string") {
throw new Error("Name of event is mandatory");
}
if (!(handler instanceof Function)) {
throw new Error("Handler of event is mandatory");
}
if (!handlers) {
handlers = [];
this._events[name] = handlers;
}
handlers.push({
handler: handler,
context: context || this,
once: false
});
},
"one": function(name) {
Subscribable.fn.on.apply(this, arguments);
this._events[name][this._events[name].length - 1].once = true;
},
"off": function(name, handler, context) {
if (typeof name === "string") {
if (this._events[name]) {
if (handler instanceof Function) {
var handlers = this._events[name];
for (var i = handlers.length - 1; i >= 0 ; --i) {
if (handlers[i].handler === handler) {
if (context) {
if (handlers[i].context === context) {
handlers.splice(i, 1);
}
} else {
handlers.splice(i, 1);
}
}
}
} else {
delete this._events[name];
}
}
} else {
this._events = Object.create(null);
}
},
"trigger": function() {
var args = [].slice.call(arguments);
if (args.length === 0) {
throw new Error("Name of event is mandatory");
}
var answer = false;
var name = args.shift();
var handlers = this._events[name];
if (handlers) {
for (var i = 0; i < handlers.length; ++i) {
var handle = handlers[i];
answer = handle.handler.apply(handle.context, args);
if (handle.once) {
handlers.splice(i, 1);
--i;
}
if (answer === false) {
return false;
}
}
}
return answer;
}
});
module.exports = Subscribable;