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