vsn 0.1.24 → 0.1.26

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 (127) hide show
  1. package/demo/demo.html +16 -7
  2. package/demo/vsn.js +1 -0
  3. package/dist/AST.d.ts +2 -1
  4. package/dist/Controller.d.ts +8 -2
  5. package/dist/Model.d.ts +0 -0
  6. package/dist/{Vision.d.ts → vsn.d.ts} +1 -0
  7. package/dist/vsn.min.js +1 -0
  8. package/package.json +8 -8
  9. package/src/AST.ts +94 -3
  10. package/src/Controller.ts +21 -4
  11. package/src/DOM.ts +1 -1
  12. package/src/Model.ts +0 -0
  13. package/src/Registry.ts +0 -2
  14. package/src/Scope.ts +6 -1
  15. package/src/Tag.ts +8 -4
  16. package/src/attributes/On.ts +2 -0
  17. package/src/attributes/Radio.ts +2 -1
  18. package/src/{Vision.ts → vsn.ts} +1 -0
  19. package/test/DOM.spec.ts +1 -1
  20. package/test/attributes/JSONAttribute.spec.ts +1 -1
  21. package/webpack.config.js +2 -2
  22. package/demo/vision.js +0 -1
  23. package/dist/AST.js +0 -2107
  24. package/dist/AST.js.map +0 -1
  25. package/dist/Attribute.js +0 -187
  26. package/dist/Attribute.js.map +0 -1
  27. package/dist/Bencmark.js +0 -179
  28. package/dist/Bencmark.js.map +0 -1
  29. package/dist/Configuration.js +0 -64
  30. package/dist/Configuration.js.map +0 -1
  31. package/dist/Controller.js +0 -30
  32. package/dist/Controller.js.map +0 -1
  33. package/dist/DOM/DOMObject.js +0 -47
  34. package/dist/DOM/DOMObject.js.map +0 -1
  35. package/dist/DOM/WrappedDocument.js +0 -34
  36. package/dist/DOM/WrappedDocument.js.map +0 -1
  37. package/dist/DOM/WrappedWindow.js +0 -45
  38. package/dist/DOM/WrappedWindow.js.map +0 -1
  39. package/dist/DOM.js +0 -547
  40. package/dist/DOM.js.map +0 -1
  41. package/dist/Formats.js +0 -44
  42. package/dist/Formats.js.map +0 -1
  43. package/dist/Query.js +0 -66
  44. package/dist/Query.js.map +0 -1
  45. package/dist/Registry.js +0 -138
  46. package/dist/Registry.js.map +0 -1
  47. package/dist/Scope.js +0 -512
  48. package/dist/Scope.js.map +0 -1
  49. package/dist/Tag/List.js +0 -85
  50. package/dist/Tag/List.js.map +0 -1
  51. package/dist/Tag.js +0 -764
  52. package/dist/Tag.js.map +0 -1
  53. package/dist/Types.js +0 -48
  54. package/dist/Types.js.map +0 -1
  55. package/dist/Vision.js +0 -162
  56. package/dist/Vision.js.map +0 -1
  57. package/dist/attributes/AddClassIf.js +0 -93
  58. package/dist/attributes/AddClassIf.js.map +0 -1
  59. package/dist/attributes/Bind.js +0 -272
  60. package/dist/attributes/Bind.js.map +0 -1
  61. package/dist/attributes/ClassConstructor.js +0 -104
  62. package/dist/attributes/ClassConstructor.js.map +0 -1
  63. package/dist/attributes/ClickRemoveClass.js +0 -114
  64. package/dist/attributes/ClickRemoveClass.js.map +0 -1
  65. package/dist/attributes/ClickToggleClass.js +0 -114
  66. package/dist/attributes/ClickToggleClass.js.map +0 -1
  67. package/dist/attributes/ControllerAttribute.js +0 -28
  68. package/dist/attributes/ControllerAttribute.js.map +0 -1
  69. package/dist/attributes/DisableIf.js +0 -94
  70. package/dist/attributes/DisableIf.js.map +0 -1
  71. package/dist/attributes/Exec.js +0 -108
  72. package/dist/attributes/Exec.js.map +0 -1
  73. package/dist/attributes/Format.js +0 -99
  74. package/dist/attributes/Format.js.map +0 -1
  75. package/dist/attributes/If.js +0 -159
  76. package/dist/attributes/If.js.map +0 -1
  77. package/dist/attributes/JSONAttribute.js +0 -118
  78. package/dist/attributes/JSONAttribute.js.map +0 -1
  79. package/dist/attributes/KeyAbstract.js +0 -117
  80. package/dist/attributes/KeyAbstract.js.map +0 -1
  81. package/dist/attributes/KeyDown.js +0 -88
  82. package/dist/attributes/KeyDown.js.map +0 -1
  83. package/dist/attributes/KeyUp.js +0 -88
  84. package/dist/attributes/KeyUp.js.map +0 -1
  85. package/dist/attributes/List.js +0 -282
  86. package/dist/attributes/List.js.map +0 -1
  87. package/dist/attributes/ListItem.js +0 -138
  88. package/dist/attributes/ListItem.js.map +0 -1
  89. package/dist/attributes/ListItemModel.js +0 -39
  90. package/dist/attributes/ListItemModel.js.map +0 -1
  91. package/dist/attributes/ModelAttribute.js +0 -29
  92. package/dist/attributes/ModelAttribute.js.map +0 -1
  93. package/dist/attributes/Name.js +0 -88
  94. package/dist/attributes/Name.js.map +0 -1
  95. package/dist/attributes/On.js +0 -132
  96. package/dist/attributes/On.js.map +0 -1
  97. package/dist/attributes/Radio.js +0 -172
  98. package/dist/attributes/Radio.js.map +0 -1
  99. package/dist/attributes/Referenced.js +0 -38
  100. package/dist/attributes/Referenced.js.map +0 -1
  101. package/dist/attributes/RootAttribute.js +0 -91
  102. package/dist/attributes/RootAttribute.js.map +0 -1
  103. package/dist/attributes/ScopeAttribute.js +0 -40
  104. package/dist/attributes/ScopeAttribute.js.map +0 -1
  105. package/dist/attributes/ScopeChange.js +0 -130
  106. package/dist/attributes/ScopeChange.js.map +0 -1
  107. package/dist/attributes/SetAttribute.js +0 -133
  108. package/dist/attributes/SetAttribute.js.map +0 -1
  109. package/dist/attributes/StandardAttribute.js +0 -186
  110. package/dist/attributes/StandardAttribute.js.map +0 -1
  111. package/dist/attributes/StyleAttribute.js +0 -183
  112. package/dist/attributes/StyleAttribute.js.map +0 -1
  113. package/dist/attributes/Template.js +0 -39
  114. package/dist/attributes/Template.js.map +0 -1
  115. package/dist/attributes/TypeAttribute.js +0 -104
  116. package/dist/attributes/TypeAttribute.js.map +0 -1
  117. package/dist/attributes/_imports.js +0 -60
  118. package/dist/attributes/_imports.js.map +0 -1
  119. package/dist/helpers/DOMHelper.js +0 -81
  120. package/dist/helpers/DOMHelper.js.map +0 -1
  121. package/dist/helpers/ElementHelper.js +0 -25
  122. package/dist/helpers/ElementHelper.js.map +0 -1
  123. package/dist/helpers/VisionHelper.js +0 -71
  124. package/dist/helpers/VisionHelper.js.map +0 -1
  125. package/dist/helpers/decorators.js +0 -38
  126. package/dist/helpers/decorators.js.map +0 -1
  127. package/main.py +0 -16
