@kosatyi/ejs 0.0.100 → 0.0.102

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.
@@ -409,7 +409,7 @@ class Compiler {
409
409
  source = `${BUFFER}.start();${source}return ${BUFFER}.end();`;
410
410
  source += `\n//# sourceURL=${path}`;
411
411
  let result = null;
412
- let params = [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT].concat(GLOBALS);
412
+ let params = [SCOPE, COMPONENT, ELEMENT, BUFFER, SAFE].concat(GLOBALS);
413
413
  try {
414
414
  result = Function.apply(null, params.concat(source));
415
415
  result.source = `(function(${params.join(',')}){\n${source}\n});`;
@@ -577,6 +577,8 @@ function createBuffer() {
577
577
  return buffer
578
578
  }
579
579
 
580
+ const PARENT = Symbol('ContextScope.parentTemplate');
581
+
580
582
  const createContextScope = (config, methods) => {
581
583
  const {
582
584
  BLOCKS,
@@ -587,14 +589,20 @@ const createContextScope = (config, methods) => {
587
589
  SAFE,
588
590
  SCOPE,
589
591
  COMPONENT,
590
- ELEMENT,
592
+ ELEMENT
591
593
  } = config.vars;
594
+ /**
595
+ *
596
+ * @type {symbol}
597
+ */
598
+
592
599
  /**
593
600
  * @name ContextScope
594
601
  * @param data
595
602
  * @constructor
596
603
  */
597
604
  function ContextScope(data) {
605
+ this[PARENT] = null;
598
606
  this[BLOCKS] = {};
599
607
  this[MACRO] = {};
600
608
  Object.assign(
@@ -604,28 +612,45 @@ const createContextScope = (config, methods) => {
604
612
  }
605
613
  Object.assign(ContextScope.prototype, methods);
606
614
  Object.defineProperty(ContextScope.prototype, BUFFER, {
607
- value: createBuffer(),
615
+ value: createBuffer()
608
616
  });
609
617
  Object.defineProperty(ContextScope.prototype, BLOCKS, {
610
618
  value: {},
611
- writable: true,
619
+ writable: true
612
620
  });
613
621
  Object.defineProperty(ContextScope.prototype, MACRO, {
614
622
  value: {},
615
- writable: true,
623
+ writable: true
616
624
  });
617
625
  Object.defineProperty(ContextScope.prototype, LAYOUT, {
618
626
  value: false,
619
- writable: true,
627
+ writable: true
620
628
  });
621
629
  Object.defineProperty(ContextScope.prototype, EXTEND, {
622
630
  value: false,
623
- writable: true,
631
+ writable: true
632
+ });
633
+ Object.defineProperty(ContextScope.prototype, PARENT, {
634
+ value: null,
635
+ writable: true
624
636
  });
625
637
  Object.defineProperties(ContextScope.prototype, {
638
+ /** @type {function} */
639
+ setParentTemplate:{
640
+ value(value){
641
+ this[PARENT] = value;
642
+ return this
643
+ }
644
+ },
645
+ /** @type {function} */
646
+ getParentTemplate: {
647
+ value(){
648
+ return this[PARENT]
649
+ }
650
+ },
626
651
  /** @type {function} */
627
652
  useSafeValue: {
628
- get: () => safeValue,
653
+ get: () => safeValue
629
654
  },
630
655
  /** @type {function} */
631
656
  useComponent: {
@@ -637,7 +662,7 @@ const createContextScope = (config, methods) => {
637
662
  throw new Error(`${COMPONENT} must be a function`)
638
663
  }
639
664
  }
640
- },
665
+ }
641
666
  },
642
667
  /** @type {function} */
643
668
  useElement: {
@@ -649,49 +674,51 @@ const createContextScope = (config, methods) => {
649
674
  throw new Error(`${ELEMENT} must be a function`)
650
675
  }
651
676
  }
652
- },
677
+ }
653
678
  },
654
679
  /** @type {()=>this[MACRO]} */
655
680
  getMacro: {
656
681
  value() {
657
682
  return this[MACRO]
658
- },
683
+ }
659
684
  },
660
685
  /** @type {function} */
661
686
  getBuffer: {
662
687
  value() {
663
688
  return this[BUFFER]
664
- },
689
+ }
665
690
  },
666
691
  /** @type {function} */
667
692
  getBlocks: {
668
693
  value() {
669
694
  return this[BLOCKS]
670
- },
695
+ }
671
696
  },
672
697
  /** @type {function} */
