simplyview 3.1.3 → 3.3.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,17 +202,17 @@
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
  }
162
209
  if (matches && matches.length) {
163
210
  let params = {};
164
- route.params.forEach((key, i2) => {
211
+ route.params.forEach((key, i) => {
165
212
  if (key == "*") {
166
213
  key = "remainder";
167
214
  }
168
- params[key] = matches[i2 + 1];
215
+ params[key] = matches[i + 1];
169
216
  });
170
217
  Object.assign(params, options);
171
218
  args.route = route;
@@ -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) => {
@@ -216,10 +263,11 @@
216
263
  link = link.parentElement;
217
264
  }
218
265
  if (link && link.pathname && link.hostname == globalThis.location.hostname && !link.link && !link.dataset.simplyCommand) {
219
- let path = getPath(link.pathname + link.hash, this.root);
220
- if (!this.has(path)) {
221
- path = getPath(link.pathname, this.root);
222
- }
266
+ let check = [link.hash, link.pathname + link.hash, link.pathname];
267
+ let path;
268
+ do {
269
+ path = getPath(check.shift(), this.baseURL);
270
+ } while (check.length && !this.has(path));
223
271
  if (this.has(path)) {
224
272
  let params = this.runListeners("goto", { path });
225
273
  if (params.path) {
@@ -233,11 +281,11 @@
233
281
  });
234
282
  }
235
283
  goto(path) {
236
- history.pushState({}, "", getURL(path, this.root));
284
+ history.pushState({}, "", getURL(path, this.baseURL));
237
285
  return this.match(path);
238
286
  }
239
287
  has(path) {
240
- path = getPath(path, this.root);
288
+ path = getPath(path, this.baseURL);
241
289
  for (let route of this.routeInfo) {
242
290
  var matches = route.match.exec(path);
243
291
  if (matches && matches.length) {
@@ -267,26 +315,29 @@
267
315
  });
268
316
  }
269
317
  init(options) {
270
- if (options.root) {
271
- this.root = options.root;
318
+ if (options.baseURL) {
319
+ this.baseURL = options.baseURL;
272
320
  }
273
321
  }
274
322
  };
275
- function getPath(path, root = "/") {
276
- if (path.substring(0, root.length) == root || root[root.length - 1] == "/" && path.length == root.length - 1 && path == root.substring(0, path.length)) {
277
- 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);
278
326
  }
279
327
  if (path[0] != "/" && path[0] != "#") {
280
328
  path = "/" + path;
281
329
  }
282
330
  return path;
283
331
  }
284
- function getURL(path, root) {
285
- path = getPath(path, root);
286
- 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] === "/") {
287
335
  path = path.substring(1);
288
336
  }
289
- return root + path;
337
+ if (path[0] == "#") {
338
+ return path;
339
+ }
340
+ return baseURL + path;
290
341
  }
