@kosatyi/ejs 0.0.100 → 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.
@@ -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,52 @@ 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
  },
703
+
678
704
  /** @type {function} */
679
705
  setExtend: {
680
706
  value(value) {
681
707
  this[EXTEND] = value;
682
- },
708
+ return this
709
+ }
683
710
  },
684
711
  /** @type {function} */
685
712
  getExtend: {
686
713
  value() {
687
714
  return this[EXTEND]
688
- },
715
+ }
689
716
  },
690
717
  /** @type {function} */
691
718
  setLayout: {
692
719
  value(layout) {
693
720
  this[LAYOUT] = layout;
694
- },
721
+ return this
722
+ }
695
723
  },
696
724
  /** @type {function} */
697
725
  getLayout: {
698
726
  value() {
699
727
  return this[LAYOUT]
700
- },
728
+ }
701
729
  },
702
730
  /** @type {function} */
703
731
  clone: {
@@ -707,14 +735,14 @@ const createContextScope = (config, methods) => {
707
735
  filter.push(BLOCKS);
708
736
  }
709
737
  return omit(this, filter)
710
- },
738
+ }
711
739
  },
712
740
  /** @type {function} */
713
741
  extend: {
714
742
  value(layout) {
715
743
  this.setExtend(true);
716
744
  this.setLayout(layout);
717
- },
745
+ }
718
746
  },
719
747
  /** @type {function} */
720
748
  echo: {
@@ -722,21 +750,21 @@ const createContextScope = (config, methods) => {
722
750
  const buffer = this.getBuffer();
723
751
  const params = [].slice.call(arguments);
724
752
  params.forEach(buffer);
725
- },
753
+ }
726
754
  },
727
755
  /** @type {function} */
728
756
  fn: {
729
757
  value(callback) {
730
758
  const buffer = this.getBuffer();
731
759
  const context = this;
732
- return function () {
760
+ return function() {
733
761
  if (isFunction(callback)) {
734
762
  buffer.backup();
735
763
  buffer(callback.apply(context, arguments));
736
764
  return buffer.restore()
737
765
  }
738
766
  }
739
- },
767
+ }
740
768
  },
741
769
  /** @type {function} */
742
770
  macro: {
@@ -744,10 +772,10 @@ const createContextScope = (config, methods) => {
744
772
  const list = this.getMacro();
745
773
  const macro = this.fn(callback);
746
774
  const context = this;
747
- list[name] = function () {
775
+ list[name] = function() {
748
776
  return context.echo(macro.apply(undefined, arguments))
749
777
  };
750
- },
778
+ }
751
779
  },
752
780
  /** @type {function} */
753
781
  call: {
@@ -758,7 +786,7 @@ const createContextScope = (config, methods) => {
758
786
  if (isFunction(macro)) {
759
787
  return macro.apply(macro, params)
760
788
  }
761
- },
789
+ }
762
790
  },
763
791
  /** @type {function} */
764
792
  block: {
@@ -782,13 +810,13 @@ const createContextScope = (config, methods) => {
782
810
  }
783
811
  };
784
812
  this.echo(current()(next()));
785
- },
813
+ }
786
814
  },
787
815
  /** @type {function} */
788
816
  hasBlock: {
789
817
  value(name) {
790
818
  return this.getBlocks().hasOwnProperty(name)
791
- },
819
+ }
792
820
  },
793
821
  /** @type {function} */
794
822
  include: {
@@ -797,7 +825,7 @@ const createContextScope = (config, methods) => {
797
825
  const params = extend(context, data || {});
798
826
  const promise = this.render(path, params);
799
827
  this.echo(promise);
800
- },
828
+ }
801
829
  },
802
830
  /** @type {function} */
803
831
  use: {
@@ -805,18 +833,18 @@ const createContextScope = (config, methods) => {
805
833
  this.echo(
806
834
  Promise.resolve(this.require(path)).then((exports) => {
807
835
  const list = this.getMacro();
808
- each(exports, function (macro, name) {
836
+ each(exports, function(macro, name) {
809
837
  list[[namespace, name].join('.')] = macro;
810
838
  });
811
839
  })
812
840
  );
813
- },
841
+ }
814
842
  },
815
843
  /** @type {function} */
816
844
  async: {
817
845
  value(promise, callback) {
818
846
  this.echo(Promise.resolve(promise).then(callback));
819
- },
847
+ }
820
848
  },
821
849
  /** @type {function} */
822
850
  get: {
@@ -825,29 +853,29 @@ const createContextScope = (config, methods) => {
825
853
  const result = path.shift();
826
854
  const prop = path.pop();
827
855
  return hasProp(result, prop) ? result[prop] : defaults
828
- },
856
+ }
829
857
  },
830
858
  /** @type {function} */
831
859
  set: {
832
860
  value(name, value) {
833
861
  const path = getPath(this, name, false);
834
- const result = path.shift();
862
+ const result= path.shift();
835
863
  const prop = path.pop();
836
- if (this.getExtend() && hasProp(result, prop)) {
864
+ if (this.getParentTemplate() && hasProp(result, prop)) {
837
865
  return result[prop]
838
866
  }
839
867
  return (result[prop] = value)
840
- },
868
+ }
841
869
  },
