-
Notifications
You must be signed in to change notification settings - Fork 1
/
mog.js
327 lines (243 loc) · 9.32 KB
/
mog.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Mog.js 0.1.0
// ==============================
// Mog - Markup Object Generator
// A core thing to extend and customize. The basic concept is that
// it attempts to manage and be a representation of the data
// contained in html, instead of a database
(function ( exports, document ) {
"use strict";
// The Core
// -----------
// Core is extended out into the Mog family of fun.
var Core = function () {
// pipeline will be used in the emitting of events
this.pipeline = {};
// this is where we keep all of our object's information
this.data = {};
this.build.apply( this, arguments );
this.initialize.apply( this, arguments );
};
Core.prototype = {
// initialize is defined when someone extends Core,
// so it's customized code tailored to the extension
initialize: function () {},
// build is sort of the Other Initialize, for example, when
// all Mogs absolutely need to do something upon construction that
// can't be overwritten in initialize.
build: function () {},
// I'm lazy
iterate: function( list, fn ) {
for ( var item in list ) {
if ( list.hasOwnProperty( item ) ) {
fn.call( this, list, item );
}
}
},
// loads up callbacks to run in event namespaces
on: function( events, callBack ) {
var cleanEvent;
events.split( "," ).forEach( function( rawEvent ) {
var cleanEvent = rawEvent.trim();
this.pipeline[cleanEvent] = this.pipeline[cleanEvent] || [];
this.pipeline[cleanEvent].push(callBack);
}, this );
return this; // chaining
},
// runs callbacks in event namesapces
trigger: function( event, value ) {
var i;
if ( this.pipeline[event] && (i = this.pipeline[event].length) ) {
while ( i-- ) {
this.pipeline[event][i].call( this, value );
}
}
},
get: function( property ) {
return this.data[property];
}
};
// extend
// ---------
// basically Backbone's extend, everyone gets it
var extend = Core.extend = function( properties ) {
var parent = this;
var Class = function () {
parent.apply( this, arguments );
};
Class.prototype = Object.create( parent.prototype );
Class.prototype.iterate( properties, function( properties, property ) {
Class.prototype[property] = properties[property];
});
Class.extend = parent.extend;
return Class;
};
// Role
// -------
// Provides the core functionality for role elements
var Role = Core.extend({
// all roles need at least this stub for disseminate
// because Mog expects roles to do something when
// Mog tells them the property they're tied has changed
disseminate: function () {}
});
// Mog
// ------
var Mog = Core.extend({
build: function ( modelName ) {
this.model = modelName;
this.roles = Mog.roles;
},
factories: {},
consume: function ( role ) {
var elements = document.querySelectorAll( "*[data-mog-" + role + "^='" + this.model + "']" ),
x = elements.length,
attribute, property, newRole;
this[role] = this[role] || {};
while (x--) {
attribute = elements[x].getAttribute("data-mog-" + role);
property = attribute.slice( attribute.indexOf( "[" ) + 1, -1 );
this[role][property] = this[role][property] || [];
newRole = new this.factories[role]({
el: elements[x],
property: property,
parent: this
});
newRole.on("change", this.set.bind( this ) );
this[role][property].push( newRole );
}
},
sync: function () {
this.roles.forEach( this.consume, this );
this.pull();
},
get: function( property ) {
return this.data[property];
},
set: function( properties, stopProp ) {
this.iterate( properties, function( properties, property ) {
this.data[property] = properties[property];
this.push(property);
// unless the event functionality is supressed, we
// trigger the event for more complex interactions
// and pass the new value
if ( !stopProp ) {
this.trigger( "set." + property, properties[property] );
}
});
},
// pulls data from the inputs into mog
pull: function () {
this.iterate( this.input, function( inputs, property ) {
inputs[property].forEach(function( input ) {
this.data[property] = input.value() || null;
}, this);
});
},
// takes [role][property] and moves its data[property] into it
disseminate: function ( elements ) {
elements.forEach(function( element, i ) {
element.disseminate( this.get( element.get("property") ) );
}, this );
},
// pushes data from mog to the inputs / outputs
push: function( property ) {
this.roles.forEach( function ( role ) {
if ( property === undefined ) {
this.iterate( this[role], function( elements, property ) {
this.disseminate( this[role][property] );
});
} else if ( this[role][property] ) {
this.disseminate( this[role][property] );
}
}, this );
}
});
// addRole
// ----------
// adds a role like input or output to what Mog will expect to find in
// the data-mog-roleName="model[property]" pattern, along with adding
// functionality for it
var addRole = Mog.addRole = function ( name, extensions ) {
Mog.roles = Mog.roles || [];
Mog.roles.push(name);
Mog.prototype.factories[name] = Role.extend( extensions );
};
Mog.addRole( "input", {
factories: {
// each input type gets its own getValue and setValue function
getValue: {
checkbox: function() {
return ( this.el.checked ) ? this.el.value : null;
},
"select-one": function() {
return this.el[this.el.selectedIndex].text;
},
"select-multiple": function() {
var length = this.el.selectedOptions.length,
collection = [];
while ( length-- ) {
collection.push( this.el.selectedOptions[length].text );
}
return collection.join(", ");
},
generic: function() {
return this.el.value;
}
},
setValue: {
radio: function( value ) {
this.el.checked = ( value === this.value() ) ? true : false;
},
checkbox: function( value ) {
this.el.checked = ( value ) ? true : false;
},
generic: function( value ) {
if (this.el !== document.activeElement) {
this.el.value = value;
}
}
}
},
disseminate: function( newValue ) {
this.value( newValue );
},
change: function() {
var properties = {};
properties[ this.get("property") ] = this.value();
this.trigger("change", properties);
},
value: function ( value ) {
return ( value !== undefined ) ? this.setValue( value ) : this.getValue();
},
initialize: function ( settings ) {
this.el = settings.el;
this.data = {
role: "input",
property: settings.property,
type: this.el.type,
checkable: ( this.el.type === "radio" || this.el.type === "checkbox" ) ? true : false
};
this.getValue = this.factories.getValue[ this.el.type ] || this.factories.getValue.generic;
this.setValue = this.factories.setValue[ this.el.type ] || this.factories.setValue.generic;
this.bind();
},
bind: function ( settings ) {
this.el.addEventListener( "change", this.change.bind( this ) );
this.el.addEventListener( "keyup", this.change.bind( this ) );
}
});
Mog.addRole( "output", {
disseminate: function( value ) {
this.el.innerHTML = value;
},
initialize: function (settings) {
this.el = settings.el;
this.data = {
role: "output",
property: settings.property
};
}
});
Mog.addRole( "button", {} );
exports.Mog = Mog;
})( this, document );