Allow relationcombo setValue to select records
authorMichael Spahn <m.spahn@metaways.de>
Tue, 18 Jul 2017 17:18:42 +0000 (19:18 +0200)
committerCornelius Weiss <c.weiss@metaways.de>
Wed, 19 Jul 2017 10:13:36 +0000 (12:13 +0200)
 - if record is not in relationstore, add it to store

Change-Id: Ia4ff58ae84fea59e7021758decd1f0f0dc07f0b9
Reviewed-on: http://gerrit.tine20.com/customers/5246
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Cornelius Weiss <c.weiss@metaways.de>
Tested-by: Cornelius Weiss <c.weiss@metaways.de>
tine20/Tinebase/js/widgets/relation/PickerCombo.js

index 3efc0d7..84803ed 100644 (file)
@@ -68,8 +68,6 @@ Tine.widgets.relation.PickerCombo = Ext.extend(Ext.Container, {
             var value = this.combo.getValue();
 
             if (value.length) {
-                var recordToAdd = this.combo.selectedRecord;
-
                 // check if another relationPicker has the same record selected
                 if (this.modelUnique) {
                     var hasDuplicate = false,
@@ -103,23 +101,7 @@ Tine.widgets.relation.PickerCombo = Ext.extend(Ext.Container, {
                 if (value != this.combo.startValue) {
                     this.removeIdFromStore(this.combo.startValue);
                 }
-
-                if (this.store.findBy(function(record, id) {
-                    if(record) {
-                        if(record.get('related_id') == value && record.get('type') == this.relationType) return true;
-                    }
-                }, this) == -1) {
-                    var relationRecord = new Tine.Tinebase.Model.Relation(Ext.apply(this.editDialog.relationsPanel.getRelationDefaults(), {
-                        related_record: recordToAdd.data,
-                        related_id: recordToAdd.id,
-                        related_model: this.fullModelName,
-                        type: this.relationType,
-                        related_degree: this.relationDegree
-                    }), recordToAdd.id);
-
-                    this.combo.startRecord = recordToAdd;
-                    this.store.add(relationRecord);
-                    }
+                this.addRecord(this.combo.selectedRecord);
             } else {
                 this.removeIdFromStore(this.combo.startValue);
             }
@@ -132,6 +114,31 @@ Tine.widgets.relation.PickerCombo = Ext.extend(Ext.Container, {
     },
 
     /**
+     * Add a record to store if not existing
+     * @param recordToAdd
+     */
+    addRecord: function (recordToAdd) {
+        var record = this.store.findBy(function (record) {
+            if (record && record.get('related_id') === this.combo.getValue() && record.get('type') === this.relationType) {
+                return true;
+            }
+        }, this);
+
+        if (record === -1) {
+            var relationRecord = new Tine.Tinebase.Model.Relation(Ext.apply(this.editDialog.relationsPanel.getRelationDefaults(), {
+                related_record: recordToAdd.data,
+                related_id: recordToAdd.id,
+                related_model: this.fullModelName,
+                type: this.relationType,
+                related_degree: this.relationDegree
+            }), recordToAdd.id);
+
+            this.combo.startRecord = recordToAdd;
+            this.store.add(relationRecord);
+        }
+    },
+
+    /**
      * removes the relation record by the related record id, if the type is the same handled by this combo
      * @param {String} id
      */
@@ -152,6 +159,11 @@ Tine.widgets.relation.PickerCombo = Ext.extend(Ext.Container, {
      * Shortcut for the equivalent method of the combo
      */
     setValue: function(v) {
+        // If v might be a record, we try to select it and if not in store we create it in relation store
+        if (v.hasOwnProperty('data')) {
+            this.addRecord(v);
+        }
+
         return this.combo.setValue(v);
     },