@base-framework/base 2.6.0 → 2.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. package/base.min.js +1 -0
  2. package/es5/base.js +2968 -0
  3. package/es5/modules/ajax.js +663 -0
  4. package/es5/modules/animation.js +188 -0
  5. package/es5/modules/animations.js +1080 -0
  6. package/es5/modules/atom.js +65 -0
  7. package/es5/modules/component.js +1310 -0
  8. package/es5/modules/data-binder.js +1131 -0
  9. package/es5/modules/data.js +1808 -0
  10. package/es5/modules/date.js +525 -0
  11. package/es5/modules/form-validator.js +324 -0
  12. package/es5/modules/history.js +126 -0
  13. package/es5/modules/html-builder.js +461 -0
  14. package/es5/modules/layout.js +1679 -0
  15. package/es5/modules/mouse.js +124 -0
  16. package/es5/modules/nav-link.js +123 -0
  17. package/es5/modules/olderversions/animations-ease.js +1095 -0
  18. package/es5/modules/olderversions/animations-update.js +1048 -0
  19. package/es5/modules/olderversions/base-animations.js +636 -0
  20. package/es5/modules/olderversions/base-component-class.js +100 -0
  21. package/es5/modules/olderversions/base-data-binder-1.js +407 -0
  22. package/es5/modules/olderversions/base-data-binder-class.js +358 -0
  23. package/es5/modules/olderversions/base-layout-parser-class.js +172 -0
  24. package/es5/modules/olderversions/base-mode-1.js +777 -0
  25. package/es5/modules/olderversions/base-model-class.js +585 -0
  26. package/es5/modules/olderversions/data-binder/element-binding/base-data-binder-class.js +358 -0
  27. package/es5/modules/olderversions/data-binder/element-binding/base-model-class.js +585 -0
  28. package/es5/modules/olderversions/data-binder/model-binding/base-data-binder-class.js +353 -0
  29. package/es5/modules/olderversions/data-binder/model-binding/base-model-class.js +604 -0
  30. package/es5/modules/olderversions/data-binder-update-watcher.js +640 -0
  31. package/es5/modules/olderversions/data-tracker.js +187 -0
  32. package/es5/modules/olderversions/event-update.js +666 -0
  33. package/es5/modules/olderversions/nav-link.js +119 -0
  34. package/es5/modules/olderversions/router-with-templates-1.js +785 -0
  35. package/es5/modules/olderversions/router-with-templates.js +701 -0
  36. package/es5/modules/prototypes/ajax.js +657 -0
  37. package/es5/modules/prototypes/atom.js +65 -0
  38. package/es5/modules/prototypes/component.js +972 -0
  39. package/es5/modules/prototypes/data-binder.js +1089 -0
  40. package/es5/modules/prototypes/data.js +1290 -0
  41. package/es5/modules/prototypes/html-builder.js +414 -0
  42. package/es5/modules/prototypes/layout.js +879 -0
  43. package/es5/modules/router.js +1680 -0
  44. package/es5/modules/state.js +274 -0
  45. package/es6/.jshintrc +3 -0
  46. package/es6/base.js +41 -0
  47. package/es6/core.js +1 -0
  48. package/es6/data-tracker.js +351 -0
  49. package/es6/events.js +602 -0
  50. package/es6/legacy/es5/base.js +2968 -0
  51. package/es6/legacy/es5/modules/ajax.js +663 -0
  52. package/es6/legacy/es5/modules/animation.js +188 -0
  53. package/es6/legacy/es5/modules/animations.js +1080 -0
  54. package/es6/legacy/es5/modules/atom.js +65 -0
  55. package/es6/legacy/es5/modules/component.js +1310 -0
  56. package/es6/legacy/es5/modules/data-binder.js +1131 -0
  57. package/es6/legacy/es5/modules/data.js +1808 -0
  58. package/es6/legacy/es5/modules/date.js +525 -0
  59. package/es6/legacy/es5/modules/form-validator.js +324 -0
  60. package/es6/legacy/es5/modules/history.js +126 -0
  61. package/es6/legacy/es5/modules/html-builder.js +461 -0
  62. package/es6/legacy/es5/modules/layout.js +1679 -0
  63. package/es6/legacy/es5/modules/mouse.js +124 -0
  64. package/es6/legacy/es5/modules/nav-link.js +123 -0
  65. package/es6/legacy/es5/modules/olderversions/animations-ease.js +1095 -0
  66. package/es6/legacy/es5/modules/olderversions/animations-update.js +1048 -0
  67. package/es6/legacy/es5/modules/olderversions/base-animations.js +636 -0
  68. package/es6/legacy/es5/modules/olderversions/base-component-class.js +100 -0
  69. package/es6/legacy/es5/modules/olderversions/base-data-binder-1.js +407 -0
  70. package/es6/legacy/es5/modules/olderversions/base-data-binder-class.js +358 -0
  71. package/es6/legacy/es5/modules/olderversions/base-layout-parser-class.js +172 -0
  72. package/es6/legacy/es5/modules/olderversions/base-mode-1.js +777 -0
  73. package/es6/legacy/es5/modules/olderversions/base-model-class.js +585 -0
  74. package/es6/legacy/es5/modules/olderversions/data-binder/element-binding/base-data-binder-class.js +358 -0
  75. package/es6/legacy/es5/modules/olderversions/data-binder/element-binding/base-model-class.js +585 -0
  76. package/es6/legacy/es5/modules/olderversions/data-binder/model-binding/base-data-binder-class.js +353 -0
  77. package/es6/legacy/es5/modules/olderversions/data-binder/model-binding/base-model-class.js +604 -0
  78. package/es6/legacy/es5/modules/olderversions/data-binder-update-watcher.js +640 -0
  79. package/es6/legacy/es5/modules/olderversions/data-tracker.js +187 -0
  80. package/es6/legacy/es5/modules/olderversions/event-update.js +666 -0
  81. package/es6/legacy/es5/modules/olderversions/nav-link.js +119 -0
  82. package/es6/legacy/es5/modules/olderversions/router-with-templates-1.js +785 -0
  83. package/es6/legacy/es5/modules/olderversions/router-with-templates.js +701 -0
  84. package/es6/legacy/es5/modules/prototypes/ajax.js +657 -0
  85. package/es6/legacy/es5/modules/prototypes/atom.js +65 -0
  86. package/es6/legacy/es5/modules/prototypes/component.js +972 -0
  87. package/es6/legacy/es5/modules/prototypes/data-binder.js +1089 -0
  88. package/es6/legacy/es5/modules/prototypes/data.js +1290 -0
  89. package/es6/legacy/es5/modules/prototypes/html-builder.js +414 -0
  90. package/es6/legacy/es5/modules/prototypes/layout.js +879 -0
  91. package/es6/legacy/es5/modules/router.js +1680 -0
  92. package/es6/legacy/es5/modules/state.js +274 -0
  93. package/es6/main.js +1331 -0
  94. package/es6/modules/ajax/ajax.js +514 -0
  95. package/es6/modules/animation/animation.js +236 -0
  96. package/es6/modules/animations/animation-controller.js +231 -0
  97. package/es6/modules/animations/animation.js +64 -0
  98. package/es6/modules/animations/attr-movement.js +66 -0
  99. package/es6/modules/animations/css-movement.js +170 -0
  100. package/es6/modules/animations/movement.js +131 -0
  101. package/es6/modules/animations/value.js +187 -0
  102. package/es6/modules/atom/atom.js +54 -0
  103. package/es6/modules/component/component.js +230 -0
  104. package/es6/modules/component/event-helper.js +119 -0
  105. package/es6/modules/component/jot.js +144 -0
  106. package/es6/modules/component/state-helper.js +262 -0
  107. package/es6/modules/component/unit.js +551 -0
  108. package/es6/modules/data/attrs.js +40 -0
  109. package/es6/modules/data/basic-data.js +500 -0
  110. package/es6/modules/data/data-utils.js +29 -0
  111. package/es6/modules/data/data.js +3 -0
  112. package/es6/modules/data/deep-data.js +541 -0
  113. package/es6/modules/data/model-service.js +528 -0
  114. package/es6/modules/data/model.js +133 -0
  115. package/es6/modules/data/simple-data.js +33 -0
  116. package/es6/modules/data-binder/connection-tracker.js +113 -0
  117. package/es6/modules/data-binder/connection.js +16 -0
  118. package/es6/modules/data-binder/data-binder.js +352 -0
  119. package/es6/modules/data-binder/data-pub-sub.js +141 -0
  120. package/es6/modules/data-binder/data-source.js +56 -0
  121. package/es6/modules/data-binder/element-source.js +219 -0
  122. package/es6/modules/data-binder/one-way-connection.js +46 -0
  123. package/es6/modules/data-binder/one-way-source.js +43 -0
  124. package/es6/modules/data-binder/source.js +36 -0
  125. package/es6/modules/data-binder/two-way-connection.js +75 -0
  126. package/es6/modules/data-binder/two-way-source.js +41 -0
  127. package/es6/modules/date/date.js +544 -0
  128. package/es6/modules/history/history.js +89 -0
  129. package/es6/modules/html-builder/html-builder.js +434 -0
  130. package/es6/modules/import/import.js +390 -0
  131. package/es6/modules/layout/layout-builder.js +1269 -0
  132. package/es6/modules/layout/layout-parser.js +134 -0
  133. package/es6/modules/layout/watcher-helper.js +282 -0
  134. package/es6/modules/mouse/mouse.js +114 -0
  135. package/es6/modules/router/component-helper.js +163 -0
  136. package/es6/modules/router/history-controller.js +216 -0
  137. package/es6/modules/router/nav-link.js +124 -0
  138. package/es6/modules/router/route.js +401 -0
  139. package/es6/modules/router/router.js +789 -0
  140. package/es6/modules/router/utils.js +31 -0
  141. package/es6/modules/state/state-target.js +91 -0
  142. package/es6/modules/state/state.js +171 -0
  143. package/es6/package-lock.json +13 -0
  144. package/es6/package.json +28 -0
  145. package/es6/shared/objects.js +99 -0
  146. package/legacy/es5/base.js +2968 -0
  147. package/legacy/es5/modules/ajax.js +663 -0
  148. package/legacy/es5/modules/animation.js +188 -0
  149. package/legacy/es5/modules/animations.js +1080 -0
  150. package/legacy/es5/modules/atom.js +65 -0
  151. package/legacy/es5/modules/component.js +1310 -0
  152. package/legacy/es5/modules/data-binder.js +1131 -0
  153. package/legacy/es5/modules/data.js +1808 -0
  154. package/legacy/es5/modules/date.js +525 -0
  155. package/legacy/es5/modules/form-validator.js +324 -0
  156. package/legacy/es5/modules/history.js +126 -0
  157. package/legacy/es5/modules/html-builder.js +461 -0
  158. package/legacy/es5/modules/layout.js +1679 -0
  159. package/legacy/es5/modules/mouse.js +124 -0
  160. package/legacy/es5/modules/nav-link.js +123 -0
  161. package/legacy/es5/modules/olderversions/animations-ease.js +1095 -0
  162. package/legacy/es5/modules/olderversions/animations-update.js +1048 -0
  163. package/legacy/es5/modules/olderversions/base-animations.js +636 -0
  164. package/legacy/es5/modules/olderversions/base-component-class.js +100 -0
  165. package/legacy/es5/modules/olderversions/base-data-binder-1.js +407 -0
  166. package/legacy/es5/modules/olderversions/base-data-binder-class.js +358 -0
  167. package/legacy/es5/modules/olderversions/base-layout-parser-class.js +172 -0
  168. package/legacy/es5/modules/olderversions/base-mode-1.js +777 -0
  169. package/legacy/es5/modules/olderversions/base-model-class.js +585 -0
  170. package/legacy/es5/modules/olderversions/data-binder/element-binding/base-data-binder-class.js +358 -0
  171. package/legacy/es5/modules/olderversions/data-binder/element-binding/base-model-class.js +585 -0
  172. package/legacy/es5/modules/olderversions/data-binder/model-binding/base-data-binder-class.js +353 -0
  173. package/legacy/es5/modules/olderversions/data-binder/model-binding/base-model-class.js +604 -0
  174. package/legacy/es5/modules/olderversions/data-binder-update-watcher.js +640 -0
  175. package/legacy/es5/modules/olderversions/data-tracker.js +187 -0
  176. package/legacy/es5/modules/olderversions/event-update.js +666 -0
  177. package/legacy/es5/modules/olderversions/nav-link.js +119 -0
  178. package/legacy/es5/modules/olderversions/router-with-templates-1.js +785 -0
  179. package/legacy/es5/modules/olderversions/router-with-templates.js +701 -0
  180. package/legacy/es5/modules/prototypes/ajax.js +657 -0
  181. package/legacy/es5/modules/prototypes/atom.js +65 -0
  182. package/legacy/es5/modules/prototypes/component.js +972 -0
  183. package/legacy/es5/modules/prototypes/data-binder.js +1089 -0
  184. package/legacy/es5/modules/prototypes/data.js +1290 -0
  185. package/legacy/es5/modules/prototypes/html-builder.js +414 -0
  186. package/legacy/es5/modules/prototypes/layout.js +879 -0
  187. package/legacy/es5/modules/router.js +1680 -0
  188. package/legacy/es5/modules/state.js +274 -0
  189. package/package.json +8 -3
  190. package/update +16 -0
