-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkoExtenders.js
More file actions
303 lines (268 loc) · 10.1 KB
/
koExtenders.js
File metadata and controls
303 lines (268 loc) · 10.1 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
// � by Caspar Goeke and Holger Finger
/**
* extends a knockout observableArray with the ability to retrieve elements by their id.
*
* @param target - ko.observableArray
* @param option -
* @returns {*}
*/
ko.extenders.sortById = function (target, option) {
target.byId = {};
/**
* insert only pushes to the list if this entity does not exist yet.
* @param entity
* @returns true if it was inserted:
*/
target.insertIfNotExist = function (entity) {
if (option && option.hasOwnProperty("id_field")) {
var id = entity[option.id_field];
}
else {
var id = entity.id;
}
if (ko.isObservable(id)) {
id = id();
}
if (!target.byId.hasOwnProperty(id)) {
target.push(entity);
return true;
}
else {
return false;
}
};
target.subscribe(function (changes) {
var id;
for (var i = 0, len = changes.length; i < len; i++) {
if (changes[i].hasOwnProperty('moved')) {
// ignore moves in the unsorted array:
continue;
}
var status = changes[i].status;
var entity = changes[i].value;
if (option && option.hasOwnProperty("id_field")) {
id = entity[option.id_field];
}
else {
id = entity.id;
}
if (ko.isObservable(id)) {
id = id();
}
if (status == 'added') {
if (id && target.byId.hasOwnProperty(id)) {
if (option && option.hasOwnProperty("do_not_warn_when_double_entries") && option["do_not_warn_when_double_entries"]) {
// warning is not issued.
}
else {
console.warn("id was already in list and appears now twice in observableArray (extended with sortByID).")
}
}
target.byId[id] = entity;
}
else if (status == 'deleted') {
delete target.byId[id];
}
}
/* this is the bruteforce approach where we rebuild the full list, instead of only the changes:
target.byId = {};
var arrayElem = target();
for (var i= 0, len=arrayElem.length; i<len; i++) {
target.byId[arrayElem[i].id()] = arrayElem[i];
}
*/
}, null, "arrayChange");
return target;
};
/**
* extend a ko.observable so that it is numeric and rounded to a specific precision.
*
* @param {ko.observable} target - the observable where this is applied
* @param {number} precision - the number of floating point digits
*/
ko.extenders.numeric = function (target, precision) {
//create a writable computed observable to intercept writes to our observable
var result = ko.pureComputed({
read: target, //always return the original observables value
write: function (newValue) {
var current = target(),
roundingMultiplier = Math.pow(10, precision),
newValueAsNum = isNaN(newValue) ? 0 : parseFloat(+newValue),
valueToWrite = Math.round(newValueAsNum * roundingMultiplier) / roundingMultiplier;
//only write if it changed
if (valueToWrite !== current) {
target(valueToWrite);
} else {
//if the rounded value is the same, but a different value was written, force a notification for the current field
if (newValueAsNum !== current) {
target.notifySubscribers(valueToWrite);
}
}
}
}).extend({ notify: 'always' });
//initialize with current value to make sure it is rounded appropriately
result(target());
//return the new computed observable
return result;
};
ko.bindingHandlers.tooltip = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
if ($(element) != undefined) {
var contElem = $(valueAccessor());
$(element).tooltip({
items: 'span',
track: true,
content: function () {
return contElem.html();
}
});
}
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
if ($(element) != undefined) {
$(element).tooltip("destroy");
var contElem = $(valueAccessor());
$(element).tooltip({
items: 'span',
track: true,
content: function () {
return contElem.html();
}
});
}
}
};
ko.bindingHandlers.disableOptionsCaption = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
ko.applyBindingsToNode(element, {
options: viewModel.items,
optionsCaption: 'please select',
optionsAfterRender: function (option, item) {
ko.applyBindingsToNode(option, {
disable: !item
}, item);
}
});
}
};
// CKEDITOR is not defined in player:
if (typeof CKEDITOR !== 'undefined') {
CKEDITOR.on('currentInstance', function () {
var instance = CKEDITOR.currentInstance;
if (instance != null) {
$("#editorToolbar").show();
}
});
// disable auto inline editing for CKeditor, because the custom binding below is doing this manually:
CKEDITOR.disableAutoInline = true;
ko.bindingHandlers.wysiwyg = {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
var ckEditorValue = valueAccessor();
var $element = $(element);
$element.attr('contenteditable', true);
var ignoreChanges = false;
var selectAll = allBindings.get('selectAll');
var instance = CKEDITOR.inline(element, {
customConfig: '/assets/js/ckeditor_config.js?FILE_VERSION_PLACEHOLDER',
on: {
change: function () {
ignoreChanges = true;
ckEditorValue(instance.getData());
ignoreChanges = false;
},
instanceReady: function () {
if (selectAll) {
//instance.execCommand( 'selectAll' );
}
}
}
});
instance.parentViewModel = viewModel;
instance.on('blur', function (e) {
$("#editorToolbar").hide();
if (viewModel.hasOwnProperty('dataModel') && viewModel.dataModel.hasOwnProperty("editText")) {
// This snippet is needed, so that clicking somewhere else will disable the edit and reediting requires a new double click.
// Otherwise there would be a bug where default trial could overwrite the edited text!
viewModel.dataModel.editText(false);
}
});
viewModel.ckInstance = instance;
instance.setData(ckEditorValue());
ckEditorValue.subscribe(function (newValue) {
if (!ignoreChanges) {
instance.setData(newValue);
}
});
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
instance.updateElement();
instance.focusManager.blur(true);
instance.destroy();
});
//instance.focus( );
}
};
}
ko.bindingHandlers.selected = {
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var selected = ko.utils.unwrapObservable(valueAccessor());
if (selected) element.select();
}
};
ko.bindingHandlers.singleClick = {
init: function (element, valueAccessor) {
var handler = valueAccessor(),
delay = 200,
clickTimeout = false;
$(element).click(function () {
if (clickTimeout !== false) {
clearTimeout(clickTimeout);
clickTimeout = false;
} else {
clickTimeout = setTimeout(function () {
clickTimeout = false;
handler();
}, delay);
}
});
}
};
// Convert Javascript date to Pg YYYY-MM-DD HH:MI:SS-08
function pgFormatDate(date) {
function zeroPad(d) {
return ("0" + d).slice(-2);
}
var timeZoneRemainingMinutes;
var timeZoneOffsetInHours = date.getTimezoneOffset() / 60;
var dayString = [date.getUTCFullYear(), zeroPad(date.getMonth() + 1), zeroPad(date.getDate())].join("-");
var timeString = [zeroPad(date.getHours()), zeroPad(date.getMinutes()), zeroPad(date.getSeconds())].join(":");
if (timeZoneOffsetInHours > 0) {
// WARNING: according to javascript spec's, the timezone has inverted sign, so we invert + to - and - to +
timeZoneRemainingMinutes = timeZoneOffsetInHours % 1;
timeZoneOffsetInHours = "-" + zeroPad(Math.floor(timeZoneOffsetInHours));
}
else if (timeZoneOffsetInHours < 0) {
timeZoneOffsetInHours = -timeZoneOffsetInHours;
timeZoneRemainingMinutes = timeZoneOffsetInHours % 1;
timeZoneOffsetInHours = "+" + zeroPad(Math.floor(timeZoneOffsetInHours));
}
else {
timeZoneRemainingMinutes = 0;
timeZoneOffsetInHours = "+00";
}
if (timeZoneRemainingMinutes != 0) {
timeZoneOffsetInHours = timeZoneOffsetInHours + ":" + zeroPad(Math.floor(timeZoneRemainingMinutes * 60));
}
return dayString + " " + timeString + timeZoneOffsetInHours;
}
ko.unapplyBindings = function ($node, remove) {
// unbind events
$node.find("*").each(function () {
$(this).unbind();
});
// Remove KO subscriptions and references
if (remove) {
ko.removeNode($node[0]);
} else {
ko.cleanNode($node[0]);
}
};