-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathko_extenders.js
249 lines (197 loc) · 5.64 KB
/
ko_extenders.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
/* Knockout extenders magic. */
ko.extenders.clamp = function clampObservable(obs, opts) {
var min = opts.min;
var max = opts.max;
var nullable = opts.nullable;
var floor = opts.floor;
var clamped = ko.computed({
read: obs,
write: function(val) {
if(val === '') {
if(!nullable) {
clamped.revert();
return;
}
else {
obs(null);
return;
}
}
if(isNaN(Number(val))) {
clamped.revert();
return;
}
if(min !== undefined && min !== null && val < min) {
val = min;
} else if(max !== undefined && max !== null && val > max) {
val = max;
}
if(floor) {
val = Math.floor(Number(val));
}
obs(Number(val));
clamped.revert();
}
});
clamped.revert = function () {
obs.notifySubscribers(obs());
clamped.notifySubscribers(clamped());
};
obs.clamped = clamped;
return obs;
}
ko.extenders.trim = function clampObservable(obs, opts) {
var trimmed = ko.computed({
read: obs,
write: function(val) {
if(val === '') {
obs(null);
}
obs(val.trim());
trimmed.revert();
}
});
trimmed.revert = function () {
obs.notifySubscribers(obs());
trimmed.notifySubscribers(trimmed());
};
obs.trimmed = trimmed;
return obs;
};
ko.extenders.throttle = function(obs, opts) {
var throttle = ko.observable(false);
var throttled = ko.computed(function() {
throttle();
return obs.peek();
});
var trigger = _.throttle(function() {
throttle(!throttle());
}, 1000);
obs.subscribeChanged(trigger);
obs.throttled = throttled;
return obs;
};
ko.extenders.track_prefill = function(obs, opts) {
obs.prefilled = ko.observable(false);
obs.prefill = function(val) {
if(!obs()) {
obs(val);
obs.prefilled(true);
}
}
obs.subscribe(function() {
obs.prefilled(false);
}, obs, 'change');
return obs;
};
ko.extenders.dirty_tracker = function(obs, opts) {
obs.saving = false;
obs.changed_during_save = false;
obs.chained = [];
obs.chained_to = [];
obs.soil_callbacks = [];
obs.save_start = function () {
obs.saving = true;
obs.changed_during_save = false;
};
obs.save_end = function() {
obs.saving = false;
if(!obs.changed_during_save) {
obs.clean();
}
};
obs.clean = function() {
obs(false); // No longer dirty.
for(var x = 0; x < obs.chained.length; x++) {
obs.chained[x].clean();
}
};
obs.soil = function(changer, oldValue, newValue) {
obs(true);
obs.changed_observable = changer;
obs.old_value = oldValue;
obs.new_value = newValue;
if(obs.saving) {
obs.changed_during_save = true;
}
for(var x = 0; x< obs.chained_to.length; x++) {
var dirty = obs.chained_to[x];
dirty.soil(changer, oldValue, newValue);
}
for(var x = 0; x < obs.soil_callbacks.length; x++) {
obs.soil_callbacks[x]();
}
};
obs.observable = function(val) {
return ko.observable(val).extend({'track_dirty': obs});
}
obs.chain_to = function(dirty) {
dirty.chained.push(obs);
obs.chained_to.push(dirty);
// obs.subscribeChanged(function(newValue, oldValue) {
// console.log("Chaining dirty");
// if(newValue && !oldValue) {
// dirty.soil(obs, obs.old_value, obs.new_value);
// }
// });
}
obs.onSoil = function(callback) {
obs.soil_callbacks.push(callback);
}
obs(false);
return obs;
}
ko.extenders.track_dirty = function(obs, dirty_bit) {
obs.subscribeChanged(function(newValue, oldValue) {
if(newValue === oldValue) {
// this happens sometimes, I think when I manually trigger notifications from other reverts.
return;
}
if(newValue === "" && oldValue === null) {
obs(null); // No, go back to null.
return;
}
if(newValue === null && oldValue === "") {
return;
// this is from me coercing it in the immediately preceding if.
}
dirty_bit.soil(obs, oldValue, newValue);
});
return obs;
}
kobe = function(opts) {
var val = opts.val;
opts = _.omit(opts, ['val']);
}
ko.observable.fn.toString = function() {
return "observable: " + ko.toJSON(this(), null, 2);
};
ko.computed.fn.toString = function() {
return "computed: " + ko.toJSON(this(), null, 2);
};
ko.observable.fn.inc = function(v) {
this(this() + v);
}
ko.observable.fn.toggle = function() {
this(!this());
}
/*
FILE ARCHIVED ON 15:24:24 Apr 13, 2020 AND RETRIEVED FROM THE
INTERNET ARCHIVE ON 16:47:17 Oct 13, 2021.
JAVASCRIPT APPENDED BY WAYBACK MACHINE, COPYRIGHT INTERNET ARCHIVE.
ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C.
SECTION 108(a)(3)).
*/
/*
playback timings (ms):
captures_list: 481.35
exclusion.robots: 0.228
exclusion.robots.policy: 0.211
RedisCDXSource: 3.965
esindex: 0.019
LoadShardBlock: 435.944 (3)
PetaboxLoader3.datanode: 441.961 (4)
CDXLines.iter: 34.637 (3)
load_resource: 199.084
PetaboxLoader3.resolve: 126.473
*/