@@ -0,0 +1,1131 @@
1
+ /* base framework module */
2
+ (function()
3
+ {
4
+ "use strict";
5
+
6
+ var lastToken = -1;
7
+
8
+ /**
9
+ * DataPubSub
10
+ *
11
+ * This is a pub sub class to allow subscribers to
12
+ * listen for updates when published by publishers.
13
+ * @class
14
+ */
15
+ var DataPubSub = base.Class.extend(
16
+ {
17
+ /**
18
+ * @constructor
19
+ */
20
+ constructor: function()
21
+ {
22
+ /**
23
+ * @member {object} callBacks
24
+ * @protected
25
+ */
26
+ this.callBacks = {};
27
+
28
+ /**
29
+ * @member {int} lastToken
30
+ * @protected
31
+ */
32
+ this.lastToken = -1;
33
+ },
34
+
35
+ /**
36
+ * This will get a subscriber array.
37
+ *
38
+ * @param {string} msg
39
+ * @return {array}
40
+ */
41
+ get: function(msg)
42
+ {
43
+ var callBacks = this.callBacks;
44
+ return (callBacks[msg] || (callBacks[msg] = []));
45
+ },
46
+
47
+ /**
48
+ * This will reset pub sub.
49
+ */
50
+ reset: function()
51
+ {
52
+ this.callBacks = {};
53
+ this.lastToken = -1;
54
+ lastToken = -1;
55
+ },
56
+
57
+ /**
58
+ * This will add a subscriber.
59
+ *
60
+ * @param {string} msg
61
+ * @param {function} callBack
62
+ * @return {string} The subscriber token.
63
+ */
64
+ on: function(msg, callBack)
65
+ {
66
+ var token = (++lastToken);
67
+ var list = this.get(msg);
68
+ list.push({
69
+ token: token,
70
+ callBack: callBack
71
+ });
72
+ return token;
73
+ },
74
+
75
+ /**
76
+ * This will remove a subscriber.
77
+ *
78
+ * @param {string} msg
79
+ * @param {string} token
80
+ */
81
+ off: function(msg, token)
82
+ {
83
+ var list = this.callBacks[msg] || false;
84
+ if(list === false)
85
+ {
86
+ return false;
87
+ }
88
+
89
+ var length = list.length;
90
+ for (var i = 0; i < length; i++ )
91
+ {
92
+ var item = list[i];
93
+ if(item.token === token)
94
+ {
95
+ list.splice(i, 1);
96
+ break;
97
+ }
98
+ }
99
+ },
100
+
101
+ /**
102
+ * This will delete a message.
103
+ *
104
+ * @param {string} msg
105
+ */
106
+ remove: function(msg)
107
+ {
108
+ var callBacks = this.callBacks;
109
+ if(callBacks[msg])
110
+ {
111
+ delete callBacks[msg];
112
+ }
113
+ },
114
+
115
+ /**
116
+ * This will publish a message.
117
+ *
118
+ * @param {string} msg
119
+ * @param {string} value
120
+ * @param {object} committer
121
+ */
122
+ publish: function(msg)
123
+ {
124
+ var i, length,
125
+ list = this.callBacks[msg] || false;
126
+ if(list === false)
127
+ {
128
+ return false;
129
+ }
130
+
131
+ var args = Array.prototype.slice.call(arguments, 1);
132
+
133
+ length = list.length;
134
+ for (i = 0; i < length; i++)
135
+ {
136
+ var item = list[i];
137
+ if(!item)
138
+ {
139
+ continue;
140
+ }
141
+ item.callBack.apply(this, args);
142
+ }
143
+ }
144
+ });
145
+
146
+ var pubSub = new DataPubSub();
147
+ base.extend.DataPubSub = DataPubSub;
148
+
149
+ /**
150
+ * Source
151
+ *
152
+ * This will create a new source to use with
153
+ * a connection.
154
+ * @class
155
+ */
156
+ var Source = base.Class.extend(
157
+ {
158
+ /**
159
+ * @constructor
160
+ */
161
+ constructor: function()
162
+ {
163
+ /**
164
+ * @member {string} msg
165
+ * @protected
166
+ */
167
+ this.msg = null;
168
+
169
+ /**
170
+ * @member {string} token
171
+ */
172
+ this.token = null;
173
+ },
174
+
175
+ /**
176
+ * This will set the token.
177
+ *
178
+ * @param {string} token
179
+ */
180
+ setToken: function(token)
181
+ {
182
+ this.token = token;
183
+ }
184
+ });
185
+
186
+ /**
187
+ * OneWaySource
188
+ *
189
+ * This will create a one way source to use with
190
+ * a connection.
191
+ * @class
192
+ * @augments Source
193
+ */
194
+ var OneWaySource = Source.extend(
195
+ {
196
+ /**
197
+ * This will setup the data source.
198
+ *
199
+ * @param {object} data
200
+ */
201
+ constructor: function(data)
202
+ {
203
+ Source.call(this);
204
+ this.data = data;
205
+ },
206
+
207
+ /**
208
+ * This will subscribe to a message.
209
+ *
210
+ * @param {string} msg
211
+ * @param {function} callBack
212
+ */
213
+ subscribe: function(msg, callBack)
214
+ {
215
+ this.msg = msg;
216
+ this.token = this.data.on(msg, callBack);
217
+ },
218
+
219
+ /**
220
+ * This will unsubscribe from the message.
221
+ */
222
+ unsubscribe: function()
223
+ {
224
+ this.data.off(this.msg, this.token);
225
+ }
226
+ });
227
+
228
+ /**
229
+ * TwoWaySource
230
+ *
231
+ * This will create a two way source to use with
232
+ * a connection.
233
+ * @class
234
+ * @augments Source
235
+ */
236
+ var TwoWaySource = Source.extend(
237
+ {
238
+ /**
239
+ * @member {function} callBack
240
+ */
241
+ callBack: null,
242
+
243
+ /**
244
+ * This will subscribe to a message.
245
+ *
246
+ * @param {string} msg
247
+ */
248
+ subscribe: function(msg)
249
+ {
250
+ this.msg = msg;
251
+ var callBack = base.bind(this, this.callBack);
252
+ this.token = pubSub.on(msg, callBack);
253
+ },
254
+
255
+ /**
256
+ * This will unsubscribe from a message.
257
+ */
258
+ unsubscribe: function()
259
+ {
260
+ pubSub.off(this.msg, this.token);
261
+ }
262
+ });
263
+
264
+ /**
265
+ * DataSource
266
+ *
267
+ * This will create a data source to use with
268
+ * a connection.
269
+ * @class
270
+ * @augments TwoWaySource
271
+ */
272
+ var DataSource = TwoWaySource.extend(
273
+ {
274
+ /**
275
+ * @constructor
276
+ * @param {object} data
277
+ * @param {string} prop
278
+ */
279
+ constructor: function(data, prop)
280
+ {
281
+ TwoWaySource.call(this);
282
+ this.data = data;
283
+ this.prop = prop;
284
+ },
285
+
286
+ /**
287
+ * This will set the data value.
288
+ *
289
+ * @param {*} value
290
+ */
291
+ set: function(value)
292
+ {
293
+ this.data.set(this.prop, value);
294
+ },
295
+
296
+ /**
297
+ * This will get the data value.
298
+ */
299
+ get: function()
300
+ {
301
+ return this.data.get(this.prop);
302
+ },
303
+
304
+ /**
305
+ * The callBack when updated.
306
+ *
307
+ * @param {*} value
308
+ * @param {object} committer
309
+ */
310
+ callBack: function(value, committer)
311
+ {
312
+ if(this.data !== committer)
313
+ {
314
+ this.data.set(this.prop, value, committer);
315
+ }
316
+ }
317
+ });
318
+
319
+ /**
320
+ * This will set an element attr by the setAttribute method.
321
+ *
322
+ * @param {object} element
323
+ * @param {string} attr
324
+ * @param {mixed} value
325
+ */
326
+ var SetAttr = function(element, attr, value)
327
+ {
328
+ base.setAttr(element, attr, value);
329
+ };
330
+
331
+ var UpdateRadioAttr = function(element, attr, value)
332
+ {
333
+ element.checked = (element.value === value);
334
+ };
335
+
336
+ var UpdateCheckboxAttr = function(element, attr, value)
337
+ {
338
+ value = (value == 1);
339
+ UpdateAttr(element, attr, value);
340
+ };
341
+
342
+ /**
343
+ * This will update an element attr by the bracket notation.
344
+ *
345
+ * @param {object} element
346
+ * @param {string} attr
347
+ * @param {nixed} value
348
+ */
349
+ var UpdateAttr = function(element, attr, value)
350
+ {
351
+ element[attr] = value;
352
+ };
353
+
354
+ var GetAttr = function(element, attr)
355
+ {
356
+ return base.getAttr(element, attr);
357
+ };
358
+
359
+ var GetAttribute = function(element, attr)
360
+ {
361
+ return element[attr];
362
+ };
363
+
364
+ /**
365
+ * ElementSource
366
+ *
367
+ * This will create an element source to use with
368
+ * a connection.
369
+ * @class
370
+ * @augments TwoWaySource
371
+ */
372
+ var ElementSource = TwoWaySource.extend(
373
+ {
374
+ /**
375
+ * @constructor
376
+ * @param {object} element
377
+ * @param {string} attr
378
+ * @param {(string|function)} [filter]
379
+ */
380
+ constructor: function(element, attr, filter)
381
+ {
382
+ TwoWaySource.call(this);
383
+ this.element = element;
384
+ this.attr = this.getAttrBind(attr);
385
+ this.addSetMethod(element, this.attr);
386
+
387
+ if(typeof filter === 'string')
388
+ {
389
+ filter = this.setupFilter(filter);
390
+ }
391
+ this.filter = filter;
392
+ },
393
+
394
+ addSetMethod: function(element, attr)
395
+ {
396
+ if(attr.substring(4, 1) === '-')
397
+ {
398
+ this.setValue = SetAttr;
399
+ this.getValue = GetAttr;
400
+ }
401
+ else
402
+ {
403
+ this.getValue = GetAttribute;
404
+
405
+ var type = element.type;
406
+ if(type)
407
+ {
408
+ switch(type)
409
+ {
410
+ case 'checkbox':
411
+ this.setValue = UpdateCheckboxAttr;
412
+ return;
413
+ case 'radio':
414
+ this.setValue = UpdateRadioAttr;
415
+ return;
416
+ }
417
+ }
418
+
419
+ this.setValue = UpdateAttr;
420
+ }
421
+ },
422
+
423
+ /**
424
+ * This will get the bind attribute.
425
+ *
426
+ * @param {string} [customAttr]
427
+ * @return {string}
428
+ */
429
+ getAttrBind: function(customAttr)
430
+ {
431
+ /* this will setup the custom attr if the prop
432
+ has specified one. */
433
+ if(customAttr)
434
+ {
435
+ return customAttr;
436
+ }
437
+
438
+ var attr = 'textContent';
439
+ /* if no custom attr has been requested we will get the
440
+ default attr of the element */
441
+ var element = this.element;
442
+ if(!(element && typeof element === 'object'))
443
+ {
444
+ return attr;
445
+ }
446
+
447
+ var tagName = element.tagName.toLowerCase();
448
+ if (tagName === "input" || tagName === "textarea" || tagName === "select")
449
+ {
450
+ var type = element.type;
451
+ if(type)
452
+ {
453
+ switch(type)
454
+ {
455
+ case 'checkbox':
456
+ attr = 'checked';
457
+ break;
458
+ case 'file':
459
+ attr = 'files';
460
+ break;
461
+ default:
462
+ attr = 'value';
463
+ }
464
+ }
465
+ else
466
+ {
467
+ attr = 'value';
468
+ }
469
+ }
470
+ return attr;
471
+ },
472
+
473
+ /**
474
+ * This will setup a filter callBack.
475
+ *
476
+ * @param {string} filter
477
+ * @return {function}
478
+ */
479
+ setupFilter: function(filter)
480
+ {
481
+ var pattern = /(\[\[[^\]]+\]\])/;
482
+ return function(value)
483
+ {
484
+ return filter.replace(pattern, value);
485
+ };
486
+ },
487
+
488
+ /**
489
+ * This will set a value on an element.
490
+ *
491
+ * @param {*} value
492
+ */
493
+ set: function(value)
494
+ {
495
+ var element = this.element;
496
+ if(!element || typeof element !== 'object')
497
+ {
498
+ return false;
499
+ }
500
+
501
+ /* this will check to apply the option filter before
502
+ setting the value */
503
+ if(this.filter)
504
+ {
505
+ value = this.filter(value);
506
+ }
507
+
508
+ this.setValue(element, this.attr, value);
509
+ },
510
+
511
+ /**
512
+ * This will get the value from an element.
513
+ */
514
+ get: function()
515
+ {
516
+ var element = this.element;
517
+ if(!element || typeof element !== 'object')
518
+ {
519
+ return '';
520
+ }
521
+
522
+ return this.getValue(element, this.attr);
523
+ },
524
+
525
+ /**
526
+ * The callBack when updated.
527
+ *
528
+ * @param {*} value
529
+ * @param {object} committer
530
+ */
531
+ callBack: function(value, committer)
532
+ {
533
+ if(committer !== this.element)
534
+ {
535
+ this.set(value);
536
+ }
537
+ }
538
+ });
539
+
540
+ /**
541
+ * Connection
542
+ *
543
+ * This will create a connection.
544
+ * @class
545
+ */
546
+ var Connection = base.Class.extend(
547
+ {
548
+ /**
549
+ * This will be used to unsubscribe.
550
+ */
551
+ unsubscribe: function()
552
+ {
553
+
554
+ }
555
+ });
556
+
557
+ /**
558
+ * OneWayConnection
559
+ *
560
+ * This will create a one way connection.
561
+ * @class
562
+ * @augments Connection
563
+ */
564
+ var OneWayConnection = Connection.extend(
565
+ {
566
+ /**
567
+ * @constructor
568
+ */
569
+ constructor: function()
570
+ {
571
+ /**
572
+ * @member {object} source
573
+ */
574
+ this.source = null;
575
+ },
576
+
577
+ /**
578
+ * This will setup the connection source.
579
+ *
580
+ * @param {object} data
581
+ * @return {object}
582
+ */
583
+ addSource: function(data)
584
+ {
585
+ return (this.source = new OneWaySource(data));
586
+ },
587
+
588
+ /**
589
+ * This will be used to unsubscribe.
590
+ * @override
591
+ */
592
+ unsubscribe: function()
593
+ {
594
+ this.source.unsubscribe();
595
+ this.source = null;
596
+ }
597
+ });
598
+
599
+ /**
600
+ * TwoWayConnection
601
+ *
602
+ * This will setup a two way connection.
603
+ * @class
604
+ * @augments Connection
605
+ */
606
+ var TwoWayConnection = Connection.extend(
607
+ {
608
+ /**
609
+ * @constructor
610
+ */
611
+ constructor: function()
612
+ {
613
+ this.element = null;
614
+ this.data = null;
615
+ },
616
+
617
+ /**
618
+ * This will add the element source.
619
+ *
620
+ * @param {object} element
621
+ * @param {string} attr
622
+ * @param {(string|function)} filter
623
+ * @return {object}
624
+ */
625
+ addElement: function(element, attr, filter)
626
+ {
627
+ return (this.element = new ElementSource(element, attr, filter));
628
+ },
629
+
630
+ /**
631
+ * This will add the data source.
632
+ *
633
+ * @param {object} data
634
+ * @param {string} prop
635
+ * @return {object}
636
+ */
637
+ addData: function(data, prop)
638
+ {
639
+ return (this.data = new DataSource(data, prop));
640
+ },
641
+
642
+ /**
643
+ * This will unsubscribe from a source.
644
+ *
645
+ * @param {object} source
646
+ */
647
+ unsubscribeSource: function(source)
648
+ {
649
+ if(source)
650
+ {
651
+ source.unsubscribe();
652
+ }
653
+ },
654
+
655
+ /**
656
+ * This will be used to unsubscribe.
657
+ * @override
658
+ */
659
+ unsubscribe: function()
660
+ {
661
+ this.unsubscribeSource(this.element);
662
+ this.unsubscribeSource(this.data);
663
+
664
+ this.element = null;
665
+ this.data = null;
666
+ }
667
+ });
668
+
669
+ /**
670
+ * ConnectionTracker
671
+ *
672
+ * This will create a new connection tracker to track active
673
+ * connections in the data binder.
674
+ * @class
675
+ */
676
+ var ConnectionTracker = base.Class.extend(
677
+ {
678
+ /**
679
+ * @constructor
680
+ */
681
+ constructor: function()
682
+ {
683
+ /**
684
+ * @member {object} connections
685
+ */
686
+ this.connections = {};
687
+ },
688
+
689
+ /**
690
+ * This will add a new connection to be tracked.
691
+ *
692
+ * @param {string} id
693
+ * @param {string} attr
694
+ * @param {object} connection
695
+ * @return {object}
696
+ */
697
+ add: function(id, attr, connection)
698
+ {
699
+ var connections = this.find(id);
700
+ return (connections[attr] = connection);
701
+ },
702
+
703
+ /**
704
+ * This will get a connection.
705
+ *
706
+ * @param {string} id
707
+ * @param {string} attr
708
+ * @return {(object|bool)}
709
+ */
710
+ get: function(id, attr)
711
+ {
712
+ var connections = this.connections[id];
713
+ if(connections)
714
+ {
715
+ return (connections[attr] || false);
716
+ }
717
+ return false;
718
+ },
719
+
720
+ /**
721
+ * This will find a connection.
722
+ *
723
+ * @param {string} id
724
+ * @return {object}
725
+ */
726
+ find: function(id)
727
+ {
728
+ var connections = this.connections;
729
+ return (connections[id] || (connections[id] = {}));
730
+ },
731
+
732
+ /**
733
+ * This will remove a connection or all connections by id.
734
+ * @param {string} id
735
+ * @param {string} [attr]
736
+ */
737
+ remove: function(id, attr)
738
+ {
739
+ var connections = this.connections[id];
740
+ if(!connections)
741
+ {
742
+ return false;
743
+ }
744
+
745
+ var connection;
746
+ if(attr)
747
+ {
748
+ connection = connections[attr];
749
+ if(connection)
750
+ {
751
+ connection.unsubscribe();
752
+ delete connections[attr];
753
+ }
754
+
755
+ /* this will remove the msg from the elements
756
+ if no elements are listed under the msg */
757
+ if(base.isEmpty(connections))
758
+ {
759
+ delete this.connections[id];
760
+ }
761
+ }
762
+ else
763
+ {
764
+ for(var prop in connections)
765
+ {
766
+ if(connections.hasOwnProperty(prop))
767
+ {
768
+ connection = connections[prop];
769
+ if(connection)
770
+ {
771
+ connection.unsubscribe();
772
+ }
773
+ }
774
+ }
775
+
776
+ delete this.connections[id];
777
+ }
778
+ }
779
+ });
780
+
781
+ /**
782
+ * DataBinder
783
+ *
784
+ * This will create a data binder object that can
785
+ * create one way and two way data bindings.
786
+ * @class
787
+ */
788
+ var DataBinder = base.Class.extend(
789
+ {
790
+ /**
791
+ * @constructor
792
+ */
793
+ constructor: function()
794
+ {
795
+ this.version = "1.0.1";
796
+ this.attr = 'bindId';
797
+
798
+ this.connections = new ConnectionTracker();
799
+
800
+ this.idCount = 0;
801
+ this.setup();
802
+ },
803
+
804
+ /**
805
+ * This will setup the events.
806
+ * @protected
807
+ */
808
+ setup: function()
809
+ {
810
+ this.setupEvents();
811
+ },
812
+
813
+ /**
814
+ * This will bind an element to a data property.
815
+ *
816
+ * @param {object} element
817
+ * @param {object} data
818
+ * @param {string} prop
819
+ * @param {(string|function)} [filter]
820
+ * @return {object} an instance of the databinder.
821
+ */
822
+ bind: function(element, data, prop, filter)
823
+ {
824
+ var bindProp = prop,
825
+ bindAttr = null;
826
+
827
+ if(prop.indexOf(':') !== -1)
828
+ {
829
+ /* this will setup the custom attr if the prop
830
+ has specified one. */
831
+ var parts = prop.split(':');
832
+ if(parts.length > 1)
833
+ {
834
+ bindProp = parts[1];
835
+ bindAttr = parts[0];
836
+ }
837
+ }
838
+
839
+ /* this will setup the model bind attr to the
840
+ element and assign a bind id attr to support
841
+ two way binding */
842
+ var connection = this.setupConnection(element, data, bindProp, bindAttr, filter);
843
+
844
+ /* we want to get the starting value of the
845
+ data and set it on our element */
846
+ var connectionElement = connection.element,
847
+ value = data.get(bindProp);
848
+ if(typeof value !== 'undefined')
849
+ {
850
+ connectionElement.set(value);
851
+ }
852
+ else
853
+ {
854
+ /* this will set the element value
855
+ as the prop value */
856
+ value = connectionElement.get();
857
+ if(value !== '')
858
+ {
859
+ connection.data.set(value);
860
+ }
861
+ }
862
+ return this;
863
+ },
864
+
865
+ /**
866
+ * This will bind an element to a data property.
867
+ *
868
+ * @protected
869
+ * @param {object} element
870
+ * @param {object} data
871
+ * @param {string} prop
872
+ * @param {string} customAttr
873
+ * @param {(string|function)} [filter]
874
+ * @return {object} The new connection.
875
+ */
876
+ setupConnection: function(element, data, prop, customAttr, filter)
877
+ {
878
+ var id = this.getBindId(element);
879
+ var connection = new TwoWayConnection();
880
+
881
+ // this will create the data source
882
+ var dataSource = connection.addData(data, prop);
883
+ // this will subscribe the data to the element
884
+ dataSource.subscribe(id);
885
+
886
+ /* this will add the data binding
887
+ attr to our element so it will subscribe to
888
+ the two data changes */
889
+ var dataId = data.getDataId(),
890
+ msg = dataId + ':' + prop;
891
+
892
+ // this will create the element source
893
+ var elementSource = connection.addElement(element, customAttr, filter);
894
+ // this will subscribe the element to the data
895
+ elementSource.subscribe(msg);
896
+
897
+ this.addConnection(id, 'bind', connection);
898
+
899
+ return connection;
900
+ },
901
+
902
+ /**
903
+ * This will add a new connection to the
904
+ * connection tracker.
905
+ *
906
+ * @protected
907
+ * @param {string} id
908
+ * @param {string} attr
909
+ * @param {object} connection
910
+ */
911
+ addConnection: function(id, attr, connection)
912
+ {
913
+ this.connections.add(id, attr, connection);
914
+ },
915
+
916
+ /**
917
+ * This will set the bind id.
918
+ *
919
+ * @param {object} element
920
+ */
921
+ setBindId: function(element)
922
+ {
923
+ var id = 'db-' + this.idCount++;
924
+ element.dataset[this.attr] = id;
925
+ element[this.attr] = id;
926
+ return id;
927
+ },
928
+
929
+ /**
930
+ * This will get the bind id.
931
+ *
932
+ * @param {object} element
933
+ * @return {string}
934
+ */
935
+ getBindId: function(element)
936
+ {
937
+ return element[this.attr] || this.setBindId(element);
938
+ },
939
+
940
+ /**
941
+ * This will unbind the element.
942
+ *
943
+ * @param {object} element
944
+ * @return {object} an instance of the data binder.
945
+ */
946
+ unbind: function(element)
947
+ {
948
+ var id = element[this.attr];
949
+ if(id)
950
+ {
951
+ this.connections.remove(id);
952
+ }
953
+ return this;
954
+ },
955
+
956
+ /**
957
+ * This will setup a watcher for an element.
958
+ *
959
+ * @param {object} element
960
+ * @param {object} data
961
+ * @param {string} prop
962
+ * @param {function} callBack
963
+ */
964
+ watch: function(element, data, prop, callBack)
965
+ {
966
+ if(!element || typeof element !== 'object')
967
+ {
968
+ return false;
969
+ }
970
+
971
+ var connection = new OneWayConnection();
972
+
973
+ // this will create the one way source
974
+ var source = connection.addSource(data);
975
+ source.subscribe(prop, callBack);
976
+
977
+ // this will add the new connection to the connection tracker
978
+ var id = this.getBindId(element);
979
+ var attr = data.getDataId() + ':' + prop;
980
+ this.addConnection(id, attr, connection);
981
+
982
+ var value = data.get(prop);
983
+ if(typeof value !== 'undefined')
984
+ {
985
+ callBack(value);
986
+ }
987
+ },
988
+
989
+ /**
990
+ * This will remove a watcher from an element.
991
+ *
992
+ * @param {object} element
993
+ * @param {object} data
994
+ * @param {string} prop
995
+ */
996
+ unwatch: function(element, data, prop)
997
+ {
998
+ if(!element || typeof element !== 'object')
999
+ {
1000
+ return false;
1001
+ }
1002
+
1003
+ var id = element[this.attr];
1004
+ if(id)
1005
+ {
1006
+ var attr = data.getDataId() + ':' + prop;
1007
+ this.connections.remove(id, attr);
1008
+ }
1009
+ },
1010
+
1011
+ /**
1012
+ * This will publish to the pub sub.
1013
+ *
1014
+ * @param {string} msg
1015
+ * @param {*} value
1016
+ * @param {object} committer
1017
+ * @return {object} an instance of the data binder.
1018
+ */
1019
+ publish: function(msg, value, committer)
1020
+ {
1021
+ pubSub.publish(msg, value, committer);
1022
+ return this;
1023
+ },
1024
+
1025
+ /**
1026
+ * This will check if an element is bound.
1027
+ *
1028
+ * @protected
1029
+ * @param {object} element
1030
+ * @return {boolean}
1031
+ */
1032
+ isDataBound: function(element)
1033
+ {
1034
+ if(element)
1035
+ {
1036
+ var id = element[this.attr];
1037
+ if(id)
1038
+ {
1039
+ return id;
1040
+ }
1041
+ }
1042
+ return false;
1043
+ },
1044
+
1045
+ /**
1046
+ * @member {array} blockedKeys
1047
+ * @protected
1048
+ */
1049
+ blockedKeys: [
1050
+ 20, //caps lock
1051
+ 37, //arrows
1052
+ 38,
1053
+ 39,
1054
+ 40
1055
+ ],
1056
+
1057
+ isBlocked: function(evt)
1058
+ {
1059
+ if(evt.type !== 'keyup')
1060
+ {
1061
+ return false;
1062
+ }
1063
+
1064
+ /* this will check to block ctrl, shift or alt +
1065
+ buttons */
1066
+ return (base.inArray(this.blockedKeys, evt.keyCode) !== -1);
1067
+ },
1068
+
1069
+ /**
1070
+ * This is the callBack for the chnage event.
1071
+ *
1072
+ * @param {object} evt
1073
+ */
1074
+ bindHandler: function(evt)
1075
+ {
1076
+ if(this.isBlocked(evt))
1077
+ {
1078
+ return true;
1079
+ }
1080
+
1081
+ var target = evt.target || evt.srcElement;
1082
+ var id = this.isDataBound(target);
1083
+ if(id)
1084
+ {
1085
+ var connection = this.connections.get(id, 'bind');
1086
+ if(connection)
1087
+ {
1088
+ var value = connection.element.get();
1089
+ /* this will publish to the ui and to the
1090
+ model that subscribes to the element */
1091
+ pubSub.publish(id, value, target);
1092
+ }
1093
+ }
1094
+ evt.stopPropagation();
1095
+ },
1096
+
1097
+ /* this will setup the on change handler and
1098
+ add the events. this needs to be setup before adding
1099
+ the events. */
1100
+ changeHandler: null,
1101
+
1102
+ /**
1103
+ * This wil setup the events.
1104
+ * @protected
1105
+ */
1106
+ setupEvents: function()
1107
+ {
1108
+ this.changeHandler = base.bind(this, this.bindHandler);
1109
+
1110
+ this.addEvents();
1111
+ },
1112
+
1113
+ /**
1114
+ * This will add the events.
1115
+ */
1116
+ addEvents: function()
1117
+ {
1118
+ base.on(["change", "keyup"], document, this.changeHandler, false);
1119
+ },
1120
+
1121
+ /**
1122
+ * This will remove the events.
1123
+ */
1124
+ removeEvents: function()
1125
+ {
1126
+ base.off(["change", "keyup"], document, this.changeHandler, false);
1127
+ }
1128
+ });
1129
+
1130
+ base.extend.DataBinder = new DataBinder();
1131
+ })();