|
| 1 | +// Knockout Observable Dictionary |
| 2 | +// (c) James Foster |
| 3 | +// License: MIT (http://www.opensource.org/licenses/mit-license.php) |
| 4 | + |
| 5 | +(function () { |
| 6 | + function DictionaryItem(key, value, dictionary) { |
| 7 | + var observableKey = new ko.observable(key); |
| 8 | + |
| 9 | + this.value = new ko.observable(value); |
| 10 | + this.key = new ko.computed({ |
| 11 | + read: observableKey, |
| 12 | + write: function (newKey) { |
| 13 | + var current = observableKey(); |
| 14 | + |
| 15 | + if (current == newKey) return; |
| 16 | + |
| 17 | + // no two items are allowed to share the same key. |
| 18 | + dictionary.remove(newKey); |
| 19 | + |
| 20 | + observableKey(newKey); |
| 21 | + } |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + ko.observableDictionary = function (dictionary, keySelector, valueSelector) { |
| 26 | + var result = {}; |
| 27 | + |
| 28 | + result.items = new ko.observableArray(); |
| 29 | + |
| 30 | + result._wrappers = {}; |
| 31 | + result._keySelector = keySelector || function (value, key) { return key; }; |
| 32 | + result._valueSelector = valueSelector || function (value) { return value; }; |
| 33 | + |
| 34 | + if (typeof keySelector == 'string') result._keySelector = function (value) { return value[keySelector]; }; |
| 35 | + if (typeof valueSelector == 'string') result._valueSelector = function (value) { return value[valueSelector]; }; |
| 36 | + |
| 37 | + ko.utils.extend(result, ko.observableDictionary['fn']); |
| 38 | + |
| 39 | + result.pushAll(dictionary); |
| 40 | + |
| 41 | + return result; |
| 42 | + }; |
| 43 | + |
| 44 | + ko.observableDictionary['fn'] = { |
| 45 | + remove: function (valueOrPredicate) { |
| 46 | + var predicate = valueOrPredicate; |
| 47 | + |
| 48 | + if (valueOrPredicate instanceof DictionaryItem) { |
| 49 | + predicate = function (item) { |
| 50 | + return item.key() === valueOrPredicate.key(); |
| 51 | + }; |
| 52 | + } |
| 53 | + else if (typeof valueOrPredicate != "function") { |
| 54 | + predicate = function (item) { |
| 55 | + return item.key() === valueOrPredicate; |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + ko.observableArray['fn'].remove.call(this.items, predicate); |
| 60 | + }, |
| 61 | + |
| 62 | + push: function (key, value) { |
| 63 | + var item = null; |
| 64 | + |
| 65 | + if (key instanceof DictionaryItem) { |
| 66 | + // handle the case where only a DictionaryItem is passed in |
| 67 | + item = key; |
| 68 | + value = key.value(); |
| 69 | + key = key.key(); |
| 70 | + } |
| 71 | + |
| 72 | + if (value === undefined) { |
| 73 | + value = this._valueSelector(key); |
| 74 | + key = this._keySelector(value); |
| 75 | + } |
| 76 | + else { |
| 77 | + value = this._valueSelector(value); |
| 78 | + } |
| 79 | + |
| 80 | + var current = this.get(key, false); |
| 81 | + if (current) { |
| 82 | + // update existing value |
| 83 | + current(value); |
| 84 | + return current; |
| 85 | + } |
| 86 | + |
| 87 | + if (!item) { |
| 88 | + item = new DictionaryItem(key, value, this); |
| 89 | + } |
| 90 | + |
| 91 | + ko.observableArray['fn'].push.call(this.items, item); |
| 92 | + |
| 93 | + return value; |
| 94 | + }, |
| 95 | + |
| 96 | + pushAll: function (dictionary) { |
| 97 | + var self = this; |
| 98 | + var items = self.items(); |
| 99 | + |
| 100 | + if (dictionary instanceof Array) { |
| 101 | + $.each(dictionary, function (index, item) { |
| 102 | + var key = self._keySelector(item, index); |
| 103 | + var value = self._valueSelector(item); |
| 104 | + items.push(new DictionaryItem(key, value, self)); |
| 105 | + }); |
| 106 | + } |
| 107 | + else { |
| 108 | + for (var prop in dictionary) { |
| 109 | + if (dictionary.hasOwnProperty(prop)) { |
| 110 | + var item = dictionary[prop]; |
| 111 | + var key = self._keySelector(item, prop); |
| 112 | + var value = self._valueSelector(item); |
| 113 | + items.push(new DictionaryItem(key, value, self)); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + self.items.valueHasMutated(); |
| 119 | + }, |
| 120 | + |
| 121 | + sort: function (method) { |
| 122 | + if (method === undefined) { |
| 123 | + method = function (a, b) { |
| 124 | + return defaultComparison(a.key(), b.key()); |
| 125 | + }; |
| 126 | + } |
| 127 | + |
| 128 | + return ko.observableArray['fn'].sort.call(this.items, method); |
| 129 | + }, |
| 130 | + |
| 131 | + indexOf: function (key) { |
| 132 | + if (key instanceof DictionaryItem) { |
| 133 | + return ko.observableArray['fn'].indexOf.call(this.items, key); |
| 134 | + } |
| 135 | + |
| 136 | + var underlyingArray = this.items(); |
| 137 | + for (var index = 0; index < underlyingArray.length; index++) { |
| 138 | + if (underlyingArray[index].key() == key) |
| 139 | + return index; |
| 140 | + } |
| 141 | + return -1; |
| 142 | + }, |
| 143 | + |
| 144 | + get: function (key, wrap) { |
| 145 | + if (wrap == false) |
| 146 | + return getValue(key, this.items()); |
| 147 | + |
| 148 | + var wrapper = this._wrappers[key]; |
| 149 | + |
| 150 | + if (wrapper == null) { |
| 151 | + wrapper = this._wrappers[key] = new ko.computed({ |
| 152 | + read: function () { |
| 153 | + var value = getValue(key, this.items()); |
| 154 | + return value ? value() : null; |
| 155 | + }, |
| 156 | + write: function (newValue) { |
| 157 | + var value = getValue(key, this.items()); |
| 158 | + |
| 159 | + if (value) |
| 160 | + value(newValue); |
| 161 | + else |
| 162 | + this.push(key, newValue); |
| 163 | + } |
| 164 | + }, this); |
| 165 | + } |
| 166 | + |
| 167 | + return wrapper; |
| 168 | + }, |
| 169 | + |
| 170 | + set: function (key, value) { |
| 171 | + return this.push(key, value); |
| 172 | + }, |
| 173 | + |
| 174 | + keys: function () { |
| 175 | + return ko.utils.arrayMap(this.items(), function (item) { return item.key(); }); |
| 176 | + }, |
| 177 | + |
| 178 | + values: function () { |
| 179 | + return ko.utils.arrayMap(this.items(), function (item) { return item.value(); }); |
| 180 | + }, |
| 181 | + |
| 182 | + removeAll: function () { |
| 183 | + this.items.removeAll(); |
| 184 | + }, |
| 185 | + |
| 186 | + toJSON: function () { |
| 187 | + var result = {}; |
| 188 | + var items = ko.utils.unwrapObservable(this.items); |
| 189 | + |
| 190 | + ko.utils.arrayForEach(items, function (item) { |
| 191 | + var key = ko.utils.unwrapObservable(item.key); |
| 192 | + var value = ko.utils.unwrapObservable(item.value); |
| 193 | + |
| 194 | + result[key] = value; |
| 195 | + }); |
| 196 | + |
| 197 | + return result; |
| 198 | + } |
| 199 | + }; |
| 200 | + |
| 201 | + function getValue(key, items) { |
| 202 | + var found = ko.utils.arrayFirst(items, function (item) { |
| 203 | + return item.key() == key; |
| 204 | + }); |
| 205 | + return found ? found.value : null; |
| 206 | + } |
| 207 | +})(); |
| 208 | + |
| 209 | + |
| 210 | +// Utility methods |
| 211 | +// --------------------------------------------- |
| 212 | +function isNumeric(n) { |
| 213 | + return !isNaN(parseFloat(n)) && isFinite(n); |
| 214 | +} |
| 215 | + |
| 216 | +function defaultComparison(a, b) { |
| 217 | + if (isNumeric(a) && isNumeric(b)) return a - b; |
| 218 | + |
| 219 | + a = a.toString(); |
| 220 | + b = b.toString(); |
| 221 | + |
| 222 | + return a == b ? 0 : (a < b ? -1 : 1); |
| 223 | +} |
| 224 | +// --------------------------------------------- |
0 commit comments