|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var module = angular.module('diffArray', []); |
| 4 | + |
| 5 | +var idStringify = LocalCollection._idStringify; |
| 6 | +var idParse = LocalCollection._idParse; |
| 7 | + |
| 8 | +// Calculates the differences between `lastSeqArray` and |
| 9 | +// `seqArray` and calls appropriate functions from `callbacks`. |
| 10 | +// Reuses Minimongo's diff algorithm implementation. |
| 11 | +var diffArray = function (lastSeqArray, seqArray, callbacks) { |
| 12 | + var diffFn = Package.minimongo.LocalCollection._diffQueryOrderedChanges; |
| 13 | + var oldIdObjects = []; |
| 14 | + var newIdObjects = []; |
| 15 | + var posOld = {}; // maps from idStringify'd ids |
| 16 | + var posNew = {}; // ditto |
| 17 | + var posCur = {}; |
| 18 | + var lengthCur = lastSeqArray.length; |
| 19 | + |
| 20 | + _.each(seqArray, function (doc, i) { |
| 21 | + newIdObjects.push({_id: doc._id}); |
| 22 | + posNew[idStringify(doc._id)] = i; |
| 23 | + }); |
| 24 | + _.each(lastSeqArray, function (doc, i) { |
| 25 | + oldIdObjects.push({_id: doc._id}); |
| 26 | + posOld[idStringify(doc._id)] = i; |
| 27 | + posCur[idStringify(doc._id)] = i; |
| 28 | + }); |
| 29 | + |
| 30 | + // Arrays can contain arbitrary objects. We don't diff the |
| 31 | + // objects. Instead we always fire 'changedAt' callback on every |
| 32 | + // object. The consumer of `observe-sequence` should deal with |
| 33 | + // it appropriately. |
| 34 | + diffFn(oldIdObjects, newIdObjects, { |
| 35 | + addedBefore: function (id, doc, before) { |
| 36 | + var position = before ? posCur[idStringify(before)] : lengthCur; |
| 37 | + |
| 38 | + _.each(posCur, function (pos, id) { |
| 39 | + if (pos >= position) |
| 40 | + posCur[id]++; |
| 41 | + }); |
| 42 | + |
| 43 | + lengthCur++; |
| 44 | + posCur[idStringify(id)] = position; |
| 45 | + |
| 46 | + callbacks.addedAt( |
| 47 | + id, |
| 48 | + seqArray[posNew[idStringify(id)]], |
| 49 | + position, |
| 50 | + before); |
| 51 | + }, |
| 52 | + movedBefore: function (id, before) { |
| 53 | + var prevPosition = posCur[idStringify(id)]; |
| 54 | + var position = before ? posCur[idStringify(before)] : lengthCur - 1; |
| 55 | + |
| 56 | + _.each(posCur, function (pos, id) { |
| 57 | + if (pos >= prevPosition && pos <= position) |
| 58 | + posCur[id]--; |
| 59 | + else if (pos <= prevPosition && pos >= position) |
| 60 | + posCur[id]++; |
| 61 | + }); |
| 62 | + |
| 63 | + posCur[idStringify(id)] = position; |
| 64 | + |
| 65 | + callbacks.movedTo( |
| 66 | + id, |
| 67 | + seqArray[posNew[idStringify(id)]], |
| 68 | + prevPosition, |
| 69 | + position, |
| 70 | + before); |
| 71 | + }, |
| 72 | + removed: function (id) { |
| 73 | + var prevPosition = posCur[idStringify(id)]; |
| 74 | + |
| 75 | + _.each(posCur, function (pos, id) { |
| 76 | + if (pos >= prevPosition) |
| 77 | + posCur[id]--; |
| 78 | + }); |
| 79 | + |
| 80 | + delete posCur[idStringify(id)]; |
| 81 | + lengthCur--; |
| 82 | + |
| 83 | + callbacks.removedAt( |
| 84 | + id, |
| 85 | + lastSeqArray[posOld[idStringify(id)]], |
| 86 | + prevPosition); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + _.each(posNew, function (pos, idString) { |
| 91 | + var id = idParse(idString); |
| 92 | + |
| 93 | + if (_.has(posOld, idString)) { |
| 94 | + var newItem = seqArray[pos]; |
| 95 | + var oldItem = lastSeqArray[posOld[idString]]; |
| 96 | + var setDiff = diffObjectChanges(oldItem, newItem); |
| 97 | + var unsetDiff = diffObjectRemovals(oldItem, newItem); |
| 98 | + |
| 99 | + if (setDiff) |
| 100 | + setDiff._id = newItem._id; |
| 101 | + |
| 102 | + if (unsetDiff) |
| 103 | + unsetDiff._id = newItem._id; |
| 104 | + |
| 105 | + if (setDiff || unsetDiff) |
| 106 | + callbacks.changedAt(id, setDiff, unsetDiff, pos); |
| 107 | + } |
| 108 | + }); |
| 109 | +}; |
| 110 | + |
| 111 | +// Takes an object and returns a shallow copy, ie. with all keys at |
| 112 | +// a one-level depth. Transforms the name of each key using dot notation |
| 113 | +var flattenObject = function (object, parentKey) { |
| 114 | + var flattened = {}; |
| 115 | + |
| 116 | + angular.forEach(object, function (value, key) { |
| 117 | + if (isActualObject(value)) { |
| 118 | + angular.extend(flattened, flattenObject(value, key)); |
| 119 | + } else { |
| 120 | + var dotNotedKey = (parentKey) ? parentKey + "." + key : key; |
| 121 | + flattened[dotNotedKey] = value; |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + return flattened; |
| 126 | +}; |
| 127 | + |
| 128 | +// Can tell whether a value is an object and not an array |
| 129 | +var isActualObject = function (value) { |
| 130 | + return angular.isObject(value) && !angular.isArray(value); |
| 131 | +}; |
| 132 | + |
| 133 | +// Diffs two objects and returns the keys that have been added or changed. |
| 134 | +// Can be used to construct a Mongo {$set: {}} modifier |
| 135 | +var diffObjectChanges = function (oldItem, newItem) { |
| 136 | + var result = {}; |
| 137 | + |
| 138 | + angular.forEach(newItem, function (value, key) { |
| 139 | + if (oldItem && angular.equals(value, oldItem[key])) |
| 140 | + return; |
| 141 | + |
| 142 | + if (isActualObject(value)) { |
| 143 | + var diff = diffObjectChanges(oldItem[key], value); |
| 144 | + if (diff) result[key] = diff; |
| 145 | + } else { |
| 146 | + result[key] = value; |
| 147 | + } |
| 148 | + |
| 149 | + // If a nested object is identical between newItem and oldItem, it |
| 150 | + // is initially attached as an empty object. Here we remove it from |
| 151 | + // the result if it was not empty from the beginning. |
| 152 | + if (isActualObject(result[key]) && _.keys(result[key]).length === 0) { |
| 153 | + if (_.keys(value).length !== 0) |
| 154 | + delete result[key]; |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + if (!(_.keys(result).length > 0 && !(_.keys(result).length === 1 && result.$$hashKey))) |
| 159 | + return undefined; |
| 160 | + else |
| 161 | + return flattenObject(result); |
| 162 | +}; |
| 163 | + |
| 164 | +// Diffs two objects and returns the keys that have been removed. |
| 165 | +// Can be used to construct a Mongo {$unset: {}} modifier |
| 166 | +var diffObjectRemovals = function (oldItem, newItem) { |
| 167 | + if (newItem == null) |
| 168 | + return true; |
| 169 | + |
| 170 | + var oldItemKeys = _.keys(oldItem); |
| 171 | + var newItemKeys = _.keys(newItem); |
| 172 | + var result = {}; |
| 173 | + |
| 174 | + angular.forEach(oldItemKeys, function (key) { |
| 175 | + if (!_.contains(newItemKeys, key)) |
| 176 | + result[key] = true; |
| 177 | + |
| 178 | + if (isActualObject(oldItem[key])) { |
| 179 | + var diff = diffObjectRemovals(oldItem[key], newItem[key]); |
| 180 | + if (diff) result[key] = diff; |
| 181 | + } |
| 182 | + }); |
| 183 | + |
| 184 | + if (_.keys(result).length === 0) |
| 185 | + return undefined; |
| 186 | + else |
| 187 | + return flattenObject(result); |
| 188 | +}; |
| 189 | + |
| 190 | +module.value('diffArray', diffArray); |
0 commit comments