-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextras.js
More file actions
525 lines (516 loc) · 15.7 KB
/
extras.js
File metadata and controls
525 lines (516 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
(function () {
var arrayNotationPattern = /\[\s*(\d+)\s*\]/g;
function makeArrayMethod(name) {
return function (keypath) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var parts = keypath.replace(arrayNotationPattern, '.$1').split('.');
var key = parts.shift();
var value = this.get()[key];
var array = value;
while (parts.length)
array = array[parts.shift()];
var result = array[name].apply(array, args);
this.set((_a = {}, _a[key] = value, _a));
return result;
var _a;
};
}
var push = makeArrayMethod('push');
var pop = makeArrayMethod('pop');
var shift = makeArrayMethod('shift');
var unshift = makeArrayMethod('unshift');
var splice = makeArrayMethod('splice');
var sort = makeArrayMethod('sort');
var reverse = makeArrayMethod('reverse');
function isDate(obj) {
return Object.prototype.toString.call(obj) === '[object Date]';
}
var scheduler = {
components: [],
running: false,
add: function (component) {
if (~scheduler.components.indexOf(component))
return;
scheduler.components.push(component);
if (!scheduler.running) {
scheduler.running = true;
requestAnimationFrame(scheduler.next);
}
},
next: function () {
var now = window.performance.now();
var hasComponents = false;
var i = scheduler.components.length;
while (i--) {
var component = scheduler.components[i];
var data = {};
var hasTweens = false;
for (var key in component._currentTweens) {
var t = component._currentTweens[key];
if (now >= t.end) {
data[key] = t.to;
delete component._currentTweens[key];
t.fulfil();
}
else {
hasTweens = true;
hasComponents = true;
if (now >= t.start) {
var p = (now - t.start) / t.duration;
data[key] = t.value(t.ease(p));
}
}
}
component._tweening = true;
component.set(data);
component._tweening = false;
if (!hasTweens)
scheduler.components.splice(i, 1);
}
if (hasComponents) {
requestAnimationFrame(scheduler.next);
}
else {
scheduler.running = false;
}
}
};
function snap(to) {
return function () { return to; };
}
function interpolate(a, b) {
if (a === b || a !== a)
return snap(a);
var type = typeof a;
if (type !== typeof b || Array.isArray(a) !== Array.isArray(b)) {
throw new Error('Cannot interpolate values of different type');
}
if (Array.isArray(a)) {
var arr_1 = b.map(function (bi, i) {
return interpolate(a[i], bi);
});
return function (t) {
return arr_1.map(function (fn) { return fn(t); });
};
}
if (type === 'object') {
if (!a || !b)
throw new Error('Object cannot be null');
if (isDate(a) && isDate(b)) {
a = a.getTime();
b = b.getTime();
var delta_1 = b - a;
return function (t) {
return new Date(a + t * delta_1);
};
}
var keys_1 = Object.keys(b);
var interpolators_1 = {};
var result_1 = {};
keys_1.forEach(function (key) {
interpolators_1[key] = interpolate(a[key], b[key]);
});
return function (t) {
keys_1.forEach(function (key) {
result_1[key] = interpolators_1[key](t);
});
return result_1;
};
}
if (type === 'number') {
var delta_2 = b - a;
return function (t) {
return a + t * delta_2;
};
}
throw new Error("Cannot interpolate " + type + " values");
}
function linear(x) {
return x;
}
function tween(key, to, options) {
var _this = this;
if (options === void 0) { options = {}; }
if (!this._currentTweens) {
this._currentTweens = Object.create(null);
this._tweening = false;
var set_1 = this.set;
this.set = function (data) {
if (!_this._tweening) {
for (var key_1 in data) {
if (_this._currentTweens[key_1])
_this._currentTweens[key_1].abort();
}
}
set_1.call(_this, data);
};
}
if (this._currentTweens[key])
this._currentTweens[key].abort();
var start = window.performance.now() + (options.delay || 0);
var duration = options.duration || 400;
var end = start + duration;
var t = {
key: key,
value: (options.interpolate || interpolate)(this.get()[key], to),
to: to,
start: start,
end: end,
duration: duration,
ease: options.easing || linear,
running: true,
abort: function () {
t.running = false;
delete _this._currentTweens[key];
}
};
this._currentTweens[key] = t;
scheduler.add(this);
var promise = new Promise(function (fulfil) {
t.fulfil = fulfil;
});
promise.abort = t.abort;
return promise;
}
function observe(key, callback, opts) {
var fn = callback.bind(this);
if (!opts || opts.init !== false) {
fn(this.get()[key]);
}
return this.on(opts && opts.defer ? 'update' : 'state', function (_a) {
var changed = _a.changed, current = _a.current, previous = _a.previous;
if (changed[key])
fn(current[key], previous && previous[key]);
});
}
function getNestedValue(obj, parts) {
for (var i = 0; i < parts.length; i += 1) {
if (!obj)
return undefined;
obj = obj[parts[i]];
}
return obj;
}
function observeDeep(keypath, callback, opts) {
var parts = keypath.replace(/\[(\d+)\]/g, '.$1').split('.');
var key = parts[0];
var fn = callback.bind(this);
var last = getNestedValue(this.get(), parts);
if (!opts || opts.init !== false)
fn(last);
return this.on(opts && opts.defer ? 'update' : 'state', function (_a) {
var changed = _a.changed, current = _a.current, previous = _a.previous;
if (changed[key]) {
var value = getNestedValue(current, parts);
if (value !== last ||
typeof value === 'object' ||
typeof value === 'function') {
fn(value, last);
last = value;
}
}
});
}
function observeMany(keys, callback, opts) {
var fn = callback.bind(this);
if (!opts || opts.init !== false) {
var state_1 = this.get();
fn(keys.map(function (key) { return state_1[key]; }), keys.map(function (key) { return undefined; }));
}
return this.on(opts && opts.defer ? 'update' : 'state', function (_a) {
var changed = _a.changed, current = _a.current, previous = _a.previous;
if (!previous)
return; // prevent double-firing if observing in oncreate
var i = keys.length;
while (i--) {
if (changed[keys[i]]) {
fn(keys.map(function (key) { return current[key]; }), keys.map(function (key) { return previous[key]; }));
return;
}
}
});
}
function getDeep(keypath) {
if (keypath === undefined) {
return this.get();
}
var keys = keypath.replace(/\[(\d+)\]/g, '.$1').split('.');
var value = this.get()[keys[0]];
for (var i = 1; i < keys.length; i++) {
value = value[keys[i]];
}
return value;
}
function setDeep(keypath, value) {
if (keypath === undefined) {
return;
}
var keys = keypath.replace(/\[(\d+)\]/g, '.$1').split('.');
var lastKey = keys.pop();
// If not a nested keypath
if (keys[0] === undefined) {
var data_1 = {};
data_1[lastKey] = value;
this.set(data_1);
return;
}
var object = this.get()[keys[0]];
for (var i = 1; i < keys.length; i++) {
object = object[keys[i]];
}
object[lastKey] = value;
var data = {};
data[keys[0]] = this.get()[keys[0]];
this.set(data);
}
var scheduler$1 = {
components: [],
running: false,
add: function (component) {
if (~scheduler$1.components.indexOf(component))
return;
scheduler$1.components.push(component);
if (!scheduler$1.running) {
scheduler$1.running = true;
requestAnimationFrame(scheduler$1.next);
}
},
next: function () {
var hasComponents = false;
var i = scheduler$1.components.length;
while (i--) {
var component = scheduler$1.components[i];
var data = {};
var hasSprings = false;
for (var key in component._springs) {
var spring_1 = component._springs[key];
if (spring_1.tick(data)) {
hasSprings = true;
hasComponents = true;
}
else {
component._springCallbacks[key]();
delete component._springs[key];
delete component._springCallbacks[key];
}
}
component._springing = true;
component.set(data);
component._springing = false;
if (!hasSprings)
scheduler$1.components.splice(i, 1);
}
if (hasComponents) {
requestAnimationFrame(scheduler$1.next);
}
else {
scheduler$1.running = false;
}
}
};
function snap$1(key, a, b, options) {
return {
key: key,
tick: function (object) {
object[key] = b;
return false;
},
update: function (object, options) {
b = object;
}
};
}
function number(key, a, b, options) {
var velocity = 0;
var stiffness = options.stiffness, damping = options.damping;
var valueThreshold = Math.abs(b - a) * 0.001;
var velocityThreshold = valueThreshold; // TODO is this right?
return {
key: key,
tick: function (object) {
var d = b - a;
var spring = stiffness * d;
var damper = damping * velocity;
var acceleration = spring - damper;
velocity += acceleration;
a += velocity;
object[key] = a;
if (velocity < velocityThreshold && Math.abs(b - a) < valueThreshold) {
object[key] = b;
return false;
}
object[key] = a;
return true;
},
update: function (object, options) {
checkCompatibility(object, b);
b = object;
stiffness = options.stiffness;
damping = options.damping;
}
};
}
function date(key, a, b, options) {
var dummy = {};
var subspring = number(key, a.getTime(), b.getTime(), options);
return {
key: key,
tick: function (object) {
if (!subspring.tick(dummy)) {
object[key] = b;
return false;
}
object[key] = new Date(dummy[key]);
return true;
},
update: function (object, options) {
checkCompatibility(object, b);
subspring.update(object.getTime(), options);
b = object;
}
};
}
function array(key, a, b, options) {
var value = [];
var subsprings = [];
for (var i = 0; i < a.length; i += 1) {
subsprings.push(getSpring(i, a[i], b[i], options));
}
return {
key: key,
tick: function (object) {
var active = false;
for (var i = 0; i < subsprings.length; i += 1) {
if (subsprings[i].tick(value))
active = true;
}
if (!active) {
object[key] = b;
return false;
}
object[key] = value;
return true;
},
update: function (object, options) {
checkCompatibility(object, b);
for (var i = 0; i < object.length; i += 1) {
subsprings[i].update(object[i], options);
}
b = object;
}
};
}
function object(key, a, b, options) {
var value = {};
var subsprings = [];
for (var k in a) {
subsprings.push(getSpring(k, a[k], b[k], options));
}
return {
key: key,
tick: function (object) {
var active = false;
for (var i = 0; i < subsprings.length; i += 1) {
if (subsprings[i].tick(value))
active = true;
}
if (!active) {
object[key] = b;
return false;
}
object[key] = value;
return true;
},
update: function (object, options) {
checkCompatibility(object, b);
for (var i = 0; i < subsprings.length; i += 1) {
subsprings[i].update(object[subsprings[i].key], options);
}
b = object;
}
};
}
function checkCompatibility(a, b) {
var type = typeof a;
if (type !== typeof b ||
Array.isArray(a) !== Array.isArray(b) ||
isDate(a) !== isDate(b)) {
throw new Error('Cannot interpolate values of different type');
}
if (type === 'object') {
if (!a || !b)
throw new Error('Object cannot be null');
if (Array.isArray(a)) {
if (a.length !== b.length) {
throw new Error('Cannot interpolate arrays of different length');
}
}
else {
if (!keysMatch(a, b))
throw new Error('Cannot interpolate differently-shaped objects');
}
}
else if (type !== 'number') {
throw new Error("Cannot interpolate " + type + " values");
}
}
function getSpring(key, a, b, options) {
if (a === b || a !== a)
return snap$1(key, a, b, options);
checkCompatibility(a, b);
if (typeof a === 'object') {
if (Array.isArray(a)) {
return array(key, a, b, options);
}
if (isDate(a)) {
return date(key, a, b, options);
}
return object(key, a, b, options);
}
return number(key, a, b, options);
}
function spring(key, to, options) {
var _this = this;
if (!this._springs) {
this._springs = Object.create(null);
this._springCallbacks = Object.create(null);
this._springing = false;
var set_1 = this.set;
this.set = function (data) {
if (!_this._springing) {
for (var key_1 in data) {
delete _this._springs[key_1];
}
}
set_1.call(_this, data);
};
}
if (this._springs[key]) {
this._springs[key].update(to, options);
}
else {
var spring_2 = getSpring(key, this.get()[key], to, options);
this._springs[key] = spring_2;
}
var promise = new Promise(function (fulfil) {
_this._springCallbacks[key] = fulfil;
});
scheduler$1.add(this);
return promise;
}
function keysMatch(a, b) {
var aKeys = Object.keys(a);
var bKeys = Object.keys(b);
if (aKeys.length !== bKeys.length)
return false;
for (var i = 0; i < aKeys.length; i += 1) {
if (!(aKeys[i] in b))
return false;
}
return true;
}
return { push, pop, shift, unshift, splice, sort, reverse, tween, observe, observeDeep, observeMany, getDeep, setDeep, spring };
})();