@webqit/oohtml 2.1.8 → 2.1.9

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.
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "keywords": [
6
6
  "namespaced-HTML",
7
7
  "html-modules",
8
- "ui-state",
8
+ "ui-bindings",
9
9
  "html-imports",
10
10
  "reflex",
11
11
  "subscript",
@@ -14,7 +14,7 @@
14
14
  "wicg-proposal"
15
15
  ],
16
16
  "homepage": "https://webqit.io/tooling/oohtml",
17
- "version": "2.1.8",
17
+ "version": "2.1.9",
18
18
  "license": "MIT",
19
19
  "repository": {
20
20
  "type": "git",
@@ -32,14 +32,14 @@
32
32
  "scripts": {
33
33
  "test": "mocha --extension .test.js --exit",
34
34
  "test:coverage": "c8 --reporter=text-lcov npm run test | coveralls",
35
- "build": "esbuild main=src/targets.browser.js html-modules=src/html-modules/targets.browser.js html-imports=src/html-imports/targets.browser.js namespaced-html=src/namespaced-html/targets.browser.js scoped-js=src/scoped-js/targets.browser.js state-api=src/state-api/targets.browser.js context-api=src/context-api/targets.browser.js --bundle --minify --sourcemap --outdir=dist",
35
+ "build": "esbuild main=src/targets.browser.js html-modules=src/html-modules/targets.browser.js html-imports=src/html-imports/targets.browser.js namespaced-html=src/namespaced-html/targets.browser.js scoped-js=src/scoped-js/targets.browser.js bindings-api=src/bindings-api/targets.browser.js context-api=src/context-api/targets.browser.js --bundle --minify --sourcemap --outdir=dist",
36
36
  "preversion": "npm run test && npm run build && git add -A dist",
37
37
  "postversion": "npm publish",
38
38
  "postpublish": "git push && git push --tags"
39
39
  },
40
40
  "dependencies": {
41
41
  "@webqit/dom": "^2.0.2",
42
- "@webqit/observer": "^2.0.1",
42
+ "@webqit/observer": "^2.0.2",
43
43
  "@webqit/subscript": "^2.1.36",
44
44
  "@webqit/util": "^0.8.9"
45
45
  },
@@ -0,0 +1,85 @@
1
+
2
+ /**
3
+ * @imports
4
+ */
5
+ import wqDom from '@webqit/dom';
6
+ import Observer from '@webqit/observer';
7
+ import { _ } from '../util.js';
8
+
9
+ /**
10
+ * @init
11
+ *
12
+ * @param Object $params
13
+ */
14
+ export default function init( $params = {} ) {
15
+ const window = this, dom = wqDom.call( window );
16
+ if ( !window.wq ) { window.wq = {}; }
17
+ window.wq.Observer = Observer;
18
+ const params = dom.meta( 'oohtml' ).copyWithDefaults( $params, {
19
+ api: { bind: 'bind', bindings: 'bindings', },
20
+ } );
21
+ exposeAPIs.call( this, params );
22
+ }
23
+
24
+ export { Observer }
25
+
26
+ /**
27
+ * @Exports
28
+ *
29
+ * The internal bindings object
30
+ * within elements and the document object.
31
+ */
32
+ function getBindingsObject( node ) {
33
+ if ( !_( node ).has( 'bindings' ) ) {
34
+ const bindingsObj = Object.create( null );
35
+ _( node ).set( 'bindings', bindingsObj );
36
+ }
37
+ return _( node ).get( 'bindings' );
38
+ }
39
+
40
+ /**
41
+ * Exposes Bindings with native APIs.
42
+ *
43
+ * @param Object params
44
+ *
45
+ * @return Void
46
+ */
47
+ function exposeAPIs( params ) {
48
+ const window = this;
49
+ // Assertions
50
+ if ( params.api.bind in window.document ) { throw new Error( `document already has a "${ params.api.bind }" property!` ); }
51
+ if ( params.api.bindings in window.document ) { throw new Error( `document already has a "${ params.api.bindings }" property!` ); }
52
+ if ( params.api.bind in window.Element.prototype ) { throw new Error( `The "Element" class already has a "${ params.api.bind }" property!` ); }
53
+ if ( params.api.bindings in window.Element.prototype ) { throw new Error( `The "Element" class already has a "${ params.api.bindings }" property!` ); }
54
+ // Definitions
55
+ Object.defineProperty( window.document, params.api.bind, { value: function( bindings, params = {} ) {
56
+ return applyBindings( window.document, bindings, params );
57
+ } });
58
+ Object.defineProperty( window.document, params.api.bindings, { get: function() {
59
+ return Observer.proxy( getBindingsObject( window.document ) );
60
+ } });
61
+ Object.defineProperty( window.Element.prototype, params.api.bind, { value: function( bindings, params = {} ) {
62
+ return applyBindings( this, bindings, params );
63
+ } });
64
+ Object.defineProperty( window.Element.prototype, params.api.bindings, { get: function() {
65
+ return Observer.proxy( getBindingsObject( this ) );
66
+ } } );
67
+ }
68
+
69
+ /**
70
+ * Exposes Bindings with native APIs.
71
+ *
72
+ * @param document|Element target
73
+ * @param Object bindings
74
+ * @param Object params
75
+ *
76
+ * @return Void
77
+ */
78
+ function applyBindings( target, bindings, params ) {
79
+ const bindingsObj = getBindingsObject( target );
80
+ const exitingKeys = Observer.ownKeys( bindingsObj, params ).filter( key => !( key in bindings ) );
81
+ return Observer.batch( bindingsObj, () => {
82
+ if ( exitingKeys.length ) { Observer.deleteProperty( bindingsObj, exitingKeys, params ); }
83
+ Observer.set( bindingsObj, bindings, params );
84
+ } );
85
+ }
package/src/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * @imports
4
4
  */
5
5
  import Observer from '@webqit/observer';
6
- import StateAPI from './state-api/index.js';
6
+ import BindingsAPI from './bindings-api/index.js';
7
7
  import ContextAPI from './context-api/index.js';
8
8
  import HTMLModules from './html-modules/index.js';
9
9
  import HTMLImports from './html-imports/index.js';
@@ -18,7 +18,7 @@ export default function init( configs = {} ) {
18
18
  if ( this.wq.oohtml ) return;
19
19
  this.wq.oohtml = {};
20
20
  // --------------
21
- StateAPI.call(this, ( configs.StateAPI || {} ) );
21
+ BindingsAPI.call(this, ( configs.BindingsAPI || {} ) );
22
22
  ContextAPI.call( this, ( configs.ContextAPI || {} ) );
23
23
  HTMLModules.call( this, ( configs.HTMLModules || {} ) );
24
24
  HTMLImports.call( this, ( configs.HTMLImports || {} ) );
@@ -0,0 +1,42 @@
1
+
2
+ /**
3
+ * @imports
4
+ */
5
+ import { expect } from 'chai';
6
+ import { createDocument } from './index.js';
7
+ import Observer from '@webqit/observer';
8
+
9
+ describe(`Bindings API`, function() {
10
+
11
+ describe( `Basic...`, function() {
12
+
13
+ const head = ``;
14
+ const body = ``;
15
+ const { document } = createDocument( head, body );
16
+
17
+ it ( `The document object and elements should expose a "bindings" property each...`, async function() {
18
+ expect( document ).to.have.property( 'bindings' );
19
+ expect( document.body ).to.have.property( 'bindings' );
20
+ } );
21
+
22
+ it ( `Bindings objects should be observable...`, async function() {
23
+ let idReceived = null;
24
+ Observer.observe( document.bindings, records => {
25
+ idReceived = records[ 0 ].key;
26
+ } );
27
+ document.bindings.someKey = 'someValue'; // new
28
+ expect( idReceived ).to.eq( 'someKey' );
29
+ // -------------
30
+ let changes = [];
31
+ Observer.observe( document.bindings, records => {
32
+ changes.push( ...records );
33
+ } );
34
+ document.bindings.someKey2 = 'someValue2'; // new
35
+ document.bind( { someKey: 'someValue'/* update */, someKey3: 'someValue3'/* new */ } );
36
+ expect( changes ).to.an( 'array' ).with.length( 4 ); // (1) sets: someKey2, (2) deletes: someKey2, (3) updates: someKey, (4) sets: someKey3;
37
+ } );
38
+
39
+ } );
40
+
41
+ } );
42
+
@@ -1,59 +0,0 @@
1
-
2
- /**
3
- * @imports
4
- */
5
- import wqDom from '@webqit/dom';
6
- import Observer from '@webqit/observer';
7
- import { _ } from '../util.js';
8
-
9
- /**
10
- * @init
11
- *
12
- * @param Object $params
13
- */
14
- export default function init( $params = {} ) {
15
- const window = this, dom = wqDom.call( window );
16
- if ( !window.wq ) { window.wq = {}; }
17
- window.wq.Observer = Observer;
18
- const params = dom.meta( 'oohtml' ).copyWithDefaults( $params, {
19
- api: { state: 'state', },
20
- } );
21
- exposeAPIs.call( this, params );
22
- }
23
-
24
- export { Observer }
25
-
26
- /**
27
- * @Exports
28
- *
29
- * The internal state object
30
- * within elements and the document object.
31
- */
32
- function getStateObject( node ) {
33
- if ( !_( node ).has( 'state' ) ) {
34
- const stateObj = Object.create( null );
35
- _( node ).set( 'state', stateObj );
36
- }
37
- return _( node ).get( 'state' );
38
- }
39
-
40
- /**
41
- * Exposes State with native APIs.
42
- *
43
- * @param Object params
44
- *
45
- * @return Void
46
- */
47
- function exposeAPIs( params ) {
48
- const window = this;
49
- // Assertions
50
- if ( params.api.state in window.document ) { throw new Error( `document already has a "${ params.api.state }" property!` ); }
51
- if ( params.api.state in window.Element.prototype ) { throw new Error( `The "Element" class already has a "${ params.api.state }" property!` ); }
52
- // Definitions
53
- Object.defineProperty( window.document, params.api.state, { get: function() {
54
- return Observer.proxy( getStateObject( window.document ) );
55
- } });
56
- Object.defineProperty( window.Element.prototype, params.api.state, { get: function() {
57
- return Observer.proxy( getStateObject( this ) );
58
- } } );
59
- }
@@ -1,34 +0,0 @@
1
-
2
- /**
3
- * @imports
4
- */
5
- import { expect } from 'chai';
6
- import { createDocument } from './index.js';
7
- import Observer from '@webqit/observer';
8
-
9
- describe(`State API`, function() {
10
-
11
- describe( `Basic...`, function() {
12
-
13
- const head = ``;
14
- const body = ``;
15
- const { document } = createDocument( head, body );
16
-
17
- it ( `The document object and elements should expose a "state" property each...`, async function() {
18
- expect( document ).to.have.property( 'state' );
19
- expect( document.body ).to.have.property( 'state' );
20
- } );
21
-
22
- it ( `State objects should be observable...`, async function() {
23
- let idReceived = null;
24
- Observer.observe( document.state, records => {
25
- idReceived = records[ 0 ].key;
26
- } );
27
- document.state.someKey = 'someValue';
28
- expect( idReceived ).to.eq( 'someKey' );
29
- } );
30
-
31
- } );
32
-
33
- } );
34
-