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