@kosatyi/ejs 0.0.99 → 0.0.101

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.
@@ -701,8 +701,8 @@ function createBuffer() {
701
701
  return buffer;
702
702
  }
703
703
 
704
+ var PARENT = Symbol('ContextScope.parentTemplate');
704
705
  var createContextScope = function createContextScope(config, methods) {
705
- var _Object$definePropert;
706
706
  var _config$vars = config.vars,
707
707
  BLOCKS = _config$vars.BLOCKS,
708
708
  MACRO = _config$vars.MACRO,
@@ -713,217 +713,300 @@ var createContextScope = function createContextScope(config, methods) {
713
713
  SCOPE = _config$vars.SCOPE,
714
714
  COMPONENT = _config$vars.COMPONENT,
715
715
  ELEMENT = _config$vars.ELEMENT;
716
+ /**
717
+ *
718
+ * @type {symbol}
719
+ */
720
+
716
721
  /**
717
722
  * @name ContextScope
718
723
  * @param data
719
724
  * @constructor
720
725
  */
721
726
  function ContextScope(data) {
727
+ this[PARENT] = null;
722
728
  this[BLOCKS] = {};
723
729
  this[MACRO] = {};
724
730
  Object.assign(this, omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT]));
725
731
  }
726
732
  Object.assign(ContextScope.prototype, methods);