package/src/AST.ts CHANGED
@@ -78,7 +78,8 @@ export enum TokenType {
78
78
  ELEMENT_REFERENCE,
79
79
  ELEMENT_ATTRIBUTE,
80
80
  ELEMENT_STYLE,
81
- ELEMENT_QUERY
81
+ ELEMENT_QUERY,
82
+ UNIT,
82
83
  }
83
84
 
84
85
  const TOKEN_PATTERNS: TokenPattern[] = [
@@ -98,6 +99,10 @@ const TOKEN_PATTERNS: TokenPattern[] = [
98
99
  type: TokenType.TYPE_FLOAT,
99
100
  pattern: /^float+/
100
101
  },
102
+ {
103
+ type: TokenType.UNIT,
104
+ pattern: /^\d+\.?\d?(?:cm|mm|in|px|pt|pc|em|ex|ch|rem|vw|vh|vmin|vmax|%)/
105
+ },
101
106
  {
102
107
  type: TokenType.TYPE_STRING,
103
108
  pattern: /^string+/
@@ -144,11 +149,11 @@ const TOKEN_PATTERNS: TokenPattern[] = [
144
149
  },
145
150
  {
146
151
  type: TokenType.ELEMENT_ATTRIBUTE,
147
- pattern: /^\.?@[_a-zA-Z0-9]*/
152
+ pattern: /^\.?@[-_a-zA-Z0-9]*/
148
153
  },
149
154
  {
150
155
  type: TokenType.ELEMENT_STYLE,
151
- pattern: /^\.?\$[a-zA-Z0-9]*/
156
+ pattern: /^\.?\$[-a-zA-Z0-9]*/
152
157
  },
153
158
  {
154
159
  type: TokenType.ELEMENT_REFERENCE,
@@ -687,6 +692,55 @@ class NumberLiteralNode extends LiteralNode<number> {
687
692
  }
688
693
  }
689
694
 
695
+ class UnitLiteral {
696
+ protected _amount: number;
697
+ protected _unit: string;
698
+
699
+ constructor(
700
+ protected _value: any
701
+ ) {
702
+ this.value = this._value;
703
+ }
704
+
705
+ get amount(): number {
706
+ return this._amount;
707
+ }
708
+
709
+ get unit(): string {
710
+ return this._unit;
711
+ }
712
+
713
+ get value(): string {
714
+ return `${this._amount}${this._unit}`;
715
+ }
716
+
717
+ set value(value: string) {
718
+ if (value.indexOf('.') > -1) {
719
+ this._amount = parseFloat(value)
720
+ } else {
721
+ this._amount = parseInt(value);
722
+ }
723
+
724
+ if (isNaN(this._amount))
725
+ this._amount = 0;
726
+
727
+ const unit = /[^\d.]+$/.exec(value);
728
+ this._unit = unit && unit[0] || '';
729
+ }
730
+
731
+ public toString() {
732
+ return this.value;
733
+ }
734
+ }
735
+
736
+ class UnitLiteralNode extends LiteralNode<UnitLiteral> {
737
+ constructor(
738
+ _value: any
739
+ ) {
740
+ super(new UnitLiteral(_value));
741
+ }
742
+ }
743
+
690
744
 
691
745
  class FunctionCallNode<T = any> extends Node implements TreeNode {
692
746
  constructor(
@@ -895,6 +949,8 @@ class ArithmeticAssignmentNode extends Node implements TreeNode {
895
949
 
896
950
  if (left instanceof Array) {
897
951
  left = this.handleArray(name, left, right, localScope);
952
+ } else if ((left as any) instanceof UnitLiteral || right instanceof UnitLiteral) {
953
+ left = this.handleUnit(name, left, right, localScope);
898
954
  } else if (Number.isFinite(left)) {
899
955
  left = this.handleNumber(name, left, right, localScope);
900
956
  } else {
@@ -958,6 +1014,38 @@ class ArithmeticAssignmentNode extends Node implements TreeNode {
958
1014
  return left;
959
1015
  }
960
1016
 
1017
+ public handleUnit(key, left, right, scope) {
1018
+ if (!(left instanceof UnitLiteral)) {
1019
+ left = new UnitLiteral(left);
1020
+ }
1021
+
1022
+ if (!(right instanceof UnitLiteral)) {
1023
+ right = new UnitLiteral(right);
1024
+ }
1025
+ const unit = left.unit || right.unit || 'px';
1026
+
1027
+ switch (this.type) {
1028
+ case TokenType.ASSIGN:
1029
+ left = right;
1030
+ break;
1031
+ case TokenType.ADD_ASSIGN:
1032
+ left = new UnitLiteral(`${left.amount+right.amount}${unit}`);
1033
+ break;
1034
+ case TokenType.SUBTRACT_ASSIGN:
1035
+ left = new UnitLiteral(`${left.amount-right.amount}${unit}`);
1036
+ break;
1037
+ case TokenType.MULTIPLY_ASSIGN:
1038
+ left = new UnitLiteral(`${left.amount*right.amount}${unit}`);
1039
+ break;
1040
+ case TokenType.DIVIDE_ASSIGN:
1041
+ left = new UnitLiteral(`${left.amount/right.amount}${unit}`);
1042
+ break;
1043
+ }
1044
+
1045
+ scope.set(key, left);
1046
+ return left;
1047
+ }
1048
+
961
1049
  public async handleDOMObject(key: string, dom: DOM, domObject: DOMObject, tag: Tag) {
962
1050
  let left = domObject.scope.get(key);
963
1051
  let right: number | Array<any> | string = await this.right.evaluate(domObject.scope, dom, tag);
@@ -1486,6 +1574,9 @@ export class Tree {
1486
1574
  node = ArithmeticAssignmentNode.parse(node, token, tokens);
1487
1575
  } else if (tokens[0].type === TokenType.WHITESPACE) {
1488
1576
  tokens.splice(0, 1);
1577
+ } else if (tokens[0].type === TokenType.UNIT) {
1578
+ node = new UnitLiteralNode(tokens[0].value);
1579
+ tokens.splice(0, 1);
1489
1580
  } else if (tokens[0].type === TokenType.BOOLEAN_LITERAL) {
1490
1581
  node = new BooleanLiteralNode(tokens[0].value);
1491
1582
  tokens.splice(0, 1);
package/src/Controller.ts CHANGED
@@ -1,10 +1,27 @@
1
1
  import {EventDispatcher} from "simple-ts-event-dispatcher";
2
+ import {Scope} from "./Scope";
2
3
  import {Tag} from "./Tag";
3
4
 
4
5
  export abstract class Controller extends EventDispatcher {
5
- protected constructor(
6
- protected tag: Tag
7
- ) {
8
- super();
6
+ protected _scope: Scope;
7
+ protected _tag: Tag;
8
+ protected _element: HTMLElement;
9
+
10
+ public get scope(): Scope {
11
+ return this._scope;
12
+ }
13
+
14
+ public get tag(): Tag {
15
+ return this._tag;
16
+ }
17
+
18
+ public get element(): HTMLElement {
19
+ return this._element;
20
+ }
21
+
22
+ public init(scope: Scope, tag: Tag, element: HTMLElement): void {
23
+ this._scope = scope;
24
+ this._tag = tag;
25
+ this._element = element;
9
26
  }
10
27
  }
package/src/DOM.ts CHANGED
@@ -183,7 +183,7 @@ export class DOM extends EventDispatcher {
183
183
  if (tag === this.root)
184
184
  continue;
185
185
 
186
- // Find closest ancestor
186
+ // Find the closest ancestor
187
187
  let parentElement: HTMLElement = tag.element.parentElement as HTMLElement;
188
188
  let foundParent = false;
189
189
  while (parentElement) {
package/src/Model.ts ADDED
File without changes
package/src/Registry.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import {EventDispatcher} from "simple-ts-event-dispatcher";
2
2
  import {IDeferred, IPromise, Promise as SimplePromise} from "simple-ts-promise";
3
3
  import {VisionHelper} from "./helpers/VisionHelper";
4
- import {benchmark} from "./Bencmark";
5
4
 
6
5
  export function register(store: string, key: string = null, setup: () => void = null) {
7
6
  return function(target: any, _key: string = null) {
@@ -28,7 +27,6 @@ export class RegistryStore extends EventDispatcher {
28
27
  this.trigger(`registered:${key}`, item);
29
28
  }
30
29
 
31
- @benchmark('RegistryStore.get')
32
30
  get(key: string): IPromise<any> {
33
31
  const deferred: IDeferred<any> = SimplePromise.defer();
34
32
 
package/src/Scope.ts CHANGED
@@ -1,4 +1,4 @@
1
- import {DataModel} from "simple-ts-models";
1
+ import {DataModel, Model} from "simple-ts-models";
2
2
  import {EventCallback, EventCallbackList, EventDispatcher, EventDispatcherCallback} from "simple-ts-event-dispatcher";
3
3
  import {Registry} from "./Registry";
4
4
  import {DOM} from "./DOM";
@@ -352,6 +352,11 @@ export class Scope extends EventDispatcher {
352
352
  }
353
353
 
354
354
  public wrap(toWrap: any, triggerUpdates: boolean = false, updateFromWrapped: boolean = true) {
355
+ if (toWrap instanceof Model) {
356
+ this.data = toWrap;
357
+ return;
358
+ }
359
+
355
360
  if (toWrap instanceof Scope)
356
361
  toWrap = toWrap.data;
357
362
 
package/src/Tag.ts CHANGED
@@ -251,10 +251,14 @@ export class Tag extends DOMObject {
251
251
  obj = new obj();
252
252
  }
253
253
 
254
- this.scope.wrap(obj, triggerUpdates, updateFromWrapped);
255
- obj['$scope'] = this.scope;
256
- obj['$tag'] = this;
257
- obj['$el'] = this.element;
254
+ if (obj instanceof Controller) {
255
+ obj.init(this.scope, this, this.element);
256
+ } else {
257
+ this.scope.wrap(obj, triggerUpdates, updateFromWrapped);
258
+ obj['$scope'] = this.scope;
259
+ obj['$tag'] = this;
260
+ obj['$el'] = this.element;
261
+ }
258
262
  return obj;
259
263
  }
260
264
 
@@ -26,6 +26,8 @@ export abstract class On extends Attribute {
26
26
  }
27
27
 
28
28
  async handleEvent(e) {
29
+ if (this.hasModifier('preventdefault'))
30
+ e.preventDefault();
29
31
  await this.handler.evaluate(this.tag.scope, this.tag.dom, this.tag);
30
32
  }
31
33
 
@@ -47,6 +47,7 @@ export class Radio extends Attribute {
47
47
  }
48
48
 
49
49
  async checkSelected() {
50
- this.tag.checked = this.boundScope.get(this.key) === this.tag.value;
50
+ const scopeValue = this.boundScope.get(this.key);
51
+ this.tag.checked = `${scopeValue}` === this.tag.value;
51
52
  }
52
53
  }
@@ -73,4 +73,5 @@ export * from './Attribute';
73
73
  export * from './AST';
74
74
  export {DOM} from './DOM';
75
75
  export {WrappedArray, Scope, ScopeReference} from './Scope';
76
+ export {Controller} from './Controller';
76
77
  export const vision: Vision = Vision.instance;
package/test/DOM.spec.ts CHANGED
@@ -1,4 +1,4 @@
1
- import "../src/Vision";
1
+ import "../src/vsn";
2
2
  import {DOM} from "../src/DOM";
3
3
  import {Registry} from "../src/Registry";
4
4
 
@@ -77,7 +77,7 @@ describe('JSONAttribute', () => {
77
77
  it("vsn-json should work with a ref path", (done) => {
78
78
  document.body.innerHTML = `
79
79
  <div vsn-name="testing">
80
- <div vsn-json:testing.test="{&quot;testing&quot;: 123}"></div>
80
+ <div vsn-json:testing.test='{"testing": 123}'></div>
81
81
  </div>
82
82
  `;
83
83
  const dom = new DOM(document);
package/webpack.config.js CHANGED
@@ -2,7 +2,7 @@ const path = require('path');
2
2
  const webpack = require("webpack");
3
3
 
4
4
  const defaultConfiguration = {
5
- entry: './src/Vision.ts',
5
+ entry: './src/vsn.ts',
6
6
  module: {
7
7
  rules: [
8
8
  {
@@ -17,7 +17,7 @@ const defaultConfiguration = {
17
17
  },
18
18
  plugins: [],
19
19
  output: {
20
- filename: 'vision.min.js',
20
+ filename: 'vsn.min.js',
21
21
  path: path.resolve(__dirname, 'dist'),
22
22
  },
23
23
  };