-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathko-slots.js
159 lines (136 loc) · 4.04 KB
/
ko-slots.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
if (ko.isObservableArray === undefined) {
ko.isObservableArray = function(instance) {
return ko.isObservable(instance) &&
typeof instance["remove"] === "function" &&
typeof instance["push"] === "function";
};
}
function arraySlotsExtender(target, options) {
options = options || {
key: null
};
var items;
if (ko.isObservableArray(target))
items = target;
else if (ko.isObservable(target))
items = target.extend({
trackArrayChanges: true
});
else
items = ko.observableArray(target);
var defaultPush = items.push.bind(items);
var defaultRemove = items.remove.bind(items);
var map = {};
var slots = {};
var key; // fn
if (options.key === null) {
var key = function(item) {
return item
};
} else if (typeof options.key === "function") {
var key = options.key;
} else {
var key = function(item) {
return ko.unwrap(item[options.key]);
}
}
function push(item) {
var k = key(item);
if (!(k in map)) {
defaultPush(item);
} else {
items.replace(get(k), item);
}
}
function removeKey(k) {
return k in map ? defaultRemove(get(k)) : [];
}
function containsKey(k) {
return (k in map);
}
function contains(item) {
return (key(item) in map);
}
function remove(item) {
return (typeof item === "function" || key(item) in map) ? defaultRemove(item) : [];
}
function get(k) {
return map[k];
}
function slot(k, defaultValue, options) {
var s = slots[k];
if (s === undefined) {
s = ko.observable(k in map ? map[k] : defaultValue);
s.defaultValue = defaultValue;
slots[k] = s;
if (!(options !== undefined && options.pure !== undefined && !options.pure)) {
var ss = s.subscribe(function() {
ss.dispose();
delete slots[k];
}, "asleep");
}
}
return s;
}
items.subscribe(function(changesList) {
changesList.forEach(function(change) {
var item = change.value;
var s, k = key(item);
if (change.status === 'added') {
map[k] = item;
(s = slots[k]) !== undefined && s(item);
} else {
delete map[k];
(s = slots[k]) !== undefined && s(s.defaultValue);
}
});
}, items, 'arrayChange');
items().forEach(function(item) {
map[key(item)] = item;
});
items.keyMap = map;
items.key = key;
items.containsKey = containsKey;
items.removeKey = removeKey;
items.contains = contains;
items.push = push;
items.add = push;
items.remove = remove;
items.get = get;
items.slot = slot;
return items;
};
function slotExtender(target, options) {
var slots = {};
function slot(value) {
var s = slots[value];
if (s === undefined) {
s = ko.observable(target.peek() === value);
slots[value] = s;
if (!(options !== undefined && options.pure !== undefined && !options.pure)) {
var ss = s.subscribe(function() {
ss.dispose();
delete slots[value];
}, "asleep");
}
}
return s;
}
var currentValue = target();
target.subscribe(function(value) {
if (value === currentValue)
return;
var s;
(s = slots[currentValue]) !== undefined && s(false);
(s = slots[value]) !== undefined && s(true);
currentValue = value;
});
target.slot = slot;
return target;
};
ko.observableCollection = function(array, options) {
return arraySlotsExtender(array, options);
};
ko.extenders.slots = function(target, options) {
return (ko.isObservableArray(target) ? arraySlotsExtender(target) : slotExtender(target));
};