727
- Object.defineProperties(ContextScope.prototype, (_Object$definePropert = {}, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_Object$definePropert, BUFFER, {
733
+ Object.defineProperty(ContextScope.prototype, BUFFER, {
728
734
  value: createBuffer()
729
- }), BLOCKS, {
735
+ });
736
+ Object.defineProperty(ContextScope.prototype, BLOCKS, {
730
737
  value: {},
731
738
  writable: true
732
- }), MACRO, {
739
+ });
740
+ Object.defineProperty(ContextScope.prototype, MACRO, {
733
741
  value: {},
734
742
  writable: true
735
- }), LAYOUT, {
743
+ });
744
+ Object.defineProperty(ContextScope.prototype, LAYOUT, {
736
745
  value: false,
737
746
  writable: true
738
- }), EXTEND, {
747
+ });
748
+ Object.defineProperty(ContextScope.prototype, EXTEND, {
739
749
  value: false,
740
750
  writable: true
741
- }), "useSafeValue", {
742
- get: function get() {
743
- return safeValue;
744
- }
745
- }), "useComponent", {
746
- get: function get() {
747
- if (isFunction(this[COMPONENT])) {
748
- return this[COMPONENT].bind(this);
749
- } else {
750
- return function () {
751
- throw new Error("".concat(COMPONENT, " must be a function"));
752
- };
751
+ });
752
+ Object.defineProperty(ContextScope.prototype, PARENT, {
753
+ value: null,
754
+ writable: true
755
+ });
756
+ Object.defineProperties(ContextScope.prototype, {
757
+ /** @type {function} */
758
+ setParentTemplate: {
759
+ value: function value(_value) {
760
+ this[PARENT] = _value;
761
+ return this;
753
762
  }
754
- }
755
- }), "useElement", {
756
- get: function get() {
757
- if (isFunction(this[ELEMENT])) {
758
- return this[ELEMENT].bind(this);
759
- } else {
760
- return function () {
761
- throw new Error("".concat(ELEMENT, " must be a function"));
762
- };
763
+ },
764
+ /** @type {function} */
765
+ getParentTemplate: {
766
+ value: function value() {
767
+ return this[PARENT];
763
768
  }
764
- }
765
- }), "getMacro", {
766
- value: function value() {
767
- return this[MACRO];
768
- }
769
- }), "getBuffer", {
770
- value: function value() {
771
- return this[BUFFER];
772
- }
773
- }), _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_Object$definePropert, "getBlocks", {
774
- value: function value() {
775
- return this[BLOCKS];
776
- }
777
- }), "setExtend", {
778
- value: function value(_value) {
779
- this[EXTEND] = _value;
780
- }
781
- }), "getExtend", {
782
- value: function value() {
783
- return this[EXTEND];
784
- }
785
- }), "setLayout", {
786
- value: function value(layout) {
787
- this[LAYOUT] = layout;
788
- }
789
- }), "getLayout", {
790
- value: function value() {
791
- return this[LAYOUT];
792
- }
793
- }), "clone", {
794
- value: function value(exclude_blocks) {
795
- var filter = [LAYOUT, EXTEND, BUFFER];
796
- if (exclude_blocks === true) {
797
- filter.push(BLOCKS);
769
+ },
770
+ /** @type {function} */
771
+ useSafeValue: {
772
+ get: function get() {
773
+ return safeValue;
798
774
  }
799
- return omit(this, filter);
800
- }
801
- }), "extend", {
802
- value: function value(layout) {
803
- this.setExtend(true);
804
- this.setLayout(layout);
805
- }
806
- }), "echo", {
807
- value: function value(layout) {
808
- var buffer = this.getBuffer();
809
- var params = [].slice.call(arguments);
810
- params.forEach(buffer);
811
- }
812
- }), "fn", {
813
- value: function value(callback) {
814
- var buffer = this.getBuffer();
815
- var context = this;
816
- return function () {
817
- if (isFunction(callback)) {
818
- buffer.backup();
819
- buffer(callback.apply(context, arguments));
820
- return buffer.restore();
775
+ },
776
+ /** @type {function} */
777
+ useComponent: {
778
+ get: function get() {
779
+ if (isFunction(this[COMPONENT])) {
780
+ return this[COMPONENT].bind(this);
781
+ } else {
782
+ return function () {
783
+ throw new Error("".concat(COMPONENT, " must be a function"));
784
+ };
821
785
  }
822
- };
823
- }
824
- }), "macro", {
825
- value: function value(name, callback) {
826
- var list = this.getMacro();
827
- var macro = this.fn(callback);
828
- var context = this;
829
- list[name] = function () {
830
- return context.echo(macro.apply(undefined, arguments));
831
- };
832
- }
833
- }), _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_Object$definePropert, "call", {
834
- value: function value(name) {
835
- var list = this.getMacro();
836
- var macro = list[name];
837
- var params = [].slice.call(arguments, 1);
838
- if (isFunction(macro)) {
839
- return macro.apply(macro, params);
840
786
  }
841
- }
842
- }), "block", {
843
- value: function value(name, callback) {
844
- var _this = this;
845
- var blocks = this.getBlocks();
846
- blocks[name] = blocks[name] || [];
847
- blocks[name].push(this.fn(callback));
848
- if (this.getExtend()) return;
849
- var list = Object.assign([], blocks[name]);
850
- var current = function current() {
851
- return list.shift();
852
- };
853
- var _next = function next() {
854
- var parent = current();
855
- if (parent) {
787
+ },
788
+ /** @type {function} */
789
+ useElement: {
790
+ get: function get() {
791
+ if (isFunction(this[ELEMENT])) {
792
+ return this[ELEMENT].bind(this);
793
+ } else {
856
794
  return function () {
857
- _this.echo(parent(_next()));
795
+ throw new Error("".concat(ELEMENT, " must be a function"));
858
796
  };
859
- } else {
860
- return noop;
861
797
  }
862
- };
863
- this.echo(current()(_next()));
864
- }
865
- }), "hasBlock", {
866
- value: function value(name) {
867
- return this.getBlocks().hasOwnProperty(name);
868
- }
869
- }), "include", {
870
- value: function value(path, data, cx) {
871
- var context = cx === false ? {} : this.clone(true);
872
- var params = extend(context, data || {});
873
- var promise = this.render(path, params);
874
- this.echo(promise);
875
- }
876
- }), "use", {
877
- value: function value(path, namespace) {
878
- var _this2 = this;
879
- this.echo(Promise.resolve(this.require(path)).then(function (exports) {
880
- var list = _this2.getMacro();
881
- each(exports, function (macro, name) {
882
- list[[namespace, name].join('.')] = macro;
883
- });
884
- }));
885
- }
886
- }), "async", {
887
- value: function value(promise, callback) {
888
- this.echo(Promise.resolve(promise).then(callback));
889
- }
890
- }), "get", {
891
- value: function value(name, defaults) {
892
- var path = getPath(this, name, true);
893
- var result = path.shift();
894
- var prop = path.pop();
895
- return hasProp(result, prop) ? result[prop] : defaults;
896
- }
897
- }), "set", {
898
- value: function value(name, _value2) {
899
- var path = getPath(this, name, false);
900
- var result = path.shift();
901
- var prop = path.pop();
902
- if (this.getExtend() && hasProp(result, prop)) {
903
- return result[prop];
904
798
  }
905
- return result[prop] = _value2;
906
- }
907
- }), "each", {
908
- value: function value(object, callback) {
909
- if (isString(object)) {
910
- object = this.get(object, []);
799
+ },
800
+ /** @type {()=>this[MACRO]} */
801
+ getMacro: {
802
+ value: function value() {
803
+ return this[MACRO];
911
804
  }
912
- each(object, callback);
913
805
  },
914
- writable: true
915
- }), "el", {
916
- value: function value(tag, attr, content) {
917
- content = isFunction(content) ? this.fn(content)() : content;
918
- this.echo(Promise.resolve(content).then(function (content) {
919
- return element(tag, attr, content);
920
- }));
806
+ /** @type {function} */
807
+ getBuffer: {
808
+ value: function value() {
809
+ return this[BUFFER];
810
+ }
921
811
  },
922
- writable: true
923
- }), _defineProperty(_Object$definePropert, "ui", {
924
- value: function value(layout) {},
925
- writable: true
926
- })));
812
+ /** @type {function} */
813
+ getBlocks: {
814
+ value: function value() {
815
+ return this[BLOCKS];
816
+ }
817
+ },
818
+ /** @type {function} */
819
+ setExtend: {
820
+ value: function value(_value2) {
821
+ this[EXTEND] = _value2;
822
+ return this;
823
+ }
824
+ },
825
+ /** @type {function} */
826
+ getExtend: {
827
+ value: function value() {
828
+ return this[EXTEND];
829
+ }
830
+ },
831
+ /** @type {function} */
832
+ setLayout: {
833
+ value: function value(layout) {
834
+ this[LAYOUT] = layout;
835
+ return this;
836
+ }
837
+ },
838
+ /** @type {function} */
839
+ getLayout: {
840
+ value: function value() {
841
+ return this[LAYOUT];
842
+ }
843
+ },
844
+ /** @type {function} */
845
+ clone: {
846
+ value: function value(exclude_blocks) {
847
+ var filter = [LAYOUT, EXTEND, BUFFER];
848
+ if (exclude_blocks === true) {
849
+ filter.push(BLOCKS);
850
+ }
851
+ return omit(this, filter);
852
+ }
853
+ },
854
+ /** @type {function} */
855
+ extend: {
856
+ value: function value(layout) {
857
+ this.setExtend(true);
858
+ this.setLayout(layout);
859
+ }
860
+ },
861
+ /** @type {function} */
862
+ echo: {
863
+ value: function value(layout) {
864
+ var buffer = this.getBuffer();
865
+ var params = [].slice.call(arguments);
866
+ params.forEach(buffer);
867
+ }
868
+ },
869
+ /** @type {function} */
870
+ fn: {
871
+ value: function value(callback) {
872
+ var buffer = this.getBuffer();
873
+ var context = this;
874
+ return function () {
875
+ if (isFunction(callback)) {
876
+ buffer.backup();
877
+ buffer(callback.apply(context, arguments));
878
+ return buffer.restore();
879
+ }
880
+ };
881
+ }
882
+ },
883
+ /** @type {function} */
884
+ macro: {
885
+ value: function value(name, callback) {
886
+ var list = this.getMacro();
887
+ var macro = this.fn(callback);
888
+ var context = this;
889
+ list[name] = function () {
890
+ return context.echo(macro.apply(undefined, arguments));
891
+ };
892
+ }
893
+ },
894
+ /** @type {function} */
895
+ call: {
896
+ value: function value(name) {
897
+ var list = this.getMacro();
898
+ var macro = list[name];
899
+ var params = [].slice.call(arguments, 1);
900
+ if (isFunction(macro)) {
901
+ return macro.apply(macro, params);
902
+ }
903
+ }
904
+ },
905
+ /** @type {function} */
906
+ block: {
907
+ value: function value(name, callback) {
908
+ var _this = this;
909
+ var blocks = this.getBlocks();
910
+ blocks[name] = blocks[name] || [];
911
+ blocks[name].push(this.fn(callback));
912
+ if (this.getExtend()) return;
913
+ var list = Object.assign([], blocks[name]);
914
+ var current = function current() {
915
+ return list.shift();
916
+ };
917
+ var _next = function next() {
918
+ var parent = current();
919
+ if (parent) {
920
+ return function () {
921
+ _this.echo(parent(_next()));
922
+ };
923
+ } else {
924
+ return noop;
925
+ }
926
+ };
927
+ this.echo(current()(_next()));
928
+ }
929
+ },
930
+ /** @type {function} */
931
+ hasBlock: {
932
+ value: function value(name) {
933
+ return this.getBlocks().hasOwnProperty(name);
934
+ }
935
+ },
936
+ /** @type {function} */
937
+ include: {
938
+ value: function value(path, data, cx) {
939
+ var context = cx === false ? {} : this.clone(true);
940
+ var params = extend(context, data || {});
941
+ var promise = this.render(path, params);
942
+ this.echo(promise);
943
+ }
944
+ },
945
+ /** @type {function} */
946
+ use: {
947
+ value: function value(path, namespace) {
948
+ var _this2 = this;
949
+ this.echo(Promise.resolve(this.require(path)).then(function (exports) {
950
+ var list = _this2.getMacro();
951
+ each(exports, function (macro, name) {
952
+ list[[namespace, name].join('.')] = macro;
953
+ });
954
+ }));
955
+ }
956
+ },
957
+ /** @type {function} */
958
+ async: {
959
+ value: function value(promise, callback) {
960
+ this.echo(Promise.resolve(promise).then(callback));
961
+ }
962
+ },
963
+ /** @type {function} */
964
+ get: {
965
+ value: function value(name, defaults) {
966
+ var path = getPath(this, name, true);
967
+ var result = path.shift();
968
+ var prop = path.pop();
969
+ return hasProp(result, prop) ? result[prop] : defaults;
970
+ }
971
+ },
972
+ /** @type {function} */
973
+ set: {
974
+ value: function value(name, _value3) {
975
+ var path = getPath(this, name, false);
976
+ var result = path.shift();
977
+ var prop = path.pop();
978
+ if (this.getParentTemplate() && hasProp(result, prop)) {
979
+ return result[prop];
980
+ }
981
+ return result[prop] = _value3;
982
+ }
983
+ },
984
+ /** @type {function} */
985
+ each: {
986
+ value: function value(object, callback) {
987
+ if (isString(object)) {
988
+ object = this.get(object, []);
989
+ }
990
+ each(object, callback);
991
+ },
992
+ writable: true
993
+ },
994
+ /** @type {function} */
995
+ el: {
996
+ value: function value(tag, attr, content) {
997
+ content = isFunction(content) ? this.fn(content)() : content;
998
+ this.echo(Promise.resolve(content).then(function (content) {
999
+ return element(tag, attr, content);
1000
+ }));
1001
+ },
1002
+ writable: true
1003
+ },
1004
+ /** @type {function} */
1005
+ ui: {
1006
+ value: function value(layout) {},
1007
+ writable: true
1008
+ }
1009
+ });
927
1010
  return ContextScope;
