@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.
@@ -1022,8 +1022,8 @@ function createBuffer() {
1022
1022
  return buffer;
1023
1023
  }
1024
1024
 
1025
+ var PARENT = Symbol('ContextScope.parentTemplate');
1025
1026
  var createContextScope = function createContextScope(config, methods) {
1026
- var _Object$definePropert;
1027
1027
  var _config$vars = config.vars,
1028
1028
  BLOCKS = _config$vars.BLOCKS,
1029
1029
  MACRO = _config$vars.MACRO,
@@ -1034,217 +1034,300 @@ var createContextScope = function createContextScope(config, methods) {
1034
1034
  SCOPE = _config$vars.SCOPE,
1035
1035
  COMPONENT = _config$vars.COMPONENT,
1036
1036
  ELEMENT = _config$vars.ELEMENT;
1037
+ /**
1038
+ *
1039
+ * @type {symbol}
1040
+ */
1041
+
1037
1042
  /**
1038
1043
  * @name ContextScope
1039
1044
  * @param data
1040
1045
  * @constructor
1041
1046
  */
1042
1047
  function ContextScope(data) {
1048
+ this[PARENT] = null;
1043
1049
  this[BLOCKS] = {};
1044
1050
  this[MACRO] = {};
1045
1051
  Object.assign(this, omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT]));
1046
1052
  }
1047
1053
  Object.assign(ContextScope.prototype, methods);
