From 11e8d5fab217e77495f1ef7d776dc49244d68869 Mon Sep 17 00:00:00 2001
From: Renaud Le Gac <legac@cppm.in2p3.fr>
Date: Fri, 23 Jan 2015 16:49:19 +0100
Subject: [PATCH] Add the class App.form.field.DictMultiDefault.

---
 static/plugin_dbui/CHANGELOG                  |   3 +-
 .../src/form/field/DictMultiDefault.js        | 100 ++++++++++++++++++
 static/scripts/dictmultidefault.js            |  59 +++++++++++
 3 files changed, 161 insertions(+), 1 deletion(-)
 create mode 100644 static/plugin_dbui/src/form/field/DictMultiDefault.js
 create mode 100644 static/scripts/dictmultidefault.js

diff --git a/static/plugin_dbui/CHANGELOG b/static/plugin_dbui/CHANGELOG
index 0f7bb4f4..1953919d 100644
--- a/static/plugin_dbui/CHANGELOG
+++ b/static/plugin_dbui/CHANGELOG
@@ -5,7 +5,8 @@ HEAD
    - pGridExport uses the latex package longtable instead of tabular.
      It also translate hyperlink using the hyperref package.
    - Consolidate by rebasing bugs fixed and improvement coming from
-     the ongoing migration to Ext JS 5.1
+     the ongoing migration to Ext JS 5.1.
+   - Add the field app.form.field.DictMultiField.
 
 0.6.1.14 (Dec 2014)
    - Add the plugin App.grid.plugin.Export.
diff --git a/static/plugin_dbui/src/form/field/DictMultiDefault.js b/static/plugin_dbui/src/form/field/DictMultiDefault.js
new file mode 100644
index 00000000..a27fdc87
--- /dev/null
+++ b/static/plugin_dbui/src/form/field/DictMultiDefault.js
@@ -0,0 +1,100 @@
+/**
+ * A dictionary handling several default values.
+ *
+ * This field is linked to a ComboBox field. The value selected
+ * in this comboBox define the default dictionary which has to be used.
+ *
+ * @since 0.6.2
+ *
+ */
+Ext.define('App.form.field.DictMultiDefault', {
+
+    extend: 'App.form.field.Dict',
+    alias: 'widget.xdictmultidefault',
+
+    /**
+     * @cfg {Ext.data.Store/String} store (required)
+     * the store should a list contains the key, selected by the comboBox,
+     * and the associated dictionary definition.
+     *
+     */
+
+    /**
+     * @cfg {String} dictField (required)
+     * The field in the store contain dictionary definition.
+     */
+
+    /**
+     * @cfg {String} comboBoxItemId (required)
+     * The itemId of the comboBox.
+     */
+
+    // private method requests by the component model of ExtJS
+    initComponent: function () {
+
+        var me = this;
+
+        // instantiate the base class
+        this.callParent(arguments);
+
+        // instantiate the store containing the relation
+        // between the key and the dictionary configuration
+        me.store = App.getStore(me.store);
+
+        // bind me to the comboBox
+        me.on('afterrender', me.bindToComboBox, me);
+    },
+
+    // private method requests by the component model of ExtJS
+    beforeDestroy: function () {
+
+        var me = this;
+
+        me.un('afterrender', me.bindToComboBox, me);
+        me.getComboBox().un('select', me.onComboBoxSelect, me);
+
+        me.callParent(arguments);
+    },
+
+    //
+    // Bind the ComboBox and the Dict field.
+    // The handler is triggered by the afterrender event.
+    //
+    bindToComboBox: function () {
+
+        "use strict";
+        var me = this;
+
+        me.getComboBox().on('select', me.onComboBoxSelect, me);
+    },
+
+    //
+    // @return {App.form.field.ComboBox}
+    //
+    getComboBox: function () {
+
+        "use strict";
+        var me = this;
+
+        return me.up('xform, form').down('#' + me.comboBoxItemId);
+    },
+
+    //
+    // Handle the ComboBox select event
+    //
+    // @param {Ext.form.field.ComboBox} combo
+    // @param {Ext.data.Model[]} records
+    // @param {Object} eOpts
+    //
+    onComboBoxSelect: function (combo, records, eOpts) {
+
+        "use strict";
+        var me = this;
+
+        me.dictCfg = records.length ? records[0].get(me.dictField) : {};
+
+        // load the dictionary with the default values
+        // current content is destroyed
+        me.setValue({});
+    }
+});
\ No newline at end of file
diff --git a/static/scripts/dictmultidefault.js b/static/scripts/dictmultidefault.js
new file mode 100644
index 00000000..8fe5191a
--- /dev/null
+++ b/static/scripts/dictmultidefault.js
@@ -0,0 +1,59 @@
+/**
+ * combo.js
+ *
+ * Script to test the Combo field.
+ *
+ */
+
+// Activate the dynamic loading for Ext JS and application classes
+App.setDynamicLoading(App.debug);
+
+// classes required by the script
+Ext.require('Ext.form.Panel');
+Ext.require('Ext.direct.Manager');
+Ext.require('Ext.direct.RemotingProvider');
+Ext.require('Ext.tip.QuickTipManager');
+Ext.require('App.form.field.ComboBox');
+Ext.require('App.form.field.DictMultiDefault');
+
+Ext.onReady(function(){
+
+    "use strict";
+
+    var form;
+
+    Ext.QuickTips.init();
+    Ext.direct.Manager.addProvider(App.REMOTE_API);
+    App.defineModels();
+
+    form = Ext.create('Ext.form.Panel', {
+        title: 'DictMultiDefault',
+        bodyPadding: 5,
+        width: 350,
+        layout: 'anchor',
+        defaults: {
+            anchor: '100%'
+        },
+        items: [{
+            fieldLabel: 'MyText',
+            xtype: 'textfield'
+        }, {
+            displayField: 'New_fieldsString',
+            emptyText: 'select a key',
+            fieldLabel: 'MyKey',
+            itemId: 'keycombo',
+            store: 'new_fieldsStore',
+            valuefield: 'New_fieldsString',
+            xtype: 'xcombobox'
+        }, {
+            comboBoxItemId: 'keycombo',
+            dictField: 'New_fieldsDictionary',
+            fieldLabel: 'MyDict',
+            height: 100,
+            store: 'new_fieldsStore',
+            xtype: 'xdictmultidefault'
+        }],
+        renderTo: Ext.getBody()
+    });
+
+});
-- 
GitLab