|
20 | 20 | if (Ember.libraries) Ember.libraries.register('Ember-CPM', EmberCPM.VERSION); |
21 | 21 |
|
22 | 22 | }).call(undefined, this, this.Ember, this.jQuery); |
| 23 | + |
| 24 | +(function(window, Ember, $, EmberCPM) { |
| 25 | + var a_slice = Array.prototype.slice, |
| 26 | + a_forEach = Ember.ArrayPolyfills.forEach, |
| 27 | + get = Ember.get; |
| 28 | + |
| 29 | + /* |
| 30 | + Returns the index where an item is to be removed from, or placed into, for |
| 31 | + an EmberCPM.Macros.concat array. |
| 32 | +
|
| 33 | + This is the index of the item within its dependent array, offset by the |
| 34 | + lengths of all prior dependent arrays. |
| 35 | + */ |
| 36 | + function getIndex(changeMeta, instanceMeta, dependentArrayDelta) { |
| 37 | + var dependentArrayGuid = Ember.guidFor(changeMeta.arrayChanged); |
| 38 | + |
| 39 | + if (!(dependentArrayGuid in instanceMeta.dependentGuidToIndex)) { |
| 40 | + recomputeGuidIndexes(instanceMeta, changeMeta.property._dependentKeys, this); |
| 41 | + } |
| 42 | + |
| 43 | + var dependentArrayLengths = instanceMeta.dependentArrayLengths, |
| 44 | + dependentArrayIndex = instanceMeta.dependentGuidToIndex[dependentArrayGuid], |
| 45 | + offset = 0, |
| 46 | + arrayIndex; |
| 47 | + |
| 48 | + // offset is the sum of the lengths of arrays to our left |
| 49 | + for (var i = 0; i < dependentArrayIndex; ++i) { |
| 50 | + offset += (dependentArrayLengths[i] || 0); |
| 51 | + } |
| 52 | + |
| 53 | + arrayIndex = offset + changeMeta.index; |
| 54 | + dependentArrayLengths[dependentArrayIndex] = (get(changeMeta.arrayChanged, 'length') || 0) + dependentArrayDelta; |
| 55 | + |
| 56 | + return arrayIndex; |
| 57 | + } |
| 58 | + |
| 59 | + function recomputeGuidIndexes(instanceMeta, keys, context) { |
| 60 | + instanceMeta.dependentGuidToIndex = {}; |
| 61 | + a_forEach.call(keys, function (key, idx) { |
| 62 | + instanceMeta.dependentGuidToIndex[Ember.guidFor(this.get(key))] = idx; |
| 63 | + }, context); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + Keeps n arrays concatenated using `Ember.ArrayComputed`. |
| 68 | +
|
| 69 | + Example: |
| 70 | + ```js |
| 71 | + obj = Ember.Object.createWithMixins({ |
| 72 | + itemsA: [], |
| 73 | + itemsB: [], |
| 74 | + itemsC: [], |
| 75 | + allItems: EmberCPM.Macros.concat('itemsA', 'itemsB', 'itemsC'); |
| 76 | + }); |
| 77 | +
|
| 78 | + obj.get('itemsA').pushObjects(['a', 'b']); |
| 79 | + obj.get('allItems') //=> ['a', 'b'] |
| 80 | +
|
| 81 | + obj.get('itemsB').pushObjects(['c']); |
| 82 | + obj.get('allItems') //=> ['a', 'b', 'c'] |
| 83 | +
|
| 84 | + obj.get('itemsC').pushObjects(['d']); |
| 85 | + obj.get('allItems') //=> ['a', 'b', 'c', 'd'] |
| 86 | +
|
| 87 | + obj.get('itemsB').pushObjects(['e', 'f']); |
| 88 | + obj.get('allItems') //=> ['a', 'b', 'c', 'e', 'f', 'd'] |
| 89 | + ``` |
| 90 | + */ |
| 91 | + EmberCPM.Macros.concat = function () { |
| 92 | + var args = a_slice.call(arguments); |
| 93 | + args.push({ |
| 94 | + initialize: function (array, changeMeta, instanceMeta) { |
| 95 | + instanceMeta.dependentArrayLengths = new Array(changeMeta.property._dependentKeys.length); |
| 96 | + // When items are added or removed, we have access to the array that was |
| 97 | + // changed, but not its dependent key, so we use its guid as the key to |
| 98 | + // determine its index in the array of dependent keys. |
| 99 | + instanceMeta.dependentGuidToIndex = {}; |
| 100 | + |
| 101 | + return array; |
| 102 | + }, |
| 103 | + |
| 104 | + addedItem: function (array, item, changeMeta, instanceMeta) { |
| 105 | + var arrayIndex = getIndex.call(this, changeMeta, instanceMeta, 0); |
| 106 | + array.insertAt(arrayIndex, item); |
| 107 | + return array; |
| 108 | + }, |
| 109 | + |
| 110 | + removedItem: function (array, item, changeMeta, instanceMeta) { |
| 111 | + var arrayIndex = getIndex.call(this, changeMeta, instanceMeta, -1); |
| 112 | + array.removeAt(arrayIndex); |
| 113 | + return array; |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + return Ember.arrayComputed.apply(null, args); |
| 118 | + }; |
| 119 | +}).call(undefined, this, this.Ember, this.jQuery, this.EmberCPM); |
| 120 | + |
23 | 121 | (function(window, Ember, $, EmberCPM) { |
24 | 122 |
|
25 | 123 | function reverseMerge(dest, source) { |
|
151 | 249 | }; |
152 | 250 |
|
153 | 251 | }).call(undefined, this, this.Ember, this.jQuery, this.EmberCPM); |
154 | | -(function(window, Ember, $, EmberCPM) { |
155 | | - var a_slice = Array.prototype.slice, |
156 | | - a_forEach = Ember.ArrayPolyfills.forEach, |
157 | | - get = Ember.get; |
158 | | - |
159 | | - /* |
160 | | - Returns the index where an item is to be removed from, or placed into, for |
161 | | - an EmberCPM.Macros.concat array. |
162 | | -
|
163 | | - This is the index of the item within its dependent array, offset by the |
164 | | - lengths of all prior dependent arrays. |
165 | | - */ |
166 | | - function getIndex(changeMeta, instanceMeta, dependentArrayDelta) { |
167 | | - var dependentArrayGuid = Ember.guidFor(changeMeta.arrayChanged); |
168 | | - |
169 | | - if (!(dependentArrayGuid in instanceMeta.dependentGuidToIndex)) { |
170 | | - recomputeGuidIndexes(instanceMeta, changeMeta.property._dependentKeys, this); |
171 | | - } |
172 | | - |
173 | | - var dependentArrayLengths = instanceMeta.dependentArrayLengths, |
174 | | - dependentArrayIndex = instanceMeta.dependentGuidToIndex[dependentArrayGuid], |
175 | | - offset = 0, |
176 | | - arrayIndex; |
177 | | - |
178 | | - // offset is the sum of the lengths of arrays to our left |
179 | | - for (var i = 0; i < dependentArrayIndex; ++i) { |
180 | | - offset += (dependentArrayLengths[i] || 0); |
181 | | - } |
182 | | - |
183 | | - arrayIndex = offset + changeMeta.index; |
184 | | - dependentArrayLengths[dependentArrayIndex] = (get(changeMeta.arrayChanged, 'length') || 0) + dependentArrayDelta; |
185 | | - |
186 | | - return arrayIndex; |
187 | | - } |
188 | | - |
189 | | - function recomputeGuidIndexes(instanceMeta, keys, context) { |
190 | | - instanceMeta.dependentGuidToIndex = {}; |
191 | | - a_forEach.call(keys, function (key, idx) { |
192 | | - instanceMeta.dependentGuidToIndex[Ember.guidFor(this.get(key))] = idx; |
193 | | - }, context); |
194 | | - } |
195 | | - |
196 | | - /** |
197 | | - Keeps n arrays concatenated using `Ember.ArrayComputed`. |
198 | | -
|
199 | | - Example: |
200 | | - ```js |
201 | | - obj = Ember.Object.createWithMixins({ |
202 | | - itemsA: [], |
203 | | - itemsB: [], |
204 | | - itemsC: [], |
205 | | - allItems: EmberCPM.Macros.concat('itemsA', 'itemsB', 'itemsC'); |
206 | | - }); |
207 | | -
|
208 | | - obj.get('itemsA').pushObjects(['a', 'b']); |
209 | | - obj.get('allItems') //=> ['a', 'b'] |
210 | | -
|
211 | | - obj.get('itemsB').pushObjects(['c']); |
212 | | - obj.get('allItems') //=> ['a', 'b', 'c'] |
213 | | -
|
214 | | - obj.get('itemsC').pushObjects(['d']); |
215 | | - obj.get('allItems') //=> ['a', 'b', 'c', 'd'] |
216 | | -
|
217 | | - obj.get('itemsB').pushObjects(['e', 'f']); |
218 | | - obj.get('allItems') //=> ['a', 'b', 'c', 'e', 'f', 'd'] |
219 | | - ``` |
220 | | - */ |
221 | | - EmberCPM.Macros.concat = function () { |
222 | | - var args = a_slice.call(arguments); |
223 | | - args.push({ |
224 | | - initialize: function (array, changeMeta, instanceMeta) { |
225 | | - instanceMeta.dependentArrayLengths = new Array(changeMeta.property._dependentKeys.length); |
226 | | - // When items are added or removed, we have access to the array that was |
227 | | - // changed, but not its dependent key, so we use its guid as the key to |
228 | | - // determine its index in the array of dependent keys. |
229 | | - instanceMeta.dependentGuidToIndex = {}; |
230 | | - |
231 | | - return array; |
232 | | - }, |
233 | | - |
234 | | - addedItem: function (array, item, changeMeta, instanceMeta) { |
235 | | - var arrayIndex = getIndex.call(this, changeMeta, instanceMeta, 0); |
236 | | - array.insertAt(arrayIndex, item); |
237 | | - return array; |
238 | | - }, |
239 | | - |
240 | | - removedItem: function (array, item, changeMeta, instanceMeta) { |
241 | | - var arrayIndex = getIndex.call(this, changeMeta, instanceMeta, -1); |
242 | | - array.removeAt(arrayIndex); |
243 | | - return array; |
244 | | - } |
245 | | - }); |
246 | | - |
247 | | - return Ember.arrayComputed.apply(null, args); |
248 | | - }; |
249 | | -}).call(undefined, this, this.Ember, this.jQuery, this.EmberCPM); |
|
0 commit comments