vsn 0.1.26 → 0.1.29

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 (303) hide show
  1. package/demo/vsn.js +1 -1
  2. package/dist/AST/ArithmeticAssignmentNode.d.ts +23 -0
  3. package/dist/AST/ArithmeticAssignmentNode.js +313 -0
  4. package/dist/AST/ArithmeticAssignmentNode.js.map +1 -0
  5. package/dist/AST/ArithmeticNode.d.ts +15 -0
  6. package/dist/AST/ArithmeticNode.js +114 -0
  7. package/dist/AST/ArithmeticNode.js.map +1 -0
  8. package/dist/AST/ArrayNode.d.ts +14 -0
  9. package/dist/AST/ArrayNode.js +114 -0
  10. package/dist/AST/ArrayNode.js.map +1 -0
  11. package/dist/AST/BlockNode.d.ts +11 -0
  12. package/dist/AST/BlockNode.js +98 -0
  13. package/dist/AST/BlockNode.js.map +1 -0
  14. package/dist/AST/BooleanLiteralNode.d.ts +5 -0
  15. package/dist/AST/BooleanLiteralNode.js +31 -0
  16. package/dist/AST/BooleanLiteralNode.js.map +1 -0
  17. package/dist/AST/ComparisonNode.d.ts +15 -0
  18. package/dist/AST/ComparisonNode.js +120 -0
  19. package/dist/AST/ComparisonNode.js.map +1 -0
  20. package/dist/AST/ConditionalNode.d.ts +13 -0
  21. package/dist/AST/ConditionalNode.js +95 -0
  22. package/dist/AST/ConditionalNode.js.map +1 -0
  23. package/dist/AST/ElementAttributeNode.d.ts +18 -0
  24. package/dist/AST/ElementAttributeNode.js +159 -0
  25. package/dist/AST/ElementAttributeNode.js.map +1 -0
  26. package/dist/AST/ElementQueryNode.d.ts +12 -0
  27. package/dist/AST/ElementQueryNode.js +111 -0
  28. package/dist/AST/ElementQueryNode.js.map +1 -0
  29. package/dist/AST/ElementStyleNode.d.ts +18 -0
  30. package/dist/AST/ElementStyleNode.js +159 -0
  31. package/dist/AST/ElementStyleNode.js.map +1 -0
  32. package/dist/AST/ForStatementNode.d.ts +17 -0
  33. package/dist/AST/ForStatementNode.js +121 -0
  34. package/dist/AST/ForStatementNode.js.map +1 -0
  35. package/dist/AST/FunctionArgumentNode.d.ts +11 -0
  36. package/dist/AST/FunctionArgumentNode.js +100 -0
  37. package/dist/AST/FunctionArgumentNode.js.map +1 -0
  38. package/dist/AST/FunctionCallNode.d.ts +13 -0
  39. package/dist/AST/FunctionCallNode.js +102 -0
  40. package/dist/AST/FunctionCallNode.js.map +1 -0
  41. package/dist/AST/IfStatementNode.d.ts +14 -0
  42. package/dist/AST/IfStatementNode.js +128 -0
  43. package/dist/AST/IfStatementNode.js.map +1 -0
  44. package/dist/AST/InNode.d.ts +15 -0
  45. package/dist/AST/InNode.js +107 -0
  46. package/dist/AST/InNode.js.map +1 -0
  47. package/dist/AST/IndexNode.d.ts +16 -0
  48. package/dist/AST/IndexNode.js +126 -0
  49. package/dist/AST/IndexNode.js.map +1 -0
  50. package/dist/AST/LiteralNode.d.ts +10 -0
  51. package/dist/AST/LiteralNode.js +74 -0
  52. package/dist/AST/LiteralNode.js.map +1 -0
  53. package/dist/AST/Node.d.ts +19 -0
  54. package/dist/AST/Node.js +117 -0
  55. package/dist/AST/Node.js.map +1 -0
  56. package/dist/AST/NotNode.d.ts +12 -0
  57. package/dist/AST/NotNode.js +103 -0
  58. package/dist/AST/NotNode.js.map +1 -0
  59. package/dist/AST/NumberLiteralNode.d.ts +5 -0
  60. package/dist/AST/NumberLiteralNode.js +36 -0
  61. package/dist/AST/NumberLiteralNode.js.map +1 -0
  62. package/dist/AST/ObjectNode.d.ts +14 -0
  63. package/dist/AST/ObjectNode.js +131 -0
  64. package/dist/AST/ObjectNode.js.map +1 -0
  65. package/dist/AST/RootScopeMemberNode.d.ts +11 -0
  66. package/dist/AST/RootScopeMemberNode.js +87 -0
  67. package/dist/AST/RootScopeMemberNode.js.map +1 -0
  68. package/dist/AST/ScopeMemberNode.d.ts +12 -0
  69. package/dist/AST/ScopeMemberNode.js +128 -0
  70. package/dist/AST/ScopeMemberNode.js.map +1 -0
  71. package/dist/AST/UnitLiteralNode.d.ts +15 -0
  72. package/dist/AST/UnitLiteralNode.js +72 -0
  73. package/dist/AST/UnitLiteralNode.js.map +1 -0
  74. package/dist/AST.d.ts +7 -60
  75. package/dist/AST.js +778 -0
  76. package/dist/AST.js.map +1 -0
  77. package/dist/Attribute.d.ts +1 -1
  78. package/dist/Attribute.js +187 -0
  79. package/dist/Attribute.js.map +1 -0
  80. package/dist/Bencmark.js +179 -0
  81. package/dist/Bencmark.js.map +1 -0
  82. package/dist/Configuration.d.ts +1 -1
  83. package/dist/Configuration.js +64 -0
  84. package/dist/Configuration.js.map +1 -0
  85. package/dist/Controller.d.ts +4 -2
  86. package/dist/Controller.js +62 -0
  87. package/dist/Controller.js.map +1 -0
  88. package/dist/DOM/DOMObject.d.ts +1 -1
  89. package/dist/DOM/DOMObject.js +47 -0
  90. package/dist/DOM/DOMObject.js.map +1 -0
  91. package/dist/DOM/WrappedDocument.js +34 -0
  92. package/dist/DOM/WrappedDocument.js.map +1 -0
  93. package/dist/DOM/WrappedWindow.js +45 -0
  94. package/dist/DOM/WrappedWindow.js.map +1 -0
  95. package/dist/DOM.d.ts +1 -1
  96. package/dist/DOM.js +547 -0
  97. package/dist/DOM.js.map +1 -0
  98. package/dist/EventDispatcher.d.ts +29 -0
  99. package/dist/EventDispatcher.js +132 -0
  100. package/dist/EventDispatcher.js.map +1 -0
  101. package/dist/Formats.js +44 -0
  102. package/dist/Formats.js.map +1 -0
  103. package/dist/MessageList.d.ts +15 -0
  104. package/dist/MessageList.js +96 -0
  105. package/dist/MessageList.js.map +1 -0
  106. package/dist/Model/Field.d.ts +8 -0
  107. package/dist/Model/Field.js +38 -0
  108. package/dist/Model/Field.js.map +1 -0
  109. package/dist/Model.d.ts +12 -0
  110. package/dist/Model.js +61 -0
  111. package/dist/Model.js.map +1 -0
  112. package/dist/Query.js +66 -0
  113. package/dist/Query.js.map +1 -0
  114. package/dist/Registry.d.ts +5 -2
  115. package/dist/Registry.js +139 -0
  116. package/dist/Registry.js.map +1 -0
  117. package/dist/Scope/DynamicScopeData.d.ts +6 -0
  118. package/dist/Scope/DynamicScopeData.js +54 -0
  119. package/dist/Scope/DynamicScopeData.js.map +1 -0
  120. package/dist/Scope/QueryReference.d.ts +10 -0
  121. package/dist/Scope/QueryReference.js +103 -0
  122. package/dist/Scope/QueryReference.js.map +1 -0
  123. package/dist/Scope/ScopeData.d.ts +4 -0
  124. package/dist/Scope/ScopeData.js +40 -0
  125. package/dist/Scope/ScopeData.js.map +1 -0
  126. package/dist/Scope/ScopeDataAbstract.d.ts +22 -0
  127. package/dist/Scope/ScopeDataAbstract.js +137 -0
  128. package/dist/Scope/ScopeDataAbstract.js.map +1 -0
  129. package/dist/Scope/ScopeReference.d.ts +10 -0
  130. package/dist/Scope/ScopeReference.js +73 -0
  131. package/dist/Scope/ScopeReference.js.map +1 -0
  132. package/dist/Scope/ScopedVariableType.d.ts +6 -0
  133. package/dist/Scope/ScopedVariableType.js +14 -0
  134. package/dist/Scope/ScopedVariableType.js.map +1 -0
  135. package/dist/Scope/WrappedArray.d.ts +16 -0
  136. package/dist/Scope/WrappedArray.js +121 -0
  137. package/dist/Scope/WrappedArray.js.map +1 -0
  138. package/dist/Scope/properties/Property.d.ts +18 -0
  139. package/dist/Scope/properties/Property.js +93 -0
  140. package/dist/Scope/properties/Property.js.map +1 -0
  141. package/dist/Scope.d.ts +4 -48
  142. package/dist/Scope.js +227 -0
  143. package/dist/Scope.js.map +1 -0
  144. package/dist/SimplePromise.d.ts +42 -0
  145. package/dist/SimplePromise.js +217 -0
  146. package/dist/SimplePromise.js.map +1 -0
  147. package/dist/Tag/List.js +85 -0
  148. package/dist/Tag/List.js.map +1 -0
  149. package/dist/Tag.js +770 -0
  150. package/dist/Tag.js.map +1 -0
  151. package/dist/Types.d.ts +1 -0
  152. package/dist/Types.js +54 -0
  153. package/dist/Types.js.map +1 -0
  154. package/dist/Validators.d.ts +7 -0
  155. package/dist/Validators.js +54 -0
  156. package/dist/Validators.js.map +1 -0
  157. package/dist/attributes/AddClassIf.js +93 -0
  158. package/dist/attributes/AddClassIf.js.map +1 -0
  159. package/dist/attributes/Bind.js +272 -0
  160. package/dist/attributes/Bind.js.map +1 -0
  161. package/dist/attributes/ClassConstructor.d.ts +1 -0
  162. package/dist/attributes/ClassConstructor.js +104 -0
  163. package/dist/attributes/ClassConstructor.js.map +1 -0
  164. package/dist/attributes/ClickRemoveClass.js +114 -0
  165. package/dist/attributes/ClickRemoveClass.js.map +1 -0
  166. package/dist/attributes/ClickToggleClass.js +114 -0
  167. package/dist/attributes/ClickToggleClass.js.map +1 -0
  168. package/dist/attributes/ControllerAttribute.js +28 -0
  169. package/dist/attributes/ControllerAttribute.js.map +1 -0
  170. package/dist/attributes/DisableIf.js +94 -0
  171. package/dist/attributes/DisableIf.js.map +1 -0
  172. package/dist/attributes/Exec.js +108 -0
  173. package/dist/attributes/Exec.js.map +1 -0
  174. package/dist/attributes/Format.js +99 -0
  175. package/dist/attributes/Format.js.map +1 -0
  176. package/dist/attributes/If.js +159 -0
  177. package/dist/attributes/If.js.map +1 -0
  178. package/dist/attributes/JSONAttribute.js +118 -0
  179. package/dist/attributes/JSONAttribute.js.map +1 -0
  180. package/dist/attributes/KeyAbstract.js +117 -0
  181. package/dist/attributes/KeyAbstract.js.map +1 -0
  182. package/dist/attributes/KeyDown.js +88 -0
  183. package/dist/attributes/KeyDown.js.map +1 -0
  184. package/dist/attributes/KeyUp.js +88 -0
  185. package/dist/attributes/KeyUp.js.map +1 -0
  186. package/dist/attributes/List.js +282 -0
  187. package/dist/attributes/List.js.map +1 -0
  188. package/dist/attributes/ListItem.js +138 -0
  189. package/dist/attributes/ListItem.js.map +1 -0
  190. package/dist/attributes/ListItemModel.js +39 -0
  191. package/dist/attributes/ListItemModel.js.map +1 -0
  192. package/dist/attributes/ModelAttribute.js +29 -0
  193. package/dist/attributes/ModelAttribute.js.map +1 -0
  194. package/dist/attributes/Name.js +88 -0
  195. package/dist/attributes/Name.js.map +1 -0
  196. package/dist/attributes/On.js +135 -0
  197. package/dist/attributes/On.js.map +1 -0
  198. package/dist/attributes/Radio.js +174 -0
  199. package/dist/attributes/Radio.js.map +1 -0
  200. package/dist/attributes/Referenced.js +38 -0
  201. package/dist/attributes/Referenced.js.map +1 -0
  202. package/dist/attributes/RootAttribute.js +91 -0
  203. package/dist/attributes/RootAttribute.js.map +1 -0
  204. package/dist/attributes/ScopeAttribute.js +40 -0
  205. package/dist/attributes/ScopeAttribute.js.map +1 -0
  206. package/dist/attributes/ScopeChange.js +130 -0
  207. package/dist/attributes/ScopeChange.js.map +1 -0
  208. package/dist/attributes/SetAttribute.js +133 -0
  209. package/dist/attributes/SetAttribute.js.map +1 -0
  210. package/dist/attributes/StandardAttribute.js +186 -0
  211. package/dist/attributes/StandardAttribute.js.map +1 -0
  212. package/dist/attributes/StyleAttribute.js +183 -0
  213. package/dist/attributes/StyleAttribute.js.map +1 -0
  214. package/dist/attributes/Template.js +39 -0
  215. package/dist/attributes/Template.js.map +1 -0
  216. package/dist/attributes/TypeAttribute.js +104 -0
  217. package/dist/attributes/TypeAttribute.js.map +1 -0
  218. package/dist/attributes/_imports.js +60 -0
  219. package/dist/attributes/_imports.js.map +1 -0
  220. package/dist/helpers/DOMHelper.js +81 -0
  221. package/dist/helpers/DOMHelper.js.map +1 -0
  222. package/dist/helpers/ElementHelper.js +25 -0
  223. package/dist/helpers/ElementHelper.js.map +1 -0
  224. package/dist/helpers/VisionHelper.js +71 -0
  225. package/dist/helpers/VisionHelper.js.map +1 -0
  226. package/dist/helpers/decorators.js +38 -0
  227. package/dist/helpers/decorators.js.map +1 -0
  228. package/dist/vsn.d.ts +11 -2
  229. package/dist/vsn.js +181 -0
  230. package/dist/vsn.js.map +1 -0
  231. package/package.json +2 -6
  232. package/src/AST/ArithmeticAssignmentNode.ts +236 -0
  233. package/src/AST/ArithmeticNode.ts +52 -0
  234. package/src/AST/ArrayNode.ts +39 -0
  235. package/src/AST/BlockNode.ts +25 -0
  236. package/src/AST/BooleanLiteralNode.ts +10 -0
  237. package/src/AST/ComparisonNode.ts +57 -0
  238. package/src/AST/ConditionalNode.ts +36 -0
  239. package/src/AST/ElementAttributeNode.ts +63 -0
  240. package/src/AST/ElementQueryNode.ts +25 -0
  241. package/src/AST/ElementStyleNode.ts +63 -0
  242. package/src/AST/ForStatementNode.ts +59 -0
  243. package/src/AST/FunctionArgumentNode.ts +27 -0
  244. package/src/AST/FunctionCallNode.ts +32 -0
  245. package/src/AST/IfStatementNode.ts +67 -0
  246. package/src/AST/InNode.ts +46 -0
  247. package/src/AST/IndexNode.ts +61 -0
  248. package/src/AST/LiteralNode.ts +17 -0
  249. package/src/AST/Node.ts +71 -0
  250. package/src/AST/NotNode.ts +41 -0
  251. package/src/AST/NumberLiteralNode.ts +14 -0
  252. package/src/AST/ObjectNode.ts +55 -0
  253. package/src/AST/RootScopeMemberNode.ts +25 -0
  254. package/src/AST/ScopeMemberNode.ts +53 -0
  255. package/src/AST/UnitLiteralNode.ts +51 -0
  256. package/src/AST.ts +34 -1094
  257. package/src/Attribute.ts +2 -2
  258. package/src/Configuration.ts +3 -3
  259. package/src/Controller.ts +10 -2
  260. package/src/DOM/DOMObject.ts +1 -1
  261. package/src/DOM.ts +3 -3
  262. package/src/EventDispatcher.ts +134 -0
  263. package/src/Formats.ts +2 -2
  264. package/src/MessageList.ts +85 -0
  265. package/src/Model/Field.ts +20 -0
  266. package/src/Model.ts +43 -0
  267. package/src/Registry.ts +13 -3
  268. package/src/Scope/DynamicScopeData.ts +29 -0
  269. package/src/Scope/QueryReference.ts +29 -0
  270. package/src/Scope/ScopeData.ts +21 -0
  271. package/src/Scope/ScopeDataAbstract.ts +126 -0
  272. package/src/Scope/ScopeReference.ts +30 -0
  273. package/src/Scope/ScopedVariableType.ts +7 -0
  274. package/src/Scope/WrappedArray.ts +88 -0
  275. package/src/Scope/properties/Property.ts +79 -0
  276. package/src/Scope.ts +31 -252
  277. package/src/SimplePromise.ts +219 -0
  278. package/src/Tag.ts +3 -3
  279. package/src/Types.ts +6 -1
  280. package/src/Validators.ts +45 -0
  281. package/src/attributes/Bind.ts +4 -4
  282. package/src/attributes/ClassConstructor.ts +2 -1
  283. package/src/attributes/JSONAttribute.ts +2 -1
  284. package/src/attributes/List.ts +4 -4
  285. package/src/attributes/Radio.ts +3 -2
  286. package/src/attributes/ScopeChange.ts +2 -2
  287. package/src/attributes/SetAttribute.ts +2 -1
  288. package/src/attributes/StandardAttribute.ts +1 -1
  289. package/src/attributes/StyleAttribute.ts +3 -2
  290. package/src/attributes/TypeAttribute.ts +1 -1
  291. package/src/vsn.ts +16 -5
  292. package/test/AST/ArithmeticAssignmentNode.spec.ts +47 -0
  293. package/test/AST.spec.ts +3 -2
  294. package/test/Controller.spec.ts +44 -0
  295. package/test/MessageList.spec.ts +101 -0
  296. package/test/Model/DataModel.spec.ts +0 -0
  297. package/test/Scope/DynamicScopeData.spec.ts +141 -0
  298. package/test/Scope.spec.ts +15 -3
  299. package/test/SimplePromise.spec.ts +271 -0
  300. package/test/Tag/TagList.spec.ts +1 -1
  301. package/test/attributes/Bind.spec.ts +5 -5
  302. package/test/attributes/ListItem.spec.ts +1 -1
  303. package/dist/vsn.min.js +0 -1
