@pageboard/html 0.10.24 → 0.10.25

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.
@@ -16,11 +16,9 @@ exports.consent_form = {
16
16
  id: "content",
17
17
  nodes: "block+"
18
18
  },
19
- upgrade: {
20
- 'content.': 'content.content'
21
- },
22
19
  html: `<form is="element-consent" class="ui form" data-transient="[transient]">
23
- <x[transient|?:template:div|fill] block-content="content"></x[transient|?:template:div|fill]>
20
+ <template block-content="content"></template>
21
+ <div class="view"></div>
24
22
  </form>`,
25
23
  scripts: ['../ui/storage.js', '../ui/consent.js'],
26
24
  stylesheets: ['../ui/consent.css']
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pageboard/html",
3
- "version": "0.10.24",
3
+ "version": "0.10.25",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "repository": {
package/ui/consent.css CHANGED
@@ -14,7 +14,18 @@ footer [block-type="consent_form"][data-transient="true"] {
14
14
  display:none;
15
15
  }
16
16
 
17
- [contenteditable] [block-type="consent_form"][data-transient="true"],
18
- [block-type="consent_form"][data-transient="true"] > template {
17
+ [block-type="consent_form"] > [block-content="content"] {
18
+ display:none;
19
+ }
20
+ [block-type="consent_form"] > .view {
21
+ position:relative;
22
+ }
23
+ [contenteditable] [block-type="consent_form"] > [block-content="content"] {
19
24
  display:block;
25
+ min-height:1em;
26
+ }
27
+
28
+ [contenteditable] [block-focused="last"] > .form[block-type="consent_form"][data-transient="true"],
29
+ .form[block-type="consent_form"][data-transient="true"][block-focused] {
30
+ display: block;
20
31
  }
package/ui/consent.js CHANGED
@@ -7,47 +7,47 @@ class HTMLCustomConsentElement extends HTMLFormElement {
7
7
  dataTransient: false
8
8
  };
9
9
 
10
- static ask() {
11
- this.waiting = false;
10
+ static explicit;
11
+
12
+ static ask(consent) {
12
13
  let tacit = true;
13
- for (const node of document.querySelectorAll('[block-type="consent_form"]')) {
14
+ const forms = document.querySelectorAll('[block-type="consent_form"]');
15
+ this.explicit = forms.length > 0;
16
+ for (const node of forms) {
14
17
  node.classList.add('visible');
15
- tacit = false;
18
+ tacit = consent && !node.querySelector(`[name="${consent}"]`) || false;
16
19
  }
17
- return !tacit;
20
+ return tacit ? "yes" : null;
18
21
  }
19
22
  setup(state) {
20
23
  if (this.isContentEditable) return;
21
- if (this.options.transient) {
22
- const tmpl = this.ownTpl.prerender();
23
- if (tmpl.content && tmpl.children.length == 0) {
24
- tmpl.appendChild(tmpl.content);
25
- }
26
- }
27
- state.consent(this);
24
+ const view = this.ownView;
25
+ view.textContent = '';
26
+ const tmpl = this.ownTpl.prerender();
27
+ view.appendChild(tmpl.content.cloneNode(true));
28
+ state.chain('consent', this);
28
29
  }
29
30
  chainConsent(state) {
30
- window.HTMLCustomFormElement.prototype.fill.call(this, state.scope.$consent);
31
+ window.HTMLCustomFormElement.prototype.fill.call(this, state.scope.storage.all());
31
32
  if (this.options.transient) this.classList.remove('visible');
32
33
  }
34
+ handleChange(e, state) {
35
+ if (e.type == "submit" || !this.elements.find(item => item.type == "submit")) {
36
+ this.handleSubmit(e, state);
37
+ }
38
+ }
33
39
  handleSubmit(e, state) {
34
40
  if (e.type == "submit") e.preventDefault();
35
41
  if (this.isContentEditable) return;
36
42
  const consents = window.HTMLCustomFormElement.prototype.read.call(this);
37
43
  for (const [key, val] of Object.entries(consents)) {
38
- Page.storage.set('consent.' + key, val);
44
+ Page.storage.set(key, val);
39
45
  }
40
- state.scope.$consent = consents;
41
46
  state.runChain('consent');
42
47
  }
43
- handleChange(e, state) {
44
- this.handleSubmit(e, state);
45
- }
46
48
  patch(state) {
47
49
  if (this.isContentEditable) return;
48
- if (this.options.transient) {
49
- this.ownTpl.prerender();
50
- }
50
+ this.ownTpl.prerender();
51
51
  }
52
52
  get ownTpl() {
53
53
  return this.children.find(
@@ -59,46 +59,27 @@ class HTMLCustomConsentElement extends HTMLFormElement {
59
59
  }
60
60
  }
61
61
 
62
- Page.ready(() => {
63
- VirtualHTMLElement.define(`element-consent`, HTMLCustomConsentElement, 'form');
64
- });
65
-
66
- Page.State.prototype.consent = function (listener) {
67
- this.scope.$consent ??= {};
62
+ Page.State.prototype.consent = function (listener, ask) {
68
63
  const { consent } = listener.constructor;
69
- const val = Page.storage.get('consent.' + consent);
70
- this.scope.$consent[consent] = val;
71
- this.chain('consent', listener);
72
- if (val === undefined) {
73
- HTMLCustomConsentElement.waiting = true;
74
- } else if (val === null) {
75
- // setup finished but no consent is done yet, ask consent
76
- this.reconsent(listener);
64
+ if (!consent) {
65
+ console.warn("Expected a static consent field", listener);
66
+ return;
77
67
  }
78
- };
79
-
80
- Page.State.prototype.reconsent = function (listener) {
81
- if (listener) this.consent(listener);
82
- const consents = this.scope.$consent;
83
- let asking = false;
84
- for (const [key, val] of Object.entries(consents)) {
85
- if (listener && key != listener.constructor.consent) continue;
86
- if (val != "yes") {
87
- asking = HTMLCustomConsentElement.ask();
88
- }
89
- if (!asking) {
90
- if (val == null) consents[key] = "yes";
91
- }
68
+ const cur = Page.storage.get(consent);
69
+ if (cur == null || ask) {
70
+ Page.storage.set(consent, HTMLCustomConsentElement.ask(consent));
92
71
  }
93
- return asking;
72
+ this.chain('consent', listener);
94
73
  };
95
74
 
96
- Page.paint((state) => {
75
+ Page.ready(() => {
76
+ VirtualHTMLElement.define(`element-consent`, HTMLCustomConsentElement, 'form');
77
+ });
78
+
79
+ Page.paint(state => {
97
80
  state.finish(() => {
98
- let run = true;
99
- if (HTMLCustomConsentElement.waiting) {
100
- if (state.reconsent()) run = false;
81
+ if (!HTMLCustomConsentElement.explicit) {
82
+ state.runChain('consent');
101
83
  }
102
- if (run) state.runChain('consent');
103
84
  });
104
85
  });
package/ui/embed.js CHANGED
@@ -1,5 +1,5 @@
1
1
  class HTMLElementEmbed extends VirtualHTMLElement {
2
- static consent = "embed";
2
+ static consent = "consent.embed";
3
3
  static defaults = {
4
4
  src: null,
5
5
  hash: null
@@ -19,7 +19,7 @@ class HTMLElementEmbed extends VirtualHTMLElement {
19
19
  return this.promise;
20
20
  }
21
21
  consent(state) {
22
- const consent = state.scope.$consent[this.constructor.consent];
22
+ const consent = Page.storage.get(this.constructor.consent);
23
23
  this.classList.toggle('denied', consent == "no");
24
24
  this.classList.toggle('waiting', consent == null);
25
25
 
@@ -57,7 +57,7 @@ class HTMLElementEmbed extends VirtualHTMLElement {
57
57
  }
58
58
  }
59
59
  captureClick(e, state) {
60
- if (this.matches('.denied')) state.reconsent(this);
60
+ if (this.matches('.denied')) state.consent(this, true);
61
61
  }
62
62
  captureLoad() {
63
63
  this.promise.done();
package/ui/storage.js CHANGED
@@ -1,43 +1,28 @@
1
1
  class UserStore {
2
+ #store = window.localStorage ?? {};
3
+ all() {
4
+ return this.#store;
5
+ }
2
6
  get(key) {
3
- let storage = window.localStorage;
4
- let val;
5
- if (storage) {
6
- try {
7
- val = storage.getItem(key);
8
- } catch(ex) {
9
- storage = null;
10
- }
11
- }
12
- if (!storage) {
13
- val = this.getCookies()[key];
7
+ try {
8
+ return this.#store.getItem(key);
9
+ } catch (ex) {
10
+ console.error(ex);
14
11
  }
15
- return val;
16
12
  }
17
13
  set(key, val) {
18
- let storage = window.localStorage;
19
- if (storage) {
20
- try {
21
- storage.setItem(key, val);
22
- } catch(ex) {
23
- storage = null;
24
- }
25
- }
26
- if (!storage) {
27
- this.setCookie(key, val);
14
+ try {
15
+ if (val == null) return this.del(key);
16
+ else return this.#store.setItem(key, val);
17
+ } catch (ex) {
18
+ console.error(ex);
28
19
  }
29
20
  }
30
21
  del(key) {
31
- let storage = window.localStorage;
32
- if (storage) {
33
- try {
34
- storage.removeItem(key);
35
- } catch(ex) {
36
- storage = null;
37
- }
38
- }
39
- if (!storage) {
40
- this.clearCookie(key);
22
+ try {
23
+ return this.#store.removeItem(key);
24
+ } catch (ex) {
25
+ console.error(ex);
41
26
  }
42
27
  }
43
28
  clearCookies(re) {
@@ -68,5 +53,5 @@ class UserStore {
68
53
  }
69
54
  }
70
55
 
71
- Page.setup(() => Page.storage = new UserStore());
56
+ Page.paint(() => Page.storage = new UserStore());
72
57