From ca31958df18a28a06da687bb12a0dec0ca7310e0 Mon Sep 17 00:00:00 2001
From: legac <renaud.legac@free.fr>
Date: Sun, 10 Nov 2013 18:37:20 +0100
Subject: [PATCH] ExtJS 4.2: Migrate several grid plugins.

---
 models/widgets_grids.py                       | 13 +--
 modules/plugin_dbui/converter.py              |  5 +-
 static/plugin_dbui/locale/dbui-lang-fr.js     |  1 +
 static/plugin_dbui/src/form.js                | 16 +++-
 static/plugin_dbui/src/grid.js                |  6 +-
 static/plugin_dbui/src/pformtooltip.js        | 15 ++--
 static/plugin_dbui/src/pgridexpertmenu.js     | 10 ++-
 static/plugin_dbui/src/pgridmathjax.js        |  8 +-
 static/plugin_dbui/src/pgridroweditorbase.js  | 40 ++++-----
 .../src/pgridroweditorcontextmenu.js          | 90 ++++++++++---------
 .../plugin_dbui/src/pgridroweditordblclick.js |  4 +-
 11 files changed, 118 insertions(+), 90 deletions(-)

diff --git a/models/widgets_grids.py b/models/widgets_grids.py
index 25002b45..e3b6525f 100644
--- a/models/widgets_grids.py
+++ b/models/widgets_grids.py
@@ -5,10 +5,10 @@
 #
 # add plugin to all grids
 #
-# dbui.configure_grids(db, plugins=['pGridRowEditorConfirmDelete',
-#                                   'pGridRowEditorContextMenu',
-#                                   'pGridRowEditorDblClick',
-#                                   'pGridExpertMenu'])
+dbui.configure_grids(db, plugins=['pGridRowEditorConfirmDelete',
+                                  'pGridRowEditorContextMenu',
+                                  'pGridRowEditorDblClick',
+                                  'pGridExpertMenu'])
 #
 # Polish categories grid
 #
@@ -17,8 +17,8 @@ gridModifier.configure_column('code', width=10)
 gridModifier.set_rownumbering(False)
 
 gridModifier.append_filter(('code', 'contains', T('bla bla...')))
-# gridModifier.set_filters(plugins=['pFormToolTip'],
-#                          width=300)
+gridModifier.set_filters(plugins=['pFormToolTip'],
+                         width=300)
 
 #
 # Define a column template for the publications grid
