forked from CodeSeven/KoLite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knockout.command.js
152 lines (123 loc) · 4.82 KB
/
knockout.command.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
// By: Hans Fjällemark and John Papa
// 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, ko);
}
}(function (ko, exports) {
if (typeof (ko) === undefined) {
throw 'Knockout is required, please ensure it is loaded before loading the commanding plug-in';
}
function wrapAccessor(accessor) {
return function () {
return accessor;
};
};
exports.command = function(options) {
var
self = function() {
return self.execute.apply(this, arguments);
},
canExecuteDelegate = options.canExecute,
executeDelegate = options.execute;
self.canExecute = ko.computed(function() {
return canExecuteDelegate ? canExecuteDelegate() : true;
});
self.execute = function (arg1, arg2) {
// Needed for anchors since they don't support the disabled state
if (!self.canExecute()) return
return executeDelegate.apply(this, [arg1, arg2]);
};
return self;
};
exports.asyncCommand = function(options) {
var
self = function() {
return self.execute.apply(this, arguments);
},
canExecuteDelegate = options.canExecute,
executeDelegate = options.execute,
completeCallback = function() {
self.isExecuting(false);
};
self.isExecuting = ko.observable();
self.canExecute = ko.computed(function() {
return canExecuteDelegate ? canExecuteDelegate(self.isExecuting()) : !self.isExecuting();
});
self.execute = function (arg1, arg2) {
// Needed for anchors since they don't support the disabled state
if (!self.canExecute()) return
var args = []; // Allow for these arguments to be passed on to execute delegate
if (executeDelegate.length >= 2) {
args.push(arg1);
}
if (executeDelegate.length >= 3) {
args.push(arg2);
}
args.push(completeCallback);
self.isExecuting(true);
return executeDelegate.apply(this, args);
};
return self;
};
ko.bindingHandlers.command = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var
value = valueAccessor(),
commands = value.execute ? { click: value } : value,
isBindingHandler = function (handler) {
return ko.bindingHandlers[handler] !== undefined;
},
initBindingHandlers = function () {
for (var command in commands) {
if (!isBindingHandler(command)) {
continue;
};
ko.bindingHandlers[command].init(
element,
wrapAccessor(commands[command].execute),
allBindingsAccessor,
viewModel,
bindingContext
);
}
},
initEventHandlers = function () {
var events = {};
for (var command in commands) {
if (!isBindingHandler(command)) {
events[command] = commands[command].execute;
}
}
ko.bindingHandlers.event.init(
element,
wrapAccessor(events),
allBindingsAccessor,
viewModel,
bindingContext);
};
initBindingHandlers();
initEventHandlers();
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var commands = valueAccessor();
var canExecute = commands.canExecute;
if (!canExecute) {
for (var command in commands) {
if (commands[command].canExecute) {
canExecute = commands[command].canExecute;
break;
}
}
}
if (!canExecute) {
return;
}
ko.bindingHandlers.enable.update(element, canExecute, allBindingsAccessor, viewModel, bindingContext);
}
};
}));