-
Notifications
You must be signed in to change notification settings - Fork 0
/
cb-ko-binding-chosen.js
229 lines (207 loc) · 10.9 KB
/
cb-ko-binding-chosen.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
/*
binding format
data-bind = "table: {
chosenOption: *** or {***} //the option passed to chosen function when create the chosen
source: ***, //source items
valueProp: *** //the property of the items in source that will be used as the value of the option
selectedValue: *** //the value that user is selected, it can be array
selectedValueItemProp: the prop of the items in selectedValue array, that will be used to match the value in source
displayProp: the property of the items in source that will be used as the text of the option, it also can be a valid expression, the "this" is current item, and we can also use $parent, $parents, $data and $root
}"
*/
(function () {
var _ = {
UO: ko.utils.unwrapObservable
};
_.CO = ko.bindingHandlers.chosen = {
elementMarkClass: "ko-bindingHandlers-chosen-element",
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
if ($(element).prop("tagName") !== "SELECT") {
throw "chosen binding can be applied only for select";
}
$(element).addClass("chosen-select");
$(element).addClass(_.CO.elementMarkClass);
var value = valueAccessor();
_.CO.updateSelect(element, value, bindingContext, viewModel);
var chosen;
//this is a bug of chosen, that is if the html havn't been show, then the chosen control (div) will be set as 0px width
//to fix this, we have manually get the original select width and use it as the option to set chosen
var chosenOpt = { width: $(element).outerWidth() + "px" };
if (value.chosenOption) {
chosenOpt = $.extend(chosenOpt, _.UO(value.chosenOption));
}
chosen = $(element).chosen(chosenOpt);
//hack: if the any parent of chosen css is set to "overflow: hidden",
//and the parent height is less to show the full chosen input and drop down, then the drowdown can't be show
//here we add a div in side the form, as the root container, then it is ok
if ($(element).parent()) {
//on a modal
}
chosen.change(function (event, data) {
_.CO.updateValue(value, $(element).val(), element, allBindingsAccessor);
//if (ko.isObservable(value.selectedValue)) {
// value.selectedValue(data.selected);
//} else {
// throw 'selectedValue must be bound to an observable field';
// //we throw this exception is becasue ,value is the binding value, will will copy from the specified property of viewModel
// //and if it is not a observable, then just value copy, which will not change the copied property of viewMode
// //value.selectedValue = data.selected;
//}
});
},
//set the bound view model property value to the selected value
updateValue: function (databoundValue, selectedOptionValue, element, allBindingsAccessor) {
//the value of option is alwasy the index so we must convet it to the corresponding data item
if ($.isArray(selectedOptionValue) && databoundValue.selectedValueItemProp) {
throw "Not supported yet";
}
var selectedOptionValueArray = [];
if ($.isArray(selectedOptionValue)) {
selectedOptionValueArray = selectedOptionValue;
} else {
selectedOptionValueArray.push(selectedOptionValue);
}
var tmpSetValueArray = [];
for (var i = 0; i < selectedOptionValueArray.length; i++) {
if (typeof (selectedOptionValueArray[i]) === "undefined" || selectedOptionValueArray[i] == null) {
continue;
}
var optionValue = $(element).children("[value=" + selectedOptionValueArray[i] + "]").data("item");
if (typeof (_.UO(databoundValue.valueProp)) !== "undefined") {
optionValue = _.UO(optionValue[_.UO(databoundValue.valueProp)]);
}
tmpSetValueArray.push(optionValue);
}
if (ko.isObservable(databoundValue.selectedValue)) {
//if multi select, then the target must be an observable array, so just update it with the array
if ($(element).prop("multiple")) {
databoundValue.selectedValue(tmpSetValueArray);
} else {
databoundValue.selectedValue(tmpSetValueArray[0]);
}
////if multi select, then the target must be an observable array, so just update it with the array
//if (tmpSetValueArray.length > 1) {
// databoundValue.selectedValue(tmpSetValueArray);
//} else {
// //check if target is an array
// if (databoundValue.selectedValue.push) {
// databoundValue.selectedValue(tmpSetValueArray);
// } else {
// databoundValue.selectedValue(tmpSetValueArray[0]);
// }
//}
} else {
throw 'selectedValue must be bound to an observable field';
//we throw this exception is becasue ,value is the binding value, will will copy from the specified property of viewModel
//and if it is not a observable, then just value copy, which will not change the copied property of viewMode
//databoundValue.selectedValue = selectedOptionValue;
}
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
_.CO.updateSelect(element, valueAccessor(), bindingContext, viewModel);
//for the case, if the selected value is not any option, but the chose is set to single select mode, then it will select the frist value, but won't trigger change event
//so we force the value are always same between the element and the viewmodel
_.CO.updateValue(valueAccessor(), $(element).val(), element, allBindingsAccessor);
$(element).trigger("chosen:updated");
},
updateSelect: function (element, bindData, bindingContext, viewModel) {
element = $(element);
element.empty();
var value = _.UO(bindData);
var source = _.UO(value.source);
if (typeof (source) === "undefined" || source == null) {
source = [];
}
var valueProp = _.UO(value.valueProp);
var selectedValue = _.UO(value.selectedValue);
var displayProp = _.UO(value.displayProp);
var selectedValueItemProp = _.UO(value.selectedValueItemProp);
var selectedValueArray = [];
if ($.isArray(selectedValue)) {
selectedValueArray = selectedValue;
} else {
selectedValueArray.push(selectedValue);
}
function inArray(array, arrayItemProp, searchValue) {
for (var j = 0; j < array.length; j++) {
var item = array[j];
if (arrayItemProp) {
item = item[arrayItemProp];
}
if (item == searchValue) {
return true;
}
}
return false;
}
for (var i = 0; i < source.length; i++) {
var sourceItemValue = _.UO(source[i]);
if (valueProp) {
sourceItemValue = _.UO(source[i][valueProp]);
}
var displayValue = _.UO(source[i]);
if (displayProp) {
if ((displayProp in displayValue)) {
displayValue = _.UO(displayValue[displayProp]);
} else {
displayValue = (function () {
var func;
if (element[0][displayProp]) {
func = element[0][displayProp];
} else {
var funcBody = "with ($bindingContext){with($bindingContextOverride) {with ($currentItem) {return " + displayProp + ";}}}"
element[0][displayProp] = func = new Function("$bindingContext", "$currentItem", "$bindingContextOverride", funcBody);
}
var bindingContextOverride = {
$parents: bindingContext.$parents.slice(0),
$data: displayValue,
$parent: viewModel
};
bindingContextOverride.$parents.splice(0, 0, viewModel);
return func(bindingContext, displayValue, bindingContextOverride);
})();
}
} else { //if displayProp is empty, it means directly show the current item
}
var opt;
if (inArray(selectedValueArray, selectedValueItemProp, sourceItemValue)) {
opt = $('<option selected="selected" value="' + i + '">' + displayValue + '</option>');
} else {
opt = $('<option value="' + i + '">' + displayValue + '</option>');
}
opt.data("item", _.UO(source[i]));
element.append(opt);
}
}
};
$(document).ready(function () {
if (typeof (tableInlineEditFinishHandler) !== "undefined") {
tableInlineEditFinishHandler.registerHandler("." + _.CO.elementMarkClass, function ($currentCell, $editor, $endEdit) {
var chosenDiv = $editor.next().filter(".chosen-container");
if (chosenDiv.length < 0) {
throw "Can't find chosen container!"
}
chosenDiv = $(chosenDiv[0]);
if (!chosenDiv.attr("tabindex")) {
chosenDiv.attr("tabindex", -1);
}
$editor.on("chosen:hiding_dropdown", function () {
setTimeout(function () {
chosenDiv.focus();
}, 100);
});
chosenDiv.blur(function (event, triggerReason) {
if (triggerReason === ko.bindingHandlers.tableCRUD.triggerBlurForKOUpdate) {
return;
}
var drop = chosenDiv.find("div.chosen-drop");
if (drop.length > 0 && drop.position().left >= -100) {
return;
}
$endEdit();
});
chosenDiv.focus();
});
}
});
})();