@@ -49,6 +49,7 @@ tpl = ['<b>{PublicationsTitle}</b><br>',
 
 gridModifier = dbui.GridModifier('publications')
 # gridModifier.append_plugins('pGridPaging', 'pGridExpertMenu', 'pGridMathJax')
+# gridModifier.append_plugins('pGridMathJax')
 gridModifier.merge_columns('title', 
                            'authors', 
                            'id_collaborations', 
diff --git a/modules/plugin_dbui/converter.py b/modules/plugin_dbui/converter.py
index 11325d01..5b877287 100644
--- a/modules/plugin_dbui/converter.py
+++ b/modules/plugin_dbui/converter.py
@@ -41,6 +41,8 @@ FTYPE_TO_XTYPE = {'boolean':      (CheckBox, {}),\
 
 # constant defining a ExtJS store
 ROOT = 'records'
+NOPAGE = ''
+PAGE = 'page'
 SUCCESS= 'success'
 TOTAL = 'count'
 
@@ -633,9 +635,8 @@ def to_jsonstore(table, **kwargs):
     cfg = DirectStore(autoLoad=False,
                       autoSync=True,
                       extraParams=base_params,
-                      idProperty=encode_field(tablename,'id'),
                       model=tablename,
-                      pageParam="",
+                      pageParam=NOPAGE,
                       root=ROOT,
                       successProperty=SUCCESS,
                       storeId=get_store_id(tablename),
diff --git a/static/plugin_dbui/locale/dbui-lang-fr.js b/static/plugin_dbui/locale/dbui-lang-fr.js
index a3c8a341..e01446c7 100644
--- a/static/plugin_dbui/locale/dbui-lang-fr.js
+++ b/static/plugin_dbui/locale/dbui-lang-fr.js
@@ -72,6 +72,7 @@ if (App.form.ListField) {
 if (App.grid.ExpertMenu) {
     Ext.apply(App.grid.ExpertMenu.prototype, {
         textExport: 'Exporter dans un fichier CSV',
+        textPlus: 'Plus',
         textReset: 'Reset'
     });
 }
diff --git a/static/plugin_dbui/src/form.js b/static/plugin_dbui/src/form.js
index 63396e7f..4c384f0b 100644
--- a/static/plugin_dbui/src/form.js
+++ b/static/plugin_dbui/src/form.js
@@ -243,11 +243,25 @@ Ext.define('App.form.FormPanel', {
 
         "use strict";
 
-        if (response.hasOwnProperty('xhr')) {
+        var msg;
+
+        console.log('proxy', response);
+
+        // invalid response from the server, HTPP 400, 500
+        if (response instanceof Ext.direct.ExceptionEvent) {
             Ext.Msg.alert(this.textError, response.xhr.responseText);
             return;
         }
 
+        // valid answer from the server, HTTP 200
+        // but something went wrong in the server validation process, ...
+        if (response instanceof Ext.direct.RemotingEvent) {
+            if (!response.result.success) {
+                Ext.Msg.alert(this.textError, response.result.errors);
+                return;
+            }
+        }
+
         /*
         var i,
             field,
diff --git a/static/plugin_dbui/src/grid.js b/static/plugin_dbui/src/grid.js
index e4032438..be6eb07f 100644
--- a/static/plugin_dbui/src/grid.js
+++ b/static/plugin_dbui/src/grid.js
@@ -95,7 +95,8 @@ Ext.define('App.grid.Grid', {
 
         "use strict";
 
-        var action;
+        var action,
+            bbar = this.getDockedItems('toolbar[dock=bottom]')[0];
 
         // private function to emit the resetgrid event
         function fireReset() {
@@ -105,7 +106,8 @@ Ext.define('App.grid.Grid', {
         // parameters defining the store load action
         // ExtJS 4.2 use this.getDockItems('toolbar[dock="bottom"]');
         action = {callback: fireReset, scope: this};
-        if (this.getBottomToolbar() instanceof Ext.PagingToolbar) {
+
+        if (bbar instanceof Ext.toolbar.Paging) {
             action.params = {start: 0, limit: this.nRows};
         }
 
diff --git a/static/plugin_dbui/src/pformtooltip.js b/static/plugin_dbui/src/pformtooltip.js
index aba700ed..9e5512c5 100644
--- a/static/plugin_dbui/src/pformtooltip.js
+++ b/static/plugin_dbui/src/pformtooltip.js
@@ -15,14 +15,14 @@ Ext.define('App.form.ToolTip', {
     /**
      * The plugin initialization
      *
-     * @param {Ext.form.FormPanel} basicform
+     * @param {Ext.form.FormPanel} form
      *
      */
-    init: function (basicform) {
+    init: function (form) {
 
         "use strict";
 
-        var form = basicform;
+        var fields;
 
         function createToolTip(c) {
 
@@ -33,11 +33,14 @@ Ext.define('App.form.ToolTip', {
             });
         }
 
-        if (basicform instanceof Ext.form.Panel) {
-            form = basicform.getForm();
+        if (form instanceof Ext.form.Panel) {
+            fields = form.getForm().getFields();
+
+        } else if (form instanceof App.grid.GridFilter) {
+            fields = form.items;
         }
 
-        form.getFields().each(function (field) {
+        fields.each(function (field) {
             if (field.tipText) {
                 field.on('render', createToolTip);
             }
diff --git a/static/plugin_dbui/src/pgridexpertmenu.js b/static/plugin_dbui/src/pgridexpertmenu.js
index adfc6b7c..aca17a1c 100644
--- a/static/plugin_dbui/src/pgridexpertmenu.js
+++ b/static/plugin_dbui/src/pgridexpertmenu.js
@@ -9,6 +9,7 @@ Ext.define('App.grid.ExpertMenu', {
 
     // Private properties for internationalisation
     textExport: 'Export to CSV file',
+    textPlus: 'Plus',
     textReset: 'Reset',
 
     /**
@@ -21,20 +22,21 @@ Ext.define('App.grid.ExpertMenu', {
 
         "use strict";
 
-        var bbar = grid.getBottomToolbar(),
+        var bbar = grid.getDockedItems('toolbar[dock=bottom]')[0],
             menu;
 
-        menu = new Ext.Toolbar.SplitButton({
+        menu = Ext.create('Ext.button.Split', {
             menu: {
                 items: [{
                     listeners: {click: this.onReset, scope: grid},
                     text: this.textReset
 
                 }, {
-                    href: App.csvUrl + '?tableName=' + App.getTableName(grid.store),
+                    href: App.csvUrl + '?tableName=' + App.getTableName(grid.getStore()),
                     text: this.textExport
                 }]
-            }
+            },
+            text: this.textPlus
         });
 
         bbar.add('->', menu);
diff --git a/static/plugin_dbui/src/pgridmathjax.js b/static/plugin_dbui/src/pgridmathjax.js
index 73329148..eeefe431 100644
--- a/static/plugin_dbui/src/pgridmathjax.js
+++ b/static/plugin_dbui/src/pgridmathjax.js
@@ -20,7 +20,7 @@ Ext.define('App.grid.MathJax', {
         "use strict";
 
         grid.getView().on('refresh', this.onProcessMath, grid);
-        grid.store.on('write', this.onProcessMath, grid);
+        grid.getStore().on('write', this.onProcessMath, grid);
     },
 
     // Private method to destroy the plugin.
@@ -33,7 +33,7 @@ Ext.define('App.grid.MathJax', {
         "use strict";
 
         grid.getView().un('refresh', this.onProcessMath, grid);
-        grid.store.un('write', this.onProcessMath, grid);
+        grid.getStore().un('write', this.onProcessMath, grid);
 
     },
 
@@ -46,7 +46,9 @@ Ext.define('App.grid.MathJax', {
 
         "use strict";
 
-        var domEl = Ext.getDom(this.el);
+        var grid = this;
+
+        var domEl = Ext.getDom(grid.getEl());
         MathJax.Hub.Queue(["Typeset", MathJax.Hub, domEl]);
 
     }
diff --git a/static/plugin_dbui/src/pgridroweditorbase.js b/static/plugin_dbui/src/pgridroweditorbase.js
index 72b4aee2..4dd5b789 100644
--- a/static/plugin_dbui/src/pgridroweditorbase.js
+++ b/static/plugin_dbui/src/pgridroweditorbase.js
@@ -12,7 +12,7 @@ Ext.define('App.grid.RowEditor',  {
     extend: 'Ext.Window',
     alias: 'plugin.pGridRowEditor',
 
-    // Private shortcuts
+    // Private short cuts
     formPanel: null,
     grid: null,
 
@@ -25,10 +25,8 @@ Ext.define('App.grid.RowEditor',  {
     textMsg: 'Select a row please',
 
     // pre-defined configuration options for the window
-    autoScroll: true,
     closeAction: 'hide',
-    constrainHeader: true,
-    defaults: {autoScroll: true},
+    layout: 'fit',
     modal: true,
     plain: true,
 
@@ -41,7 +39,8 @@ Ext.define('App.grid.RowEditor',  {
 
         "use strict";
 
-        var table;
+        var table,
+            store;
 
         this.grid = grid;
 
@@ -49,15 +48,14 @@ Ext.define('App.grid.RowEditor',  {
         // since it can be shared between different plugins
         this.grid.rowEditor = this;
 
-        // The user can only select one row of the grid
-        this.grid.selModel = new Ext.grid.RowSelectionModel({
-            singleSelect: true
-        });
+        // The user can only select one row
+        this.grid.getSelectionModel().setSelectionMode('SINGLE');
 
         // Add a listener to hide the window when the transaction is successful
         // or when the transaction failed
-        this.grid.store.on('write', this.onWrite, this);
-        this.grid.store.on('exception', this.onException, this);
+        store = this.grid.getStore();
+        store.on('write', this.onWrite, this);
+        store.getProxy().on('exception', this.onException, this);
 
         // get the formPanel configuration and instantiate the object
         table = App.getTableName(this.grid.store);
@@ -94,13 +92,8 @@ Ext.define('App.grid.RowEditor',  {
         var formCfg = response.result;
 
         // instantiate the form
-        this.formPanel = new App.form.FormPanel(formCfg);
+        this.formPanel = Ext.create('App.form.FormPanel', formCfg);
         this.add(this.formPanel);
-
-        // add a listeners to catch the store exception
-        this.grid.store.on('exception',
-                           this.formPanel.onStoreException,
-                           this.formPanel);
     },
 
     //
@@ -123,12 +116,13 @@ Ext.define('App.grid.RowEditor',  {
 
         "use strict";
 
-        this.grid = null;
+        var store;
 
-        grid.store.un('write', this.onWrite, this);
-        grid.store.un('exception', this.onException, this);
+        this.grid = null;
 
-        grid.store.un('exception', this.formPanel.onStoreException, this.formPanel);
+        store = grid.getStore();
+        store.un('write', this.onWrite, this);
+        store.getProxy().un('exception', this.onException, this);
 
         Ext.destroyMembers(this, 'formPanel');
     },
@@ -145,7 +139,7 @@ Ext.define('App.grid.RowEditor',  {
 
         "use strict";
 
-        var record = this.grid.getSelectionModel().getSelected();
+        var record = this.grid.getSelectionModel().getLastSelected();
 
         if (!record) {
             Ext.MessageBox.alert('Warning', this.textMsg);
@@ -274,7 +268,7 @@ Ext.define('App.grid.RowEditor',  {
         this.formPanel.indexSelectedRecord = undefined;
 
         // get the selected record
-        record = this.grid.getSelectionModel().getSelected();
+        record = this.grid.getSelectionModel().getLastSelected();
 
         // update
         if (record) {
diff --git a/static/plugin_dbui/src/pgridroweditorcontextmenu.js b/static/plugin_dbui/src/pgridroweditorcontextmenu.js
index 69336416..f8520429 100644
--- a/static/plugin_dbui/src/pgridroweditorcontextmenu.js
+++ b/static/plugin_dbui/src/pgridroweditorcontextmenu.js
@@ -12,6 +12,9 @@ Ext.define('App.grid.RowEditorContextMenu',  {
     extend: 'Object',
     alias: 'plugin.pGridRowEditorContextMenu',
 
+    // private short cut
+    menu: null,
+
     // private properties for internationalization
     textAdd: 'Add',
     textCreate: 'Create',
@@ -29,47 +32,46 @@ Ext.define('App.grid.RowEditorContextMenu',  {
 
         "use strict";
 
-        // short cuts to ease handlers and destroy methods
-        this.menu = new Ext.menu.Menu();
-
         // protection
         if (!grid.rowEditor) {
             throw new Error('no grid row editor !!!');
         }
 
+        this.menu = Ext.create('Ext.menu.Menu', {
+            items : [{
+                text: this.textAdd,
+                iconCls: 'xaction-create',
+                handler: grid.rowEditor.onAddRow,
+                scope: grid.rowEditor
+            }, '-', {
+                text: this.textDuplicate,
+                conCls: 'xaction-duplicate',
+                handler: grid.rowEditor.onDuplicateRow,
+                scope: grid.rowEditor
+            }, {
+                text: this.textUpdate,
+                iconCls: 'xaction-update',
+                handler: grid.rowEditor.onEditRow,
+                scope: grid.rowEditor
+            }, {
+                text: this.textView,
+                iconCls: 'xaction-view',
+                handler: grid.rowEditor.onViewRow,
+                scope: grid.rowEditor
+            }, '-', {
+                text: this.textDestroy,
+                iconCls: 'xaction-destroy',
+                handler: grid.rowEditor.onDeleteRow,
+                scope: grid.rowEditor
+            }]
+        });
+
         grid.on({
             'containercontextmenu': this.onContainerContextMenu,
             'headercontextmenu': this.onHeaderContextMenu,
-            'rowcontextmenu': this.onRowContextMenu,
+            'itemcontextmenu': this.onItemContextMenu,
             scope: this
         });
-
-        this.menu.add({
-            text: this.textAdd,
-            iconCls: 'xaction-create',
-            handler: grid.rowEditor.onAddRow,
-            scope: grid.rowEditor
-        }, '-', {
-            text: this.textDuplicate,
-            iconCls: 'xaction-duplicate',
-            handler: grid.rowEditor.onDuplicateRow,
-            scope: grid.rowEditor
-        }, {
-            text: this.textUpdate,
-            iconCls: 'xaction-update',
-            handler: grid.rowEditor.onEditRow,
-            scope: grid.rowEditor
-        }, {
-            text: this.textView,
-            iconCls: 'xaction-view',
-            handler: grid.rowEditor.onViewRow,
-            scope: grid.rowEditor
-        }, '-', {
-            text: this.textDestroy,
-            iconCls: 'xaction-destroy',
-            handler: grid.rowEditor.onDeleteRow,
-            scope: grid.rowEditor
-        });
     },
 
     //
@@ -87,7 +89,7 @@ Ext.define('App.grid.RowEditorContextMenu',  {
 
         grid.un('containercontextmenu', this.onContainerContextMenu, this);
         grid.un('headercontextmenu', this.onHeaderContextMenu, this);
-        grid.un('rowcontextmenu', this.onRowContextMenu, this);
+        grid.un('itemcontextmenu', this.onItemContextMenu, this);
 
         Ext.destroyMembers(this, 'menu');
     },
@@ -97,9 +99,10 @@ Ext.define('App.grid.RowEditorContextMenu',  {
      *
      * @param {App.grid.Grid} grid
      * @param {Ext.EventObject} event
+     * @param {Object} eOpts
      *
      */
-    onContainerContextMenu: function (grid, event) {
+    onContainerContextMenu: function (grid, event, eOpts) {
 
         "use strict";
 
@@ -108,14 +111,17 @@ Ext.define('App.grid.RowEditorContextMenu',  {
     },
 
     /**
-     * Inhibit the context menu when right clicking in the header.
+     * Inhibit the context menu when right clicking in the grid header.
      *
-     * @param {App.grid.Grid} grid
-     * @param {Number} columnIndex
+     * @param {Ext.grid.header.Container} gridheader
+     * @param {Ext.grid.column.Column} column
      * @param {Ext.EventObject} event
+     * @param {HTMLElement} html
+     * @param {Object} eOpts
      *
      */
-    onHeaderContextMenu: function (grid, columIndex, event) {
+    onHeaderContextMenu: function (gridheader, colum, event, html, eOpts) {
+
         "use strict";
         event.stopEvent();
     },
@@ -123,17 +129,19 @@ Ext.define('App.grid.RowEditorContextMenu',  {
     /**
      * Show the context menu when right clicking in the non-empty grid.
      *
-     * @param {App.grid.Grid} grid
-     * @param {Number} rowIndex
+     * @param {Ext.view.view} view
+     * @param {Ext.data.Model} record
+     * @param {HTMLElement} item
      * @param {Ext.EventObject} event
+     * @param {Object} eOpts
      *
      */
-    onRowContextMenu: function (grid, rowIndex, event) {
+    onItemContextMenu: function (view, record, item, index, event, eOpts) {
 
         "use strict";
 
         event.stopEvent();
-        grid.selModel.selectRow(rowIndex);
+        //grid.selModel.selectRow(rowIndex);
         this.menu.showAt(event.getXY());
     }
 });
\ No newline at end of file
diff --git a/static/plugin_dbui/src/pgridroweditordblclick.js b/static/plugin_dbui/src/pgridroweditordblclick.js
index c3c33f1c..05569a0b 100644
--- a/static/plugin_dbui/src/pgridroweditordblclick.js
+++ b/static/plugin_dbui/src/pgridroweditordblclick.js
@@ -23,7 +23,7 @@ Ext.define('App.grid.RowEditorDblClick',  {
             throw new Error('no grid row editor !!!');
         }
 
-        grid.on('rowdblclick', grid.rowEditor.onEditRow, grid.rowEditor);
+        grid.on('itemdblclick', grid.rowEditor.onEditRow, grid.rowEditor);
     },
 
     //
@@ -36,6 +36,6 @@ Ext.define('App.grid.RowEditorDblClick',  {
 
         "use strict";
 
-        grid.un('rowdblclick', grid.rowEditor.onEditRow, grid.rowEditor);
+        grid.un('itemdblclick', grid.rowEditor.onEditRow, grid.rowEditor);
     }
 });
\ No newline at end of file
-- 
GitLab