928
1011
  };
929
1012
  var _scope = /*#__PURE__*/new WeakMap();
@@ -991,21 +1074,43 @@ var EJS = /*#__PURE__*/function () {
991
1074
  _classPrivateFieldGet2(_template, this).configure(_classPrivateFieldGet2(_config, this));
992
1075
  return _classPrivateFieldGet2(_config, this);
993
1076
  }
1077
+ }, {
1078
+ key: "filePath",
1079
+ value: function filePath(name) {
1080
+ return ext(name, _classPrivateFieldGet2(_config, this).extension);
1081
+ }
1082
+ }, {
1083
+ key: "require",
1084
+ value: function require(name) {
1085
+ var scope = this.context({});
1086
+ return _assertClassBrand(_EJS_brand, this, _output).call(this, this.filePath(name), scope).then(function () {
1087
+ return scope.getMacro();
1088
+ });
1089
+ }
994
1090
  }, {
995
1091
  key: "render",
996
1092
  value: function render(name, data) {
997
- var _this = this;
998
- var filepath = ext(name, _classPrivateFieldGet2(_config, this).extension);
999
1093
  var scope = this.context(data);
1000
- return _assertClassBrand(_EJS_brand, this, _output).call(this, filepath, scope).then(function (content) {
1094
+ return _assertClassBrand(_EJS_brand, this, _output).call(this, this.filePath(name), scope).then(this.outputContent(name, scope));
1095
+ }
1096
+ }, {
1097
+ key: "outputContent",
1098
+ value: function outputContent(name, scope) {
1099
+ var _this = this;
1100
+ return function (content) {
1001
1101
  if (scope.getExtend()) {
1002
1102
  scope.setExtend(false);
1003
- var layout = scope.getLayout();
1004
- var _data = scope.clone();
1005
- return _this.render(layout, _data);
1103
+ return _this.renderLayout(scope.getLayout(), scope, name);
1006
1104
  }
1007
1105
  return content;
1008
- });
1106
+ };
1107
+ }
1108
+ }, {
1109
+ key: "renderLayout",
1110
+ value: function renderLayout(name, data, parent) {
1111
+ var scope = this.context(data);
1112
+ if (parent) scope.setParentTemplate(parent);
1113
+ return _assertClassBrand(_EJS_brand, this, _output).call(this, this.filePath(name), scope).then(this.outputContent(name, scope));
1009
1114
  }
1010
1115
  }, {
1011
1116
  key: "helpers",
@@ -1032,15 +1137,6 @@ var EJS = /*#__PURE__*/function () {
1032
1137
  value: function create(options) {
1033
1138
  return new this.constructor(options);
1034
1139
  }
1035
- }, {
1036
- key: "require",
1037
- value: function require(name) {
1038
- var filepath = ext(name, _classPrivateFieldGet2(_config, this).extension);
1039
- var scope = this.context({});
1040
- return _assertClassBrand(_EJS_brand, this, _output).call(this, filepath, scope).then(function () {
1041
- return scope.getMacro();
1042
- });
1043
- }
1044
1140
  }]);
1045
1141
  }();
1046
1142
  function _output(path, scope) {