1048
- Object.defineProperties(ContextScope.prototype, (_Object$definePropert = {}, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_Object$definePropert, BUFFER, {
1054
+ Object.defineProperty(ContextScope.prototype, BUFFER, {
1049
1055
  value: createBuffer()
1050
- }), BLOCKS, {
1056
+ });
1057
+ Object.defineProperty(ContextScope.prototype, BLOCKS, {
1051
1058
  value: {},
1052
1059
  writable: true
1053
- }), MACRO, {
1060
+ });
1061
+ Object.defineProperty(ContextScope.prototype, MACRO, {
1054
1062
  value: {},
1055
1063
  writable: true
1056
- }), LAYOUT, {
1064
+ });
1065
+ Object.defineProperty(ContextScope.prototype, LAYOUT, {
1057
1066
  value: false,
1058
1067
  writable: true
1059
- }), EXTEND, {
1068
+ });
1069
+ Object.defineProperty(ContextScope.prototype, EXTEND, {
1060
1070
  value: false,
1061
1071
  writable: true
1062
- }), "useSafeValue", {
1063
- get: function get() {
1064
- return safeValue;
1065
- }
1066
- }), "useComponent", {
1067
- get: function get() {
1068
- if (isFunction(this[COMPONENT])) {
1069
- return this[COMPONENT].bind(this);
1070
- } else {
1071
- return function () {
1072
- throw new Error("".concat(COMPONENT, " must be a function"));
1073
- };
1072
+ });
1073
+ Object.defineProperty(ContextScope.prototype, PARENT, {
1074
+ value: null,
1075
+ writable: true
1076
+ });
1077
+ Object.defineProperties(ContextScope.prototype, {
1078
+ /** @type {function} */
1079
+ setParentTemplate: {
1080
+ value: function value(_value) {
1081
+ this[PARENT] = _value;
1082
+ return this;
1074
1083
  }
1075
- }
1076
- }), "useElement", {
1077
- get: function get() {
1078
- if (isFunction(this[ELEMENT])) {
1079
- return this[ELEMENT].bind(this);
1080
- } else {
1081
- return function () {
1082
- throw new Error("".concat(ELEMENT, " must be a function"));
1083
- };
1084
+ },
1085
+ /** @type {function} */
1086
+ getParentTemplate: {
1087
+ value: function value() {
1088
+ return this[PARENT];
1084
1089
  }
1085
- }
1086
- }), "getMacro", {
1087
- value: function value() {
1088
- return this[MACRO];
1089
- }
1090
- }), "getBuffer", {
1091
- value: function value() {
1092
- return this[BUFFER];
1093
- }
1094
- }), _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_Object$definePropert, "getBlocks", {
1095
- value: function value() {
1096
- return this[BLOCKS];
1097
- }
1098
- }), "setExtend", {
1099
- value: function value(_value) {
1100
- this[EXTEND] = _value;
1101
- }
1102
- }), "getExtend", {
1103
- value: function value() {
1104
- return this[EXTEND];
1105
- }
1106
- }), "setLayout", {
1107
- value: function value(layout) {
1108
- this[LAYOUT] = layout;
1109
- }
1110
- }), "getLayout", {
1111
- value: function value() {
1112
- return this[LAYOUT];
1113
- }
1114
- }), "clone", {
1115
- value: function value(exclude_blocks) {
1116
- var filter = [LAYOUT, EXTEND, BUFFER];
1117
- if (exclude_blocks === true) {
1118
- filter.push(BLOCKS);
1090
+ },
1091
+ /** @type {function} */
1092
+ useSafeValue: {
1093
+ get: function get() {
1094
+ return safeValue;
1119
1095
  }
1120
- return omit(this, filter);
1121
- }
1122
- }), "extend", {
1123
- value: function value(layout) {
1124
- this.setExtend(true);
1125
- this.setLayout(layout);
1126
- }
1127
- }), "echo", {
1128
- value: function value(layout) {
1129
- var buffer = this.getBuffer();
1130
- var params = [].slice.call(arguments);
1131
- params.forEach(buffer);
1132
- }
1133
- }), "fn", {
1134
- value: function value(callback) {
1135
- var buffer = this.getBuffer();
1136
- var context = this;
1137
- return function () {
1138
- if (isFunction(callback)) {
1139
- buffer.backup();
1140
- buffer(callback.apply(context, arguments));
1141
- return buffer.restore();
1096
+ },
1097
+ /** @type {function} */
1098
+ useComponent: {
1099
+ get: function get() {
1100
+ if (isFunction(this[COMPONENT])) {
1101
+ return this[COMPONENT].bind(this);
1102
+ } else {
1103
+ return function () {
1104
+ throw new Error("".concat(COMPONENT, " must be a function"));
1105
+ };
1142
1106
  }
1143
- };
1144
- }
1145
- }), "macro", {
1146
- value: function value(name, callback) {
1147
- var list = this.getMacro();
1148
- var macro = this.fn(callback);
1149
- var context = this;
1150
- list[name] = function () {
1151
- return context.echo(macro.apply(undefined, arguments));
1152
- };
1153
- }
1154
- }), _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_Object$definePropert, "call", {
1155
- value: function value(name) {
1156
- var list = this.getMacro();
1157
- var macro = list[name];
1158
- var params = [].slice.call(arguments, 1);
1159
- if (isFunction(macro)) {
1160
- return macro.apply(macro, params);
1161
1107
  }
1162
- }
1163
- }), "block", {
1164
- value: function value(name, callback) {
1165
- var _this = this;
1166
- var blocks = this.getBlocks();
1167
- blocks[name] = blocks[name] || [];
1168
- blocks[name].push(this.fn(callback));
1169
- if (this.getExtend()) return;
1170
- var list = Object.assign([], blocks[name]);
1171
- var current = function current() {
1172
- return list.shift();
1173
- };
1174
- var _next = function next() {
1175
- var parent = current();
1176
- if (parent) {
1108
+ },
1109
+ /** @type {function} */
1110
+ useElement: {
1111
+ get: function get() {
1112
+ if (isFunction(this[ELEMENT])) {
1113
+ return this[ELEMENT].bind(this);
1114
+ } else {
1177
1115
  return function () {
1178
- _this.echo(parent(_next()));
1116
+ throw new Error("".concat(ELEMENT, " must be a function"));
1179
1117
  };
1180
- } else {
1181
- return noop;
1182
1118
  }
1183
- };
1184
- this.echo(current()(_next()));
1185
- }
1186
- }), "hasBlock", {
1187
- value: function value(name) {
1188
- return this.getBlocks().hasOwnProperty(name);
1189
- }
1190
- }), "include", {
1191
- value: function value(path, data, cx) {
1192
- var context = cx === false ? {} : this.clone(true);
1193
- var params = extend(context, data || {});
1194
- var promise = this.render(path, params);
1195
- this.echo(promise);
1196
- }
1197
- }), "use", {
1198
- value: function value(path, namespace) {
1199
- var _this2 = this;
1200
- this.echo(Promise.resolve(this.require(path)).then(function (exports) {
1201
- var list = _this2.getMacro();
1202
- each(exports, function (macro, name) {
1203
- list[[namespace, name].join('.')] = macro;
1204
- });
1205
- }));
1206
- }
1207
- }), "async", {
1208
- value: function value(promise, callback) {
1209
- this.echo(Promise.resolve(promise).then(callback));
1210
- }
1211
- }), "get", {
1212
- value: function value(name, defaults) {
1213
- var path = getPath(this, name, true);
1214
- var result = path.shift();
1215
- var prop = path.pop();
1216
- return hasProp(result, prop) ? result[prop] : defaults;
1217
- }
1218
- }), "set", {
1219
- value: function value(name, _value2) {
1220
- var path = getPath(this, name, false);
1221
- var result = path.shift();
1222
- var prop = path.pop();
1223
- if (this.getExtend() && hasProp(result, prop)) {
1224
- return result[prop];
1225
1119
  }
1226
- return result[prop] = _value2;
1227
- }
1228
- }), "each", {
1229
- value: function value(object, callback) {
1230
- if (isString(object)) {
1231
- object = this.get(object, []);
1120
+ },
1121
+ /** @type {()=>this[MACRO]} */
1122
+ getMacro: {
1123
+ value: function value() {
1124
+ return this[MACRO];
1232
1125
  }
1233
- each(object, callback);
1234
1126
  },
1235
- writable: true
1236
- }), "el", {
1237
- value: function value(tag, attr, content) {
1238
- content = isFunction(content) ? this.fn(content)() : content;
1239
- this.echo(Promise.resolve(content).then(function (content) {
1240
- return element(tag, attr, content);
1241
- }));
1127
+ /** @type {function} */
1128
+ getBuffer: {
1129
+ value: function value() {
1130
+ return this[BUFFER];
1131
+ }
1242
1132
  },
1243
- writable: true
1244
- }), _defineProperty(_Object$definePropert, "ui", {
1245
- value: function value(layout) {},
1246
- writable: true
1247
- })));
1133
+ /** @type {function} */
1134
+ getBlocks: {
1135
+ value: function value() {
1136
+ return this[BLOCKS];
1137
+ }
1138
+ },
1139
+ /** @type {function} */
1140
+ setExtend: {
1141
+ value: function value(_value2) {
1142
+ this[EXTEND] = _value2;
1143
+ return this;
1144
+ }
1145
+ },
1146
+ /** @type {function} */
1147
+ getExtend: {
1148
+ value: function value() {
1149
+ return this[EXTEND];
1150
+ }
1151
+ },
1152
+ /** @type {function} */
1153
+ setLayout: {
1154
+ value: function value(layout) {
1155
+ this[LAYOUT] = layout;
1156
+ return this;
1157
+ }
1158
+ },
1159
+ /** @type {function} */
1160
+ getLayout: {
1161
+ value: function value() {
1162
+ return this[LAYOUT];
1163
+ }
1164
+ },
1165
+ /** @type {function} */
1166
+ clone: {
1167
+ value: function value(exclude_blocks) {
1168
+ var filter = [LAYOUT, EXTEND, BUFFER];
1169
+ if (exclude_blocks === true) {
1170
+ filter.push(BLOCKS);
1171
+ }
1172
+ return omit(this, filter);
1173
+ }
1174
+ },
1175
+ /** @type {function} */
1176
+ extend: {
1177
+ value: function value(layout) {
1178
+ this.setExtend(true);
1179
+ this.setLayout(layout);
1180
+ }
1181
+ },
1182
+ /** @type {function} */
1183
+ echo: {
1184
+ value: function value(layout) {
1185
+ var buffer = this.getBuffer();
1186
+ var params = [].slice.call(arguments);
1187
+ params.forEach(buffer);
1188
+ }
1189
+ },
1190
+ /** @type {function} */
1191
+ fn: {
1192
+ value: function value(callback) {
1193
+ var buffer = this.getBuffer();
1194
+ var context = this;
1195
+ return function () {
1196
+ if (isFunction(callback)) {
1197
+ buffer.backup();
1198
+ buffer(callback.apply(context, arguments));
1199
+ return buffer.restore();
1200
+ }
1201
+ };
1202
+ }
1203
+ },
1204
+ /** @type {function} */
1205
+ macro: {
1206
+ value: function value(name, callback) {
1207
+ var list = this.getMacro();
1208
+ var macro = this.fn(callback);
1209
+ var context = this;
1210
+ list[name] = function () {
1211
+ return context.echo(macro.apply(undefined, arguments));
1212
+ };
1213
+ }
1214
+ },
1215
+ /** @type {function} */
1216
+ call: {
1217
+ value: function value(name) {
1218
+ var list = this.getMacro();
1219
+ var macro = list[name];
1220
+ var params = [].slice.call(arguments, 1);
1221
+ if (isFunction(macro)) {
1222
+ return macro.apply(macro, params);
1223
+ }
1224
+ }
1225
+ },
1226
+ /** @type {function} */
1227
+ block: {
1228
+ value: function value(name, callback) {
1229
+ var _this = this;
1230
+ var blocks = this.getBlocks();
1231
+ blocks[name] = blocks[name] || [];
1232
+ blocks[name].push(this.fn(callback));
1233
+ if (this.getExtend()) return;
1234
+ var list = Object.assign([], blocks[name]);
1235
+ var current = function current() {
1236
+ return list.shift();
1237
+ };
1238
+ var _next = function next() {
1239
+ var parent = current();
1240
+ if (parent) {
1241
+ return function () {
1242
+ _this.echo(parent(_next()));
1243
+ };
1244
+ } else {
1245
+ return noop;
1246
+ }
1247
+ };
1248
+ this.echo(current()(_next()));
1249
+ }
1250
+ },
1251
+ /** @type {function} */
1252
+ hasBlock: {
1253
+ value: function value(name) {
1254
+ return this.getBlocks().hasOwnProperty(name);
1255
+ }
1256
+ },
1257
+ /** @type {function} */
1258
+ include: {
1259
+ value: function value(path, data, cx) {
1260
+ var context = cx === false ? {} : this.clone(true);
1261
+ var params = extend(context, data || {});
1262
+ var promise = this.render(path, params);
1263
+ this.echo(promise);
1264
+ }
1265
+ },
1266
+ /** @type {function} */
1267
+ use: {
1268
+ value: function value(path, namespace) {
1269
+ var _this2 = this;
1270
+ this.echo(Promise.resolve(this.require(path)).then(function (exports) {
1271
+ var list = _this2.getMacro();
1272
+ each(exports, function (macro, name) {
1273
+ list[[namespace, name].join('.')] = macro;
1274
+ });
1275
+ }));
1276
+ }
1277
+ },
1278
+ /** @type {function} */
1279
+ async: {
1280
+ value: function value(promise, callback) {
1281
+ this.echo(Promise.resolve(promise).then(callback));
1282
+ }
1283
+ },
1284
+ /** @type {function} */
1285
+ get: {
1286
+ value: function value(name, defaults) {
1287
+ var path = getPath(this, name, true);
1288
+ var result = path.shift();
1289
+ var prop = path.pop();
1290
+ return hasProp(result, prop) ? result[prop] : defaults;
1291
+ }
1292
+ },
1293
+ /** @type {function} */
1294
+ set: {
1295
+ value: function value(name, _value3) {
1296
+ var path = getPath(this, name, false);
1297
+ var result = path.shift();
1298
+ var prop = path.pop();
1299
+ if (this.getParentTemplate() && hasProp(result, prop)) {
1300
+ return result[prop];
1301
+ }
1302
+ return result[prop] = _value3;
1303
+ }
1304
+ },
1305
+ /** @type {function} */
1306
+ each: {
1307
+ value: function value(object, callback) {
1308
+ if (isString(object)) {
1309
+ object = this.get(object, []);
1310
+ }
1311
+ each(object, callback);
1312
+ },
1313
+ writable: true
1314
+ },
1315
+ /** @type {function} */
1316
+ el: {
1317
+ value: function value(tag, attr, content) {
1318
+ content = isFunction(content) ? this.fn(content)() : content;
1319
+ this.echo(Promise.resolve(content).then(function (content) {
1320
+ return element(tag, attr, content);
1321
+ }));
1322
+ },
1323
+ writable: true
1324
+ },
1325
+ /** @type {function} */
1326
+ ui: {
1327
+ value: function value(layout) {},
1328
+ writable: true
1329
+ }
1330
+ });
1248
1331
  return ContextScope;
