-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroot.js
More file actions
291 lines (250 loc) · 7.65 KB
/
root.js
File metadata and controls
291 lines (250 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Root Version SC.11.01.12
///////////////////////////////////////////////////////////////////////////////
// RootJS is basically a way to write much easier OOP code
///////////////////////////////////////////////////////////////////////////////
(function($, window, document, undefined) {
"use strict";
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
////// Root
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
window.Root = {
_construct : function() {}, // construct method
_init : function() {}, // method called on every instantiation
proto : function () { // shortcut to parent object
return Object.getPrototypeOf(this);
},
// borrowed and modifed from jQuery source. They do it the best.
// we use our own check methods though
extend : function() {
var options, name, src, copy, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = true;
// Handle case when target is a string or something (possible in deep copy)
if ( typeof target !== "object" && typeof target != "function" ) {
target = {};
}
for ( ; i < length; i++ ) {
// Only deal with non-null/undefined values
if ( (options = arguments[ i ]) != null ) {
// Extend the base object
for ( name in options ) {
src = target[ name ];
copy = options[ name ];
// Prevent never-ending loop
if ( target === copy ) {
continue;
}
// Recurse if we're merging plain objects or arrays
if ( deep && copy && ( this.isPlainObject(copy) )) {
if(src && Array.isArray(src)) {
clone = src && Array.isArray(src) ? src : [];
} else {
clone = src && this.isPlainObject(src) ? src : {};
}
// Never move original objects, clone them
target[ name ] = this.extend( deep, clone, copy );
// Don't bring in undefined values
} else if ( copy !== undefined ) {
target[ name ] = copy;
}
}
}
}
// Return the modified object
return target;
},
// tests if object is a plain object
isPlainObject : function(obj) {
// typeof null == 'object' haha
if(obj !== null && typeof obj === "object") {
// yea!
return this.proto.call(obj).hasOwnProperty("hasOwnProperty");
} else {
return false;
}
},
// wrapper for set and get
// takes the property you wanna set, and calls the method on 'this'
// optional placeholder to init with
setter : function(prop, method, placeHolder) {
var placeHolder = placeHolder;
Object.defineProperty( this, prop, {
get : function() {return placeHolder},
set : function(val) {
placeHolder = val;
this[method](val);
}
});
},
// convience method
define : function() {
return this.inherit.apply(this,arguments);
},
on : function(type,method,scope) {
var fn = function(e) { method.call(scope, e, this); };
$(this).on(type,fn);
return this;
},
super : function() {
// call the constructor of the parents parent
this.proto().proto()._construct.call(this)
},
// object inheritance method
// takes an object to extend the class with, &| a dom element to use
inherit: function(values, el) {
// create a copy of the parent and copy over new values
// normally Object.create() takes a 2nd param to extend properties. But when you use that,
// you can't use use an easy JSON structure, you have to define enumerable, writable, and configurable and value
// FOR EVERY PROPERTY. So.. we just do it ourselves with this.extend
var parent = this, instance;
if(typeof el != "undefined") values.el = el;
// handle arrays
if(Array.isArray(values)) {
instance = values;
this.extend(instance, parent);
} else {
instance = Object.create(parent);
// now do a deep copy
this.extend(instance, values);
}
// if the parent element has a constructor, call it on the instances
if(parent.hasOwnProperty("_construct")) { parent._construct.apply(instance); }
// traverse back up looking for super methods
var _parent = parent;
while(!Root.isPlainObject(_parent)) {
if(_parent.hasOwnProperty("_super")) {
_parent._super.call(instance);
break;
} else {
_parent = _parent.proto();
}
}
// if i have an _init function, call me
if(instance.hasOwnProperty("_init")) { instance._init(); }
// return the new instance
return instance;
},
jQueryPlugin : function(name) {
// pull the name out of the first argument
var args = Array.prototype.slice.call(arguments);
args.splice(0,1);
// this does our Grid = Root.inherit(stuff)
// so this is just like the main definition of the main object
var Obj = this.inherit.apply(this,args);
// create the jQuery Plugin
$.fn[name] = function(user_opts) {
var args = Array.prototype.slice.call(arguments),
$ret = $(),
el;
for(var i=0;i<this.length;i++) {
el = this[i];
if(typeof el.instance == "undefined") {
if(!user_opts) user_opts = {};
// store the instance on the element
// THIS LINE HAS BEEN ALTERED FOR OPENJSGRID
el.instance = Obj.inherit(user_opts, el);
}
// passing a string calls a method
if(typeof user_opts === "string") {
var method = user_opts, property = user_opts;
// call the method, passing it all params (except first 1)
if(typeof el.instance[method] == "function") {
args.splice(0,1); // first arg is the method name, we dont need this
el.instance[method].apply(el.instance,args);
return el;
} else {
// if there was just a property, get it
if(args.length == 1) {
return el.instance[property];
// if there was a property and a value, set it
} else if (args.length == 2) {
el.instance[property] = args[1];
return args[1];
}
}
// passed an object in
} else {
this.extend(el.instance, user_opts);
}
// push our new elements onto the jquery collection
// THIS LINE HAS BEEN ALTERED FOR OPENJSGRID
// normally you would just return the el. but in this case, we change the el
$ret.push(el.instance.el);
}
return $ret;
//return $ret;
}
}
};
// jquery helper
$.fn._on = function(type,sel,method,scope,extraData) {
if(!scope) {
scope = method;
method = sel;
sel = undefined;
}
var fn = function(e, data) {
method.call(scope, e, this, data);
};
sel ? $(this).on(type,sel, fn) : $(this).on(type,fn);
return this;
};
// Root Collections
var _array = window.Root.inherit([]);
window.Root.Collection = _array.define({
has: function(value) {
if ( this.indexOf(value) !== -1 ) {
return true;
} else {
return false;
}
},
add: function(value) {
if ( !this.has(value) ) {
this.push(value);
return true;
} else {
return false;
}
},
remove: function(value) {
var index = this.indexOf(value);
if ( index !== -1 ) {
this.splice(index, 1);
}
return this;
},
clear: function() {
this.splice(0, this.length);
},
index: function() {
return this.indexOf(this);
},
random: function() {
return this[Math.ceil(Math.random() * this.length - 1)];
},
get: function(item){
return this[item];
}
});
window.Root.IndexCollection = Root.Collection.inherit({
source: null,
add: function(id) {
if(!~this.indexOf(id)){
this.push(id);
return true;
}
return this;
},
remove: function(id) {
console.log('id', id);
var index = this.indexOf(id);
if ( index !== -1 ) {
this.splice(index, 1);
}
return this;
}
});
})(jQuery, this, document);