-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathractive.js
16619 lines (12871 loc) · 413 KB
/
ractive.js
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Ractive.js v0.7.3
Sat Apr 25 2015 13:52:38 GMT-0400 (EDT) - commit da40f81c660ba2f09c45a09a9c20fdd34ee36d80
http://ractivejs.org
http://twitter.com/RactiveJS
Released under the MIT License.
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
global.Ractive = factory()
}(this, function () { 'use strict';
var TEMPLATE_VERSION = 3;
var defaultOptions = {
// render placement:
el: void 0,
append: false,
// template:
template: { v: TEMPLATE_VERSION, t: [] },
// parse: // TODO static delimiters?
preserveWhitespace: false,
sanitize: false,
stripComments: true,
delimiters: ["{{", "}}"],
tripleDelimiters: ["{{{", "}}}"],
interpolate: false,
// data & binding:
data: {},
computed: {},
magic: false,
modifyArrays: true,
adapt: [],
isolated: false,
twoway: true,
lazy: false,
// transitions:
noIntro: false,
transitionsEnabled: true,
complete: void 0,
// css:
css: null,
noCssTransform: false
};
var config_defaults = defaultOptions;
// These are a subset of the easing equations found at
// https://raw.github.com/danro/easing-js - license info
// follows:
// --------------------------------------------------
// easing.js v0.5.4
// Generic set of easing functions with AMD support
// https://github.com/danro/easing-js
// This code may be freely distributed under the MIT license
// http://danro.mit-license.org/
// --------------------------------------------------
// All functions adapted from Thomas Fuchs & Jeremy Kahn
// Easing Equations (c) 2003 Robert Penner, BSD license
// https://raw.github.com/danro/easing-js/master/LICENSE
// --------------------------------------------------
// In that library, the functions named easeIn, easeOut, and
// easeInOut below are named easeInCubic, easeOutCubic, and
// (you guessed it) easeInOutCubic.
//
// You can add additional easing functions to this list, and they
// will be globally available.
var static_easing = {
linear: function (pos) {
return pos;
},
easeIn: function (pos) {
return Math.pow(pos, 3);
},
easeOut: function (pos) {
return Math.pow(pos - 1, 3) + 1;
},
easeInOut: function (pos) {
if ((pos /= 0.5) < 1) {
return 0.5 * Math.pow(pos, 3);
}
return 0.5 * (Math.pow(pos - 2, 3) + 2);
}
};
/*global console, navigator */
var isClient, isJsdom, hasConsole, environment__magic, namespaces, svg, vendors;
isClient = typeof document === "object";
isJsdom = typeof navigator !== "undefined" && /jsDom/.test(navigator.appName);
hasConsole = typeof console !== "undefined" && typeof console.warn === "function" && typeof console.warn.apply === "function";
try {
Object.defineProperty({}, "test", { value: 0 });
environment__magic = true;
} catch (e) {
environment__magic = false;
}
namespaces = {
html: "http://www.w3.org/1999/xhtml",
mathml: "http://www.w3.org/1998/Math/MathML",
svg: "http://www.w3.org/2000/svg",
xlink: "http://www.w3.org/1999/xlink",
xml: "http://www.w3.org/XML/1998/namespace",
xmlns: "http://www.w3.org/2000/xmlns/",
v: 'http://schemas.microsoft.com/visio/2003/SVGExtensions/'
};
if (typeof document === "undefined") {
svg = false;
} else {
svg = document && document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1");
}
vendors = ["o", "ms", "moz", "webkit"];
var createElement, matches, dom__div, methodNames, unprefixed, prefixed, dom__i, j, makeFunction;
// Test for SVG support
if (!svg) {
createElement = function (type, ns) {
if (ns && ns !== namespaces.html) {
throw "This browser does not support namespaces other than http://www.w3.org/1999/xhtml. The most likely cause of this error is that you're trying to render SVG in an older browser. See http://docs.ractivejs.org/latest/svg-and-older-browsers for more information";
}
return document.createElement(type);
};
} else {
createElement = function (type, ns) {
if (!ns || ns === namespaces.html) {
return document.createElement(type);
}
return document.createElementNS(ns, type);
};
}
function getElement(input) {
var output;
if (!input || typeof input === "boolean") {
return;
}
if (typeof window === "undefined" || !document || !input) {
return null;
}
// We already have a DOM node - no work to do. (Duck typing alert!)
if (input.nodeType) {
return input;
}
// Get node from string
if (typeof input === "string") {
// try ID first
output = document.getElementById(input);
// then as selector, if possible
if (!output && document.querySelector) {
output = document.querySelector(input);
}
// did it work?
if (output && output.nodeType) {
return output;
}
}
// If we've been given a collection (jQuery, Zepto etc), extract the first item
if (input[0] && input[0].nodeType) {
return input[0];
}
return null;
}
if (!isClient) {
matches = null;
} else {
dom__div = createElement("div");
methodNames = ["matches", "matchesSelector"];
makeFunction = function (methodName) {
return function (node, selector) {
return node[methodName](selector);
};
};
dom__i = methodNames.length;
while (dom__i-- && !matches) {
unprefixed = methodNames[dom__i];
if (dom__div[unprefixed]) {
matches = makeFunction(unprefixed);
} else {
j = vendors.length;
while (j--) {
prefixed = vendors[dom__i] + unprefixed.substr(0, 1).toUpperCase() + unprefixed.substring(1);
if (dom__div[prefixed]) {
matches = makeFunction(prefixed);
break;
}
}
}
}
// IE8...
if (!matches) {
matches = function (node, selector) {
var nodes, parentNode, i;
parentNode = node.parentNode;
if (!parentNode) {
// empty dummy <div>
dom__div.innerHTML = "";
parentNode = dom__div;
node = node.cloneNode();
dom__div.appendChild(node);
}
nodes = parentNode.querySelectorAll(selector);
i = nodes.length;
while (i--) {
if (nodes[i] === node) {
return true;
}
}
return false;
};
}
}
function detachNode(node) {
if (node && typeof node.parentNode !== "unknown" && node.parentNode) {
node.parentNode.removeChild(node);
}
return node;
}
function safeToStringValue(value) {
return value == null || !value.toString ? "" : value;
}
var legacy = null;
var create, defineProperty, defineProperties;
try {
Object.defineProperty({}, "test", { value: 0 });
if (isClient) {
Object.defineProperty(document.createElement("div"), "test", { value: 0 });
}
defineProperty = Object.defineProperty;
} catch (err) {
// Object.defineProperty doesn't exist, or we're in IE8 where you can
// only use it with DOM objects (what were you smoking, MSFT?)
defineProperty = function (obj, prop, desc) {
obj[prop] = desc.value;
};
}
try {
try {
Object.defineProperties({}, { test: { value: 0 } });
} catch (err) {
// TODO how do we account for this? noMagic = true;
throw err;
}
if (isClient) {
Object.defineProperties(createElement("div"), { test: { value: 0 } });
}
defineProperties = Object.defineProperties;
} catch (err) {
defineProperties = function (obj, props) {
var prop;
for (prop in props) {
if (props.hasOwnProperty(prop)) {
defineProperty(obj, prop, props[prop]);
}
}
};
}
try {
Object.create(null);
create = Object.create;
} catch (err) {
// sigh
create = (function () {
var F = function () {};
return function (proto, props) {
var obj;
if (proto === null) {
return {};
}
F.prototype = proto;
obj = new F();
if (props) {
Object.defineProperties(obj, props);
}
return obj;
};
})();
}
function utils_object__extend(target) {
for (var _len = arguments.length, sources = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
sources[_key - 1] = arguments[_key];
}
var prop, source;
while (source = sources.shift()) {
for (prop in source) {
if (hasOwn.call(source, prop)) {
target[prop] = source[prop];
}
}
}
return target;
}
function fillGaps(target) {
for (var _len = arguments.length, sources = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
sources[_key - 1] = arguments[_key];
}
sources.forEach(function (s) {
for (var key in s) {
if (s.hasOwnProperty(key) && !(key in target)) {
target[key] = s[key];
}
}
});
return target;
}
var hasOwn = Object.prototype.hasOwnProperty;
// thanks, http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/
var is__toString = Object.prototype.toString,
arrayLikePattern = /^\[object (?:Array|FileList)\]$/;
function isArray(thing) {
return is__toString.call(thing) === "[object Array]";
}
function isArrayLike(obj) {
return arrayLikePattern.test(is__toString.call(obj));
}
function isEqual(a, b) {
if (a === null && b === null) {
return true;
}
if (typeof a === "object" || typeof b === "object") {
return false;
}
return a === b;
}
function is__isNumeric(thing) {
return !isNaN(parseFloat(thing)) && isFinite(thing);
}
function isObject(thing) {
return thing && is__toString.call(thing) === "[object Object]";
}
var noop = function () {};
/* global console */
var alreadyWarned = {},
log,
printWarning,
welcome;
if (hasConsole) {
(function () {
var welcomeIntro = ["%cRactive.js %c0.7.3 %cin debug mode, %cmore...", "color: rgb(114, 157, 52); font-weight: normal;", "color: rgb(85, 85, 85); font-weight: normal;", "color: rgb(85, 85, 85); font-weight: normal;", "color: rgb(82, 140, 224); font-weight: normal; text-decoration: underline;"];
var welcomeMessage = "You're running Ractive 0.7.3 in debug mode - messages will be printed to the console to help you fix problems and optimise your application.\n\nTo disable debug mode, add this line at the start of your app:\n Ractive.DEBUG = false;\n\nTo disable debug mode when your app is minified, add this snippet:\n Ractive.DEBUG = /unminified/.test(function(){/*unminified*/});\n\nGet help and support:\n http://docs.ractivejs.org\n http://stackoverflow.com/questions/tagged/ractivejs\n http://groups.google.com/forum/#!forum/ractive-js\n http://twitter.com/ractivejs\n\nFound a bug? Raise an issue:\n https://github.com/ractivejs/ractive/issues\n\n";
welcome = function () {
var hasGroup = !!console.groupCollapsed;
console[hasGroup ? "groupCollapsed" : "log"].apply(console, welcomeIntro);
console.log(welcomeMessage);
if (hasGroup) {
console.groupEnd(welcomeIntro);
}
welcome = noop;
};
printWarning = function (message, args) {
welcome();
// extract information about the instance this message pertains to, if applicable
if (typeof args[args.length - 1] === "object") {
var options = args.pop();
var ractive = options ? options.ractive : null;
if (ractive) {
// if this is an instance of a component that we know the name of, add
// it to the message
var _name = undefined;
if (ractive.component && (_name = ractive.component.name)) {
message = "<" + _name + "> " + message;
}
var node = undefined;
if (node = options.node || ractive.fragment && ractive.fragment.rendered && ractive.find("*")) {
args.push(node);
}
}
}
console.warn.apply(console, ["%cRactive.js: %c" + message, "color: rgb(114, 157, 52);", "color: rgb(85, 85, 85);"].concat(args));
};
log = function () {
console.log.apply(console, arguments);
};
})();
} else {
printWarning = log = welcome = noop;
}
function format(message, args) {
return message.replace(/%s/g, function () {
return args.shift();
});
}
function fatal(message) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
message = format(message, args);
throw new Error(message);
}
function logIfDebug() {
if (_Ractive.DEBUG) {
log.apply(null, arguments);
}
}
function warn(message) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
message = format(message, args);
printWarning(message, args);
}
function warnOnce(message) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
message = format(message, args);
if (alreadyWarned[message]) {
return;
}
alreadyWarned[message] = true;
printWarning(message, args);
}
function warnIfDebug() {
if (_Ractive.DEBUG) {
warn.apply(null, arguments);
}
}
function warnOnceIfDebug() {
if (_Ractive.DEBUG) {
warnOnce.apply(null, arguments);
}
}
// Error messages that are used (or could be) in multiple places
var badArguments = "Bad arguments";
var noRegistryFunctionReturn = "A function was specified for \"%s\" %s, but no %s was returned";
var missingPlugin = function (name, type) {
return "Missing \"" + name + "\" " + type + " plugin. You may need to download a plugin via http://docs.ractivejs.org/latest/plugins#" + type + "s";
};
function findInViewHierarchy(registryName, ractive, name) {
var instance = findInstance(registryName, ractive, name);
return instance ? instance[registryName][name] : null;
}
function findInstance(registryName, ractive, name) {
while (ractive) {
if (name in ractive[registryName]) {
return ractive;
}
if (ractive.isolated) {
return null;
}
ractive = ractive.parent;
}
}
var interpolate = function (from, to, ractive, type) {
if (from === to) {
return snap(to);
}
if (type) {
var interpol = findInViewHierarchy("interpolators", ractive, type);
if (interpol) {
return interpol(from, to) || snap(to);
}
fatal(missingPlugin(type, "interpolator"));
}
return static_interpolators.number(from, to) || static_interpolators.array(from, to) || static_interpolators.object(from, to) || snap(to);
};
var shared_interpolate = interpolate;
function snap(to) {
return function () {
return to;
};
}
var interpolators = {
number: function (from, to) {
var delta;
if (!is__isNumeric(from) || !is__isNumeric(to)) {
return null;
}
from = +from;
to = +to;
delta = to - from;
if (!delta) {
return function () {
return from;
};
}
return function (t) {
return from + t * delta;
};
},
array: function (from, to) {
var intermediate, interpolators, len, i;
if (!isArray(from) || !isArray(to)) {
return null;
}
intermediate = [];
interpolators = [];
i = len = Math.min(from.length, to.length);
while (i--) {
interpolators[i] = shared_interpolate(from[i], to[i]);
}
// surplus values - don't interpolate, but don't exclude them either
for (i = len; i < from.length; i += 1) {
intermediate[i] = from[i];
}
for (i = len; i < to.length; i += 1) {
intermediate[i] = to[i];
}
return function (t) {
var i = len;
while (i--) {
intermediate[i] = interpolators[i](t);
}
return intermediate;
};
},
object: function (from, to) {
var properties, len, interpolators, intermediate, prop;
if (!isObject(from) || !isObject(to)) {
return null;
}
properties = [];
intermediate = {};
interpolators = {};
for (prop in from) {
if (hasOwn.call(from, prop)) {
if (hasOwn.call(to, prop)) {
properties.push(prop);
interpolators[prop] = shared_interpolate(from[prop], to[prop]);
} else {
intermediate[prop] = from[prop];
}
}
}
for (prop in to) {
if (hasOwn.call(to, prop) && !hasOwn.call(from, prop)) {
intermediate[prop] = to[prop];
}
}
len = properties.length;
return function (t) {
var i = len,
prop;
while (i--) {
prop = properties[i];
intermediate[prop] = interpolators[prop](t);
}
return intermediate;
};
}
};
var static_interpolators = interpolators;
// This function takes a keypath such as 'foo.bar.baz', and returns
// all the variants of that keypath that include a wildcard in place
// of a key, such as 'foo.bar.*', 'foo.*.baz', 'foo.*.*' and so on.
// These are then checked against the dependants map (ractive.viewmodel.depsMap)
// to see if any pattern observers are downstream of one or more of
// these wildcard keypaths (e.g. 'foo.bar.*.status')
var utils_getPotentialWildcardMatches = getPotentialWildcardMatches;
var starMaps = {};
function getPotentialWildcardMatches(keypath) {
var keys, starMap, mapper, i, result, wildcardKeypath;
keys = keypath.split(".");
if (!(starMap = starMaps[keys.length])) {
starMap = getStarMap(keys.length);
}
result = [];
mapper = function (star, i) {
return star ? "*" : keys[i];
};
i = starMap.length;
while (i--) {
wildcardKeypath = starMap[i].map(mapper).join(".");
if (!result.hasOwnProperty(wildcardKeypath)) {
result.push(wildcardKeypath);
result[wildcardKeypath] = true;
}
}
return result;
}
// This function returns all the possible true/false combinations for
// a given number - e.g. for two, the possible combinations are
// [ true, true ], [ true, false ], [ false, true ], [ false, false ].
// It does so by getting all the binary values between 0 and e.g. 11
function getStarMap(num) {
var ones = "",
max,
binary,
starMap,
mapper,
i,
j,
l,
map;
if (!starMaps[num]) {
starMap = [];
while (ones.length < num) {
ones += 1;
}
max = parseInt(ones, 2);
mapper = function (digit) {
return digit === "1";
};
for (i = 0; i <= max; i += 1) {
binary = i.toString(2);
while (binary.length < num) {
binary = "0" + binary;
}
map = [];
l = binary.length;
for (j = 0; j < l; j++) {
map.push(mapper(binary[j]));
}
starMap[i] = map;
}
starMaps[num] = starMap;
}
return starMaps[num];
}
var refPattern = /\[\s*(\*|[0-9]|[1-9][0-9]+)\s*\]/g;
var patternPattern = /\*/;
var keypathCache = {};
var Keypath = function (str) {
var keys = str.split(".");
this.str = str;
if (str[0] === "@") {
this.isSpecial = true;
this.value = decodeKeypath(str);
}
this.firstKey = keys[0];
this.lastKey = keys.pop();
this.isPattern = patternPattern.test(str);
this.parent = str === "" ? null : getKeypath(keys.join("."));
this.isRoot = !str;
};
Keypath.prototype = {
equalsOrStartsWith: function (keypath) {
return keypath === this || this.startsWith(keypath);
},
join: function (str) {
return getKeypath(this.isRoot ? String(str) : this.str + "." + str);
},
replace: function (oldKeypath, newKeypath) {
if (this === oldKeypath) {
return newKeypath;
}
if (this.startsWith(oldKeypath)) {
return newKeypath === null ? newKeypath : getKeypath(this.str.replace(oldKeypath.str + ".", newKeypath.str + "."));
}
},
startsWith: function (keypath) {
if (!keypath) {
// TODO under what circumstances does this happen?
return false;
}
return keypath && this.str.substr(0, keypath.str.length + 1) === keypath.str + ".";
},
toString: function () {
throw new Error("Bad coercion");
},
valueOf: function () {
throw new Error("Bad coercion");
},
wildcardMatches: function () {
return this._wildcardMatches || (this._wildcardMatches = utils_getPotentialWildcardMatches(this.str));
}
};
function assignNewKeypath(target, property, oldKeypath, newKeypath) {
var existingKeypath = target[property];
if (existingKeypath && (existingKeypath.equalsOrStartsWith(newKeypath) || !existingKeypath.equalsOrStartsWith(oldKeypath))) {
return;
}
target[property] = existingKeypath ? existingKeypath.replace(oldKeypath, newKeypath) : newKeypath;
return true;
}
function decodeKeypath(keypath) {
var value = keypath.slice(2);
if (keypath[1] === "i") {
return is__isNumeric(value) ? +value : value;
} else {
return value;
}
}
function getKeypath(str) {
if (str == null) {
return str;
}
// TODO it *may* be worth having two versions of this function - one where
// keypathCache inherits from null, and one for IE8. Depends on how
// much of an overhead hasOwnProperty is - probably negligible
if (!keypathCache.hasOwnProperty(str)) {
keypathCache[str] = new Keypath(str);
}
return keypathCache[str];
}
function getMatchingKeypaths(ractive, keypath) {
var keys, key, matchingKeypaths;
keys = keypath.str.split(".");
matchingKeypaths = [rootKeypath];
while (key = keys.shift()) {
if (key === "*") {
// expand to find all valid child keypaths
matchingKeypaths = matchingKeypaths.reduce(expand, []);
} else {
if (matchingKeypaths[0] === rootKeypath) {
// first key
matchingKeypaths[0] = getKeypath(key);
} else {
matchingKeypaths = matchingKeypaths.map(concatenate(key));
}
}
}
return matchingKeypaths;
function expand(matchingKeypaths, keypath) {
var wrapper, value, keys;
if (keypath.isRoot) {
keys = [].concat(Object.keys(ractive.viewmodel.data), Object.keys(ractive.viewmodel.mappings), Object.keys(ractive.viewmodel.computations));
} else {
wrapper = ractive.viewmodel.wrapped[keypath.str];
value = wrapper ? wrapper.get() : ractive.viewmodel.get(keypath);
keys = value ? Object.keys(value) : null;
}
if (keys) {
keys.forEach(function (key) {
if (key !== "_ractive" || !isArray(value)) {
matchingKeypaths.push(keypath.join(key));
}
});
}
return matchingKeypaths;
}
}
function concatenate(key) {
return function (keypath) {
return keypath.join(key);
};
}
function normalise(ref) {
return ref ? ref.replace(refPattern, ".$1") : "";
}
var rootKeypath = getKeypath("");
var shared_add = add;
var shared_add__errorMessage = "Cannot add to a non-numeric value";
function add(root, keypath, d) {
if (typeof keypath !== "string" || !is__isNumeric(d)) {
throw new Error("Bad arguments");
}
var value = undefined,
changes = undefined;
if (/\*/.test(keypath)) {
changes = {};
getMatchingKeypaths(root, getKeypath(normalise(keypath))).forEach(function (keypath) {
var value = root.viewmodel.get(keypath);
if (!is__isNumeric(value)) {
throw new Error(shared_add__errorMessage);
}
changes[keypath.str] = value + d;
});
return root.set(changes);
}
value = root.get(keypath);
if (!is__isNumeric(value)) {
throw new Error(shared_add__errorMessage);
}
return root.set(keypath, +value + d);
}
var prototype_add = Ractive$add;
function Ractive$add(keypath, d) {
return shared_add(this, keypath, d === undefined ? 1 : +d);
}
var requestAnimationFrame;
// If window doesn't exist, we don't need requestAnimationFrame
if (typeof window === "undefined") {
requestAnimationFrame = null;
} else {
// https://gist.github.com/paulirish/1579671
(function (vendors, lastTime, window) {
var x, setTimeout;
if (window.requestAnimationFrame) {
return;
}
for (x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x] + "RequestAnimationFrame"];
}
if (!window.requestAnimationFrame) {
setTimeout = window.setTimeout;
window.requestAnimationFrame = function (callback) {
var currTime, timeToCall, id;
currTime = Date.now();
timeToCall = Math.max(0, 16 - (currTime - lastTime));
id = setTimeout(function () {
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
}
})(vendors, 0, window);
requestAnimationFrame = window.requestAnimationFrame;
}