@@ -0,0 +1,7 @@
1
+
2
+ export class ScopeVariableType {
3
+ public static readonly Integer: string = 'integer';
4
+ public static readonly Float: string = 'float';
5
+ public static readonly Boolean: string = 'boolean';
6
+ public static readonly String: string = 'string';
7
+ }
@@ -0,0 +1,88 @@
1
+ import {EventDispatcher, EventDispatcherCallback} from "../EventDispatcher";
2
+
3
+ export class WrappedArray<T> extends Array<T> {
4
+ private dispatcher: EventDispatcher;
5
+ public readonly $wrapped: boolean = true;
6
+
7
+ constructor(...items: T[]) {
8
+ super(...items);
9
+ Object.setPrototypeOf(this, WrappedArray.prototype);
10
+ this.dispatcher = new EventDispatcher();
11
+ }
12
+
13
+ dispatch(event: string, ...args: any[]) {
14
+ this.dispatcher.dispatch(event, ...args);
15
+ }
16
+
17
+ on(event: string, callback: EventDispatcherCallback) {
18
+ this.dispatcher.on(event, callback);
19
+ }
20
+
21
+ off(event: string, key?: number) {
22
+ this.dispatcher.off(event, key);
23
+ }
24
+
25
+ once(event: string, callback: EventDispatcherCallback) {
26
+ this.dispatcher.once(event, callback);
27
+ }
28
+
29
+ push(...items: T[]): number {
30
+ const num: number = super.push(...items);
31
+
32
+ this.dispatch('push', ...items);
33
+ this.dispatch('change', {
34
+ 'added': items
35
+ });
36
+ for (const item of items) {
37
+ this.dispatch('add', item);
38
+ }
39
+ return num;
40
+ }
41
+
42
+ splice(start: number, deleteCount?: number): T[] {
43
+ const removed: T[] = super.splice(start, deleteCount);
44
+
45
+ this.dispatch('change', {
46
+ 'removed': removed
47
+ });
48
+ for (const item of removed) {
49
+ this.dispatch('remove', item);
50
+ }
51
+
52
+ return removed;
53
+ }
54
+
55
+ get(key: string) {
56
+ const keys: string[] = [
57
+ 'length'
58
+ ];
59
+ return keys.indexOf(key) > -1 ? this[key] : undefined;
60
+ }
61
+
62
+ get length(): number {
63
+ let c: number = 0;
64
+ for (const item of this) {
65
+ c += 1;
66
+ }
67
+ return c;
68
+ }
69
+
70
+ set length(num: number) {
71
+ this.setLength(num);
72
+ }
73
+
74
+ setLength(num: number) {
75
+ let c: number = 0;
76
+ const toRemove: T[] = [];
77
+ for (const item of this) {
78
+ c += 1;
79
+ if (c >= num) {
80
+ toRemove.push(item);
81
+ }
82
+ }
83
+
84
+ for (const item of toRemove) {
85
+ this.splice(this.indexOf(item), 1);
86
+ }
87
+ }
88
+ }
@@ -0,0 +1,79 @@
1
+ import {EventDispatcher} from "../../EventDispatcher";
2
+ import {Registry} from "../../Registry";
3
+
4
+ export function property(propertyType = Property, config: {} | null = {}) {
5
+ return function(target: any, key: string) {
6
+ if(target.__properties__ == undefined) {
7
+ target.__properties__ = [];
8
+ }
9
+
10
+ // Abstract/extended classes share __properties__
11
+ if(target.__properties__.indexOf(key) == -1)
12
+ target.__properties__.push(key);
13
+
14
+ const getter = function() {
15
+ return [propertyType, config];
16
+ };
17
+
18
+ Object.defineProperty(target, '__'+key+'__', {
19
+ get: getter,
20
+ set: v => {},
21
+ enumerable:false,
22
+ configurable: true
23
+ });
24
+ }
25
+ }
26
+
27
+ export interface IPropertyConfig {
28
+ type?: string;
29
+ default?: any;
30
+ }
31
+
32
+ export class Property extends EventDispatcher {
33
+ _type: string = 'any';
34
+ _value: any;
35
+ config: IPropertyConfig;
36
+
37
+ constructor(value?:any, config?: IPropertyConfig) {
38
+ super();
39
+ this.config = config;
40
+ this.type = config.type || 'any';
41
+
42
+ this.value = value;
43
+ }
44
+
45
+ castType(value) {
46
+ const caster = Registry.instance.types.getSynchronous(this.type);
47
+ return caster(value);
48
+ }
49
+
50
+ set value(v:any) {
51
+ const oldValue = this._value;
52
+ this._value = this.castType(v);
53
+ if (this._value !== oldValue) {
54
+ this.dispatch('change', {
55
+ oldValue:oldValue,
56
+ value:v
57
+ });
58
+ }
59
+ }
60
+
61
+ get value() {
62
+ return this._value;
63
+ }
64
+
65
+ getData() {
66
+ return this.value;
67
+ }
68
+
69
+ set type(type: string) {
70
+ if (this._type != type) {
71
+ this._type = type;
72
+ this.value = this.value; // Need to reset value to have it cast to the new type
73
+ }
74
+ }
75
+
76
+ get type(): string {
77
+ return this._type;
78
+ }
79
+ }
package/src/Scope.ts CHANGED
@@ -1,218 +1,15 @@
1
- import {DataModel, Model} from "simple-ts-models";
2
- import {EventCallback, EventCallbackList, EventDispatcher, EventDispatcherCallback} from "simple-ts-event-dispatcher";
3
- import {Registry} from "./Registry";
4
- import {DOM} from "./DOM";
1
+ import {EventDispatcher} from "./EventDispatcher";
2
+ import {ScopeReference} from "./Scope/ScopeReference";
3
+ import {QueryReference} from "./Scope/QueryReference";
4
+ import {WrappedArray} from "./Scope/WrappedArray";
5
+ import {ScopeData} from "./Scope/ScopeData";
6
+ import {DynamicScopeData} from "./Scope/DynamicScopeData";
5
7
 
