aberdeen 1.2.0 → 1.3.0

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.
package/dist/aberdeen.js CHANGED
@@ -154,6 +154,9 @@ var lastPrio = 0;
154
154
 
155
155
  class Scope {
156
156
  prio = --lastPrio;
157
+ onChange(index) {
158
+ queue(this);
159
+ }
157
160
  remove() {
158
161
  const lastNode = this.getLastNode();
159
162
  if (lastNode)
@@ -164,7 +167,6 @@ class Scope {
164
167
 
165
168
  class ContentScope extends Scope {
166
169
  cleaners;
167
- inSvgNamespace = false;
168
170
  constructor(cleaners = []) {
169
171
  super();
170
172
  this.cleaners = cleaners;
@@ -203,13 +205,14 @@ class ContentScope extends Scope {
203
205
  }
204
206
 
205
207
  class ChainedScope extends ContentScope {
206
- parentElement;
208
+ el;
209
+ svg;
207
210
  prevSibling;
208
- constructor(parentElement, useParentCleaners = false) {
211
+ constructor(el, svg, useParentCleaners = false) {
209
212
  super(useParentCleaners ? currentScope.cleaners : []);
210
- this.parentElement = parentElement;
211
- this.inSvgNamespace = currentScope.inSvgNamespace;
212
- if (parentElement === currentScope.parentElement) {
213
+ this.el = el;
214
+ this.svg = svg;
215
+ if (el === currentScope.el) {
213
216
  this.prevSibling = currentScope.getChildPrevSibling();
214
217
  currentScope.lastChild = this;
215
218
  }
@@ -226,8 +229,8 @@ class ChainedScope extends ContentScope {
226
229
 
227
230
  class RegularScope extends ChainedScope {
228
231
  renderer;
229
- constructor(parentElement, renderer) {
230
- super(parentElement);
232
+ constructor(el, svg, renderer) {
233
+ super(el, svg);
231
234
  this.renderer = renderer;
232
235
  this.redraw();
233
236
  }
@@ -244,20 +247,22 @@ class RegularScope extends ChainedScope {
244
247
  }
245
248
 
246
249
  class RootScope extends ContentScope {
247
- parentElement = document.body;
250
+ el = document.body;
251
+ svg = false;
248
252
  getPrecedingNode() {
249
253
  return;
250
254
  }
251
255
  }
252
256
 
253
257
  class MountScope extends ContentScope {
254
- parentElement;
258
+ el;
255
259
  renderer;
256
- constructor(parentElement, renderer) {
260
+ svg;
261
+ constructor(el, renderer) {
257
262
  super();
258
- this.parentElement = parentElement;
263
+ this.el = el;
259
264
  this.renderer = renderer;
260
- this.inSvgNamespace = currentScope.inSvgNamespace;
265
+ this.svg = el.namespaceURI === "http://www.w3.org/2000/svg";
261
266
  this.redraw();
262
267
  currentScope.cleaners.push(this);
263
268
  }
@@ -303,8 +308,8 @@ function findLastNodeInPrevSiblings(sibling) {
303
308
  class ResultScope extends ChainedScope {
304
309
  renderer;
305
310
  result = optProxy({ value: undefined });
306
- constructor(parentElement, renderer) {
307
- super(parentElement);
311
+ constructor(renderer) {
312
+ super(currentScope.el, currentScope.svg);
308
313
  this.renderer = renderer;
309
314
  this.redraw();
310
315
  }
@@ -323,8 +328,9 @@ class ResultScope extends ChainedScope {
323
328
  class SetArgScope extends ChainedScope {
324
329
  key;
325
330
  target;
326
- constructor(parentElement, key, target) {
327
- super(parentElement);
331
+ svg = false;
332
+ constructor(el, key, target) {
333
+ super(el, el.namespaceURI === "http://www.w3.org/2000/svg");
328
334
  this.key = key;
329
335
  this.target = target;
330
336
  this.redraw();
@@ -332,7 +338,7 @@ class SetArgScope extends ChainedScope {
332
338
  redraw() {
333
339
  const savedScope = currentScope;
334
340
  currentScope = this;
335
- applyArg(this.key, this.target.value);
341
+ applyArg(this.el, this.key, this.target.value);
336
342
  currentScope = savedScope;
337
343
  }
338
344
  }
@@ -340,7 +346,7 @@ class SetArgScope extends ChainedScope {
340
346
  class OnEachScope extends Scope {
341
347
  renderer;
342
348
  makeSortKey;
343
- parentElement = currentScope.parentElement;
349
+ parentElement = currentScope.el;
344
350
  prevSibling;
345
351
  target;
346
352
  byIndex = new Map;
@@ -411,13 +417,14 @@ class OnEachItemScope extends ContentScope {
411
417
  parent;
412
418
  itemIndex;
413
419
  sortKey;
414
- parentElement;
420
+ el;
421
+ svg;
415
422
  constructor(parent, itemIndex, topRedraw) {
416
423
  super();
417
424
  this.parent = parent;
418
425
  this.itemIndex = itemIndex;
419
- this.parentElement = parent.parentElement;
420
- this.inSvgNamespace = currentScope.inSvgNamespace;
426
+ this.el = parent.parentElement;
427
+ this.svg = currentScope.svg;
421
428
  this.parent.byIndex.set(this.itemIndex, this);
422
429
  this.lastChild = this;
423
430
  if (topRedraw)
@@ -509,8 +516,12 @@ class OnEachItemScope extends ContentScope {
509
516
  this.delete();
510
517
  }
511
518
  }
512
- function addNode(node) {
513
- const parentEl = currentScope.parentElement;
519
+ function addNode(el, node) {
520
+ if (el !== currentScope.el) {
521
+ el.appendChild(node);
522
+ return;
523
+ }
524
+ const parentEl = currentScope.el;
514
525
  const prevNode = currentScope.getInsertAfterNode();
515
526
  parentEl.insertBefore(node, prevNode ? prevNode.nextSibling : parentEl.firstChild);
516
527
  currentScope.lastChild = node;
@@ -843,7 +854,7 @@ var mapHandler = {
843
854
  };
844
855
  var proxyMap = new WeakMap;
845
856
  function optProxy(value) {
846
- if (typeof value !== "object" || !value || value[TARGET_SYMBOL] !== undefined) {
857
+ if (typeof value !== "object" || !value || value[TARGET_SYMBOL] !== undefined || NO_COPY in value) {
847
858
  return value;
848
859
  }
849
860
  let proxied = proxyMap.get(value);
@@ -943,7 +954,7 @@ function copyRecursive(dst, src, flags) {
943
954
  changed = true;
944
955
  } else if (dstValue !== srcValue) {
945
956
  if (srcValue && typeof srcValue === "object") {
946
- if (typeof dstValue === "object" && dstValue && srcValue.constructor === dstValue.constructor) {
957
+ if (typeof dstValue === "object" && dstValue && srcValue.constructor === dstValue.constructor && !(NO_COPY in srcValue)) {
947
958
  changed = copyRecursive(dstValue, srcValue, flags) || changed;
948
959
  continue;
949
960
  }
@@ -977,7 +988,7 @@ function copyRecursive(dst, src, flags) {
977
988
  dstValue = EMPTY;
978
989
  if (dstValue !== srcValue) {
979
990
  if (srcValue && typeof srcValue === "object") {
980
- if (typeof dstValue === "object" && dstValue && srcValue.constructor === dstValue.constructor) {
991
+ if (typeof dstValue === "object" && dstValue && srcValue.constructor === dstValue.constructor && !(NO_COPY in srcValue)) {
981
992
  changed = copyRecursive(dstValue, srcValue, flags) || changed;
982
993
  continue;
983
994
  }
@@ -1007,7 +1018,7 @@ function copyRecursive(dst, src, flags) {
1007
1018
  const dstValue = dst.hasOwnProperty(key) ? dst[key] : EMPTY;
1008
1019
  if (dstValue !== srcValue) {
1009
1020
  if (srcValue && typeof srcValue === "object") {
1010
- if (typeof dstValue === "object" && dstValue && srcValue.constructor === dstValue.constructor) {
1021
+ if (typeof dstValue === "object" && dstValue && srcValue.constructor === dstValue.constructor && !(NO_COPY in srcValue)) {
1011
1022
  changed = copyRecursive(dstValue, srcValue, flags) || changed;
1012
1023
  continue;
1013
1024
  }
@@ -1039,7 +1050,11 @@ function copyRecursive(dst, src, flags) {
1039
1050
  var MERGE = 1;
1040
1051
  var COPY_SUBSCRIBE = 32;
1041
1052
  var COPY_EMIT = 64;
1053
+ var NO_COPY = Symbol("NO_COPY");
1054
+ Promise.prototype[NO_COPY] = true;
1042
1055
  function clone(src) {
1056
+ if (NO_COPY in src)
1057
+ return src;
1043
1058
  const copied = Array.isArray(src) ? [] : src instanceof Map ? new Map : Object.create(Object.getPrototypeOf(src));
1044
1059
  copyImpl(copied, src, MERGE);
1045
1060
  return copied;
@@ -1107,8 +1122,7 @@ function applyBind(el, target) {
1107
1122
  });
1108
1123
  }
1109
1124
  var SPECIAL_PROPS = {
1110
- create: (value) => {
1111
- const el = currentScope.parentElement;
1125
+ create: (el, value) => {
1112
1126
  if (currentScope !== topRedrawScope)
1113
1127
  return;
1114
1128
  if (typeof value === "function") {
@@ -1122,149 +1136,118 @@ var SPECIAL_PROPS = {
1122
1136
  })();
1123
1137
  }
1124
1138
  },
1125
- destroy: (value) => {
1126
- const el = currentScope.parentElement;
1139
+ destroy: (el, value) => {
1127
1140
  onDestroyMap.set(el, value);
1128
1141
  },
1129
- html: (value) => {
1130
- const tmpParent = document.createElement(currentScope.parentElement.tagName);
1142
+ html: (el, value) => {
1143
+ const tmpParent = document.createElement(currentScope.el.tagName);
1131
1144
  tmpParent.innerHTML = `${value}`;
1132
1145
  while (tmpParent.firstChild)
1133
- addNode(tmpParent.firstChild);
1146
+ addNode(el, tmpParent.firstChild);
1134
1147
  },
1135
- text: (value) => {
1136
- addNode(document.createTextNode(value));
1148
+ text: (el, value) => {
1149
+ addNode(el, document.createTextNode(value));
1137
1150
  }
1138
1151
  };
1139
1152
  function $(...args) {
1140
- let savedCurrentScope;
1141
- let err;
1142
- let result;
1143
- let nextArgIsProp;
1144
- for (let arg of args) {
1145
- if (nextArgIsProp) {
1146
- applyArg(nextArgIsProp, arg);
1147
- nextArgIsProp = undefined;
1148
- } else if (arg == null || arg === false) {} else if (typeof arg === "string") {
1149
- let pos = 0;
1153
+ let el = currentScope.el;
1154
+ let svg = currentScope.svg;
1155
+ const argCount = args.length;
1156
+ for (let argIndex = 0;argIndex < argCount; argIndex++) {
1157
+ const arg = args[argIndex];
1158
+ if (arg == null || arg === false) {} else if (typeof arg === "string") {
1150
1159
  let argLen = arg.length;
1151
- while (pos < argLen) {
1152
- let nextSpace = arg.indexOf(" ", pos);
1153
- if (nextSpace < 0)
1154
- nextSpace = arg.length;
1155
- let part = arg.substring(pos, nextSpace);
1156
- const oldPos = pos;
1157
- pos = nextSpace + 1;
1158
- const firstIs = part.indexOf("=");
1159
- const firstColon = part.indexOf(":");
1160
- if (firstIs >= 0 && (firstColon < 0 || firstIs < firstColon)) {
1161
- const prop = part.substring(0, firstIs);
1162
- if (firstIs < part.length - 1) {
1163
- let value = part.substring(firstIs + 1);
1164
- if (value[0] === '"') {
1165
- const closeIndex = arg.indexOf('"', firstIs + 2 + oldPos);
1166
- if (closeIndex < 0)
1167
- throw new Error(`Unterminated string for '${prop}'`);
1168
- value = arg.substring(firstIs + 2 + oldPos, closeIndex);
1169
- pos = closeIndex + 1;
1170
- if (arg[pos] === " ")
1171
- pos++;
1172
- }
1173
- applyArg(prop, value);
1174
- continue;
1175
- } else {
1176
- if (pos < argLen)
1177
- throw new Error(`No value given for '${part}'`);
1178
- nextArgIsProp = prop;
1160
+ let nextPos = 0;
1161
+ for (let pos = 0;pos < argLen; pos = nextPos + 1) {
1162
+ nextPos = findFirst(arg, " .=:#", pos);
1163
+ const next = arg[nextPos];
1164
+ if (next === ":" || next === "=") {
1165
+ let key = arg.substring(pos, nextPos);
1166
+ if (next === ":")
1167
+ key = "$" + key;
1168
+ if (nextPos + 1 >= argLen) {
1169
+ applyArg(el, key, args[++argIndex]);
1179
1170
  break;
1180
1171
  }
1181
- }
1182
- let text;
1183
- if (firstColon >= 0) {
1184
- text = arg.substring(firstColon + 1 + oldPos);
1185
- part = part.substring(0, firstColon);
1186
- if (!text) {
1187
- if (pos < argLen)
1188
- throw new Error(`No value given for '${part}'`);
1189
- nextArgIsProp = "text";
1190
- break;
1191
- }
1192
- pos = argLen;
1193
- }
1194
- let classes;
1195
- const classPos = part.indexOf(".");
1196
- if (classPos >= 0) {
1197
- classes = part.substring(classPos + 1);
1198
- part = part.substring(0, classPos);
1199
- }
1200
- if (part) {
1201
- const svg = currentScope.inSvgNamespace || part === "svg";
1202
- if (svg) {
1203
- result = document.createElementNS("http://www.w3.org/2000/svg", part);
1172
+ if (arg[nextPos + 1] === '"') {
1173
+ const endIndex = findFirst(arg, '"', nextPos + 2);
1174
+ const value = arg.substring(nextPos + 2, endIndex);
1175
+ applyArg(el, key, value);
1176
+ nextPos = endIndex;
1204
1177
  } else {
1205
- result = document.createElement(part);
1178
+ const endIndex = findFirst(arg, " ", nextPos + 1);
1179
+ const value = arg.substring(nextPos + 1, endIndex);
1180
+ applyArg(el, key, value);
1181
+ nextPos = endIndex;
1206
1182
  }
1207
- addNode(result);
1208
- if (!savedCurrentScope)
1209
- savedCurrentScope = currentScope;
1210
- const newScope = new ChainedScope(result, true);
1211
- if (svg)
1212
- newScope.inSvgNamespace = true;
1213
- if (topRedrawScope === currentScope)
1214
- topRedrawScope = newScope;
1215
- currentScope = newScope;
1216
- }
1217
- if (text)
1218
- addNode(document.createTextNode(text));
1219
- if (classes) {
1220
- const el = currentScope.parentElement;
1221
- el.classList.add(...classes.split("."));
1222
- if (!savedCurrentScope) {
1223
- clean(() => el.classList.remove(...classes.split(".")));
1183
+ } else {
1184
+ if (nextPos > pos) {
1185
+ const tag = arg.substring(pos, nextPos);
1186
+ svg ||= tag === "svg";
1187
+ let newEl = svg ? document.createElementNS("http://www.w3.org/2000/svg", tag) : document.createElement(tag);
1188
+ addNode(el, newEl);
1189
+ el = newEl;
1190
+ }
1191
+ if (next === "#") {
1192
+ const text = nextPos + 1 < argLen ? arg.substring(nextPos + 1) : args[++argIndex];
1193
+ applyArg(el, "text", text);
1194
+ break;
1195
+ }
1196
+ if (next === ".") {
1197
+ let classEnd = findFirst(arg, " #=.", nextPos + 1);
1198
+ if (arg[classEnd] === "=" && classEnd + 1 >= argLen) {
1199
+ applyArg(el, arg.substring(nextPos, classEnd), args[++argIndex]);
1200
+ nextPos = classEnd;
1201
+ } else {
1202
+ let className = arg.substring(nextPos + 1, classEnd);
1203
+ el.classList.add(className || args[++argIndex]);
1204
+ nextPos = classEnd - 1;
1205
+ }
1224
1206
  }
1225
1207
  }
1226
1208
  }
1227
1209
  } else if (typeof arg === "object") {
1228
1210
  if (arg.constructor !== Object) {
1229
1211
  if (arg instanceof Node) {
1230
- addNode(arg);
1212
+ addNode(el, arg);
1231
1213
  if (arg instanceof Element) {
1232
- if (!savedCurrentScope)
1233
- savedCurrentScope = currentScope;
1234
- currentScope = new ChainedScope(arg, true);
1235
- currentScope.lastChild = arg.lastChild || undefined;
1214
+ el = arg;
1215
+ svg = arg.namespaceURI === "http://www.w3.org/2000/svg";
1236
1216
  }
1237
1217
  } else {
1238
- err = `Unexpected argument: ${arg}`;
1239
- break;
1218
+ throw new Error(`Unexpected argument: ${arg}`);
1240
1219
  }
1241
1220
  } else {
1242
1221
  for (const key of Object.keys(arg)) {
1243
- const val = arg[key];
1244
- applyArg(key, val);
1222
+ applyArg(el, key, arg[key]);
1245
1223
  }
1246
1224
  }
1247
1225
  } else if (typeof arg === "function") {
1248
- new RegularScope(currentScope.parentElement, arg);
1226
+ new RegularScope(el, svg, arg);
1249
1227
  } else {
1250
- err = `Unexpected argument: ${arg}`;
1251
- break;
1228
+ throw new Error(`Unexpected argument: ${arg}`);
1252
1229
  }
1253
1230
  }
1254
- if (nextArgIsProp !== undefined)
1255
- throw new Error(`No value given for '${nextArgIsProp}='`);
1256
- if (savedCurrentScope)
1257
- currentScope = savedCurrentScope;
1258
- if (err)
1259
- throw new Error(err);
1260
- return result;
1231
+ return el;
1232
+ }
1233
+ function findFirst(str, chars, startPos) {
1234
+ if (chars.length === 1) {
1235
+ const idx = str.indexOf(chars, startPos);
1236
+ return idx >= 0 ? idx : str.length;
1237
+ }
1238
+ const strLen = str.length;
1239
+ for (let i = startPos;i < strLen; i++) {
1240
+ if (chars.indexOf(str[i]) >= 0)
1241
+ return i;
1242
+ }
1243
+ return strLen;
1261
1244
  }
1262
1245
  var cssCount = 0;
1263
1246
  function insertCss(style, global = false) {
1264
1247
  const prefix = global ? "" : `.AbdStl${++cssCount}`;
1265
1248
  const css = styleToCss(style, prefix);
1266
1249
  if (css)
1267
- $(`style:${css}`);
1250
+ $(`style#${css}`);
1268
1251
  return prefix;
1269
1252
  }
1270
1253
  function styleToCss(style, prefix) {
@@ -1291,8 +1274,7 @@ ${styleToCss(v, prefix)}}
1291
1274
  ${rules}`;
1292
1275
  return rules;
1293
1276
  }
1294
- function applyArg(key, value) {
1295
- const el = currentScope.parentElement;
1277
+ function applyArg(el, key, value) {
1296
1278
  if (typeof value === "object" && value !== null && value[TARGET_SYMBOL]) {
1297
1279
  if (key === "bind") {
1298
1280
  applyBind(el, value);
@@ -1312,10 +1294,11 @@ function applyArg(key, value) {
1312
1294
  else
1313
1295
  el.style[name] = `${value}`;
1314
1296
  } else if (value == null) {} else if (key in SPECIAL_PROPS) {
1315
- SPECIAL_PROPS[key](value);
1297
+ SPECIAL_PROPS[key](el, value);
1316
1298
  } else if (typeof value === "function") {
1317
1299
  el.addEventListener(key, value);
1318
- clean(() => el.removeEventListener(key, value));
1300
+ if (el === currentScope.el)
1301
+ clean(() => el.removeEventListener(key, value));
1319
1302
  } else if (value === true || value === false || key === "value" || key === "selectedIndex") {
1320
1303
  el[key] = value;
1321
1304
  } else {
@@ -1331,13 +1314,13 @@ function setErrorHandler(handler) {
1331
1314
  onError = handler || defaultOnError;
1332
1315
  }
1333
1316
  function getParentElement() {
1334
- return currentScope.parentElement;
1317
+ return currentScope.el;
1335
1318
  }
1336
1319
  function clean(cleaner) {
1337
1320
  currentScope.cleaners.push(cleaner);
1338
1321
  }
1339
1322
  function derive(func) {
1340
- return new ResultScope(currentScope.parentElement, func).result;
1323
+ return new ResultScope(func).result;
1341
1324
  }
1342
1325
  function mount(parentElement, func) {
1343
1326
  new MountScope(parentElement, func);
@@ -1402,7 +1385,7 @@ function multiMap(source, func) {
1402
1385
  }
1403
1386
  function partition(source, func) {
1404
1387
  const unproxiedOut = {};
1405
- const out = proxy(unproxiedOut);
1388
+ const out = optProxy(unproxiedOut);
1406
1389
  onEach(source, (item, key) => {
1407
1390
  const rsp = func(item, key);
1408
1391
  if (rsp != null) {
@@ -1428,24 +1411,22 @@ function partition(source, func) {
1428
1411
  }
1429
1412
  function dump(data) {
1430
1413
  if (data && typeof data === "object") {
1431
- let label;
1432
- if (data instanceof Array) {
1433
- label = "<array>";
1434
- } else if (data instanceof Map) {
1435
- label = "<Map>";
1414
+ const name = data.constructor.name.toLowerCase() || "unknown object";
1415
+ $(`#<${name}>`);
1416
+ if (NO_COPY in data) {
1417
+ $("# [NO_COPY]");
1436
1418
  } else {
1437
- label = "<object>";
1438
- }
1439
- $({ text: label });
1440
- $("ul", () => {
1441
- onEach(data, (value, key) => {
1442
- $(`li:${JSON.stringify(key)}: `, () => {
1443
- dump(value);
1419
+ $("ul", () => {
1420
+ onEach(data, (value, key) => {
1421
+ $("li", () => {
1422
+ $(`#${JSON.stringify(key)}: `);
1423
+ dump(value);
1424
+ });
1444
1425
  });
1445
1426
  });
1446
- });
1447
- } else {
1448
- $({ text: JSON.stringify(data) });
1427
+ }
1428
+ } else if (data !== undefined) {
1429
+ $("#" + JSON.stringify(data));
1449
1430
  }
1450
1431
  return data;
1451
1432
  }
@@ -1461,7 +1442,7 @@ function handleError(e, showMessage) {
1461
1442
  }
1462
1443
  try {
1463
1444
  if (showMessage)
1464
- $("div.aberdeen-error:Error");
1445
+ $("div.aberdeen-error#Error");
1465
1446
  } catch {}
1466
1447
  }
1467
1448
  function withEmitHandler(handler, func) {
@@ -1500,8 +1481,9 @@ export {
1500
1481
  copy,
1501
1482
  clone,
1502
1483
  clean,
1484
+ NO_COPY,
1503
1485
  $
1504
1486
  };
1505
1487
 
1506
- //# debugId=1793ACD02DE96EE964756E2164756E21
1488
+ //# debugId=C3082837141CF76064756E2164756E21
1507
1489
  //# sourceMappingURL=aberdeen.js.map