jails-js 6.0.1-beta.3 → 6.0.1-beta.5

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/dist/index.js CHANGED
@@ -1,4 +1,3 @@
1
- document.createElement("textarea");
2
1
  const g = {
3
2
  scope: {}
4
3
  };
@@ -11,6 +10,9 @@ const rAF = (fn) => {
11
10
  const uuid = () => {
12
11
  return Math.random().toString(36).substring(2, 9);
13
12
  };
13
+ const dup = (o) => {
14
+ return JSON.parse(JSON.stringify(o));
15
+ };
14
16
  const safe = (execute, val) => {
15
17
  try {
16
18
  return execute();
@@ -788,13 +790,13 @@ const Component = ({ name, module, dependencies, node, templates: templates2, si
788
790
  const _model = module.model || {};
789
791
  const initialState = new Function(`return ${node.getAttribute("html-model") || "{}"}`)();
790
792
  const tplid = node.getAttribute("tplid");
791
- const scopeid = node.getAttribute("html-scope-id");
793
+ const scopeid = node.getAttribute("html-scopeid");
792
794
  const tpl = templates2[tplid];
793
- const data = g.scope[scopeid];
794
- const model = ((_a = module == null ? void 0 : module.model) == null ? void 0 : _a.apply) ? _model({ elm: node, initialState }) : _model;
795
- const state = Object.assign({}, data, model, initialState);
796
- const view = module.view ? module.view : (data2) => data2;
797
- let updates = [];
795
+ const scope = g.scope[scopeid];
796
+ const model = dup(((_a = module == null ? void 0 : module.model) == null ? void 0 : _a.apply) ? _model({ elm: node, initialState }) : _model);
797
+ const state = Object.assign({}, scope, model, initialState);
798
+ const view = module.view ? module.view : (data) => data;
799
+ let preserve = [];
798
800
  const base = {
799
801
  name,
800
802
  model,
@@ -810,29 +812,32 @@ const Component = ({ name, module, dependencies, node, templates: templates2, si
810
812
  * @State
811
813
  */
812
814
  state: {
813
- save(data2) {
814
- if (data2.constructor === Function) {
815
- data2(state);
815
+ protected(list) {
816
+ if (list) {
817
+ preserve = list;
818
+ } else {
819
+ return preserve;
820
+ }
821
+ },
822
+ save(data) {
823
+ if (data.constructor === Function) {
824
+ data(state);
816
825
  } else {
817
- Object.assign(state, data2);
826
+ Object.assign(state, data);
818
827
  }
819
828
  },
820
- set(data2) {
829
+ set(data) {
821
830
  if (!document.body.contains(node)) {
822
831
  return;
823
832
  }
824
- const newstate = data2.constructor === Function ? Object.assign({}, data2(state)) : Object.assign(state, data2);
825
- updates.push(data2);
826
- return new Promise((resolve) => {
827
- rAF(() => {
828
- Object.assign.apply(null, [newstate, ...updates]);
829
- if (updates.length) {
830
- render(newstate);
831
- resolve(newstate);
832
- updates = [];
833
- }
834
- });
835
- });
833
+ if (data.constructor === Function) {
834
+ data(state);
835
+ } else {
836
+ Object.assign(state, data);
837
+ }
838
+ const newstate = Object.assign({}, state, scope);
839
+ render(newstate);
840
+ return Promise.resolve(newstate);
836
841
  },
837
842
  get() {
838
843
  return Object.assign({}, state);
@@ -872,17 +877,17 @@ const Component = ({ name, module, dependencies, node, templates: templates2, si
872
877
  node.removeEventListener(ev, callback.handler);
873
878
  }
874
879
  },
875
- trigger(ev, selectorOrCallback, data2) {
880
+ trigger(ev, selectorOrCallback, data) {
876
881
  if (selectorOrCallback.constructor === String) {
877
882
  Array.from(node.querySelectorAll(selectorOrCallback)).forEach((children) => {
878
- children.dispatchEvent(new CustomEvent(ev, { bubbles: true, detail: { args: data2 } }));
883
+ children.dispatchEvent(new CustomEvent(ev, { bubbles: true, detail: { args: data } }));
879
884
  });
880
885
  } else {
881
- node.dispatchEvent(new CustomEvent(ev, { bubbles: true, detail: { args: data2 } }));
886
+ node.dispatchEvent(new CustomEvent(ev, { bubbles: true, detail: { args: data } }));
882
887
  }
883
888
  },
884
- emit(ev, data2) {
885
- node.dispatchEvent(new CustomEvent(ev, { bubbles: true, detail: { args: data2 } }));
889
+ emit(ev, data) {
890
+ node.dispatchEvent(new CustomEvent(ev, { bubbles: true, detail: { args: data } }));
886
891
  },
887
892
  unmount(fn) {
888
893
  node.addEventListener(":unmount", fn);
@@ -895,24 +900,17 @@ const Component = ({ name, module, dependencies, node, templates: templates2, si
895
900
  rAF((_) => Idiomorph.morph(element, clone, IdiomorphOptions));
896
901
  }
897
902
  };
898
- const render = (data2) => {
899
- const html = tpl.render.call(view(data2), node, safe, g);
903
+ const render = (data) => {
904
+ const html = tpl.render.call(view(data), node, safe, g);
900
905
  Idiomorph.morph(node, html, IdiomorphOptions(node));
901
- node.querySelectorAll("[tplid]").forEach((element) => {
902
- if (!element.base) return;
903
- const base2 = element.base;
904
- const props = Object.keys(base2.model).reduce((acc, key) => {
905
- if (key in data2) {
906
- if (!acc) acc = {};
907
- acc[key] = data2[key];
908
- }
909
- return acc;
910
- }, null);
911
- if (props) {
912
- base2.state.set(props);
913
- }
906
+ rAF(() => {
907
+ node.querySelectorAll("[tplid]").forEach((element) => {
908
+ if (!element.base) return;
909
+ element.base.state.protected().forEach((key) => delete data[key]);
910
+ element.base.state.set(data);
911
+ });
912
+ rAF(() => g.scope = {});
914
913
  });
915
- rAF(() => g.scope = {});
916
914
  };
917
915
  node.base = base;
918
916
  return module.default(base);
@@ -979,8 +977,7 @@ const template = (target, { components: components2 }) => {
979
977
  setTemplates(clone, components2);
980
978
  return templates;
981
979
  };
982
- const compile = (outerHTML) => {
983
- const html = transformAttributes(outerHTML);
980
+ const compile = (html) => {
984
981
  const parsedHtml = JSON.stringify(html);
985
982
  return new Function("$element", "safe", "$g", `
986
983
  var $data = this;
@@ -1004,7 +1001,7 @@ const tagElements = (target, keys) => {
1004
1001
  const transformAttributes = (html) => {
1005
1002
  const regexTags = new RegExp(`\\${config.tags[0]}(.+?)\\${config.tags[1]}`, "g");
1006
1003
  return html.replace(/jails___scope-id/g, "%%_=$scopeid_%%").replace(regexTags, "%%_=$1_%%").replace(/html-(allowfullscreen|async|autofocus|autoplay|checked|controls|default|defer|disabled|formnovalidate|inert|ismap|itemscope|loop|multiple|muted|nomodule|novalidate|open|playsinline|readonly|required|reversed|selected)=\"(.*?)\"/g, `%%_if(safe(function(){ return $2 })){_%%$1%%_}_%%`).replace(/html-(.*?)=\"(.*?)\"/g, (all, key, value) => {
1007
- if (key === "key" || key === "model" || key === "scope-id") {
1004
+ if (key === "key" || key === "model" || key === "scopeid") {
1008
1005
  return all;
1009
1006
  }
1010
1007
  if (value) {
@@ -1026,7 +1023,8 @@ const transformTemplate = (clone) => {
1026
1023
  const split = htmlFor.match(/(.*)\sin\s(.*)/) || "";
1027
1024
  const varname = split[1];
1028
1025
  const object = split[2];
1029
- const open = document.createTextNode(`%%_ ;(function(){ var $index = 0; for(var $key in safe(function(){ return ${object} }) ){ var $scopeid = Math.random().toString(36).substring(2, 9); var ${varname} = ${object}[$key]; $g.scope[$scopeid] = { ${varname} :${varname}, ${object}: ${object}, $index: $index, $key: $key }; _%%`);
1026
+ const objectname = object.split(/\./).shift();
1027
+ const open = document.createTextNode(`%%_ ;(function(){ var $index = 0; for(var $key in safe(function(){ return ${object} }) ){ var $scopeid = Math.random().toString(36).substring(2, 9); var ${varname} = ${object}[$key]; $g.scope[$scopeid] = Object.assign({}, { ${objectname}: ${objectname} }, { ${varname} :${varname}, $index: $index, $key: $key }); _%%`);
1030
1028
  const close = document.createTextNode(`%%_ $index++; } })() _%%`);
1031
1029
  wrap(open, element, close);
1032
1030
  }
@@ -1053,24 +1051,13 @@ const setTemplates = (clone, components2) => {
1053
1051
  Array.from(clone.querySelectorAll("[tplid]")).reverse().forEach((node) => {
1054
1052
  const tplid = node.getAttribute("tplid");
1055
1053
  const name = node.localName;
1056
- node.setAttribute("html-scope-id", "jails___scope-id");
1054
+ node.setAttribute("html-scopeid", "jails___scope-id");
1057
1055
  if (name in components2 && components2[name].module.template) {
1058
1056
  const children = node.innerHTML;
1059
1057
  const html2 = components2[name].module.template({ elm: node, children });
1060
- if (html2.constructor === Promise) {
1061
- html2.then((htmlstring) => {
1062
- node.innerHTML = htmlstring;
1063
- const html3 = node.outerHTML;
1064
- templates[tplid] = {
1065
- template: html3,
1066
- render: compile(html3)
1067
- };
1068
- });
1069
- } else {
1070
- node.innerHTML = html2;
1071
- }
1058
+ node.innerHTML = html2;
1072
1059
  }
1073
- const html = node.outerHTML;
1060
+ const html = transformAttributes(node.outerHTML);
1074
1061
  templates[tplid] = {
1075
1062
  template: html,
1076
1063
  render: compile(html)