fixes tags merging by removing duplicates
[tine20] / tine20 / Tinebase / js / widgets / dialog / DuplicateResolveGridPanel.js
index bb236a0..88ffee4 100644 (file)
@@ -508,9 +508,8 @@ Tine.widgets.dialog.DuplicateResolveStore = Ext.extend(Ext.data.GroupingStore, {
             }
 
             // special merge for tags
-            // TODO generalize me
             if (resolveRecord.get('fieldName') == 'tags') {
-                resolveRecord.set('finalValue', Tine.Tinebase.common.assertComparable([].concat(this.resolveStrategy != 'discard' ? mine : []).concat(this.resolveStrategy != 'keep' ? theirs : [])));
+                resolveRecord.set('finalValue', this.mergeTags(mine, theirs));
             } else {
                 resolveRecord.set('finalValue', location === 'mine' ? mine : theirs);
             }
@@ -521,6 +520,30 @@ Tine.widgets.dialog.DuplicateResolveStore = Ext.extend(Ext.data.GroupingStore, {
         
         this.commitChanges();
     },
+
+    /**
+     * merge tags
+     *
+     * @param {Array} mine
+     * @param {Array} theirs
+     *
+     * TODO generalize me for "merge-able" values
+     */
+    mergeTags: function(mine, theirs) {
+        var result = [],
+            records = Tine.Tinebase.common.assertComparable([].concat(this.resolveStrategy != 'discard' ? mine : []).concat(this.resolveStrategy != 'keep' ? theirs : [])),
+            recordIds = [];
+
+        // remove duplicates (TODO should be simplified)
+        Ext.each(records, function(value) {
+            if (recordIds.indexOf(value['id']) < 0) {
+                result.push(value);
+                recordIds.push(value['id']);
+            }
+        });
+
+        return result;
+    },
     
     checkEditGrant: function() {
         var grant = ! this.recordClass.getMeta('containerProperty') ? true : this.duplicates[this.duplicateIdx].get('container_id') ? this.duplicates[this.duplicateIdx].get(this.recordClass.getMeta('containerProperty')).account_grants['editGrant'] : false;