-
Notifications
You must be signed in to change notification settings - Fork 49
/
knockout.activity.js
58 lines (49 loc) · 1.86 KB
/
knockout.activity.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
// By: Hans Fjällemark and John Papa
// Rewritten by Alex Cornejo
// https://github.com/CodeSeven/KoLite
(function (factory) {
if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
factory(require('knockout'), exports);
} else if (typeof define === 'function' && define.amd) {
define(['knockout', 'exports'], factory);
} else {
factory(ko);
}
}(function (ko) {
if (typeof ko === 'undefined') {
throw 'Knockout is required, please ensure it is loaded before loading the activity plug-in';
}
function getOptions(overrides) {
var options = ko.utils.extend({}, ko.bindingHandlers.activity.defaultOptions);
return ko.utils.extend(options, overrides);
}
ko.bindingHandlers.activity = {
init: function (element, valueAccessor, allBindingsAccessor) {
var
options = getOptions(allBindingsAccessor().activityOptions),
activityIndicator = document.createElement(options.container);
element.activityIndicator = activityIndicator;
element.insertBefore(activityIndicator, element.firstChild);
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
element.removeChild(activityIndicator);
delete element.activityIndicator;
});
},
update: function (element, valueAccessor, allBindingsAccessor) {
var
options = getOptions(allBindingsAccessor().activityOptions),
value = ko.utils.unwrapObservable(valueAccessor()),
activity = typeof value === 'function' ? value() : value;
if (activity) {
element.activityIndicator.className = options.activityClass;
} else {
element.activityIndicator.className = options.inactiveClass;
}
}
};
ko.bindingHandlers.activity.defaultOptions = {
activityClass: 'fa fa-spinner fa-spin',
container: 'i',
inactiveClass: ''
};
}));