6
- export class ScopeReference {
7
- private _scope: Scope;
8
- private _key: string;
9
- private _value: any;
10
-
11
- constructor(
12
- scope: Scope = null,
13
- key: string = null,
14
- value: any = null
15
- ) {
16
- this._scope = scope;
17
- this._key = key;
18
- this._value = value;
19
- }
20
-
21
- public async getScope() {
22
- return this._scope;
23
- }
24
-
25
- public async getKey() {
26
- return this._key;
27
- }
28
-
29
- public async getValue() {
30
- return this._value;
31
- }
32
- }
33
-
34
- export class QueryReference extends ScopeReference {
35
- constructor(
36
- public readonly path: string,
37
- public readonly scope: Scope
38
- ) {
39
- super();
40
- }
41
-
42
- public async getScope() {
43
- let parts: string[] = this.path.split('.');
44
- parts = parts.splice(0, parts.length - 1);
45
- const qResult = await DOM.instance.eval(parts.join('.'));
46
- return qResult.length === 1 ? qResult[0].scope : qResult.map((dobj) => dobj.scope);
47
- }
48
-
49
- public async getKey() {
50
- const parts: string[] = this.path.split('.');
51
- return parts[parts.length - 1];
52
- }
53
-
54
- public async getValue() {
55
- return await DOM.instance.eval(this.path);
56
- }
57
- }
58
-
59
- export class ScopeVariableType {
60
- public static readonly Integer: string = 'integer';
61
- public static readonly Float: string = 'float';
62
- public static readonly Boolean: string = 'boolean';
63
- public static readonly String: string = 'string';
64
- }
65
-
66
- export class WrappedArray<T> extends Array<T> {
67
- private _listeners: EventCallbackList;
68
- private _lastKey: number;
69
- public readonly $wrapped: boolean = true;
70
-
71
- constructor(...items: T[]) {
72
- super(...items);
73
- Object.setPrototypeOf(this, WrappedArray.prototype);
74
- this._lastKey = 0;
75
- this._listeners = {};
76
- }
77
-
78
- push(...items: T[]): number {
79
- const num: number = super.push(...items);
80
-
81
- this.trigger('push', ...items);
82
- this.trigger('change', {
83
- 'added': items
84
- });
85
- for (const item of items) {
86
- this.trigger('add', item);
87
- }
88
- return num;
89
- }
90
-
91
- splice(start: number, deleteCount?: number): T[] {
92
- const removed: T[] = super.splice(start, deleteCount);
93
-
94
- this.trigger('change', {
95
- 'removed': removed
96
- });
97
- for (const item of removed) {
98
- this.trigger('remove', item);
99
- }
100
-
101
- return removed;
102
- }
103
-
104
- get(key: string) {
105
- const keys: string[] = [
106
- 'length'
107
- ];
108
- return keys.indexOf(key) > -1 ? this[key] : undefined;
109
- }
110
-
111
- get length(): number {
112
- let c: number = 0;
113
- for (const item of this) {
114
- c += 1;
115
- }
116
- return c;
117
- }
118
-
119
- set length(num: number) {
120
- this.setLength(num);
121
- }
122
-
123
- setLength(num: number) {
124
- let c: number = 0;
125
- const toRemove: T[] = [];
126
- for (const item of this) {
127
- c += 1;
128
- if (c >= num) {
129
- toRemove.push(item);
130
- }
131
- }
132
-
133
- for (const item of toRemove) {
134
- this.splice(this.indexOf(item), 1);
135
- }
136
- }
137
-
138
- bind(event: string, fct: EventDispatcherCallback, context?: any, once?: boolean): number {
139
- once = once || false;
140
- this._lastKey++;
141
- this._listeners[event] = this._listeners[event] || [];
142
- this._listeners[event].push(new EventCallback(fct, this._lastKey, once, context));
143
- return this._lastKey;
144
- }
145
-
146
- once(event: string, fct: EventDispatcherCallback, context?: any): number {
147
- return this.bind(event, fct, context, true);
148
- }
149
-
150
- unbind(event: string, key?: number): boolean {
151
- if(!(event in this._listeners)) return false;
152
- if(key) {
153
- for(const cb of this._listeners[event]) {
154
- if(key == cb.key) {
155
- this._listeners[event].splice(this._listeners[event].indexOf(cb), 1);
156
- return true;
157
- }
158
- }
159
- } else {
160
- this._listeners[event] = [];
161
- return true;
162
- }
163
- return false;
164
- }
165
-
166
- unbindWithContext(event: string, context: any): number {
167
- if(!(event in this._listeners)) return 0;
168
- let toRemove: EventCallback[] = [],
169
- cnt = 0;
170
-
171
- for(const cb of this._listeners[event]) {
172
- if(context == cb.context) {
173
- toRemove.push(cb);
174
- }
175
- }
176
-
177
- for(const cb of toRemove) {
178
- this._listeners[event].splice(this._listeners[event].indexOf(cb), 1);
179
- cnt++;
180
- }
181
- return cnt;
182
- }
183
-
184
- getListener(event: string, key: number): EventCallback | undefined {
185
- for(const cb of this._listeners[event]) {
186
- if(key == cb.key)
187
- return cb;
188
- }
189
- }
190
-
191
- trigger(event: string, ...args: any[]): void {
192
- if(!(event in this._listeners)) return;
193
-
194
- for(let i = 0; i < this._listeners[event].length; i++) {
195
- const cb: EventCallback = this._listeners[event][i];
196
-
197
- // We need to unbind callbacks before they're called to prevent
198
- // infinite loops if the event is somehow triggered within the
199
- // callback
200
- if(cb.once) {
201
- this.unbind(event, cb.key);
202
- i--;
203
- }
204
-
205
- cb.call(args);
206
- }
207
- }
208
- }
209
8
 