673
698
  setExtend: {
674
699
  value(value) {
675
700
  this[EXTEND] = value;
676
- },
701
+ return this
702
+ }
677
703
  },
678
704
  /** @type {function} */
679
705
  getExtend: {
680
706
  value() {
681
707
  return this[EXTEND]
682
- },
708
+ }
683
709
  },
684
710
  /** @type {function} */
685
711
  setLayout: {
686
712
  value(layout) {
687
713
  this[LAYOUT] = layout;
688
- },
714
+ return this
715
+ }
689
716
  },
690
717
  /** @type {function} */
691
718
  getLayout: {
692
719
  value() {
693
720
  return this[LAYOUT]
694
- },
721
+ }
695
722
  },
696
723
  /** @type {function} */
697
724
  clone: {
@@ -701,14 +728,14 @@ const createContextScope = (config, methods) => {
701
728
  filter.push(BLOCKS);
702
729
  }
703
730
  return omit(this, filter)
704
- },
731
+ }
705
732
  },
706
733
  /** @type {function} */
707
734
  extend: {
708
735
  value(layout) {
709
736
  this.setExtend(true);
710
737
  this.setLayout(layout);
711
- },
738
+ }
712
739
  },
713
740
  /** @type {function} */
714
741
  echo: {
@@ -716,21 +743,21 @@ const createContextScope = (config, methods) => {
716
743
  const buffer = this.getBuffer();
717
744
  const params = [].slice.call(arguments);
718
745
  params.forEach(buffer);
719
- },
746
+ }
720
747
  },
721
748
  /** @type {function} */
722
749
  fn: {
723
750
  value(callback) {
724
751
  const buffer = this.getBuffer();
725
752
  const context = this;
726
- return function () {
753
+ return function() {
727
754
  if (isFunction(callback)) {
728
755
  buffer.backup();
729
756
  buffer(callback.apply(context, arguments));
730
757
  return buffer.restore()
731
758
  }
732
759
  }
733
- },
760
+ }
734
761
  },
735
762
  /** @type {function} */
736
763
  macro: {
@@ -738,10 +765,10 @@ const createContextScope = (config, methods) => {
738
765
  const list = this.getMacro();
739
766
  const macro = this.fn(callback);
740
767
  const context = this;
741
- list[name] = function () {
768
+ list[name] = function() {
742
769
  return context.echo(macro.apply(undefined, arguments))
743
770
  };
744
- },
771
+ }
745
772
  },
746
773
  /** @type {function} */
747
774
  call: {
@@ -752,7 +779,7 @@ const createContextScope = (config, methods) => {
752
779
  if (isFunction(macro)) {
753
780
  return macro.apply(macro, params)
754
781
  }
755
- },
782
+ }
756
783
  },
757
784
  /** @type {function} */
758
785
  block: {
@@ -776,13 +803,13 @@ const createContextScope = (config, methods) => {
776
803
  }
777
804
  };
778
805
  this.echo(current()(next()));
779
- },
806
+ }
780
807
  },
781
808
  /** @type {function} */
782
809
  hasBlock: {
783
810
  value(name) {
784
811
  return this.getBlocks().hasOwnProperty(name)
785
- },
812
+ }
786
813
  },
787
814
  /** @type {function} */
788
815
  include: {
@@ -791,7 +818,7 @@ const createContextScope = (config, methods) => {
791
818
  const params = extend(context, data || {});
792
819
  const promise = this.render(path, params);
793
820
  this.echo(promise);
794
- },
821
+ }
795
822
  },
796
823
  /** @type {function} */
797
824
  use: {
@@ -799,18 +826,18 @@ const createContextScope = (config, methods) => {
799
826
  this.echo(
800
827
  Promise.resolve(this.require(path)).then((exports) => {
801
828
  const list = this.getMacro();
802
- each(exports, function (macro, name) {
829
+ each(exports, function(macro, name) {
803
830
  list[[namespace, name].join('.')] = macro;
804
831
  });
805
832
  })
806
833
  );
807
- },
834
+ }
808
835
  },
809
836
  /** @type {function} */
810
837
  async: {
811
838
  value(promise, callback) {
812
839
  this.echo(Promise.resolve(promise).then(callback));
813
- },
840
+ }
814
841
  },
815
842
  /** @type {function} */
816
843
  get: {
@@ -819,29 +846,29 @@ const createContextScope = (config, methods) => {
819
846
  const result = path.shift();
820
847
  const prop = path.pop();
821
848
  return hasProp(result, prop) ? result[prop] : defaults
822
- },
849
+ }
823
850
  },
824
851
  /** @type {function} */
