-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathroot.js
212 lines (176 loc) · 6.49 KB
/
root.js
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
// Root Version SC.05.27.12
///////////////////////////////////////////////////////////////////////////////
// RootJS is basically a way to write much easier OOP code
// Root.DOM is a helper object to inherit from, so you get a bunch of really cool
// dom stuff, like events, objects tied to elements, and a jQuery plugin arch
///////////////////////////////////////////////////////////////////////////////
(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);
},
// 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); }
// 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({opts : 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
return el.instance[method].apply(el.instance,args);
} 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) {
if(!scope) {
scope = method;
method = sel;
sel = undefined;
}
var fn = function(e) { method.call(scope, e, this); }
sel ? $(this).on(type,sel, fn) : $(this).on(type,fn);
return this;
}
})(jQuery,this,document)