@gogocat/data-bind 1.11.0 → 2.0.0

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 (274) hide show
  1. package/.editorconfig +14 -14
  2. package/.vscode/launch.json +12 -12
  3. package/CONFIGURATION.md +294 -0
  4. package/REACTIVE_MODE.md +553 -0
  5. package/README.md +266 -829
  6. package/babel.config.json +30 -0
  7. package/dist/js/_escape.d.ts +14 -0
  8. package/dist/js/_escape.d.ts.map +1 -0
  9. package/dist/js/applyBinding.d.ts +11 -0
  10. package/dist/js/applyBinding.d.ts.map +1 -0
  11. package/dist/js/attrBinding.d.ts +12 -0
  12. package/dist/js/attrBinding.d.ts.map +1 -0
  13. package/dist/js/binder.d.ts +67 -0
  14. package/dist/js/binder.d.ts.map +1 -0
  15. package/dist/js/changeBinding.d.ts +19 -0
  16. package/dist/js/changeBinding.d.ts.map +1 -0
  17. package/dist/js/commentWrapper.d.ts +39 -0
  18. package/dist/js/commentWrapper.d.ts.map +1 -0
  19. package/dist/js/config.d.ts +55 -0
  20. package/dist/js/config.d.ts.map +1 -0
  21. package/dist/js/createBindingOption.d.ts +32 -0
  22. package/dist/js/createBindingOption.d.ts.map +1 -0
  23. package/dist/js/createEventBinding.d.ts +10 -0
  24. package/dist/js/createEventBinding.d.ts.map +1 -0
  25. package/dist/js/cssBinding.d.ts +15 -0
  26. package/dist/js/cssBinding.d.ts.map +1 -0
  27. package/dist/js/dataBind.js +2772 -2519
  28. package/dist/js/dataBind.min.js +8 -1
  29. package/dist/js/dataBind.min.js.map +1 -1
  30. package/dist/js/domWalker.d.ts +9 -0
  31. package/dist/js/domWalker.d.ts.map +1 -0
  32. package/dist/js/forOfBinding.d.ts +12 -0
  33. package/dist/js/forOfBinding.d.ts.map +1 -0
  34. package/dist/js/hoverBinding.d.ts +13 -0
  35. package/dist/js/hoverBinding.d.ts.map +1 -0
  36. package/dist/js/ifBinding.d.ts +12 -0
  37. package/dist/js/ifBinding.d.ts.map +1 -0
  38. package/dist/js/index.d.ts +10 -0
  39. package/dist/js/index.d.ts.map +1 -0
  40. package/dist/js/modelBinding.d.ts +12 -0
  41. package/dist/js/modelBinding.d.ts.map +1 -0
  42. package/dist/js/postProcess.d.ts +3 -0
  43. package/dist/js/postProcess.d.ts.map +1 -0
  44. package/dist/js/pubSub.d.ts +11 -0
  45. package/dist/js/pubSub.d.ts.map +1 -0
  46. package/dist/js/reactiveProxy.d.ts +28 -0
  47. package/dist/js/reactiveProxy.d.ts.map +1 -0
  48. package/dist/js/renderForOfBinding.d.ts +8 -0
  49. package/dist/js/renderForOfBinding.d.ts.map +1 -0
  50. package/dist/js/renderIfBinding.d.ts +22 -0
  51. package/dist/js/renderIfBinding.d.ts.map +1 -0
  52. package/dist/js/renderIteration.d.ts +16 -0
  53. package/dist/js/renderIteration.d.ts.map +1 -0
  54. package/dist/js/renderTemplate.d.ts +14 -0
  55. package/dist/js/renderTemplate.d.ts.map +1 -0
  56. package/dist/js/renderTemplatesBinding.d.ts +19 -0
  57. package/dist/js/renderTemplatesBinding.d.ts.map +1 -0
  58. package/dist/js/showBinding.d.ts +13 -0
  59. package/dist/js/showBinding.d.ts.map +1 -0
  60. package/dist/js/switchBinding.d.ts +13 -0
  61. package/dist/js/switchBinding.d.ts.map +1 -0
  62. package/dist/js/textBinding.d.ts +13 -0
  63. package/dist/js/textBinding.d.ts.map +1 -0
  64. package/dist/js/types/_escape.d.ts +14 -0
  65. package/dist/js/types/_escape.d.ts.map +1 -0
  66. package/dist/js/types/applyBinding.d.ts +11 -0
  67. package/dist/js/types/applyBinding.d.ts.map +1 -0
  68. package/dist/js/types/attrBinding.d.ts +12 -0
  69. package/dist/js/types/attrBinding.d.ts.map +1 -0
  70. package/dist/js/types/binder.d.ts +67 -0
  71. package/dist/js/types/binder.d.ts.map +1 -0
  72. package/dist/js/types/changeBinding.d.ts +19 -0
  73. package/dist/js/types/changeBinding.d.ts.map +1 -0
  74. package/dist/js/types/commentWrapper.d.ts +39 -0
  75. package/dist/js/types/commentWrapper.d.ts.map +1 -0
  76. package/dist/js/types/config.d.ts +55 -0
  77. package/dist/js/types/config.d.ts.map +1 -0
  78. package/dist/js/types/createBindingOption.d.ts +32 -0
  79. package/dist/js/types/createBindingOption.d.ts.map +1 -0
  80. package/dist/js/types/createEventBinding.d.ts +10 -0
  81. package/dist/js/types/createEventBinding.d.ts.map +1 -0
  82. package/dist/js/types/cssBinding.d.ts +15 -0
  83. package/dist/js/types/cssBinding.d.ts.map +1 -0
  84. package/dist/js/types/domWalker.d.ts +9 -0
  85. package/dist/js/types/domWalker.d.ts.map +1 -0
  86. package/dist/js/types/forOfBinding.d.ts +12 -0
  87. package/dist/js/types/forOfBinding.d.ts.map +1 -0
  88. package/dist/js/types/hoverBinding.d.ts +13 -0
  89. package/dist/js/types/hoverBinding.d.ts.map +1 -0
  90. package/dist/js/types/ifBinding.d.ts +12 -0
  91. package/dist/js/types/ifBinding.d.ts.map +1 -0
  92. package/dist/js/types/index.d.ts +10 -0
  93. package/dist/js/types/index.d.ts.map +1 -0
  94. package/dist/js/types/modelBinding.d.ts +12 -0
  95. package/dist/js/types/modelBinding.d.ts.map +1 -0
  96. package/dist/js/types/postProcess.d.ts +3 -0
  97. package/dist/js/types/postProcess.d.ts.map +1 -0
  98. package/dist/js/types/pubSub.d.ts +11 -0
  99. package/dist/js/types/pubSub.d.ts.map +1 -0
  100. package/dist/js/types/reactiveProxy.d.ts +28 -0
  101. package/dist/js/types/reactiveProxy.d.ts.map +1 -0
  102. package/dist/js/types/renderForOfBinding.d.ts +8 -0
  103. package/dist/js/types/renderForOfBinding.d.ts.map +1 -0
  104. package/dist/js/types/renderIfBinding.d.ts +22 -0
  105. package/dist/js/types/renderIfBinding.d.ts.map +1 -0
  106. package/dist/js/types/renderIteration.d.ts +16 -0
  107. package/dist/js/types/renderIteration.d.ts.map +1 -0
  108. package/dist/js/types/renderTemplate.d.ts +14 -0
  109. package/dist/js/types/renderTemplate.d.ts.map +1 -0
  110. package/dist/js/types/renderTemplatesBinding.d.ts +19 -0
  111. package/dist/js/types/renderTemplatesBinding.d.ts.map +1 -0
  112. package/dist/js/types/showBinding.d.ts +13 -0
  113. package/dist/js/types/showBinding.d.ts.map +1 -0
  114. package/dist/js/types/switchBinding.d.ts +13 -0
  115. package/dist/js/types/switchBinding.d.ts.map +1 -0
  116. package/dist/js/types/textBinding.d.ts +13 -0
  117. package/dist/js/types/textBinding.d.ts.map +1 -0
  118. package/dist/js/types/types.d.ts +111 -0
  119. package/dist/js/types/types.d.ts.map +1 -0
  120. package/dist/js/types/util.d.ts +119 -0
  121. package/dist/js/types/util.d.ts.map +1 -0
  122. package/dist/js/types.d.ts +111 -0
  123. package/dist/js/types.d.ts.map +1 -0
  124. package/dist/js/util.d.ts +119 -0
  125. package/dist/js/util.d.ts.map +1 -0
  126. package/eslint.config.js +124 -0
  127. package/examples/DBMONSTER_COMPARISON.md +123 -0
  128. package/examples/afterRenderDemo.html +119 -0
  129. package/examples/bootstrap/css/animate.css +1579 -1579
  130. package/examples/bootstrap/css/bootstrap.min.css +6 -6
  131. package/examples/bootstrap/css/homeservices.css +378 -390
  132. package/examples/bootstrap/css/open-iconic.css +511 -511
  133. package/examples/bootstrap/fonts/open-iconic.svg +543 -543
  134. package/examples/bootstrap/js/compMessageDialog.js +20 -19
  135. package/examples/bootstrap/js/compSearchBar.js +12 -19
  136. package/examples/bootstrap/js/compSearchResults.js +50 -46
  137. package/examples/bootstrap/js/featureAdsResult.json +65 -65
  138. package/examples/bootstrap/js/searchResult.json +57 -57
  139. package/examples/bootstrap.html +343 -332
  140. package/examples/css/baseTodo.css +141 -141
  141. package/examples/css/dbMonsterStyles.css +27 -27
  142. package/examples/css/indexTodo.css +374 -374
  143. package/examples/dbmonsterForOfReactive.html +40 -0
  144. package/examples/dbmonsterReact.html +19 -0
  145. package/examples/forOfBindingSimpleDebug.html +45 -0
  146. package/examples/form.html +20 -4
  147. package/examples/globalConfig.html +131 -0
  148. package/examples/js/afterRenderDemo.js +190 -0
  149. package/examples/js/appTodo.js +46 -46
  150. package/examples/js/attrBindingDemo.js +2 -2
  151. package/examples/js/dbMonApp.js +24 -26
  152. package/examples/js/dbMonAppReact.jsx +79 -0
  153. package/examples/js/dbMonAppReactive.js +28 -0
  154. package/examples/js/fiberDemo.js +4 -4
  155. package/examples/js/filtersDemo.js +8 -8
  156. package/examples/js/forOfDemo.js +7 -9
  157. package/examples/js/forOfDemoComplex.js +44 -17
  158. package/examples/js/form.js +44 -12
  159. package/examples/js/globalConfig.js +117 -0
  160. package/examples/js/ifBindingDemo.js +16 -16
  161. package/examples/js/reactiveDemo.js +119 -0
  162. package/examples/js/switchBindingDemo.js +8 -8
  163. package/examples/react-dbmonster/dist/bundle.js +43 -0
  164. package/examples/react-dbmonster/package-lock.json +537 -0
  165. package/examples/react-dbmonster/package.json +16 -0
  166. package/examples/react-dbmonster/src/index.jsx +80 -0
  167. package/examples/reactiveDemo.html +127 -0
  168. package/examples/refreshRateTest.html +75 -75
  169. package/index.html +841 -0
  170. package/package.json +31 -34
  171. package/rollup.config.js +79 -36
  172. package/src/{_escape.js → _escape.ts} +19 -17
  173. package/src/applyBinding.ts +179 -0
  174. package/src/{attrBinding.js → attrBinding.ts} +14 -13
  175. package/src/binder.ts +289 -0
  176. package/src/changeBinding.ts +93 -0
  177. package/src/{commentWrapper.js → commentWrapper.ts} +33 -30
  178. package/src/config.ts +107 -0
  179. package/src/createBindingOption.ts +91 -0
  180. package/src/createEventBinding.ts +88 -0
  181. package/src/{cssBinding.js → cssBinding.ts} +13 -11
  182. package/src/{domWalker.js → domWalker.ts} +44 -30
  183. package/src/{forOfBinding.js → forOfBinding.ts} +4 -3
  184. package/src/hoverBinding.ts +84 -0
  185. package/src/{ifBinding.js → ifBinding.ts} +14 -12
  186. package/src/index.ts +53 -0
  187. package/src/{modelBinding.js → modelBinding.ts} +11 -9
  188. package/src/postProcess.ts +22 -0
  189. package/src/{pubSub.js → pubSub.ts} +24 -15
  190. package/src/reactiveProxy.ts +285 -0
  191. package/src/{renderForOfBinding.js → renderForOfBinding.ts} +55 -33
  192. package/src/{renderIfBinding.js → renderIfBinding.ts} +45 -20
  193. package/src/renderIteration.ts +53 -0
  194. package/src/renderTemplate.ts +165 -0
  195. package/src/renderTemplatesBinding.ts +73 -0
  196. package/src/{showBinding.js → showBinding.ts} +4 -3
  197. package/src/{switchBinding.js → switchBinding.ts} +18 -15
  198. package/src/{textBinding.js → textBinding.ts} +5 -4
  199. package/src/types.ts +124 -0
  200. package/src/util.ts +810 -0
  201. package/test/css/reporter.css +9 -9
  202. package/test/fixtures/dataBindBootstrap.html +2 -2
  203. package/test/fixtures/formBindings.html +9 -1
  204. package/test/globals.d.ts +19 -0
  205. package/test/helpers/testHelper.js +46 -11
  206. package/test/mocks/featureAdsResult.json +65 -65
  207. package/test/mocks/searchResult.json +57 -57
  208. package/test/specs/{attrBinding.spec.js → attrBinding.spec.ts} +103 -106
  209. package/test/specs/{binder.spec.js → binder.spec.ts} +29 -27
  210. package/test/specs/blurBinding.spec.ts +60 -0
  211. package/test/specs/chainableUse.spec.ts +125 -0
  212. package/test/specs/clickBinding.spec.ts +194 -0
  213. package/test/specs/{cssBinding.spec.js → cssBinding.spec.ts} +72 -79
  214. package/test/specs/{dataBindBootstrap.spec.js → dataBindBootstrap.spec.ts} +332 -313
  215. package/test/specs/{filter.spec.js → filter.spec.ts} +75 -76
  216. package/test/specs/{forOfBinding.spec.js → forOfBinding.spec.ts} +208 -219
  217. package/test/specs/formBinding.spec.ts +272 -0
  218. package/test/specs/ifBinding.spec.ts +165 -0
  219. package/test/specs/{nestedComponent.spec.js → nestedComponent.spec.ts} +88 -88
  220. package/test/specs/reactiveProxy.spec.ts +465 -0
  221. package/test/specs/{showBinding.spec.js → showBinding.spec.ts} +148 -149
  222. package/test/specs/{switchBinding.spec.js → switchBinding.spec.ts} +172 -173
  223. package/test/specs/templateBinding.spec.ts +273 -0
  224. package/test/specs/{textBinding.spec.js → textBinding.spec.ts} +47 -48
  225. package/test/tsconfig.json +31 -0
  226. package/test-output.txt +200 -0
  227. package/test-reactive.html +224 -0
  228. package/tsconfig.json +28 -0
  229. package/vendors/lodash.custom.js +4577 -4577
  230. package/vendors/lodash.custom.min.js +45 -45
  231. package/vitest.config.js +27 -0
  232. package/.eslintrc.js +0 -1
  233. package/.grunt/grunt-contrib-jasmine/boot.js +0 -161
  234. package/.grunt/grunt-contrib-jasmine/dist/js/dataBind.js +0 -9
  235. package/.grunt/grunt-contrib-jasmine/grunt-template-jasmine-istanbul/reporter.js +0 -23
  236. package/.grunt/grunt-contrib-jasmine/jasmine-html.js +0 -853
  237. package/.grunt/grunt-contrib-jasmine/jasmine.css +0 -271
  238. package/.grunt/grunt-contrib-jasmine/jasmine.js +0 -9761
  239. package/.grunt/grunt-contrib-jasmine/jasmine_favicon.png +0 -0
  240. package/.grunt/grunt-contrib-jasmine/json2.js +0 -489
  241. package/.grunt/grunt-contrib-jasmine/reporter.js +0 -107
  242. package/coverage/coverage.json +0 -1
  243. package/coverage/lcov/lcov-report/base.css +0 -213
  244. package/coverage/lcov/lcov-report/index.html +0 -93
  245. package/coverage/lcov/lcov-report/js/dataBind.js.html +0 -6596
  246. package/coverage/lcov/lcov-report/js/index.html +0 -93
  247. package/coverage/lcov/lcov-report/prettify.css +0 -1
  248. package/coverage/lcov/lcov-report/prettify.js +0 -1
  249. package/coverage/lcov/lcov-report/sort-arrow-sprite.png +0 -0
  250. package/coverage/lcov/lcov-report/sorter.js +0 -158
  251. package/coverage/lcov/lcov.info +0 -1991
  252. package/eslintrc.json +0 -40
  253. package/examples/bootstrap/js/bootstrap.min.js +0 -6
  254. package/examples/bootstrap/js/popper.min.js +0 -5
  255. package/examples/bootstrap/js/searchSuggestion.js +0 -58
  256. package/examples/bootstrap/js/typeahead.jquery.js +0 -1538
  257. package/gruntfile.js +0 -92
  258. package/gulpfile.js +0 -32
  259. package/src/binder.js +0 -422
  260. package/src/changeBinding.js +0 -57
  261. package/src/config.js +0 -65
  262. package/src/createBindingOption.js +0 -66
  263. package/src/createEventBinding.js +0 -46
  264. package/src/eventSystem.js +0 -46
  265. package/src/hoverBinding.js +0 -57
  266. package/src/index.js +0 -26
  267. package/src/renderTemplate.js +0 -128
  268. package/src/util.js +0 -648
  269. package/test/specs/blurBinding.spec.js +0 -57
  270. package/test/specs/formBinding.spec.js +0 -292
  271. package/test/specs/ifBinding.spec.js +0 -169
  272. package/test/specs/templateBinding.spec.js +0 -117
  273. package/vendors/jasmine-jquery.js +0 -841
  274. package/vendors/jquery-3.2.1.min.js +0 -4
