jails-js 6.0.1-beta.4 → 6.0.1-beta.6

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,6 +1,11 @@
1
+ const textarea = document.createElement("textarea");
1
2
  const g = {
2
3
  scope: {}
3
4
  };
5
+ const decodeHTML = (text) => {
6
+ textarea.innerHTML = text;
7
+ return textarea.value;
8
+ };
4
9
  const rAF = (fn) => {
5
10
  if (requestAnimationFrame)
6
11
  return requestAnimationFrame(fn);
@@ -10,6 +15,9 @@ const rAF = (fn) => {
10
15
  const uuid = () => {
11
16
  return Math.random().toString(36).substring(2, 9);
12
17
  };
18
+ const dup = (o) => {
19
+ return JSON.parse(JSON.stringify(o));
20
+ };
13
21
  const safe = (execute, val) => {
14
22
  try {
15
23
  return execute();
@@ -787,13 +795,13 @@ const Component = ({ name, module, dependencies, node, templates: templates2, si
787
795
  const _model = module.model || {};
788
796
  const initialState = new Function(`return ${node.getAttribute("html-model") || "{}"}`)();
789
797
  const tplid = node.getAttribute("tplid");
790
- const scopeid = node.getAttribute("html-scope-id");
798
+ const scopeid = node.getAttribute("html-scopeid");
791
799
  const tpl = templates2[tplid];
792
- const data = g.scope[scopeid];
793
- const model = ((_a = module == null ? void 0 : module.model) == null ? void 0 : _a.apply) ? _model({ elm: node, initialState }) : _model;
794
- const state = Object.assign({}, data, model, initialState);
795
- const view = module.view ? module.view : (data2) => data2;
796
- let updates = [];
800
+ const scope = g.scope[scopeid];
801
+ const model = dup(((_a = module == null ? void 0 : module.model) == null ? void 0 : _a.apply) ? _model({ elm: node, initialState }) : _model);
802
+ const state = Object.assign({}, scope, model, initialState);
803
+ const view = module.view ? module.view : (data) => data;
804
+ let preserve = [];
797
805
  const base = {
798
806
  name,
799
807
  model,
@@ -809,34 +817,32 @@ const Component = ({ name, module, dependencies, node, templates: templates2, si
809
817
  * @State
810
818
  */
811
819
  state: {
812
- save(data2) {
813
- if (data2.constructor === Function) {
814
- data2(state);
820
+ protected(list) {
821
+ if (list) {
822
+ preserve = list;
815
823
  } else {
816
- Object.assign(state, data2);
824
+ return preserve;
817
825
  }
818
826
  },
819
- set(data2) {
827
+ save(data) {
828
+ if (data.constructor === Function) {
829
+ data(state);
830
+ } else {
831
+ Object.assign(state, data);
832
+ }
833
+ },
834
+ set(data) {
820
835
  if (!document.body.contains(node)) {
821
836
  return;
822
837
  }
823
- if (data2.constructor === Function) {
824
- data2(state);
838
+ if (data.constructor === Function) {
839
+ data(state);
825
840
  } else {
826
- Object.assign(state, data2);
841
+ Object.assign(state, data);
827
842
  }
828
- const newstate = Object.assign({}, state);
829
- updates.push(data2);
830
- return new Promise((resolve) => {
831
- rAF(() => {
832
- Object.assign.apply(null, [newstate, ...updates]);
833
- if (updates.length) {
834
- render(newstate);
835
- resolve(newstate);
836
- updates = [];
837
- }
838
- });
839
- });
843
+ const newstate = Object.assign({}, state, scope);
844
+ render(newstate);
845
+ return Promise.resolve(newstate);
840
846
  },
841
847
  get() {
842
848
  return Object.assign({}, state);
@@ -876,46 +882,37 @@ const Component = ({ name, module, dependencies, node, templates: templates2, si
876
882
  node.removeEventListener(ev, callback.handler);
877
883
  }
878
884
  },
879
- trigger(ev, selectorOrCallback, data2) {
885
+ trigger(ev, selectorOrCallback, data) {
880
886
  if (selectorOrCallback.constructor === String) {
881
887
  Array.from(node.querySelectorAll(selectorOrCallback)).forEach((children) => {
882
- children.dispatchEvent(new CustomEvent(ev, { bubbles: true, detail: { args: data2 } }));
888
+ children.dispatchEvent(new CustomEvent(ev, { bubbles: true, detail: { args: data } }));
883
889
  });
884
890
  } else {
885
- node.dispatchEvent(new CustomEvent(ev, { bubbles: true, detail: { args: data2 } }));
891
+ node.dispatchEvent(new CustomEvent(ev, { bubbles: true, detail: { args: data } }));
886
892
  }
887
893
  },
888
- emit(ev, data2) {
889
- node.dispatchEvent(new CustomEvent(ev, { bubbles: true, detail: { args: data2 } }));
894
+ emit(ev, data) {
895
+ node.dispatchEvent(new CustomEvent(ev, { bubbles: true, detail: { args: data } }));
890
896
  },
891
897
  unmount(fn) {
892
898
  node.addEventListener(":unmount", fn);
893
899
  },
894
900
  innerHTML(target, html_) {
895
- const element = html_ ? target : elm;
901
+ const element = html_ ? target : node;
896
902
  const clone = element.cloneNode();
897
903
  const html = html_ ? html_ : target;
898
904
  clone.innerHTML = html;
899
905
  rAF((_) => Idiomorph.morph(element, clone, IdiomorphOptions));
900
906
  }
901
907
  };
902
- const render = (data2) => {
903
- const html = tpl.render.call(view(data2), node, safe, g);
908
+ const render = (data) => {
909
+ const html = tpl.render.call(view(data), node, safe, g);
904
910
  Idiomorph.morph(node, html, IdiomorphOptions(node));
905
911
  rAF(() => {
906
912
  node.querySelectorAll("[tplid]").forEach((element) => {
907
913
  if (!element.base) return;
908
- const base2 = element.base;
909
- const props = Object.keys(base2.model).reduce((acc, key) => {
910
- if (key in data2) {
911
- if (!acc) acc = {};
912
- acc[key] = data2[key];
913
- }
914
- return acc;
915
- }, null);
916
- if (props) {
917
- base2.state.set(props);
918
- }
914
+ element.base.state.protected().forEach((key) => delete data[key]);
915
+ element.base.state.set(data);
919
916
  });
920
917
  rAF(() => g.scope = {});
921
918
  });
@@ -985,16 +982,15 @@ const template = (target, { components: components2 }) => {
985
982
  setTemplates(clone, components2);
986
983
  return templates;
987
984
  };
988
- const compile = (outerHTML) => {
989
- const html = transformAttributes(outerHTML);
985
+ const compile = (html) => {
990
986
  const parsedHtml = JSON.stringify(html);
991
987
  return new Function("$element", "safe", "$g", `
992
988
  var $data = this;
993
989
  with( $data ){
994
990
  var output=${parsedHtml.replace(/%%_=(.+?)_%%/g, function(_, variable) {
995
- return '"+safe(function(){return ' + variable + ';})+"';
991
+ return '"+safe(function(){return ' + decodeHTML(variable) + ';})+"';
996
992
  }).replace(/%%_(.+?)_%%/g, function(_, variable) {
997
- return '";' + variable + '\noutput+="';
993
+ return '";' + decodeHTML(variable) + '\noutput+="';
998
994
  })};return output;
999
995
  }
1000
996
  `);
@@ -1010,7 +1006,7 @@ const tagElements = (target, keys) => {
1010
1006
  const transformAttributes = (html) => {
1011
1007
  const regexTags = new RegExp(`\\${config.tags[0]}(.+?)\\${config.tags[1]}`, "g");
1012
1008
  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) => {
1013
- if (key === "key" || key === "model" || key === "scope-id") {
1009
+ if (key === "key" || key === "model" || key === "scopeid") {
1014
1010
  return all;
1015
1011
  }
1016
1012
  if (value) {
@@ -1032,7 +1028,8 @@ const transformTemplate = (clone) => {
1032
1028
  const split = htmlFor.match(/(.*)\sin\s(.*)/) || "";
1033
1029
  const varname = split[1];
1034
1030
  const object = split[2];
1035
- 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 }; _%%`);
1031
+ const objectname = object.split(/\./).shift();
1032
+ 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 }); _%%`);
1036
1033
  const close = document.createTextNode(`%%_ $index++; } })() _%%`);
1037
1034
  wrap(open, element, close);
1038
1035
  }
@@ -1059,24 +1056,13 @@ const setTemplates = (clone, components2) => {
1059
1056
  Array.from(clone.querySelectorAll("[tplid]")).reverse().forEach((node) => {
1060
1057
  const tplid = node.getAttribute("tplid");
1061
1058
  const name = node.localName;
1062
- node.setAttribute("html-scope-id", "jails___scope-id");
1059
+ node.setAttribute("html-scopeid", "jails___scope-id");
1063
1060
  if (name in components2 && components2[name].module.template) {
1064
1061
  const children = node.innerHTML;
1065
1062
  const html2 = components2[name].module.template({ elm: node, children });
1066
- if (html2.constructor === Promise) {
1067
- html2.then((htmlstring) => {
1068
- node.innerHTML = htmlstring;
1069
- const html3 = node.outerHTML;
1070
- templates[tplid] = {
1071
- template: html3,
1072
- render: compile(html3)
1073
- };
1074
- });
1075
- } else {
1076
- node.innerHTML = html2;
1077
- }
1063
+ node.innerHTML = html2;
1078
1064
  }
1079
- const html = node.outerHTML;
1065
+ const html = transformAttributes(node.outerHTML);
1080
1066
  templates[tplid] = {
1081
1067
  template: html,
1082
1068
  render: compile(html)