Tab layout, first commit with new third party packages
[openemr.git] / public / assets / knockout-3-4-0 / src / binding / selectExtensions.js
blob450a27e949f1c3c8752061fb81918692a28537a1
1 (function () {
2     var hasDomDataExpandoProperty = '__ko__hasDomDataOptionValue__';
4     // Normally, SELECT elements and their OPTIONs can only take value of type 'string' (because the values
5     // are stored on DOM attributes). ko.selectExtensions provides a way for SELECTs/OPTIONs to have values
6     // that are arbitrary objects. This is very convenient when implementing things like cascading dropdowns.
7     ko.selectExtensions = {
8         readValue : function(element) {
9             switch (ko.utils.tagNameLower(element)) {
10                 case 'option':
11                     if (element[hasDomDataExpandoProperty] === true)
12                         return ko.utils.domData.get(element, ko.bindingHandlers.options.optionValueDomDataKey);
13                     return ko.utils.ieVersion <= 7
14                         ? (element.getAttributeNode('value') && element.getAttributeNode('value').specified ? element.value : element.text)
15                         : element.value;
16                 case 'select':
17                     return element.selectedIndex >= 0 ? ko.selectExtensions.readValue(element.options[element.selectedIndex]) : undefined;
18                 default:
19                     return element.value;
20             }
21         },
23         writeValue: function(element, value, allowUnset) {
24             switch (ko.utils.tagNameLower(element)) {
25                 case 'option':
26                     switch(typeof value) {
27                         case "string":
28                             ko.utils.domData.set(element, ko.bindingHandlers.options.optionValueDomDataKey, undefined);
29                             if (hasDomDataExpandoProperty in element) { // IE <= 8 throws errors if you delete non-existent properties from a DOM node
30                                 delete element[hasDomDataExpandoProperty];
31                             }
32                             element.value = value;
33                             break;
34                         default:
35                             // Store arbitrary object using DomData
36                             ko.utils.domData.set(element, ko.bindingHandlers.options.optionValueDomDataKey, value);
37                             element[hasDomDataExpandoProperty] = true;
39                             // Special treatment of numbers is just for backward compatibility. KO 1.2.1 wrote numerical values to element.value.
40                             element.value = typeof value === "number" ? value : "";
41                             break;
42                     }
43                     break;
44                 case 'select':
45                     if (value === "" || value === null)       // A blank string or null value will select the caption
46                         value = undefined;
47                     var selection = -1;
48                     for (var i = 0, n = element.options.length, optionValue; i < n; ++i) {
49                         optionValue = ko.selectExtensions.readValue(element.options[i]);
50                         // Include special check to handle selecting a caption with a blank string value
51                         if (optionValue == value || (optionValue == "" && value === undefined)) {
52                             selection = i;
53                             break;
54                         }
55                     }
56                     if (allowUnset || selection >= 0 || (value === undefined && element.size > 1)) {
57                         element.selectedIndex = selection;
58                     }
59                     break;
60                 default:
61                     if ((value === null) || (value === undefined))
62                         value = "";
63                     element.value = value;
64                     break;
65             }
66         }
67     };
68 })();
70 ko.exportSymbol('selectExtensions', ko.selectExtensions);
71 ko.exportSymbol('selectExtensions.readValue', ko.selectExtensions.readValue);
72 ko.exportSymbol('selectExtensions.writeValue', ko.selectExtensions.writeValue);