210
9
  export class Scope extends EventDispatcher {
211
10
  public wrapped: any;
212
- protected data: DataModel;
213
- protected types: {[key: string]: string;} = {};
11
+ protected data: ScopeData;
214
12
  protected children: Scope[];
215
- protected _keys: string[];
216
13
  protected _parentScope: Scope;
217
14
 
218
15
  constructor(
@@ -222,8 +19,8 @@ export class Scope extends EventDispatcher {
222
19
  if (parent)
223
20
  this.parentScope = parent;
224
21
  this.children = [];
225
- this.data = new DataModel({});
226
- this._keys = [];
22
+ this.data = new DynamicScopeData({});
23
+ this.data.addRelay(this);
227
24
  }
228
25
 
229
26
  public get parentScope(): Scope {
@@ -283,54 +80,30 @@ export class Scope extends EventDispatcher {
283
80
  }
284
81
 
285
82
  set(key: string, value: any) {
286
- if (this.data[key] === undefined)
287
- this.data.createField(key);
288
-
289
- if (typeof value === 'string') {
290
- const valueType = this.getType(key);
291
- const caster = Registry.instance.types.getSynchronous(valueType);
292
- if (caster) {
293
- value = caster(value);
294
- }
295
-
296
- if ([ScopeVariableType.Integer, ScopeVariableType.Float].indexOf(valueType) > -1 && isNaN(value)) {
297
- value = null;
298
- }
299
- }
83
+ if (!this.data.hasProperty(key))
84
+ this.data.createProperty(key);
300
85
 
301
- if (this.data[key] !== value) {
302
- const previousValue = this.data[key];
303
- this.data[key] = value;
304
- const event = {
305
- value: value,
306
- previousValue: previousValue,
307
- key: key
308
- };
309
-
310
- this.trigger(`change:${key}`, event);
311
- this.trigger('change', key, event);
312
- }
313
-
314
- if (this._keys.indexOf(key) === -1)
315
- this._keys.push(key);
86
+ this.data[key] = value;
316
87
  }
317
88
 
318
89
  get keys(): string[] {
319
- return [...this._keys];
90
+ return this.data.keys;
320
91
  }
321
92
 
322
93
  has(key: string): boolean {
323
- return this._keys.indexOf(key) > -1;
94
+ return this.data.hasProperty(key);
324
95
  }
325
96
 
326
97
  setType(key: string, type: string) {
327
- this.types[key] = type;
98
+ const property = this.data.getProperty(key, true);
99
+ property.type = type;
328
100
  if (this.has(key))
329
101
  this.set(key, this.get(key));
330
102
  }
331
103
 
332
104
  getType(key: string): string {
333
- return this.types[key] || ScopeVariableType.String;
105
+ const property = this.data.getProperty(key);
106
+ return property.type;
334
107
  }
335
108
 
336
109
  extend(data) {
@@ -340,7 +113,7 @@ export class Scope extends EventDispatcher {
340
113
  }
341
114
 
342
115
  clear() {
343
- for (const key of this._keys) {
116
+ for (const key of this.data.keys) {
344
117
  if (['function', 'object'].indexOf(typeof this.get(key)) > -1) continue;
345
118
  this.set(key, null);
346
119
  }
@@ -352,8 +125,14 @@ export class Scope extends EventDispatcher {
352
125
  }
353
126
 
354
127
  public wrap(toWrap: any, triggerUpdates: boolean = false, updateFromWrapped: boolean = true) {
355
- if (toWrap instanceof Model) {
128
+ if (toWrap instanceof ScopeData) {
129
+ if (this.data instanceof EventDispatcher) {
130
+ this.data.removeRelay(this);
131
+ this.data.deconstruct();
132
+ }
133
+ this.wrapped = toWrap;
356
134
  this.data = toWrap;
135
+ this.data.addRelay(this);
357
136
  return;
358
137
  }
359
138
 
@@ -386,8 +165,8 @@ export class Scope extends EventDispatcher {
386
165
  if (!(this.wrapped[field] instanceof WrappedArray)) {
387
166
  this.wrapped[field] = new WrappedArray(...toWrap[field]);
388
167
  }
389
- this.wrapped[field].bind('change', (...args) => {
390
- this.trigger(`change:${field}`, ...args);
168
+ this.wrapped[field].on('change', (...args) => {
169
+ this.dispatch(`change:${field}`, ...args);
391
170
  })
392
171
  }
393
172
 
@@ -418,14 +197,14 @@ export class Scope extends EventDispatcher {
418
197
  });
419
198
 
420
199
  if (triggerUpdates)
421
- this.trigger(`change:${field}`);
200
+ this.dispatch(`change:${field}`);
422
201
  }
423
202
 
424
203
  this.wrapped.get = this.get.bind(this);
425
204
  this.wrapped.set = this.set.bind(this);
426
- this.wrapped.bind = this.bind.bind(this);
205
+ this.wrapped.on = this.on.bind(this);
427
206
  this.wrapped.once = this.once.bind(this);
428
- this.wrapped.unbind = this.unbind.bind(this);
207
+ this.wrapped.off = this.off.bind(this);
429
208
  }
430
209
 
431
210
  public unwrap(): void {