825
852
  set: {
826
853
  value(name, value) {
827
854
  const path = getPath(this, name, false);
828
- const result = path.shift();
855
+ const result= path.shift();
829
856
  const prop = path.pop();
830
- if (this.getExtend() && hasProp(result, prop)) {
857
+ if (this.getParentTemplate() && hasProp(result, prop)) {
831
858
  return result[prop]
832
859
  }
833
860
  return (result[prop] = value)
834
- },
861
+ }
835
862
  },
836
863
  /** @type {function} */
837
864
  each: {
838
- value: function (object, callback) {
865
+ value: function(object, callback) {
839
866
  if (isString(object)) {
840
867
  object = this.get(object, []);
841
868
  }
842
869
  each(object, callback);
843
870
  },
844
- writable: true,
871
+ writable: true
845
872
  },
846
873
  /** @type {function} */
847
874
  el: {
@@ -853,28 +880,33 @@ const createContextScope = (config, methods) => {
853
880
  )
854
881
  );
855
882
  },
856
- writable: true,
883
+ writable: true
857
884
  },
858
885
  /** @type {function} */
859
886
  ui: {
860
- value(layout) {},
861
- writable: true,
862
- },
887
+ value(layout) {
888
+ },
889
+ writable: true
890
+ }
863
891
  });
864
892
  return ContextScope
865
893
  };
866
894
 
