Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/inertia-v3_beta.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243(function() { // Inertia's Class Module V2 [www.khanacademy.org/cs/_/5398825551822848] // Class Module acts like the ES6 `class` keyword replacement Define("Class", function() { var Util = require("Util"), Fn = require("Func"), _ = Util._, args = Util.args, Class, Static; // Set Static Methods Static = { // Add Methods to a Class Method: function() { var Parent = this.SuperClass && this.SuperClass.prototype; _.each(args(arguments), function(obj) { obj = Util.FnVal(obj, [this, this.constructor], this.prototype); var _keys = _.keys(obj); _.each(_keys, function(i) { var val = obj[i], preVal = val; // If a Parent Class is Present, Set any argument/params named `$super` to the `Parent` if (_.isFunction(val)) { if (Parent && val.argNames && val.argNames()[0] === "$super") { val = _.wrap(function() { return function() { return (Parent[i].apply(this, arguments)); }; }(), preVal); } val.valueOf = preVal.valueOf.bind(preVal); val.toString = preVal.toString.bind(preVal); } this.prototype[i] = val; // Redefinition Error Fix if (typeof val === "object" && val !== undefined && val !== null && (val.get || val.set || val.$$prop) && !val._class) { Object.defineProperty(this.prototype, i, val); } }, this); }, this); return this; }, // Set Static Methods Static: function() { _.each(Util.args(arguments), function(obj) { obj = Util.FnVal(obj, [this, this.constructor], this.prototype); var _keys = _.keys(obj); _.each(_keys, function(i) { var val = obj[i], preVal = val; if (_.isFunction(val)) { val.valueOf = preVal.valueOf.bind(preVal); val.toString = preVal.toString.bind(preVal); } this[i] = val; // Redefinition Error Fix if (typeof val === "object" && val !== undefined && val !== null && (val.get || val.set || val.$$prop) && !val._class) { Object.defineProperty(this, i, val); } }, this); }, this); return this; }, // Set Defaults or Backups for Objects Default: function(obj) { return function() { var result; _.each(args(arguments).reverse(), function(val) { if (!_.isUndefined(Util.path(obj, val.split(".")[0]))) { result = Util.path(obj, val); } }, this); return result; }.bind(this); }, // Create an Alias/Copy of a Static Method that can function as a Prototype Method Alias: function(obj, chainable, notStatic) { var result = {}; chainable = chainable || []; _.each(obj, function(val, i) { result[i] = function() { var _args = notStatic ? Array.from(arguments) : [this] .concat(Array.from(arguments)); if (chainable.includes(i)) { val.apply(this, _args); return this; } return val.apply(this, _args); };
var toStr = val.toString.bind(val); result[i].toString = chainable.includes(i) ? Fn('return ' + toStr() + '+"return this;";') : toStr; result[i].valueOf = val.valueOf.bind(val); }); return result; }, // Access Attributes and Properties of a Class (It has many Uses) Attr: function(path, val) { if (_.isObject(path) && !_.isArray(path)) { return _.extend(this, path); } else if (_.isArray(path)) { if (_.isUndefined(val)) { return _.map(path, function(key) { return Util.path(this, key); }, this); } else { _.each(path, function(key) { Util.path(this, key, val); }, this); } } else if (path && val) { this[path] = val; } else { return Util.path(this, path); } return this; }, // Create Classes Create: function () { var Class, SubClass, Parent, arg = args(arguments); // SubClass Constructor SubClass = function() {}; // Set Parent Constructor here is any if (arg[0] && _.isFunction(arg[0]) || (this && _.isArray(this.SubClasses))) { if (_.isArray(this.SubClasses)) { Parent = this; } else { Parent = arg.shift(); } } // Class Object Class = function() { // Current Class if (!(this instanceof Class)) { return Fn.new(Class, arguments); } this._args = arguments; // Arguements // Initialize Class return this.init.apply(this, arguments); }; // Extend Parent Class If any if (Parent) { Parent.prototype.constructor = Parent; SubClass.prototype = Parent.prototype; Class.prototype = new SubClass(); void(Parent.SubClasses && Parent.SubClasses.push(Class)); } Class.SuperClass = Parent; // Current Class's Parent if any Class.SubClasses = []; // List of SubClasses // Extend Static Class _.extend(Class, Static, { // Based on [khanacademy.org/cs/_/4684587452399616] _freed: [], // For creating more efficient Classes // Creates efficient new Classes new: function (arg) { var _class; arg = arg.length ? arg : [arg]; if (Class._freed.length > 0) { _class = Class._freed.pop(); _class.init.apply(_class, arg); } else { _class = Util.new(Class, arg); } return Util.new(Class, arg); }, }); // Give Chainability _.extend(Class.prototype, Fn, Static, Class, { // Put Classes in a place to be recycled free: function () { Class._freed.push(this); }, }); // Add Methods to Class Class.Method.apply(Class, arg); // Set Current Class Type if (!Class.prototype._class) { Class.prototype._class = "Object"; } // Set Class Constructor Class.prototype.constructor = Class; if (!Class.prototype.init) { Class.prototype.init = function() {}; } else { // Set toString & toValue Class.toString = Class.prototype.init.toString; Class.toValue = Class.prototype.init.toValue; } // Call Super Class Methods Class.prototype.CallSuper = function(method) { var _Parent = null, $ = this, arg = args(arguments, 1), _const = $, _super = _const.SuperClass; // Climb prototype chain to find method not equal to callee's method while (_super) { if ($[method] !== _super.prototype[method]) { _Parent = _super.prototype[method]; break; } $ = _super.prototype; _const = $.constructor; _super = _const.SuperClass; } if (!_Parent) { println(method + ', method not found in prototype chain.'); return; } return (arg.length > 0) ? _Parent.apply(this, arg) : _Parent.bind(this) (); }; return Class; }, // Easy Access to Configurable attributes get: function (val) { var _val = Object.constructor("with (this) return " + val); _val.toString = val.toString; return { get: _val }; }, set: function (val) { var _val = Object.constructor("with (this) return " + val); _val.toString = val.toString; return { set: _val }; } }; // Alias Methods _.extend(Static, { Extends: Static.Create, // Extend from another Class // Add Prototype Methods to Class Method: Static.Method, AddTo: Static.Method, Prop: Static.Method, }); // Create lowercase alias Methods _.each(Static, function (val, i) { Static[i.toLowerCase()] = val; }); // Class Object Class = Static.Create; _.extend(Class, Static); return Class; });})(); // Class