842
870
  /** @type {function} */
843
871
  each: {
844
- value: function (object, callback) {
872
+ value: function(object, callback) {
845
873
  if (isString(object)) {
846
874
  object = this.get(object, []);
847
875
  }
848
876
  each(object, callback);
849
877
  },
850
- writable: true,
878
+ writable: true
851
879
  },
852
880
  /** @type {function} */
853
881
  el: {
@@ -859,28 +887,33 @@ const createContextScope = (config, methods) => {
859
887
  )
860
888
  );
861
889
  },
862
- writable: true,
890
+ writable: true
863
891
  },
864
892
  /** @type {function} */
865
893
  ui: {
866
- value(layout) {},
867
- writable: true,
868
- },
894
+ value(layout) {
895
+ },
896
+ writable: true
897
+ }
869
898
  });
870
899
  return ContextScope
871
900
  };
872
901
 
873
902
  class Context {
874
903
  #scope
904
+
875
905
  constructor(config, methods) {
876
906
  this.configure(config, methods);
877
907
  }
908
+
878
909
  create(data) {
879
910
  return new this.#scope(data)
880
911
  }
912
+
881
913
  configure(config, methods) {
882
914
  this.#scope = createContextScope(config, methods);
883
915
  }
916
+
884
917
  helpers(methods) {
885
918
  extend(this.#scope.prototype, methods || {});
886
919
  }
@@ -893,6 +926,7 @@ class EJS {
893
926
  #compiler
894
927
  #cache
895
928
  #template
929
+
896
930
  constructor(options) {
897
931
  configSchema(this.#config, options || {});
898
932
  this.#context = new Context(this.#config, this.#extend);
@@ -900,19 +934,11 @@ class EJS {
900
934
  this.#cache = new Cache(this.#config);
901
935
  this.#template = new Template(this.#config, this.#cache, this.#compiler);
902
936
  //
903
- bindContext(this, [
904
- 'configure',
905
- 'create',
906
- 'render',
907
- 'require',
908
- 'context',
909
- 'preload',
910
- 'compile',
911
- 'helpers',
912
- ]);
937
+ bindContext(this, ['configure', 'create', 'render', 'require', 'context', 'preload', 'compile', 'helpers']);
913
938
  //
914
939
  this.helpers({ require: this.require, render: this.render });
915
940
  }
941
+
916
942
  configure(options) {
917
943
  configSchema(this.#config, options || {});
918
944
  this.#context.configure(this.#config, this.#extend);
@@ -921,18 +947,30 @@ class EJS {
921
947
  this.#template.configure(this.#config);
922
948
  return this.#config
923
949
  }
950
+ filePath(name) {
951
+ return ext(name, this.#config.extension)
952
+ }
953
+ require(name) {
954
+ const scope = this.context({});
955
+ return this.#output(this.filePath(name), scope).then(() => scope.getMacro())
956
+ }
924
957
  render(name, data) {
925
- const filepath = ext(name, this.#config.extension);
926
958
  const scope = this.context(data);
927
- return this.#output(filepath, scope).then((content) => {
959
+ return this.#output(this.filePath(name), scope).then(this.outputContent(name,scope))
960
+ }
961
+ outputContent(name,scope){
962
+ return (content) => {
928
963
  if (scope.getExtend()) {
929
964
  scope.setExtend(false);
930
- const layout = scope.getLayout();
931
- const data = scope.clone();
932
- return this.render(layout, data)
965
+ return this.renderLayout(scope.getLayout(),scope,name)
933
966
  }
934
967
  return content
935
- })
968
+ }
969
+ }
970
+ renderLayout(name, data, parent) {
971
+ const scope = this.context(data);
972
+ if (parent) scope.setParentTemplate(parent);
973
+ return this.#output(this.filePath(name), scope).then(this.outputContent(name,scope))
936
974
  }
937
975
  helpers(methods) {
938
976
  this.#context.helpers(extend(this.#extend, methods));
@@ -943,30 +981,21 @@ class EJS {
943
981
  compile(content, path) {
944
982
  return this.#compiler.compile(content, path)
945
983
  }
984
+
946
985
  preload(list) {
947
986
  return this.#cache.load(list || {})
948
987
  }
988
+
949
989
  create(options) {
950
990
  return new this.constructor(options)
951
991
  }
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
- }
992
+
993
+
957
994
  #output(path, scope) {
958
995
  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
- );
996
+ const params = [scope, scope.getBuffer(), scope.useSafeValue, scope.useComponent, scope.useElement].concat(globalHelpers
997
+ .filter((name) => isFunction(scope[name]))
998
+ .map((name) => scope[name].bind(scope)));
970
999
  return this.#template
971
1000
  .get(path)
972
1001
  .then((callback) => callback.apply(scope, params))