867
895
  class Context {
868
896
  #scope
897
+
869
898
  constructor(config, methods) {
870
899
  this.configure(config, methods);
871
900
  }
901
+
872
902
  create(data) {
873
903
  return new this.#scope(data)
874
904
  }
905
+
875
906
  configure(config, methods) {
876
907
  this.#scope = createContextScope(config, methods);
877
908
  }
909
+
878
910
  helpers(methods) {
879
911
  extend(this.#scope.prototype, methods || {});
880
912
  }
@@ -887,6 +919,7 @@ class EJS {
887
919
  #compiler
888
920
  #cache
889
921
  #template
922
+
890
923
  constructor(options) {
891
924
  configSchema(this.#config, options || {});
892
925
  this.#context = new Context(this.#config, this.#extend);
@@ -894,19 +927,11 @@ class EJS {
894
927
  this.#cache = new Cache(this.#config);
895
928
  this.#template = new Template(this.#config, this.#cache, this.#compiler);
896
929
  //
897
- bindContext(this, [
898
- 'configure',
899
- 'create',
900
- 'render',
901
- 'require',
902
- 'context',
903
- 'preload',
904
- 'compile',
905
- 'helpers',
906
- ]);
930
+ bindContext(this, ['configure', 'create', 'render', 'require', 'context', 'preload', 'compile', 'helpers']);
907
931
  //
908
932
  this.helpers({ require: this.require, render: this.render });
909
933
  }
934
+
910
935
  configure(options) {
911
936
  configSchema(this.#config, options || {});
912
937
  this.#context.configure(this.#config, this.#extend);
@@ -915,55 +940,66 @@ class EJS {
915
940
  this.#template.configure(this.#config);
916
941
  return this.#config
917
942
  }
943
+
944
+ filePath(name) {
945
+ return ext(name, this.#config.extension)
946
+ }
947
+
948
+ require(name) {
949
+ const scope = this.context({});
950
+ return this.#output(this.filePath(name), scope).then(() => scope.getMacro())
951
+ }
952
+
918
953
  render(name, data) {
919
- const filepath = ext(name, this.#config.extension);
920
954
  const scope = this.context(data);
921
- return this.#output(filepath, scope).then((content) => {
955
+ return this.#output(this.filePath(name), scope).then(this.outputContent(name, scope))
956
+ }
957
+
958
+ outputContent(name, scope) {
959
+ return (content) => {
922
960
  if (scope.getExtend()) {
923
961
  scope.setExtend(false);
924
- const layout = scope.getLayout();
925
- const data = scope.clone();
926
- return this.render(layout, data)
962
+ return this.renderLayout(scope.getLayout(), scope, name)
927
963
  }
928
964
  return content
929
- })
965
+ }
966
+ }
967
+
968
+ renderLayout(name, data, parent) {
969
+ const scope = this.context(data);
970
+ if (parent) scope.setParentTemplate(parent);
971
+ return this.#output(this.filePath(name), scope).then(this.outputContent(name, scope))
930
972
  }
973
+
931
974
  helpers(methods) {
932
975
  this.#context.helpers(extend(this.#extend, methods));
933
976
  }
977
+
934
978
  context(data) {
935
979
  return this.#context.create(data)
936
980
  }
981
+
937
982
  compile(content, path) {
938
983
  return this.#compiler.compile(content, path)
939
984
  }
985
+
940
986
  preload(list) {
941
987
  return this.#cache.load(list || {})
942
988
  }
989
+
943
990
  create(options) {
944
991
  return new this.constructor(options)
945
992
  }
946
- require(name) {
947
- const filepath = ext(name, this.#config.extension);
948
- const scope = this.context({});
949
- return this.#output(filepath, scope).then(() => scope.getMacro())
950
- }
993
+
951
994
  #output(path, scope) {
952
995
  const { globalHelpers } = this.#config;
953
- const params = [
954
- scope,
955
- scope.getBuffer(),
956
- scope.useSafeValue,
957
- scope.useComponent,
958
- scope.useElement,
959
- ].concat(
960
- globalHelpers
961
- .filter((name) => isFunction(scope[name]))
962
- .map((name) => scope[name].bind(scope))
963
- );
996
+ const params = [scope, scope.useComponent, scope.useElement, scope.getBuffer(), scope.useSafeValue];
997
+ const globals = globalHelpers
998
+ .filter((name) => isFunction(scope[name]))
999
+ .map((name) => scope[name].bind(scope));
964
1000
  return this.#template
965
1001
  .get(path)
966
- .then((callback) => callback.apply(scope, params))
1002
+ .then((callback) => callback.apply(scope, params.concat(globals)))
967
1003
  }
968
1004
  }
969
1005
 
@@ -513,7 +513,7 @@
513
513
  source = "".concat(BUFFER, ".start();").concat(source, "return ").concat(BUFFER, ".end();");
514
514
  source += "\n//# sourceURL=".concat(path);
515
515
  var result = null;
516
- var params = [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT].concat(GLOBALS);
516
+ var params = [SCOPE, COMPONENT, ELEMENT, BUFFER, SAFE].concat(GLOBALS);
517
517
  try {
518
518
  result = Function.apply(null, params.concat(source));
519
519
  result.source = "(function(".concat(params.join(','), "){\n").concat(source, "\n});");
@@ -705,6 +705,7 @@
705
705
  return buffer;
706
706
  }
707
707
 
708
+ var PARENT = Symbol('ContextScope.parentTemplate');
708
709
  var createContextScope = function createContextScope(config, methods) {
709
710
  var _config$vars = config.vars,
710
711
  BLOCKS = _config$vars.BLOCKS,
@@ -716,12 +717,18 @@
716
717
  SCOPE = _config$vars.SCOPE,
717
718
  COMPONENT = _config$vars.COMPONENT,
718
719
  ELEMENT = _config$vars.ELEMENT;
720
+ /**
721
+ *
722
+ * @type {symbol}
723
+ */
724
+
719
725
  /**
720
726
  * @name ContextScope
721
727
  * @param data
722
728
  * @constructor
723
729
  */
724
730
  function ContextScope(data) {
731
+ this[PARENT] = null;
725
732
  this[BLOCKS] = {};
726
733
  this[MACRO] = {};
727
734
  Object.assign(this, omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT]));
@@ -746,7 +753,24 @@
746
753
  value: false,
747
754
  writable: true
748
755
  });
756
+ Object.defineProperty(ContextScope.prototype, PARENT, {
757
+ value: null,
758
+ writable: true
759
+ });
749
760
  Object.defineProperties(ContextScope.prototype, {
761
+ /** @type {function} */
762
+ setParentTemplate: {
763
+ value: function value(_value) {
764
+ this[PARENT] = _value;
765
+ return this;
766
+ }
767
+ },
768
+ /** @type {function} */
769
+ getParentTemplate: {
770
+ value: function value() {
771
+ return this[PARENT];
772
+ }
773
+ },
750
774
  /** @type {function} */
751
775
  useSafeValue: {
752
776
  get: function get() {
@@ -797,8 +821,9 @@
797
821
  },
798
822
  /** @type {function} */
799
823
  setExtend: {
800
- value: function value(_value) {
801
- this[EXTEND] = _value;
824
+ value: function value(_value2) {
825
+ this[EXTEND] = _value2;
826
+ return this;
802
827
  }
803
828
  },
804
829
  /** @type {function} */
@@ -811,6 +836,7 @@
811
836
  setLayout: {
812
837
  value: function value(layout) {
813
838
  this[LAYOUT] = layout;
839
+ return this;
814
840
  }
815
841
  },
816
842
  /** @type {function} */
@@ -949,14 +975,14 @@
949
975
  },
950
976
  /** @type {function} */
951
977
  set: {
952
- value: function value(name, _value2) {
978
+ value: function value(name, _value3) {
953
979
  var path = getPath(this, name, false);
954
980
  var result = path.shift();
955
981
  var prop = path.pop();
956
- if (this.getExtend() && hasProp(result, prop)) {
982
+ if (this.getParentTemplate() && hasProp(result, prop)) {
957
983
  return result[prop];
958
984
  }
959
- return result[prop] = _value2;
985
+ return result[prop] = _value3;
960
986
  }
961
987
  },
962
988
  /** @type {function} */
@@ -1052,21 +1078,43 @@
1052
1078
  _classPrivateFieldGet2(_template, this).configure(_classPrivateFieldGet2(_config, this));
1053
1079
  return _classPrivateFieldGet2(_config, this);
1054
1080
  }
1081
+ }, {
1082
+ key: "filePath",
1083
+ value: function filePath(name) {
1084
+ return ext(name, _classPrivateFieldGet2(_config, this).extension);
1085
+ }
1086
+ }, {
1087
+ key: "require",
1088
+ value: function require(name) {
1089
+ var scope = this.context({});
1090
+ return _assertClassBrand(_EJS_brand, this, _output).call(this, this.filePath(name), scope).then(function () {
1091
+ return scope.getMacro();
1092
+ });
1093
+ }
1055
1094
  }, {
1056
1095
  key: "render",
1057
1096
  value: function render(name, data) {
1058
- var _this = this;
1059
- var filepath = ext(name, _classPrivateFieldGet2(_config, this).extension);
1060
1097
  var scope = this.context(data);
1061
- return _assertClassBrand(_EJS_brand, this, _output).call(this, filepath, scope).then(function (content) {
1098
+ return _assertClassBrand(_EJS_brand, this, _output).call(this, this.filePath(name), scope).then(this.outputContent(name, scope));
1099
+ }
1100
+ }, {
1101
+ key: "outputContent",
1102
+ value: function outputContent(name, scope) {
1103
+ var _this = this;
1104
+ return function (content) {
1062
1105
  if (scope.getExtend()) {
1063
1106
  scope.setExtend(false);
1064
- var layout = scope.getLayout();
1065
- var _data = scope.clone();
1066
- return _this.render(layout, _data);
1107
+ return _this.renderLayout(scope.getLayout(), scope, name);
1067
1108
  }
1068
1109
  return content;
1069
- });
1110
+ };
1111
+ }
1112
+ }, {
1113
+ key: "renderLayout",
1114
+ value: function renderLayout(name, data, parent) {
1115
+ var scope = this.context(data);
1116
+ if (parent) scope.setParentTemplate(parent);
1117
+ return _assertClassBrand(_EJS_brand, this, _output).call(this, this.filePath(name), scope).then(this.outputContent(name, scope));
1070
1118
  }
1071
1119
  }, {
1072
1120
  key: "helpers",
@@ -1093,27 +1141,19 @@
1093
1141
  value: function create(options) {
1094
1142
  return new this.constructor(options);
1095
1143
  }
1096
- }, {
1097
- key: "require",
1098
- value: function require(name) {
1099
- var filepath = ext(name, _classPrivateFieldGet2(_config, this).extension);
1100
- var scope = this.context({});
1101
- return _assertClassBrand(_EJS_brand, this, _output).call(this, filepath, scope).then(function () {
1102
- return scope.getMacro();
1103
- });
1104
- }
1105
1144
  }]);
1106
1145
  }();
1107
1146
  function _output(path, scope) {
1108
1147
  var _classPrivateFieldGet2$1 = _classPrivateFieldGet2(_config, this),
1109
1148
  globalHelpers = _classPrivateFieldGet2$1.globalHelpers;
1110
- var params = [scope, scope.getBuffer(), scope.useSafeValue, scope.useComponent, scope.useElement].concat(globalHelpers.filter(function (name) {
1149
+ var params = [scope, scope.useComponent, scope.useElement, scope.getBuffer(), scope.useSafeValue];
1150
+ var globals = globalHelpers.filter(function (name) {
1111
1151
  return isFunction(scope[name]);
1112
1152
  }).map(function (name) {
1113
1153
  return scope[name].bind(scope);
1114
- }));
1154
+ });
1115
1155
  return _classPrivateFieldGet2(_template, this).get(path).then(function (callback) {
1116
- return callback.apply(scope, params);
1156
+ return callback.apply(scope, params.concat(globals));
1117
1157
  });
1118
1158
  }
1119
1159