simplyview 3.1.4 → 3.4.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.
@@ -1,5 +1,8 @@
1
1
  (() => {
2
2
  // src/activate.mjs
3
+ if (!Symbol.onDestroy) {
4
+ Symbol.onDestroy = Symbol("onDestroy");
5
+ }
3
6
  var listeners = /* @__PURE__ */ new Map();
4
7
  var activate = {
5
8
  addListener: (name, callback) => {
@@ -30,7 +33,12 @@
30
33
  const activate2 = node?.dataset?.simplyActivate;
31
34
  if (activate2 && listeners.has(activate2)) {
32
35
  for (let callback of listeners.get(activate2)) {
33
- callback.call(node);
36
+ const onDestroy = callback.call(node);
37
+ if (typeof onDestroy == "function") {
38
+ node[Symbol.onDestroy] = onDestroy;
39
+ } else if (typeof onDestroy != "undefined") {
40
+ console.warn("activate listener may only return a de-activate function, instead got", onDestroy);
41
+ }
34
42
  }
35
43
  }
36
44
  }
@@ -40,13 +48,26 @@
40
48
  if (change.type == "childList") {
41
49
  for (let node of change.addedNodes) {
42
50
  if (node.querySelectorAll) {
43
- var toActivate = Array.from(node.querySelectorAll("[data-simply-activate]"));
51
+ let toActivate = Array.from(node.querySelectorAll("[data-simply-activate]"));
44
52
  if (node.matches("[data-simply-activate]")) {
45
53
  toActivate.push(node);
46
54
  }
47
55
  activateNodes = activateNodes.concat(toActivate);
48
56
  }
49
57
  }
58
+ for (let node of change.removedNodes) {
59
+ if (node.querySelectorAll) {
60
+ let toDestroy = Array.from(node.querySelectorAll("[data-simply-activate]"));
61
+ if (node.matches["[data-simply-activate"]) {
62
+ toDestroy.push(node);
63
+ }
64
+ for (let child of toDestroy) {
65
+ if (child[Symbol.onDestroy]) {
66
+ child[Symbol.onDestroy].call(child);
67
+ }
68
+ }
69
+ }
70
+ }
50
71
  }
51
72
  }
52
73
  for (let node of activateNodes) {
@@ -67,13 +88,54 @@
67
88
  options.app = app2;
68
89
  }
69
90
  if (options.app) {
91
+ const waitHandler = {
92
+ apply(target, thisArg, argumentsList) {
93
+ try {
94
+ const result = target(...argumentsList);
95
+ if (result instanceof Promise) {
96
+ options.app.hooks.wait(true);
97
+ return result.finally(() => {
98
+ options.app.hooks.wait(false, target);
99
+ });
100
+ }
101
+ return result;
102
+ } catch (err) {
103
+ }
104
+ }
105
+ };
106
+ const functionHandler = {
107
+ apply(target, thisArg, argumentsList) {
108
+ try {
109
+ const result = target(...argumentsList);
110
+ if (result instanceof Promise) {
111
+ if (options.app.hooks.wait) {
112
+ options.app.hooks.wait(true, target);
113
+ return result.catch((err) => {
114
+ return options.app.hooks.error(err, target);
115
+ }).finally(() => {
116
+ options.app.hooks.wait(false, target);
117
+ });
118
+ } else {
119
+ return result.catch((err) => {
120
+ return options.app.hooks.error(err, target);
121
+ });
122
+ }
123
+ }
124
+ return result;
125
+ } catch (err) {
126
+ return options.app.hooks.error(err, target);
127
+ }
128
+ }
129
+ };
70
130
  const actionHandler = {
71
131
  get(target, property) {
72
132
  if (!target[property]) {
73
133
  return void 0;
74
134
  }
75
- if (target.catch) {
135
+ if (options.app.hooks?.error) {
76
136
  return new Proxy(target[property].bind(options.app), functionHandler);
137
+ } else if (options.app.hooks?.wait) {
138
+ return new Proxy(target[property].bind(options.app), waitHandler);
77
139
  } else {
78
140
  return target[property].bind(options.app);
79
141
  }
@@ -84,21 +146,6 @@
84
146
  return options;
85
147
  }
86
148
  }
87
- var functionHandler = {
88
- apply(target, thisArg, argumentsList) {
89
- try {
90
- const result = target(...argumentsList);
91
- if (result instanceof Promise) {
92
- return result.catch((err) => {
93
- return thisArg.catch(err);
94
- });
95
- }
96
- return result;
97
- } catch (err) {
98
- return thisArg.catch(err);
99
- }
100
- }
101
- };
102
149
 
103
150
  // src/route.mjs
104
151
  function routes(options, optionsCompat) {
@@ -111,7 +158,7 @@
111
158
  }
112
159
  var SimplyRoute = class {
113
160
  constructor(options = {}) {
114
- this.root = options.root || "/";
161
+ this.baseURL = options.baseURL || "/";
115
162
  this.app = options.app || {};
116
163
  this.addMissingSlash = !!options.addMissingSlash;
117
164
  this.matchExact = !!options.matchExact;
@@ -155,7 +202,7 @@
155
202
  matches = route.match.exec(path + "/");
156
203
  if (matches) {
157
204
  path += "/";
158
- history.replaceState({}, "", getURL(path, this.root));
205
+ history.replaceState({}, "", getURL(path, this.baseURL));
159
206
  }
160
207
  }
161
208
  }
@@ -181,7 +228,7 @@
181
228
  return false;
182
229
  }
183
230
  runListeners(action, params) {
184
- if (!Object.keys(this.listeners[action])) {
231
+ if (!this.listeners[action] || !Object.keys(this.listeners[action])) {
185
232
  return;
186
233
  }
187
234
  Object.keys(this.listeners[action]).forEach((route) => {
@@ -200,8 +247,8 @@
200
247
  }
201
248
  handleEvents() {
202
249
  globalThis.addEventListener("popstate", () => {
203
- if (this.match(getPath(document.location.pathname + document.location.hash, this.root)) === false) {
204
- this.match(getPath(document.location.pathname, this.root));
250
+ if (this.match(getPath(document.location.pathname + document.location.hash, this.baseURL)) === false) {
251
+ this.match(getPath(document.location.pathname, this.baseURL));
205
252
  }
206
253
  });
207
254
  this.app.container.addEventListener("click", (evt) => {
@@ -219,7 +266,7 @@
219
266
  let check = [link.hash, link.pathname + link.hash, link.pathname];
220
267
  let path;
221
268
  do {
222
- path = getPath(check.shift(), this.root);
269
+ path = getPath(check.shift(), this.baseURL);
223
270
  } while (check.length && !this.has(path));
224
271
  if (this.has(path)) {
225
272
  let params = this.runListeners("goto", { path });
@@ -234,11 +281,11 @@
234
281
  });
235
282
  }
236
283
  goto(path) {
237
- history.pushState({}, "", getURL(path, this.root));
284
+ history.pushState({}, "", getURL(path, this.baseURL));
238
285
  return this.match(path);
239
286
  }
240
287
  has(path) {
241
- path = getPath(path, this.root);
288
+ path = getPath(path, this.baseURL);
242
289
  for (let route of this.routeInfo) {
243
290
  var matches = route.match.exec(path);
244
291
  if (matches && matches.length) {
@@ -268,29 +315,29 @@
268
315
  });
269
316
  }
270
317
  init(options) {
271
- if (options.root) {
272
- this.root = options.root;
318
+ if (options.baseURL) {
319
+ this.baseURL = options.baseURL;
273
320
  }
274
321
  }
275
322
  };
276
- function getPath(path, root = "/") {
277
- if (path.substring(0, root.length) == root || root[root.length - 1] == "/" && path.length == root.length - 1 && path == root.substring(0, path.length)) {
278
- path = path.substring(root.length);
323
+ function getPath(path, baseURL = "/") {
324
+ if (path.substring(0, baseURL.length) == baseURL || baseURL[baseURL.length - 1] == "/" && path.length == baseURL.length - 1 && path == baseURL.substring(0, path.length)) {
325
+ path = path.substring(baseURL.length);
279
326
  }
280
327
  if (path[0] != "/" && path[0] != "#") {
281
328
  path = "/" + path;
282
329
  }
283
330
  return path;
284
331
  }
285
- function getURL(path, root) {
286
- path = getPath(path, root);
287
- if (root[root.length - 1] === "/" && path[0] === "/") {
332
+ function getURL(path, baseURL) {
333
+ path = getPath(path, baseURL);
334
+ if (baseURL[baseURL.length - 1] === "/" && path[0] === "/") {
288
335
  path = path.substring(1);
289
336
  }
290
337
  if (path[0] == "#") {
291
338
  return path;
292
339
  }
293
- return root + path;
340
+ return baseURL + path;
294
341
  }
295
342
  function getRegexpFromRoute(route, exact = false) {
296
343
  if (exact) {
@@ -590,12 +637,52 @@
590
637
  }
591
638
  }
592
639
 
640
+ // src/highlight.mjs
641
+ function html(strings, ...values) {
642
+ const outputArray = values.map(
643
+ (value, index) => `${strings[index]}${value}`
644
+ );
645
+ return outputArray.join("") + strings[strings.length - 1];
646
+ }
647
+ function css(strings, ...values) {
648
+ return html(strings, ...values);
649
+ }
650
+
593
651
  // src/app.mjs
594
652
  var SimplyApp = class {
595
653
  constructor(options = {}) {
596
654
  this.container = options.container || document.body;
655
+ if (options.components) {
656
+ mergeComponents(options, options.components);
657
+ }
597
658
  for (let key in options) {
598
659
  switch (key) {
660
+ case "html":
661
+ for (const name in options.html) {
662
+ const element = document.createElement("div");
663
+ element.innerHTML = options.html[name];
664
+ let template = this.container.querySelector("template#" + name);
665
+ if (!template) {
666
+ template = document.createElement("template");
667
+ template.id = name;
668
+ template.content.append(...element.children);
669
+ this.container.appendChild(template);
670
+ } else {
671
+ template.content.replaceChildren(...element.children);
672
+ }
673
+ }
674
+ break;
675
+ case "css":
676
+ for (const name in options.css) {
677
+ let style = this.container.querySelector("style#" + name);
678
+ if (!style) {
679
+ style = document.createElement("style");
680
+ style.id = name;
681
+ this.container.appendChild(style);
682
+ }
683
+ style.innerHTML = options.css[name];
684
+ }
685
+ break;
599
686
  case "commands":
600
687
  this.commands = commands({ app: this, container: this.container, commands: options.commands });
601
688
  break;
@@ -603,16 +690,13 @@
603
690
  case "keyboard":
604
691
  this.keys = keys({ app: this, keys: options.keys });
605
692
  break;
693
+ case "root":
694
+ // backwards compatibility
695
+ case "baseURL":
696
+ this.baseURL = options[key];
697
+ break;
606
698
  case "routes":
607
699
  this.routes = routes({ app: this, routes: options.routes });
608
- this.routes.handleEvents();
609
- globalThis.setTimeout(() => {
610
- if (this.routes.has(globalThis.location?.hash)) {
611
- this.routes.match(globalThis.location.hash);
612
- } else {
613
- this.routes.match(globalThis.location?.pathname + globalThis.location?.hash);
614
- }
615
- });
616
700
  break;
617
701
  case "actions":
618
702
  this.actions = actions({ app: this, actions: options.actions });
@@ -626,7 +710,36 @@
626
710
  case "view":
627
711
  this.view = view({ app: this, view: options.view });
628
712
  break;
713
+ case "hooks":
714
+ const moduleHandler = {
715
+ get: (target, property) => {
716
+ if (!target[property]) {
717
+ return void 0;
718
+ }
719
+ if (typeof target[property] == "function") {
720
+ return new Proxy(target[property], functionHandler);
721
+ } else if (target[property] && typeof target[property] == "object") {
722
+ return new Proxy(target[property], moduleHandler);
723
+ } else {
724
+ return target[property];
725
+ }
726
+ }
727
+ };
728
+ const functionHandler = {
729
+ apply: (target, thisArg, argumentsList) => {
730
+ return target.apply(this, argumentsList);
731
+ }
732
+ };
733
+ this[key] = new Proxy(options[key], moduleHandler);
734
+ break;
735
+ components:
736
+ this.components = components;
737
+ break;
738
+ prototype:
739
+ __proto__:
740
+ break;
629
741
  default:
742
+ console.log('simply.app: unknown initialization option "' + key + '", added as-is');
630
743
  this[key] = options[key];
631
744
  break;
632
745
  }
@@ -635,10 +748,82 @@
635
748
  get app() {
636
749
  return this;
637
750
  }
751
+ async start() {
752
+ if (this.components) {
753
+ for (const name in this.components) {
754
+ if (this.components[name].hooks?.start) {
755
+ await this.components[name].hooks.start();
756
+ }
757
+ }
758
+ }
759
+ if (this.hooks?.start) {
760
+ await this.hooks.start();
761
+ }
762
+ if (this.routes) {
763
+ if (this.baseURL) {
764
+ this.routes.init({ baseURL: this.baseURL });
765
+ }
766
+ this.routes.handleEvents();
767
+ globalThis.setTimeout(() => {
768
+ if (this.routes.has(globalThis.location?.hash)) {
769
+ this.routes.match(globalThis.location.hash);
770
+ } else {
771
+ this.routes.match(globalThis.location?.pathname + globalThis.location?.hash);
772
+ }
773
+ });
774
+ }
775
+ }
638
776
  };
639
777
  function app(options = {}) {
640
778
  return new SimplyApp(options);
641
779
  }
780
+ if (!globalThis.html) {
781
+ globalThis.html = html;
782
+ }
783
+ if (!globalThis.css) {
784
+ globalThis.css = css;
785
+ }
786
+ function mergeOptions(options, otherOptions) {
787
+ for (const key in otherOptions) {
788
+ switch (typeof otherOptions[key]) {
789
+ case "object":
790
+ if (!otherOptions[key]) {
791
+ continue;
792
+ }
793
+ if (!options[key]) {
794
+ options[key] = otherOptions[key];
795
+ } else {
796
+ mergeOptions(options[key], otherOptions[key]);
797
+ }
798
+ break;
799
+ default:
800
+ options[key] = otherOptions[key];
801
+ }
802
+ }
803
+ }
804
+ function mergeComponents(options, components2) {
805
+ for (const name in components2) {
806
+ const component = components2[name];
807
+ if (component.components) {
808
+ mergeComponents(options, component.components);
809
+ }
810
+ options.components[name] = component;
811
+ for (const key in component) {
812
+ switch (key) {
813
+ case "hooks":
814
+ // don't merge these, app.hooks.start will trigger each components start hook
815
+ case "components":
816
+ break;
817
+ default:
818
+ if (!options[key]) {
819
+ options[key] = /* @__PURE__ */ Object.create(null);
820
+ }
821
+ mergeOptions(options[key], component[key]);
822
+ break;
823
+ }
824
+ }
825
+ }
826
+ }
642
827
 
643
828
  // src/include.mjs
644
829
  function throttle(callbackFunction, intervalTime) {
@@ -741,8 +926,8 @@
741
926
  importScript();
742
927
  }
743
928
  },
744
- html: (html, link) => {
745
- let fragment = globalThis.document.createRange().createContextualFragment(html);
929
+ html: (html2, link) => {
930
+ let fragment = globalThis.document.createRange().createContextualFragment(html2);
746
931
  const stylesheets = fragment.querySelectorAll('link[rel="stylesheet"],style');
747
932
  for (let stylesheet of stylesheets) {
748
933
  if (stylesheet.href) {
@@ -789,8 +974,8 @@
789
974
  continue;
790
975
  }
791
976
  console.log("simply-include: loaded " + link.href);
792
- const html = await response.text();
793
- include.html(html, link);
977
+ const html2 = await response.text();
978
+ include.html(html2, link);
794
979
  link.parentNode.removeChild(link);
795
980
  }
796
981
  };
@@ -1,2 +1,2 @@
1
- (()=>{var h=new Map,N={addListener:(t,e)=>{h.has(t)||h.set(t,[]),h.get(t).push(e),V(t)},removeListener:(t,e)=>{if(!h.has(t))return!1;h.set(t,h.get(t).filter(r=>r!=e))}};function V(t){let e=document.querySelectorAll('[data-simply-activate="'+t+'"]');if(e)for(let r of e)S(r)}function S(t){let e=t?.dataset?.simplyActivate;if(e&&h.has(e))for(let r of h.get(e))r.call(t)}function z(t){let e=[];for(let a of t)if(a.type=="childList"){for(let n of a.addedNodes)if(n.querySelectorAll){var r=Array.from(n.querySelectorAll("[data-simply-activate]"));n.matches("[data-simply-activate]")&&r.push(n),e=e.concat(r)}}for(let a of e)S(a)}var W=new MutationObserver(z);W.observe(document,{subtree:!0,childList:!0});function y(t,e){if(e){let r=t;t=e,t.app=r}if(t.app){let r={get(a,n){if(a[n])return a.catch?new Proxy(a[n].bind(t.app),Y):a[n].bind(t.app)}};return new Proxy(t.actions,r)}else return t}var Y={apply(t,e,r){try{let a=t(...r);return a instanceof Promise?a.catch(n=>e.catch(n)):a}catch(a){return e.catch(a)}}};function g(t,e){if(e){let r=t;t=e,t.app=t}return new L(t)}var L=class{constructor(e={}){this.root=e.root||"/",this.app=e.app||{},this.addMissingSlash=!!e.addMissingSlash,this.matchExact=!!e.matchExact,this.clear(),e.routes&&this.load(e.routes)}load(e){G(e,this.routeInfo,this.matchExact)}clear(){this.routeInfo=[],this.listeners={match:{},call:{},goto:{},finish:{}}}match(e,r){let a={path:e,options:r};a=this.runListeners("match",a),e=a.path?a.path:e;let n;if(!e)return this.match(document.location.pathname+document.location.hash)?!0:this.match(document.location.pathname);e=f(e);for(let i of this.routeInfo)if(n=i.match.exec(e),this.addMissingSlash&&!n?.length&&e&&e[e.length-1]!="/"&&(n=i.match.exec(e+"/"),n&&(e+="/",history.replaceState({},"",P(e,this.root)))),n&&n.length){let s={};i.params.forEach((c,o)=>{c=="*"&&(c="remainder"),s[c]=n[o+1]}),Object.assign(s,r),a.route=i,a.params=s,a=this.runListeners("call",a),s=a.params?a.params:s;let l=new URLSearchParams(document.location.search);return a.result=i.action.call(this.app,s,l),this.runListeners("finish",a),a.result}return!1}runListeners(e,r){if(Object.keys(this.listeners[e]))return Object.keys(this.listeners[e]).forEach(a=>{var n=M(a);if(n.exec(r.path)){var i;for(let s of this.listeners[e][a])i=s.call(this.app,r),i&&(r=i)}}),r}handleEvents(){globalThis.addEventListener("popstate",()=>{this.match(f(document.location.pathname+document.location.hash,this.root))===!1&&this.match(f(document.location.pathname,this.root))}),this.app.container.addEventListener("click",e=>{if(!e.ctrlKey&&e.which==1){for(var r=e.target;r&&r.tagName!="A";)r=r.parentElement;if(r&&r.pathname&&r.hostname==globalThis.location.hostname&&!r.link&&!r.dataset.simplyCommand){let a=[r.hash,r.pathname+r.hash,r.pathname],n;do n=f(a.shift(),this.root);while(a.length&&!this.has(n));if(this.has(n)){let i=this.runListeners("goto",{path:n});if(i.path&&this.goto(i.path))return e.preventDefault(),!1}}}})}goto(e){return history.pushState({},"",P(e,this.root)),this.match(e)}has(e){e=f(e,this.root);for(let a of this.routeInfo){var r=a.match.exec(e);if(r&&r.length)return!0}return!1}addListener(e,r,a){if(["goto","match","call","finish"].indexOf(e)==-1)throw new Error("Unknown action "+e);this.listeners[e][r]||(this.listeners[e][r]=[]),this.listeners[e][r].push(a)}removeListener(e,r,a){if(["match","call","finish"].indexOf(e)==-1)throw new Error("Unknown action "+e);this.listeners[e][r]&&(this.listeners[e][r]=this.listeners[e][r].filter(n=>n!=a))}init(e){e.root&&(this.root=e.root)}};function f(t,e="/"){return(t.substring(0,e.length)==e||e[e.length-1]=="/"&&t.length==e.length-1&&t==e.substring(0,t.length))&&(t=t.substring(e.length)),t[0]!="/"&&t[0]!="#"&&(t="/"+t),t}function P(t,e){return t=f(t,e),e[e.length-1]==="/"&&t[0]==="/"&&(t=t.substring(1)),t[0]=="#"?t:e+t}function M(t,e=!1){return e?new RegExp("^"+t.replace(/:\w+/g,"([^/]+)").replace(/:\*/,"(.*)")+"(\\?|$)"):new RegExp("^"+t.replace(/:\w+/g,"([^/]+)").replace(/:\*/,"(.*)"))}function G(t,e,r=!1){let a=Object.keys(t),n=/:(\w+|\*)/g;for(let i of a){let s=[],l=[];do s=n.exec(i),s&&l.push(s[1]);while(s);e.push({match:M(i,r),params:l,action:t[i]})}return e}var T=class{constructor(e={}){e.app||(e.app={}),e.app.container||(e.app.container=document.body),this.app=e.app,this.$handlers=e.handlers||Q,e.commands&&Object.assign(this,e.commands);let r=a=>{let n=J(a,this.$handlers);if(!n)return;if(!this[n.name]){console.error("simply.command: undefined command "+n.name,n.source);return}if(this[n.name].call(e.app,n.source,n.value)!==!0)return a.preventDefault(),a.stopPropagation(),!1};e.app.container.addEventListener("click",r),e.app.container.addEventListener("submit",r),e.app.container.addEventListener("change",r),e.app.container.addEventListener("input",r)}call(e,r,a){if(!this[e]){console.error("simply.command: undefined command "+e);return}return this[e].call(this.app,r,a)}action(e){console.warn("deprecated call to `this.commands.action`");let r=Array.from(arguments).slice();return r.shift(),this.app.actions[e](...r)}appendHandler(e){this.$handlers.push(e)}prependHandler(e){this.$handlers.unshift(e)}};function b(t={},e){if(e){let r=t;t=e,t.app=t}return new T(t)}function J(t,e){var r=t.target.closest("[data-simply-command]");if(r){for(let a of e)if(r.matches(a.match))return a.check(r,t)?{name:r.dataset.simplyCommand,source:r,value:a.get(r)}:null}return null}var Q=[{match:"input,select,textarea",get:function(t){if(t.tagName==="SELECT"&&t.multiple){let e=[];for(let r of t.options)r.selected&&e.push(r.value);return e}return t.dataset.simplyValue||t.value},check:function(t,e){return e.type=="change"||t.dataset.simplyImmediate&&e.type=="input"}},{match:"a,button",get:function(t){return t.dataset.simplyValue||t.href||t.value},check:function(t,e){return e.type=="click"&&e.ctrlKey==!1&&e.button==0}},{match:"form",get:function(t){let e={};for(let r of Array.from(t.elements)){if(r.tagName=="INPUT"&&(r.type=="checkbox"||r.type=="radio")&&!r.checked)return;e[r.name]&&!Array.isArray(e[r.name])&&(e[r.name]=[e[r.name]]),Array.isArray(e[r.name])?e[r.name].push(r.value):e[r.name]=r.value}return e},check:function(t,e){return e.type=="submit"}},{match:"*",get:function(t){return t.dataset.simplyValue},check:function(t,e){return e.type=="click"&&e.ctrlKey==!1&&e.button==0}}];var m=Object.freeze({Compose:229,Control:17,Meta:224,Alt:18,Shift:16}),E=class{constructor(e={}){e.app||(e.app={}),e.app.container||(e.app.container=document.body),Object.assign(this,e.keys);let r=a=>{if(a.isComposing||a.keyCode===m.Compose||a.defaultPrevented||!a.target)return;let n="default";a.target.closest("[data-simply-keyboard]")&&(n=a.target.closest("[data-simply-keyboard]").dataset.simplyKeyboard);let i=[];a.ctrlKey&&a.keyCode!=m.Control&&i.push("Control"),a.metaKey&&a.keyCode!=m.Meta&&i.push("Meta"),a.altKey&&a.keyCode!=m.Alt&&i.push("Alt"),a.shiftKey&&a.keyCode!=m.Shift&&i.push("Shift"),i.push(a.key.toLowerCase());let s=[],l=event.target.closest("[data-simply-keyboard]");for(;l;)s.push(l.dataset.simplyKeyboard),l=l.parentNode.closest("[data-simply-keyboard]");s.push("");let c,o,U=["+","-"];for(let F in s){c=s[F],c==""?o="default":(o=c,c+=".");for(let _ of U){let u=i.join(_);if(this[o]&&typeof this[o][u]=="function"&&!this[o][u].call(e.app,a)){a.preventDefault();return}if(typeof this[o+u]=="function"&&!this[o+u].call(e.app,a)){a.preventDefault();return}if(this[n]&&this[n][u]){let p=e.app.container.querySelectorAll('[data-simply-accesskey="'+c+u+'"]');p.length&&(p.forEach($=>$.click()),a.preventDefault())}}}};e.app.container.addEventListener("keydown",r)}};function v(t={},e){if(e){let r=t;t=e,t.app=t}return new E(t)}function k(t,e){if(e){let r=t;t=e,t.app=t}if(t.app){t.app.view=t.view||{};let r=()=>{let a=t.app.view,n=globalThis.editor.data.getDataPath(t.app.container||document.body);t.app.view=globalThis.editor.currentData[n],Object.assign(t.app.view,a)};return globalThis.editor&&globalThis.editor.currentData?r():document.addEventListener("simply-content-loaded",r),t.app.view}else return t.view}var x=class{constructor(e={}){this.container=e.container||document.body;for(let r in e)switch(r){case"commands":this.commands=b({app:this,container:this.container,commands:e.commands});break;case"keys":case"keyboard":this.keys=v({app:this,keys:e.keys});break;case"routes":this.routes=g({app:this,routes:e.routes}),this.routes.handleEvents(),globalThis.setTimeout(()=>{this.routes.has(globalThis.location?.hash)?this.routes.match(globalThis.location.hash):this.routes.match(globalThis.location?.pathname+globalThis.location?.hash)});break;case"actions":this.actions=y({app:this,actions:e.actions}),this.action=function(a){console.warn("deprecated call to `this.action`");let n=Array.from(arguments).slice();return n.shift(),this.actions[a](...n)};break;case"view":this.view=k({app:this,view:e.view});break;default:this[r]=e[r];break}}get app(){return this}};function O(t={}){return new x(t)}function X(t,e){let r=0;return()=>{let a=arguments;r||(r=globalThis.setTimeout(()=>{t.apply(this,a),r=0},e))}}var Z=globalThis.requestIdleCallback?t=>{globalThis.requestIdleCallback(t,{timeout:500})}:globalThis.requestAnimationFrame;function q(t,e){let r=new URL(t,e);return d.cacheBuster&&r.searchParams.set("cb",d.cacheBuster),r.href}var j,ee={},C=globalThis.document.querySelector("head"),H=globalThis.document.currentScript,I,R;H?R=H.src:(I=(()=>{var t=document.getElementsByTagName("script"),e=t.length-1,r=t[e];return()=>r.src})(),R=I());var te=async()=>new Promise(function(t){var e=globalThis.document.createElement("script");e.src="https://cdn.jsdelivr.net/gh/simplyedit/simplyview/dist/simply.include.next.js",e.async=!1,globalThis.document.addEventListener("simply-include-next",()=>{C.removeChild(e),t()},{once:!0,passive:!0}),C.appendChild(e)}),w=[],d={cacheBuster:null,scripts:(t,e)=>{let r=t.slice(),a=()=>{let n=r.shift();if(!n)return;let i=[].map.call(n.attributes,l=>l.name),s=globalThis.document.createElement("script");for(let l of i)s.setAttribute(l,n.getAttribute(l));if(s.removeAttribute("data-simply-location"),!s.src)s.innerHTML=n.innerHTML,te().then(()=>{let l=w[n.dataset.simplyLocation];l.parentNode.insertBefore(s,l),l.parentNode.removeChild(l),a()});else{s.src=q(s.src,e),!s.hasAttribute("async")&&!s.hasAttribute("defer")&&(s.async=!1);let l=w[n.dataset.simplyLocation];l.parentNode.insertBefore(s,l),l.parentNode.removeChild(l),ee[s.src]=!0,a()}};r.length&&a()},html:(t,e)=>{let r=globalThis.document.createRange().createContextualFragment(t),a=r.querySelectorAll('link[rel="stylesheet"],style');for(let s of a)s.href&&(s.href=q(s.href,e.href)),C.appendChild(s);let n=globalThis.document.createDocumentFragment(),i=r.querySelectorAll("script");if(i.length){for(let s of i){let l=globalThis.document.createComment(s.src||"inline script");s.parentNode.insertBefore(l,s),s.dataset.simplyLocation=w.length,w.push(l),n.appendChild(s)}globalThis.setTimeout(function(){d.scripts(Array.from(n.children),e?e.href:globalThis.location.href)},10)}e.parentNode.insertBefore(r,e||null)}},B={},re=async t=>{let e=[].reduce.call(t,(r,a)=>(a.rel=="simply-include-once"&&B[a.href]?a.parentNode.removeChild(a):(B[a.href]=!0,a.rel="simply-include-loading",r.push(a)),r),[]);for(let r of e){if(!r.href)return;let a=await fetch(r.href);if(!a.ok){console.log("simply-include: failed to load "+r.href);continue}console.log("simply-include: loaded "+r.href);let n=await a.text();d.html(n,r),r.parentNode.removeChild(r)}},D=X(()=>{Z(()=>{var t=globalThis.document.querySelectorAll('link[rel="simply-include"],link[rel="simply-include-once"]');t.length&&re(t)})}),ae=()=>{j=new MutationObserver(D),j.observe(globalThis.document,{subtree:!0,childList:!0})};ae();D();var A=class extends HTMLElement{constructor(){super();let e=this.getAttribute("rel"),r=document.getElementById(e);if(r){let a=r.content.cloneNode(!0);for(let n of a.childNodes){let i=n.cloneNode(!0);i.nodeType==document.ELEMENT_NODE&&i.querySelectorAll("template").forEach(function(s){s.setAttribute("simply-render","")}),this.parentNode.insertBefore(i,this)}this.parentNode.removeChild(this)}}};customElements.get("simply-render")||customElements.define("simply-render",A);var K={activate:N,action:y,app:O,command:b,include:d,key:v,route:g,view:k};globalThis.simply=K;var Ne=K;})();
1
+ (()=>{Symbol.onDestroy||(Symbol.onDestroy=Symbol("onDestroy"));var h=new Map,N={addListener:(t,e)=>{h.has(t)||h.set(t,[]),h.get(t).push(e),G(t)},removeListener:(t,e)=>{if(!h.has(t))return!1;h.set(t,h.get(t).filter(r=>r!=e))}};function G(t){let e=document.querySelectorAll('[data-simply-activate="'+t+'"]');if(e)for(let r of e)P(r)}function P(t){let e=t?.dataset?.simplyActivate;if(e&&h.has(e))for(let r of h.get(e)){let a=r.call(t);typeof a=="function"?t[Symbol.onDestroy]=a:typeof a<"u"&&console.warn("activate listener may only return a de-activate function, instead got",a)}}function J(t){let e=[];for(let r of t)if(r.type=="childList"){for(let a of r.addedNodes)if(a.querySelectorAll){let i=Array.from(a.querySelectorAll("[data-simply-activate]"));a.matches("[data-simply-activate]")&&i.push(a),e=e.concat(i)}for(let a of r.removedNodes)if(a.querySelectorAll){let i=Array.from(a.querySelectorAll("[data-simply-activate]"));a.matches["[data-simply-activate"]&&i.push(a);for(let s of i)s[Symbol.onDestroy]&&s[Symbol.onDestroy].call(s)}}for(let r of e)P(r)}var Q=new MutationObserver(J);Q.observe(document,{subtree:!0,childList:!0});function y(t,e){if(e){let r=t;t=e,t.app=r}if(t.app){let r={apply(s,n,l){try{let c=s(...l);return c instanceof Promise?(t.app.hooks.wait(!0),c.finally(()=>{t.app.hooks.wait(!1,s)})):c}catch{}}},a={apply(s,n,l){try{let c=s(...l);return c instanceof Promise?t.app.hooks.wait?(t.app.hooks.wait(!0,s),c.catch(o=>t.app.hooks.error(o,s)).finally(()=>{t.app.hooks.wait(!1,s)})):c.catch(o=>t.app.hooks.error(o,s)):c}catch(c){return t.app.hooks.error(c,s)}}},i={get(s,n){if(s[n])return t.app.hooks?.error?new Proxy(s[n].bind(t.app),a):t.app.hooks?.wait?new Proxy(s[n].bind(t.app),r):s[n].bind(t.app)}};return new Proxy(t.actions,i)}else return t}function b(t,e){if(e){let r=t;t=e,t.app=t}return new L(t)}var L=class{constructor(e={}){this.baseURL=e.baseURL||"/",this.app=e.app||{},this.addMissingSlash=!!e.addMissingSlash,this.matchExact=!!e.matchExact,this.clear(),e.routes&&this.load(e.routes)}load(e){X(e,this.routeInfo,this.matchExact)}clear(){this.routeInfo=[],this.listeners={match:{},call:{},goto:{},finish:{}}}match(e,r){let a={path:e,options:r};a=this.runListeners("match",a),e=a.path?a.path:e;let i;if(!e)return this.match(document.location.pathname+document.location.hash)?!0:this.match(document.location.pathname);e=f(e);for(let s of this.routeInfo)if(i=s.match.exec(e),this.addMissingSlash&&!i?.length&&e&&e[e.length-1]!="/"&&(i=s.match.exec(e+"/"),i&&(e+="/",history.replaceState({},"",D(e,this.baseURL)))),i&&i.length){let n={};s.params.forEach((c,o)=>{c=="*"&&(c="remainder"),n[c]=i[o+1]}),Object.assign(n,r),a.route=s,a.params=n,a=this.runListeners("call",a),n=a.params?a.params:n;let l=new URLSearchParams(document.location.search);return a.result=s.action.call(this.app,n,l),this.runListeners("finish",a),a.result}return!1}runListeners(e,r){if(!(!this.listeners[e]||!Object.keys(this.listeners[e])))return Object.keys(this.listeners[e]).forEach(a=>{var i=R(a);if(i.exec(r.path)){var s;for(let n of this.listeners[e][a])s=n.call(this.app,r),s&&(r=s)}}),r}handleEvents(){globalThis.addEventListener("popstate",()=>{this.match(f(document.location.pathname+document.location.hash,this.baseURL))===!1&&this.match(f(document.location.pathname,this.baseURL))}),this.app.container.addEventListener("click",e=>{if(!e.ctrlKey&&e.which==1){for(var r=e.target;r&&r.tagName!="A";)r=r.parentElement;if(r&&r.pathname&&r.hostname==globalThis.location.hostname&&!r.link&&!r.dataset.simplyCommand){let a=[r.hash,r.pathname+r.hash,r.pathname],i;do i=f(a.shift(),this.baseURL);while(a.length&&!this.has(i));if(this.has(i)){let s=this.runListeners("goto",{path:i});if(s.path&&this.goto(s.path))return e.preventDefault(),!1}}}})}goto(e){return history.pushState({},"",D(e,this.baseURL)),this.match(e)}has(e){e=f(e,this.baseURL);for(let a of this.routeInfo){var r=a.match.exec(e);if(r&&r.length)return!0}return!1}addListener(e,r,a){if(["goto","match","call","finish"].indexOf(e)==-1)throw new Error("Unknown action "+e);this.listeners[e][r]||(this.listeners[e][r]=[]),this.listeners[e][r].push(a)}removeListener(e,r,a){if(["match","call","finish"].indexOf(e)==-1)throw new Error("Unknown action "+e);this.listeners[e][r]&&(this.listeners[e][r]=this.listeners[e][r].filter(i=>i!=a))}init(e){e.baseURL&&(this.baseURL=e.baseURL)}};function f(t,e="/"){return(t.substring(0,e.length)==e||e[e.length-1]=="/"&&t.length==e.length-1&&t==e.substring(0,t.length))&&(t=t.substring(e.length)),t[0]!="/"&&t[0]!="#"&&(t="/"+t),t}function D(t,e){return t=f(t,e),e[e.length-1]==="/"&&t[0]==="/"&&(t=t.substring(1)),t[0]=="#"?t:e+t}function R(t,e=!1){return e?new RegExp("^"+t.replace(/:\w+/g,"([^/]+)").replace(/:\*/,"(.*)")+"(\\?|$)"):new RegExp("^"+t.replace(/:\w+/g,"([^/]+)").replace(/:\*/,"(.*)"))}function X(t,e,r=!1){let a=Object.keys(t),i=/:(\w+|\*)/g;for(let s of a){let n=[],l=[];do n=i.exec(s),n&&l.push(n[1]);while(n);e.push({match:R(s,r),params:l,action:t[s]})}return e}var T=class{constructor(e={}){e.app||(e.app={}),e.app.container||(e.app.container=document.body),this.app=e.app,this.$handlers=e.handlers||ee,e.commands&&Object.assign(this,e.commands);let r=a=>{let i=Z(a,this.$handlers);if(!i)return;if(!this[i.name]){console.error("simply.command: undefined command "+i.name,i.source);return}if(this[i.name].call(e.app,i.source,i.value)!==!0)return a.preventDefault(),a.stopPropagation(),!1};e.app.container.addEventListener("click",r),e.app.container.addEventListener("submit",r),e.app.container.addEventListener("change",r),e.app.container.addEventListener("input",r)}call(e,r,a){if(!this[e]){console.error("simply.command: undefined command "+e);return}return this[e].call(this.app,r,a)}action(e){console.warn("deprecated call to `this.commands.action`");let r=Array.from(arguments).slice();return r.shift(),this.app.actions[e](...r)}appendHandler(e){this.$handlers.push(e)}prependHandler(e){this.$handlers.unshift(e)}};function g(t={},e){if(e){let r=t;t=e,t.app=t}return new T(t)}function Z(t,e){var r=t.target.closest("[data-simply-command]");if(r){for(let a of e)if(r.matches(a.match))return a.check(r,t)?{name:r.dataset.simplyCommand,source:r,value:a.get(r)}:null}return null}var ee=[{match:"input,select,textarea",get:function(t){if(t.tagName==="SELECT"&&t.multiple){let e=[];for(let r of t.options)r.selected&&e.push(r.value);return e}return t.dataset.simplyValue||t.value},check:function(t,e){return e.type=="change"||t.dataset.simplyImmediate&&e.type=="input"}},{match:"a,button",get:function(t){return t.dataset.simplyValue||t.href||t.value},check:function(t,e){return e.type=="click"&&e.ctrlKey==!1&&e.button==0}},{match:"form",get:function(t){let e={};for(let r of Array.from(t.elements)){if(r.tagName=="INPUT"&&(r.type=="checkbox"||r.type=="radio")&&!r.checked)return;e[r.name]&&!Array.isArray(e[r.name])&&(e[r.name]=[e[r.name]]),Array.isArray(e[r.name])?e[r.name].push(r.value):e[r.name]=r.value}return e},check:function(t,e){return e.type=="submit"}},{match:"*",get:function(t){return t.dataset.simplyValue},check:function(t,e){return e.type=="click"&&e.ctrlKey==!1&&e.button==0}}];var d=Object.freeze({Compose:229,Control:17,Meta:224,Alt:18,Shift:16}),E=class{constructor(e={}){e.app||(e.app={}),e.app.container||(e.app.container=document.body),Object.assign(this,e.keys);let r=a=>{if(a.isComposing||a.keyCode===d.Compose||a.defaultPrevented||!a.target)return;let i="default";a.target.closest("[data-simply-keyboard]")&&(i=a.target.closest("[data-simply-keyboard]").dataset.simplyKeyboard);let s=[];a.ctrlKey&&a.keyCode!=d.Control&&s.push("Control"),a.metaKey&&a.keyCode!=d.Meta&&s.push("Meta"),a.altKey&&a.keyCode!=d.Alt&&s.push("Alt"),a.shiftKey&&a.keyCode!=d.Shift&&s.push("Shift"),s.push(a.key.toLowerCase());let n=[],l=event.target.closest("[data-simply-keyboard]");for(;l;)n.push(l.dataset.simplyKeyboard),l=l.parentNode.closest("[data-simply-keyboard]");n.push("");let c,o,V=["+","-"];for(let z in n){c=n[z],c==""?o="default":(o=c,c+=".");for(let W of V){let u=s.join(W);if(this[o]&&typeof this[o][u]=="function"&&!this[o][u].call(e.app,a)){a.preventDefault();return}if(typeof this[o+u]=="function"&&!this[o+u].call(e.app,a)){a.preventDefault();return}if(this[i]&&this[i][u]){let p=e.app.container.querySelectorAll('[data-simply-accesskey="'+c+u+'"]');p.length&&(p.forEach(Y=>Y.click()),a.preventDefault())}}}};e.app.container.addEventListener("keydown",r)}};function k(t={},e){if(e){let r=t;t=e,t.app=t}return new E(t)}function v(t,e){if(e){let r=t;t=e,t.app=t}if(t.app){t.app.view=t.view||{};let r=()=>{let a=t.app.view,i=globalThis.editor.data.getDataPath(t.app.container||document.body);t.app.view=globalThis.editor.currentData[i],Object.assign(t.app.view,a)};return globalThis.editor&&globalThis.editor.currentData?r():document.addEventListener("simply-content-loaded",r),t.app.view}else return t.view}function x(t,...e){return e.map((a,i)=>`${t[i]}${a}`).join("")+t[t.length-1]}function q(t,...e){return x(t,...e)}var A=class{constructor(e={}){this.container=e.container||document.body,e.components&&M(e,e.components);for(let r in e)switch(r){case"html":for(let s in e.html){let n=document.createElement("div");n.innerHTML=e.html[s];let l=this.container.querySelector("template#"+s);l?l.content.replaceChildren(...n.children):(l=document.createElement("template"),l.id=s,l.content.append(...n.children),this.container.appendChild(l))}break;case"css":for(let s in e.css){let n=this.container.querySelector("style#"+s);n||(n=document.createElement("style"),n.id=s,this.container.appendChild(n)),n.innerHTML=e.css[s]}break;case"commands":this.commands=g({app:this,container:this.container,commands:e.commands});break;case"keys":case"keyboard":this.keys=k({app:this,keys:e.keys});break;case"root":case"baseURL":this.baseURL=e[r];break;case"routes":this.routes=b({app:this,routes:e.routes});break;case"actions":this.actions=y({app:this,actions:e.actions}),this.action=function(s){console.warn("deprecated call to `this.action`");let n=Array.from(arguments).slice();return n.shift(),this.actions[s](...n)};break;case"view":this.view=v({app:this,view:e.view});break;case"hooks":let a={get:(s,n)=>{if(s[n])return typeof s[n]=="function"?new Proxy(s[n],i):s[n]&&typeof s[n]=="object"?new Proxy(s[n],a):s[n]}},i={apply:(s,n,l)=>s.apply(this,l)};this[r]=new Proxy(e[r],a);break;default:console.log('simply.app: unknown initialization option "'+r+'", added as-is'),this[r]=e[r];break}}get app(){return this}async start(){if(this.components)for(let e in this.components)this.components[e].hooks?.start&&await this.components[e].hooks.start();this.hooks?.start&&await this.hooks.start(),this.routes&&(this.baseURL&&this.routes.init({baseURL:this.baseURL}),this.routes.handleEvents(),globalThis.setTimeout(()=>{this.routes.has(globalThis.location?.hash)?this.routes.match(globalThis.location.hash):this.routes.match(globalThis.location?.pathname+globalThis.location?.hash)}))}};function H(t={}){return new A(t)}globalThis.html||(globalThis.html=x);globalThis.css||(globalThis.css=q);function j(t,e){for(let r in e)switch(typeof e[r]){case"object":if(!e[r])continue;t[r]?j(t[r],e[r]):t[r]=e[r];break;default:t[r]=e[r]}}function M(t,e){for(let r in e){let a=e[r];a.components&&M(t,a.components),t.components[r]=a;for(let i in a)switch(i){case"hooks":case"components":break;default:t[i]||(t[i]=Object.create(null)),j(t[i],a[i]);break}}}function te(t,e){let r=0;return()=>{let a=arguments;r||(r=globalThis.setTimeout(()=>{t.apply(this,a),r=0},e))}}var re=globalThis.requestIdleCallback?t=>{globalThis.requestIdleCallback(t,{timeout:500})}:globalThis.requestAnimationFrame;function U(t,e){let r=new URL(t,e);return m.cacheBuster&&r.searchParams.set("cb",m.cacheBuster),r.href}var I,ae={},C=globalThis.document.querySelector("head"),B=globalThis.document.currentScript,K,O;B?O=B.src:(K=(()=>{var t=document.getElementsByTagName("script"),e=t.length-1,r=t[e];return()=>r.src})(),O=K());var ne=async()=>new Promise(function(t){var e=globalThis.document.createElement("script");e.src="https://cdn.jsdelivr.net/gh/simplyedit/simplyview/dist/simply.include.next.js",e.async=!1,globalThis.document.addEventListener("simply-include-next",()=>{C.removeChild(e),t()},{once:!0,passive:!0}),C.appendChild(e)}),w=[],m={cacheBuster:null,scripts:(t,e)=>{let r=t.slice(),a=()=>{let i=r.shift();if(!i)return;let s=[].map.call(i.attributes,l=>l.name),n=globalThis.document.createElement("script");for(let l of s)n.setAttribute(l,i.getAttribute(l));if(n.removeAttribute("data-simply-location"),!n.src)n.innerHTML=i.innerHTML,ne().then(()=>{let l=w[i.dataset.simplyLocation];l.parentNode.insertBefore(n,l),l.parentNode.removeChild(l),a()});else{n.src=U(n.src,e),!n.hasAttribute("async")&&!n.hasAttribute("defer")&&(n.async=!1);let l=w[i.dataset.simplyLocation];l.parentNode.insertBefore(n,l),l.parentNode.removeChild(l),ae[n.src]=!0,a()}};r.length&&a()},html:(t,e)=>{let r=globalThis.document.createRange().createContextualFragment(t),a=r.querySelectorAll('link[rel="stylesheet"],style');for(let n of a)n.href&&(n.href=U(n.href,e.href)),C.appendChild(n);let i=globalThis.document.createDocumentFragment(),s=r.querySelectorAll("script");if(s.length){for(let n of s){let l=globalThis.document.createComment(n.src||"inline script");n.parentNode.insertBefore(l,n),n.dataset.simplyLocation=w.length,w.push(l),i.appendChild(n)}globalThis.setTimeout(function(){m.scripts(Array.from(i.children),e?e.href:globalThis.location.href)},10)}e.parentNode.insertBefore(r,e||null)}},$={},se=async t=>{let e=[].reduce.call(t,(r,a)=>(a.rel=="simply-include-once"&&$[a.href]?a.parentNode.removeChild(a):($[a.href]=!0,a.rel="simply-include-loading",r.push(a)),r),[]);for(let r of e){if(!r.href)return;let a=await fetch(r.href);if(!a.ok){console.log("simply-include: failed to load "+r.href);continue}console.log("simply-include: loaded "+r.href);let i=await a.text();m.html(i,r),r.parentNode.removeChild(r)}},F=te(()=>{re(()=>{var t=globalThis.document.querySelectorAll('link[rel="simply-include"],link[rel="simply-include-once"]');t.length&&se(t)})}),ie=()=>{I=new MutationObserver(F),I.observe(globalThis.document,{subtree:!0,childList:!0})};ie();F();var S=class extends HTMLElement{constructor(){super();let e=this.getAttribute("rel"),r=document.getElementById(e);if(r){let a=r.content.cloneNode(!0);for(let i of a.childNodes){let s=i.cloneNode(!0);s.nodeType==document.ELEMENT_NODE&&s.querySelectorAll("template").forEach(function(n){n.setAttribute("simply-render","")}),this.parentNode.insertBefore(s,this)}this.parentNode.removeChild(this)}}};customElements.get("simply-render")||customElements.define("simply-render",S);var _={activate:N,action:y,app:H,command:g,include:m,key:k,route:b,view:v};globalThis.simply=_;var qe=_;})();
2
2
  //# sourceMappingURL=simply.everything.min.js.map