1249
1332
  };
1250
1333
  var _scope = /*#__PURE__*/new WeakMap();
@@ -1312,21 +1395,43 @@ var EJS = /*#__PURE__*/function () {
1312
1395
  _classPrivateFieldGet2(_template, this).configure(_classPrivateFieldGet2(_config, this));
1313
1396
  return _classPrivateFieldGet2(_config, this);
1314
1397
  }
1398
+ }, {
1399
+ key: "filePath",
1400
+ value: function filePath(name) {
1401
+ return ext(name, _classPrivateFieldGet2(_config, this).extension);
1402
+ }
1403
+ }, {
1404
+ key: "require",
1405
+ value: function require(name) {
1406
+ var scope = this.context({});
1407
+ return _assertClassBrand(_EJS_brand, this, _output).call(this, this.filePath(name), scope).then(function () {
1408
+ return scope.getMacro();
1409
+ });
1410
+ }
1315
1411
  }, {
1316
1412
  key: "render",
1317
1413
  value: function render(name, data) {
1318
- var _this = this;
1319
- var filepath = ext(name, _classPrivateFieldGet2(_config, this).extension);
1320
1414
  var scope = this.context(data);
1321
- return _assertClassBrand(_EJS_brand, this, _output).call(this, filepath, scope).then(function (content) {
1415
+ return _assertClassBrand(_EJS_brand, this, _output).call(this, this.filePath(name), scope).then(this.outputContent(name, scope));
1416
+ }
1417
+ }, {
1418
+ key: "outputContent",
1419
+ value: function outputContent(name, scope) {
1420
+ var _this = this;
1421
+ return function (content) {
1322
1422
  if (scope.getExtend()) {
1323
1423
  scope.setExtend(false);
1324
- var layout = scope.getLayout();
1325
- var _data = scope.clone();
1326
- return _this.render(layout, _data);
1424
+ return _this.renderLayout(scope.getLayout(), scope, name);
1327
1425
  }
1328
1426
  return content;
1329
- });
1427
+ };
1428
+ }
1429
+ }, {
1430
+ key: "renderLayout",
1431
+ value: function renderLayout(name, data, parent) {
1432
+ var scope = this.context(data);
1433
+ if (parent) scope.setParentTemplate(parent);
1434
+ return _assertClassBrand(_EJS_brand, this, _output).call(this, this.filePath(name), scope).then(this.outputContent(name, scope));
1330
1435
  }
1331
1436
  }, {
1332
1437
  key: "helpers",
@@ -1353,15 +1458,6 @@ var EJS = /*#__PURE__*/function () {
1353
1458
  value: function create(options) {
1354
1459
  return new this.constructor(options);
1355
1460
  }
1356
- }, {
1357
- key: "require",
1358
- value: function require(name) {
1359
- var filepath = ext(name, _classPrivateFieldGet2(_config, this).extension);
1360
- var scope = this.context({});
1361
- return _assertClassBrand(_EJS_brand, this, _output).call(this, filepath, scope).then(function () {
1362
- return scope.getMacro();
1363
- });
1364
- }
1365
1461
  }]);
1366
1462
  }();
1367
1463
  function _output(path, scope) {