package/gruntfile.js DELETED
@@ -1,92 +0,0 @@
1
- module.exports = function(grunt) {
2
- 'use strict';
3
-
4
- // Project configuration.
5
- grunt.initConfig({
6
- pkg: grunt.file.readJSON('package.json'),
7
-
8
- // create a static server for jasmine / PhantomJS
9
- connect: {
10
- test: {
11
- options: {
12
- hostname: 'localhost',
13
- port: 8887,
14
- },
15
- },
16
- debug: {
17
- options: {
18
- hostname: 'localhost',
19
- port: 8889,
20
- keepalive: true,
21
- open: 'http://localhost:8889/_SpecRunner.html',
22
- },
23
- },
24
- },
25
-
26
- // this is just config for jasmine - not a grunt module
27
- jasmineTestSettings: {
28
- src: ['./dist/js/dataBind.min.js'],
29
- host: 'http://localhost:8887',
30
- specs: './test/specs/*.spec.js',
31
- styles: './test/css/reporter.css',
32
- vendor: ['./vendors/jquery-3.2.1.min.js', './vendors/jasmine-jquery.js'],
33
- helpers: ['./test/helpers/testHelper.js'],
34
- },
35
-
36
- jasmine: {
37
- test: {
38
- src: '<%= jasmineTestSettings.src %>',
39
- options: {
40
- 'styles': '<%= jasmineTestSettings.styles %>',
41
- 'host': '<%= jasmineTestSettings.host %>',
42
- 'specs': '<%= jasmineTestSettings.specs %>',
43
- 'vendor': '<%= jasmineTestSettings.vendor %>',
44
- 'helpers': '<%= jasmineTestSettings.helpers %>',
45
- 'keepRunner': true,
46
- '--web-security': 'no',
47
- '--local-to-remote-url-access': true,
48
- '--ignore-ssl-errors': true,
49
- },
50
- },
51
- // code test coverage
52
- coverage: {
53
- src: '<%= jasmineTestSettings.src %>',
54
- options: {
55
- 'host': '<%= jasmineTestSettings.host %>',
56
- 'specs': '<%= jasmineTestSettings.specs %>',
57
- 'vendor': '<%= jasmineTestSettings.vendor %>',
58
- 'helpers': '<%= jasmineTestSettings.helpers %>',
59
- 'keepRunner': true,
60
- '--web-security': 'no',
61
- '--local-to-remote-url-access': true,
62
- '--ignore-ssl-errors': true,
63
-
64
- 'template': require('grunt-template-jasmine-istanbul'),
65
- 'templateOptions': {
66
- coverage: './coverage/coverage.json',
67
- report: [
68
- {
69
- type: 'text-summary',
70
- },
71
- {
72
- type: 'lcov',
73
- options: {
74
- dir: './coverage/lcov',
75
- },
76
- },
77
- ],
78
- },
79
- },
80
- },
81
- },
82
- });
83
-
84
- grunt.loadNpmTasks('grunt-contrib-connect');
85
- grunt.loadNpmTasks('grunt-contrib-jasmine');
86
- grunt.loadNpmTasks('grunt-open');
87
-
88
- // jasmine test via PhantomJS with 'connect' local server - for local dev and debug
89
- grunt.registerTask('test', ['connect:test', 'jasmine:test', 'connect:debug']);
90
- // jasmine test with coverage - this if for build server
91
- grunt.registerTask('test:coverage', ['connect:test', 'jasmine:coverage']);
92
- };
package/gulpfile.js DELETED
@@ -1,32 +0,0 @@
1
- const gulp = require('gulp');
2
- const sourcemaps = require('gulp-sourcemaps');
3
- const uglify = require('gulp-uglify-es').default;
4
- const rename = require('gulp-rename');
5
- const replace = require('gulp-replace');
6
-
7
- const pkg = require('./package.json');
8
- const verionToken = '@version@';
9
-
10
- const distJsPath = './dist/js/';
11
- const bundledFile = 'dataBind.js';
12
-
13
- // set release version to source
14
- gulp.task('versioning', function(cb) {
15
- return gulp
16
- .src(`${distJsPath}${bundledFile}`)
17
- .pipe(replace(verionToken, `${pkg.version}`))
18
- .pipe(gulp.dest(distJsPath));
19
- });
20
-
21
- // uglify
22
- gulp.task('compress', function(cb) {
23
- return gulp
24
- .src(`${distJsPath}${bundledFile}`)
25
- .pipe(rename({extname: '.min.js'}))
26
- .pipe(sourcemaps.init())
27
- .pipe(uglify())
28
- .pipe(sourcemaps.write(''))
29
- .pipe(gulp.dest(distJsPath));
30
- });
31
-
32
- gulp.task('default', gulp.series('versioning', 'compress'));
package/src/binder.js DELETED
@@ -1,422 +0,0 @@
1
- import * as config from './config';
2
- import {
3
- debounceRaf,
4
- each,
5
- throwErrorMessage,
6
- } from './util';
7
- import renderTemplate from './renderTemplate';
8
- import hoverBinding from './hoverBinding';
9
- import changeBinding from './changeBinding';
10
- import modelBinding from './modelBinding';
11
- import textBinding from './textBinding';
12
- import showBinding from './showBinding';
13
- import cssBinding from './cssBinding';
14
- import attrBinding from './attrBinding';
15
- import forOfBinding from './forOfBinding';
16
- import ifBinding from './ifBinding';
17
- import switchBinding from './switchBinding';
18
- import createBindingCache from './domWalker';
19
- import createEventBinding from './createEventBinding';
20
- import createBindingOption from './createBindingOption';
21
- import * as pubSub from './pubSub';
22
-
23
- let compIdIndex = 0;
24
-
25
- class Binder {
26
- constructor($rootElement, viewModel, bindingAttrs) {
27
- if (!$rootElement || $rootElement.nodeType !== 1 || viewModel === null || typeof viewModel !== 'object') {
28
- throw new TypeError('$rootElement or viewModel is invalid');
29
- }
30
-
31
- this.initRendered = false;
32
-
33
- this.compId = compIdIndex += 1;
34
-
35
- this.$rootElement = $rootElement;
36
-
37
- this.viewModel = viewModel;
38
-
39
- this.bindingAttrs = bindingAttrs;
40
-
41
- this.render = debounceRaf(this.render, this);
42
-
43
- this.isServerRendered = this.$rootElement.getAttribute(config.serverRenderedAttr) !== null;
44
-
45
- // inject instance into viewModel
46
- this.viewModel.APP = this;
47
-
48
- // add $root pointer to viewModel so binding can be refer as $root.something
49
- this.viewModel.$root = this.viewModel;
50
-
51
- // 1st step
52
- // parsView walk the DOM and create binding cache that holds each element's binding details
53
- // this binding cache is like AST for render and update
54
- this.parseView();
55
-
56
- // for jquery user set viewModel referece to $rootElement for easy debug
57
- // otherwise use Expando to attach viewModel to $rootElement
58
- this.$rootElement[config.bindingDataReference.rootDataKey] = this.viewModel;
59
-
60
- return this;
61
- }
62
-
63
- /**
64
- * parseView
65
- * @description
66
- * @return {this}
67
- * traver from $rootElement to find each data-bind-* element
68
- * then apply data binding
69
- */
70
- parseView() {
71
- this.elementCache = createBindingCache({
72
- rootNode: this.$rootElement,
73
- bindingAttrs: this.bindingAttrs,
74
- });
75
-
76
- // updateElementCache if server rendered on init
77
- if (this.isServerRendered && !this.initRendered) {
78
- this.updateElementCache({
79
- templateCache: true,
80
- });
81
- }
82
- return this;
83
- }
84
-
85
- /**
86
- * updateElementCache
87
- * @param {object} opt
88
- * @description call createBindingCache to parse view and generate bindingCache
89
- */
90
- updateElementCache(opt = {}) {
91
- const elementCache = opt.elementCache || this.elementCache;
92
-
93
- if (opt.allCache) {
94
- // walk dom from root element to regenerate elementCache
95
- this.elementCache = createBindingCache({
96
- rootNode: this.$rootElement,
97
- bindingAttrs: this.bindingAttrs,
98
- });
99
- }
100
- // walk from first rendered template node to create/update child bindingCache
101
- if (opt.allCache || opt.templateCache) {
102
- if (elementCache[this.bindingAttrs.tmp] && elementCache[this.bindingAttrs.tmp].length) {
103
- elementCache[this.bindingAttrs.tmp].forEach((cache) => {
104
- // set skipCheck as skipForOfParseFn whenever an node has
105
- // both template and forOf bindings
106
- // then the template bindingCache should be an empty object
107
- let skipForOfParseFn = null;
108
- if (cache.el.hasAttribute(this.bindingAttrs.forOf)) {
109
- skipForOfParseFn = () => {
110
- return true;
111
- };
112
- }
113
- cache.bindingCache = createBindingCache({
114
- rootNode: cache.el,
115
- bindingAttrs: this.bindingAttrs,
116
- skipCheck: skipForOfParseFn,
117
- isRenderedTemplate: opt.isRenderedTemplates,
118
- });
119
- });
120
- }
121
- }
122
- }
123
-
124
- render(opt = {}) {
125
- let updateOption = {};
126
-
127
- if (!this.initRendered) {
128
- // only update eventsBinding if server rendered
129
- if (this.isServerRendered) {
130
- this.$rootElement.removeAttribute(config.serverRenderedAttr);
131
- updateOption = createBindingOption(config.bindingUpdateConditions.serverRendered, opt);
132
- } else {
133
- updateOption = createBindingOption(config.bindingUpdateConditions.init, opt);
134
- }
135
- } else {
136
- // when called again only update visualBinding options
137
- updateOption = createBindingOption('', opt);
138
- }
139
-
140
- // create postProcessQueue before start rendering
141
- this.postProcessQueue = [];
142
-
143
- const renderBindingOption = {
144
- ctx: this,
145
- elementCache: this.elementCache,
146
- updateOption: updateOption,
147
- bindingAttrs: this.bindingAttrs,
148
- viewModel: this.viewModel,
149
- };
150
-
151
- // always render template binding first
152
- // render and apply binding to template(s)
153
- // this is an share function therefore passing 'this' context
154
- renderTemplatesBinding(renderBindingOption);
155
-
156
- // apply bindings to rest of the DOM
157
- Binder.applyBinding(renderBindingOption);
158
-
159
- // trigger postProcess
160
- Binder.postProcess(this.postProcessQueue);
161
- // clear postProcessQueue
162
- this.postProcessQueue.length = 0;
163
- delete this.postProcessQueue;
164
-
165
- this.initRendered = true;
166
- }
167
-
168
- static applyBinding({ctx, elementCache, updateOption, bindingAttrs, viewModel}) {
169
- if (!elementCache || !updateOption) {
170
- return;
171
- }
172
-
173
- // the follow binding should be in order for better efficiency
174
-
175
- // apply forOf Binding
176
- if (updateOption.forOfBinding && elementCache[bindingAttrs.forOf] && elementCache[bindingAttrs.forOf].length) {
177
- elementCache[bindingAttrs.forOf].forEach((cache) => {
178
- forOfBinding(cache, viewModel, bindingAttrs, updateOption.forceRender);
179
- });
180
- }
181
-
182
- // apply attr Binding
183
- if (updateOption.attrBinding && elementCache[bindingAttrs.attr] && elementCache[bindingAttrs.attr].length) {
184
- elementCache[bindingAttrs.attr].forEach((cache) => {
185
- attrBinding(cache, viewModel, bindingAttrs, updateOption.forceRender);
186
- });
187
- }
188
-
189
- // apply if Binding
190
- if (updateOption.ifBinding && elementCache[bindingAttrs.if] && elementCache[bindingAttrs.if].length) {
191
- elementCache[bindingAttrs.if].forEach((cache) => {
192
- ifBinding(cache, viewModel, bindingAttrs, updateOption.forceRender);
193
- });
194
- }
195
-
196
- // apply show Binding
197
- if (updateOption.showBinding && elementCache[bindingAttrs.show] && elementCache[bindingAttrs.show].length) {
198
- elementCache[bindingAttrs.show].forEach((cache) => {
199
- showBinding(cache, viewModel, bindingAttrs, updateOption.forceRender);
200
- });
201
- }
202
-
203
- // apply switch Binding
204
- if (updateOption.switchBinding && elementCache[bindingAttrs.switch] && elementCache[bindingAttrs.switch].length) {
205
- elementCache[bindingAttrs.switch].forEach((cache) => {
206
- switchBinding(cache, viewModel, bindingAttrs, updateOption.forceRender);
207
- });
208
- }
209
-
210
- // apply text binding
211
- if (updateOption.textBinding && elementCache[bindingAttrs.text] && elementCache[bindingAttrs.text].length) {
212
- elementCache[bindingAttrs.text].forEach((cache) => {
213
- textBinding(cache, viewModel, bindingAttrs, updateOption.forceRender);
214
- });
215
- }
216
-
217
- // apply cssBinding
218
- if (updateOption.cssBinding && elementCache[bindingAttrs.css] && elementCache[bindingAttrs.css].length) {
219
- elementCache[bindingAttrs.css].forEach((cache) => {
220
- cssBinding(cache, viewModel, bindingAttrs, updateOption.forceRender);
221
- });
222
- }
223
-
224
- // apply model binding
225
- if (updateOption.modelBinding && elementCache[bindingAttrs.model] && elementCache[bindingAttrs.model].length) {
226
- elementCache[bindingAttrs.model].forEach((cache) => {
227
- modelBinding(cache, viewModel, bindingAttrs, updateOption.forceRender);
228
- });
229
- }
230
-
231
- // apply change binding
232
- if (updateOption.changeBinding && elementCache[bindingAttrs.change] && elementCache[bindingAttrs.change].length) {
233
- elementCache[bindingAttrs.change].forEach((cache) => {
234
- changeBinding(cache, viewModel, bindingAttrs, updateOption.forceRender);
235
- });
236
- }
237
-
238
- // apply submit binding
239
- if (updateOption.submitBinding && elementCache[bindingAttrs.submit] && elementCache[bindingAttrs.submit].length) {
240
- elementCache[bindingAttrs.submit].forEach((cache) => {
241
- createEventBinding({
242
- cache,
243
- forceRender: updateOption.forceRender,
244
- type: 'submit',
245
- viewModel,
246
- });
247
- });
248
- }
249
-
250
- // apply click binding
251
- if (updateOption.clickBinding && elementCache[bindingAttrs.click] && elementCache[bindingAttrs.click].length) {
252
- elementCache[bindingAttrs.click].forEach((cache) => {
253
- createEventBinding({
254
- cache,
255
- forceRender: updateOption.forceRender,
256
- type: 'click',
257
- viewModel,
258
- });
259
- });
260
- }
261
-
262
- // apply double click binding
263
- if (updateOption.dblclickBinding && elementCache[bindingAttrs.dblclick] && elementCache[bindingAttrs.dblclick].length) {
264
- elementCache[bindingAttrs.dblclick].forEach((cache) => {
265
- createEventBinding({
266
- cache,
267
- forceRender: updateOption.forceRender,
268
- type: 'dblclick',
269
- viewModel,
270
- });
271
- });
272
- }
273
-
274
- // apply blur binding
275
- if (updateOption.blurBinding && elementCache[bindingAttrs.blur] && elementCache[bindingAttrs.blur].length) {
276
- elementCache[bindingAttrs.blur].forEach((cache) => {
277
- createEventBinding({
278
- cache,
279
- forceRender: updateOption.forceRender,
280
- type: 'blur',
281
- viewModel,
282
- });
283
- });
284
- }
285
-
286
- // apply focus binding
287
- if (updateOption.focusBinding && elementCache[bindingAttrs.focus] && elementCache[bindingAttrs.focus].length) {
288
- elementCache[bindingAttrs.focus].forEach((cache) => {
289
- createEventBinding({
290
- cache,
291
- forceRender: updateOption.forceRender,
292
- type: 'focus',
293
- viewModel,
294
- });
295
- });
296
- }
297
-
298
- // apply hover binding
299
- if (updateOption.hoverBinding && elementCache[bindingAttrs.hover] && elementCache[bindingAttrs.hover].length) {
300
- elementCache[bindingAttrs.hover].forEach((cache) => {
301
- hoverBinding(cache, viewModel, bindingAttrs, updateOption.forceRender);
302
- });
303
- }
304
- }
305
-
306
- static postProcess(tasks) {
307
- if (!tasks || !tasks.length) {
308
- return;
309
- }
310
- each(tasks, (index, task) => {
311
- if (typeof task === 'function') {
312
- try {
313
- task();
314
- } catch (err) {
315
- throwErrorMessage(err, 'Error postProcess: ' + String(task));
316
- }
317
- }
318
- });
319
- }
320
-
321
- subscribe(eventName = '', fn) {
322
- pubSub.subscribeEvent(this, eventName, fn);
323
- return this;
324
- }
325
-
326
- subscribeOnce(eventName = '', fn) {
327
- pubSub.subscribeEventOnce(this, eventName, fn);
328
- return this;
329
- }
330
-
331
- unsubscribe(eventName = '') {
332
- pubSub.unsubscribeEvent(this.compId, eventName);
333
- return this;
334
- }
335
-
336
- unsubscribeAll() {
337
- pubSub.unsubscribeAllEvent(this.compId);
338
- return this;
339
- }
340
-
341
- publish(eventName = '', ...args) {
342
- pubSub.publishEvent(eventName, ...args);
343
- return this;
344
- }
345
- }
346
-
347
- const renderTemplatesBinding = ({ctx, elementCache, updateOption, bindingAttrs, viewModel}) => {
348
- if (!elementCache || !bindingAttrs) {
349
- return false;
350
- }
351
- // render and apply binding to template(s) and forOf DOM
352
- if (elementCache[bindingAttrs.tmp] && elementCache[bindingAttrs.tmp].length) {
353
- // when re-render call with {templateBinding: true}
354
- // template and nested templates
355
- if (updateOption.templateBinding) {
356
- // overwrite updateOption with 'init' bindingUpdateConditions
357
- updateOption = createBindingOption(config.bindingUpdateConditions.init);
358
-
359
- elementCache[bindingAttrs.tmp].forEach(($element) => {
360
- renderTemplate($element, viewModel, bindingAttrs, elementCache);
361
- });
362
- // update cache after all template(s) rendered
363
- ctx.updateElementCache({
364
- templateCache: true,
365
- elementCache: elementCache,
366
- isRenderedTemplates: true,
367
- });
368
- }
369
- // enforce render even element is not in DOM tree
370
- updateOption.forceRender = true;
371
-
372
- // apply bindings to rendered templates element
373
- elementCache[bindingAttrs.tmp].forEach((cache) => {
374
- Binder.applyBinding({
375
- elementCache: cache.bindingCache,
376
- updateOption: updateOption,
377
- bindingAttrs: bindingAttrs,
378
- viewModel: viewModel,
379
- });
380
- });
381
- }
382
- return true;
383
- };
384
-
385
-
386
- /**
387
- * renderIteration
388
- * @param {object} opt
389
- * @description
390
- * render element's binding by supplied elementCache
391
- * This function is desidned for FoOf, If, switch bindings
392
- */
393
- const renderIteration = ({elementCache, iterationVm, bindingAttrs, isRegenerate}) => {
394
- const bindingUpdateOption = isRegenerate ? createBindingOption(config.bindingUpdateConditions.init) : createBindingOption();
395
-
396
- // enforce render even element is not in DOM tree
397
- bindingUpdateOption.forceRender = true;
398
-
399
- // render and apply binding to template(s)
400
- // this is an share function therefore passing current APP 'this' context
401
- // viewModel is a dynamic generated iterationVm
402
- renderTemplatesBinding({
403
- ctx: iterationVm.$root ? iterationVm.$root.APP : iterationVm.APP,
404
- elementCache: elementCache,
405
- updateOption: bindingUpdateOption,
406
- bindingAttrs: bindingAttrs,
407
- viewModel: iterationVm,
408
- });
409
-
410
- Binder.applyBinding({
411
- elementCache: elementCache,
412
- updateOption: bindingUpdateOption,
413
- bindingAttrs: bindingAttrs,
414
- viewModel: iterationVm,
415
- });
416
- };
417
-
418
- export {
419
- Binder,
420
- renderTemplatesBinding,
421
- renderIteration,
422
- };
@@ -1,57 +0,0 @@
1
- /* eslint-disable no-invalid-this */
2
- import {
3
- getViewModelValue,
4
- setViewModelValue,
5
- resolveViewModelContext,
6
- resolveParamList,
7
- } from './util';
8
- import _escape from './_escape';
9
-
10
- /**
11
- * changeBinding
12
- * @description input element on change event binding. DOM -> viewModel update
13
- * @param {object} cache
14
- * @param {object} viewModel
15
- * @param {object} bindingAttrs
16
- * @param {boolean} forceRender
17
- */
18
- const changeBinding = (cache, viewModel, bindingAttrs, forceRender) => {
19
- const handlerName = cache.dataKey;
20
- let paramList = cache.parameters;
21
- const modelDataKey = cache.el.getAttribute(bindingAttrs.model);
22
- let newValue = '';
23
- let oldValue = '';
24
- let viewModelContext;
25
- const APP = viewModel.APP || viewModel.$root.APP;
26
-
27
- if (!handlerName || (!forceRender && !APP.$rootElement.contains(cache.el))) {
28
- return;
29
- }
30
-
31
- const handlerFn = getViewModelValue(viewModel, handlerName);
32
-
33
- if (typeof handlerFn === 'function') {
34
- viewModelContext = resolveViewModelContext(viewModel, handlerName);
35
- paramList = paramList ? resolveParamList(viewModel, paramList) : [];
36
-
37
- function changeHandler(e) {
38
- const $this = this;
39
- const isCheckbox = $this.type === 'checkbox';
40
- newValue = isCheckbox ? $this.checked : _escape($this.value);
41
- // set data to viewModel
42
- if (modelDataKey) {
43
- oldValue = getViewModelValue(viewModel, modelDataKey);
44
- setViewModelValue(viewModel, modelDataKey, newValue);
45
- }
46
- const args = [e, e.currentTarget, newValue, oldValue].concat(paramList);
47
- handlerFn.apply(viewModelContext, args);
48
- oldValue = newValue;
49
- }
50
-
51
- // assing on change event
52
- cache.el.removeEventListener('change', changeHandler, false);
53
- cache.el.addEventListener('change', changeHandler, false);
54
- }
55
- };
56
-
57
- export default changeBinding;
package/src/config.js DELETED
@@ -1,65 +0,0 @@
1
- const bindingAttrs = {
2
- comp: 'data-bind-comp',
3
- tmp: 'data-bind-tmp',
4
- text: 'data-bind-text',
5
- click: 'data-bind-click',
6
- dblclick: 'data-bind-dblclick',
7
- blur: 'data-bind-blur',
8
- focus: 'data-bind-focus',
9
- hover: 'data-bind-hover',
10
- change: 'data-bind-change',
11
- submit: 'data-bind-submit',
12
- model: 'data-bind-model',
13
- show: 'data-bind-show',
14
- css: 'data-bind-css',
15
- attr: 'data-bind-attr',
16
- forOf: 'data-bind-for',
17
- if: 'data-bind-if',
18
- switch: 'data-bind-switch',
19
- case: 'data-bind-case',
20
- default: 'data-bind-default',
21
- };
22
- const serverRenderedAttr = 'data-server-rendered';
23
- const dataIndexAttr = 'data-index';
24
- const commentPrefix = {
25
- forOf: 'data-forOf_',
26
- if: 'data-if_',
27
- case: 'data-case_',
28
- default: 'data-default_',
29
- };
30
- const commentSuffix = '_end';
31
-
32
- const bindingDataReference = {
33
- rootDataKey: '$root',
34
- currentData: '$data',
35
- currentIndex: '$index',
36
- mouseEnterHandlerName: 'in',
37
- mouseLeaveHandlerName: 'out',
38
- };
39
-
40
- const bindingUpdateConditions = {
41
- serverRendered: 'SERVER-RENDERED',
42
- init: 'INIT',
43
- };
44
-
45
- // maximum string length before running regex
46
- const maxDatakeyLength = 250;
47
-
48
- const constants = {
49
- filters: {
50
- ONCE: 'once',
51
- },
52
- PARENT_REF: '_parent',
53
- };
54
-
55
- export {
56
- bindingAttrs,
57
- dataIndexAttr,
58
- serverRenderedAttr,
59
- commentPrefix,
60
- commentSuffix,
61
- bindingUpdateConditions,
62
- bindingDataReference,
63
- maxDatakeyLength,
64
- constants,
65
- };