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