291
342
  function getRegexpFromRoute(route, exact = false) {
292
343
  if (exact) {
@@ -514,7 +565,7 @@
514
565
  keyboards.push("");
515
566
  let keyboard, subkeyboard;
516
567
  let separators = ["+", "-"];
517
- for (i in keyboards) {
568
+ for (let i in keyboards) {
518
569
  keyboard = keyboards[i];
519
570
  if (keyboard == "") {
520
571
  subkeyboard = "default";
@@ -599,12 +650,13 @@
599
650
  case "keyboard":
600
651
  this.keys = keys({ app: this, keys: options.keys });
601
652
  break;
653
+ case "root":
654
+ // backwards compatibility
655
+ case "baseURL":
656
+ this.baseURL = options[key];
657
+ break;
602
658
  case "routes":
603
659
  this.routes = routes({ app: this, routes: options.routes });
604
- this.routes.handleEvents();
605
- globalThis.setTimeout(() => {
606
- this.routes.match(globalThis.location?.pathname + globalThis.location?.hash);
607
- });
608
660
  break;
609
661
  case "actions":
610
662
  this.actions = actions({ app: this, actions: options.actions });
@@ -618,7 +670,30 @@
618
670
  case "view":
619
671
  this.view = view({ app: this, view: options.view });
620
672
  break;
673
+ case "hooks":
674
+ const moduleHandler = {
675
+ get: (target, property) => {
676
+ if (!target[property]) {
677
+ return void 0;
678
+ }
679
+ if (typeof target[property] == "function") {
680
+ return new Proxy(target[property], functionHandler);
681
+ } else if (target[property] && typeof target[property] == "object") {
682
+ return new Proxy(target[property], moduleHandler);
683
+ } else {
684
+ return target[property];
685
+ }
686
+ }
687
+ };
688
+ const functionHandler = {
689
+ apply: (target, thisArg, argumentsList) => {
690
+ return target.apply(this, argumentsList);
691
+ }
692
+ };
693
+ this[key] = new Proxy(options[key], moduleHandler);
694
+ break;
621
695
  default:
696
+ console.log('simply.app: unknown initialization option "' + key + '", added as-is');
622
697
  this[key] = options[key];
623
698
  break;
624
699
  }
@@ -627,6 +702,24 @@
627
702
  get app() {
628
703
  return this;
629
704
  }
705
+ async start() {
706
+ if (this.hooks?.start) {
707
+ await this.hooks.start();
708
+ }
709
+ if (this.routes) {
710
+ if (this.baseURL) {
711
+ this.routes.init({ baseURL: this.baseURL });
712
+ }
713
+ this.routes.handleEvents();
714
+ globalThis.setTimeout(() => {
715
+ if (this.routes.has(globalThis.location?.hash)) {
716
+ this.routes.match(globalThis.location.hash);
717
+ } else {
718
+ this.routes.match(globalThis.location?.pathname + globalThis.location?.hash);
719
+ }
720
+ });
721
+ }
722
+ }
630
723
  };
631
724
  function app(options = {}) {
632
725
  return new SimplyApp(options);
@@ -1,2 +1,2 @@
1
- (()=>{var h=new Map,S={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)P(r)}function P(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)P(a)}var W=new MutationObserver(z);W.observe(document,{subtree:!0,childList:!0});function g(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 b(t,e){if(e){let r=t;t=e,t.app=t}return new E(t)}var E=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 l of this.routeInfo)if(n=l.match.exec(e),this.addMissingSlash&&!n?.length&&e&&e[e.length-1]!="/"&&(n=l.match.exec(e+"/"),n&&(e+="/",history.replaceState({},"",M(e,this.root)))),n&&n.length){let s={};l.params.forEach((o,u)=>{o=="*"&&(o="remainder"),s[o]=n[u+1]}),Object.assign(s,r),a.route=l,a.params=s,a=this.runListeners("call",a),s=a.params?a.params:s;let c=new URLSearchParams(document.location.search);return a.result=l.action.call(this.app,s,c),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=O(a);if(n.exec(r.path)){var l;for(let s of this.listeners[e][a])l=s.call(this.app,r),l&&(r=l)}}),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=f(r.pathname+r.hash,this.root);if(this.has(a)||(a=f(r.pathname,this.root)),this.has(a)){let n=this.runListeners("goto",{path:a});if(n.path&&this.goto(n.path))return e.preventDefault(),!1}}}})}goto(e){return history.pushState({},"",M(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 M(t,e){return t=f(t,e),e[e.length-1]==="/"&&t[0]==="/"&&(t=t.substring(1)),e+t}function O(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 l of a){let s=[],c=[];do s=n.exec(l),s&&c.push(s[1]);while(s);e.push({match:O(l,r),params:c,action:t[l]})}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 v(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 p=Object.freeze({Compose:229,Control:17,Meta:224,Alt:18,Shift:16}),x=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===p.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 l=[];a.ctrlKey&&a.keyCode!=p.Control&&l.push("Control"),a.metaKey&&a.keyCode!=p.Meta&&l.push("Meta"),a.altKey&&a.keyCode!=p.Alt&&l.push("Alt"),a.shiftKey&&a.keyCode!=p.Shift&&l.push("Shift"),l.push(a.key.toLowerCase());let s=[],c=event.target.closest("[data-simply-keyboard]");for(;c;)s.push(c.dataset.simplyKeyboard),c=c.parentNode.closest("[data-simply-keyboard]");s.push("");let o,u,F=["+","-"];for(i in s){o=s[i],o==""?u="default":(u=o,o+=".");for(let _ of F){let d=l.join(_);if(this[u]&&typeof this[u][d]=="function"&&!this[u][d].call(e.app,a)){a.preventDefault();return}if(typeof this[u+d]=="function"&&!this[u+d].call(e.app,a)){a.preventDefault();return}if(this[n]&&this[n][d]){let y=e.app.container.querySelectorAll('[data-simply-accesskey="'+o+d+'"]');y.length&&(y.forEach($=>$.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 x(t)}function w(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 C=class{constructor(e={}){this.container=e.container||document.body;for(let r in e)switch(r){case"commands":this.commands=v({app:this,container:this.container,commands:e.commands});break;case"keys":case"keyboard":this.keys=k({app:this,keys:e.keys});break;case"routes":this.routes=b({app:this,routes:e.routes}),this.routes.handleEvents(),globalThis.setTimeout(()=>{this.routes.match(globalThis.location?.pathname+globalThis.location?.hash)});break;case"actions":this.actions=g({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=w({app:this,view:e.view});break;default:this[r]=e[r];break}}get app(){return this}};function q(t={}){return new C(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 j(t,e){let r=new URL(t,e);return m.cacheBuster&&r.searchParams.set("cb",m.cacheBuster),r.href}var H,ee={},A=globalThis.document.querySelector("head"),I=globalThis.document.currentScript,R,B;I?B=I.src:(R=(()=>{var t=document.getElementsByTagName("script"),e=t.length-1,r=t[e];return()=>r.src})(),B=R());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",()=>{A.removeChild(e),t()},{once:!0,passive:!0}),A.appendChild(e)}),L=[],m={cacheBuster:null,scripts:(t,e)=>{let r=t.slice(),a=()=>{let n=r.shift();if(!n)return;let l=[].map.call(n.attributes,c=>c.name),s=globalThis.document.createElement("script");for(let c of l)s.setAttribute(c,n.getAttribute(c));if(s.removeAttribute("data-simply-location"),!s.src)s.innerHTML=n.innerHTML,te().then(()=>{let c=L[n.dataset.simplyLocation];c.parentNode.insertBefore(s,c),c.parentNode.removeChild(c),a()});else{s.src=j(s.src,e),!s.hasAttribute("async")&&!s.hasAttribute("defer")&&(s.async=!1);let c=L[n.dataset.simplyLocation];c.parentNode.insertBefore(s,c),c.parentNode.removeChild(c),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=j(s.href,e.href)),A.appendChild(s);let n=globalThis.document.createDocumentFragment(),l=r.querySelectorAll("script");if(l.length){for(let s of l){let c=globalThis.document.createComment(s.src||"inline script");s.parentNode.insertBefore(c,s),s.dataset.simplyLocation=L.length,L.push(c),n.appendChild(s)}globalThis.setTimeout(function(){m.scripts(Array.from(n.children),e?e.href:globalThis.location.href)},10)}e.parentNode.insertBefore(r,e||null)}},D={},re=async t=>{let e=[].reduce.call(t,(r,a)=>(a.rel=="simply-include-once"&&D[a.href]?a.parentNode.removeChild(a):(D[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();m.html(n,r),r.parentNode.removeChild(r)}},K=X(()=>{Z(()=>{var t=globalThis.document.querySelectorAll('link[rel="simply-include"],link[rel="simply-include-once"]');t.length&&re(t)})}),ae=()=>{H=new MutationObserver(K),H.observe(globalThis.document,{subtree:!0,childList:!0})};ae();K();var N=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 l=n.cloneNode(!0);l.nodeType==document.ELEMENT_NODE&&l.querySelectorAll("template").forEach(function(s){s.setAttribute("simply-render","")}),this.parentNode.insertBefore(l,this)}this.parentNode.removeChild(this)}}};customElements.get("simply-render")||customElements.define("simply-render",N);var U={activate:S,action:g,app:q,command:v,include:m,key:k,route:b,view:w};globalThis.simply=U;var Ne=U;})();
1
+ (()=>{Symbol.onDestroy||(Symbol.onDestroy=Symbol("onDestroy"));var h=new Map,C={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)N(r)}function N(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 z(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)N(r)}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={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 g(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){Y(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({},"",P(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=D(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({},"",P(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 P(t,e){return t=f(t,e),e[e.length-1]==="/"&&t[0]==="/"&&(t=t.substring(1)),t[0]=="#"?t:e+t}function D(t,e=!1){return e?new RegExp("^"+t.replace(/:\w+/g,"([^/]+)").replace(/:\*/,"(.*)")+"(\\?|$)"):new RegExp("^"+t.replace(/:\w+/g,"([^/]+)").replace(/:\*/,"(.*)"))}function Y(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:D(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||J,e.commands&&Object.assign(this,e.commands);let r=a=>{let i=G(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 b(t={},e){if(e){let r=t;t=e,t.app=t}return new T(t)}function G(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 J=[{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}),x=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 i="default";a.target.closest("[data-simply-keyboard]")&&(i=a.target.closest("[data-simply-keyboard]").dataset.simplyKeyboard);let s=[];a.ctrlKey&&a.keyCode!=m.Control&&s.push("Control"),a.metaKey&&a.keyCode!=m.Meta&&s.push("Meta"),a.altKey&&a.keyCode!=m.Alt&&s.push("Alt"),a.shiftKey&&a.keyCode!=m.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,K=["+","-"];for(let F in n){c=n[F],c==""?o="default":(o=c,c+=".");for(let _ of K){let u=s.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[i]&&this[i][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 x(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,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}var E=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"root":case"baseURL":this.baseURL=e[r];break;case"routes":this.routes=g({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=k({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(){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 R(t={}){return new E(t)}function Q(t,e){let r=0;return()=>{let a=arguments;r||(r=globalThis.setTimeout(()=>{t.apply(this,a),r=0},e))}}var X=globalThis.requestIdleCallback?t=>{globalThis.requestIdleCallback(t,{timeout:500})}:globalThis.requestAnimationFrame;function U(t,e){let r=new URL(t,e);return d.cacheBuster&&r.searchParams.set("cb",d.cacheBuster),r.href}var q,Z={},A=globalThis.document.querySelector("head"),H=globalThis.document.currentScript,M,O;H?O=H.src:(M=(()=>{var t=document.getElementsByTagName("script"),e=t.length-1,r=t[e];return()=>r.src})(),O=M());var ee=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",()=>{A.removeChild(e),t()},{once:!0,passive:!0}),A.appendChild(e)}),w=[],d={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,ee().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),Z[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)),A.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(){d.scripts(Array.from(i.children),e?e.href:globalThis.location.href)},10)}e.parentNode.insertBefore(r,e||null)}},j={},te=async t=>{let e=[].reduce.call(t,(r,a)=>(a.rel=="simply-include-once"&&j[a.href]?a.parentNode.removeChild(a):(j[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();d.html(i,r),r.parentNode.removeChild(r)}},I=Q(()=>{X(()=>{var t=globalThis.document.querySelectorAll('link[rel="simply-include"],link[rel="simply-include-once"]');t.length&&te(t)})}),re=()=>{q=new MutationObserver(I),q.observe(globalThis.document,{subtree:!0,childList:!0})};re();I();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 B={activate:C,action:y,app:R,command:b,include:d,key:v,route:g,view:k};globalThis.simply=B;var Se=B;})();
2
2
  //# sourceMappingURL=simply.everything.min.js.map