New components, enhance applets
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.ria / src / argeo-ria-lib / security / class / org / argeo / security / ria / RolesApplet.js
index 3d6ad869fb85b2a757148d6fa5994c6b43886e04..3803968a9a9a797f33d730eb131dd3f1a4d0dbfb 100644 (file)
@@ -27,12 +27,38 @@ qx.Class.define("org.argeo.security.ria.RolesApplet",
         * Commands definition, see {@link org.argeo.ria.event.CommandsManager#definitions} 
         */
        commands : {
-               init : {}
+               init : {
+                       "edit_role" : {
+                               label           : "Edit Role", 
+                               icon            : "ria/window-close.png",
+                               shortcut        : "Control+r",
+                               enabled         : true,
+                               menu            : "Roles",
+                               toolbar         : "role",
+                               callback        : function(e){
+                                       // Call service to delete
+                                       this.setGuiMode("edit");
+                               },
+                               selectionChange : function(viewName, data){
+                                       if(viewName != "roles") return;
+                                       this.setEnabled(!(data == null || !data.length || data.length > 1));                                    
+                               },
+                               command         : null
+                       }                                               
+               }
        },
        viewSelection : {
                nullable:false, 
                check:"org.argeo.ria.components.ViewSelection"
        },
+       guiMode : {
+               apply : "_applyGuiMode"
+       },
+       chooserOriginalSelection : {},
+       chooserSelectionModified : {
+               init:false,
+               event : "chooserSelectionWasModified"
+       },
        instanceId : {init:""},
        instanceLabel : {init:""}
   },
@@ -53,13 +79,167 @@ qx.Class.define("org.argeo.security.ria.RolesApplet",
                                        return new qx.ui.table.columnmodel.Resize(obj)
                                }
                        });
-               this.table.setStatusBarVisible(false);                  
+               this.table.setStatusBarVisible(false);
+               this.table.setShowCellFocusIndicator(false);
+               this.table.setColumnVisibilityButtonVisible(false);
+               this.table.highlightFocusedRow(false);                  
                viewPane.add(this.table, {height:"100%"});
+               this.table.getSelectionModel().addListener("changeSelection", function(){
+                       this._selectionToValues(this.table.getSelectionModel(), this.getViewSelection());
+               }, this);
+               
                
-               var selectionModel = this.table.getSelectionModel(); 
-               selectionModel.addListener("changeSelection", function(){
-                       var ranges = selectionModel.getSelectedRanges();
+               this.rolesUsersStub = {"ROLE_ADMIN":["root","mbaudier"],"ROLE_USER":["mbaudier","cdujeu"]};
+                               
+               var buttonPane = new qx.ui.container.Composite(new qx.ui.layout.HBox(2, "right"));
+               this.getView().header.setPadding(0);
+               if(this.getView().headerLabel) this.getView().headerLabel.setMargin(8);
+               this.getView().header.add(buttonPane, {edge:"east"});
+       
+               this.toggleButton = new qx.ui.form.ToggleButton("Filter", "ria/go-right.png");
+               this.toggleButton.set({
+                       show:"icon",
+                       margin:2,
+                       toolTip :new qx.ui.tooltip.ToolTip("Apply automatic filtering on Users list")                   
+               });
+               buttonPane.add(this.toggleButton);
+                               
+               // TOGGLE THE GUI MODES                 
+               this.toggleButton.addListener("changeChecked", function(event){
+                       this.setGuiMode(event.getData()?"filter":"clear");
+               }, this);
+               
+               this.saveButton = new qx.ui.form.Button("Save");
+               this.saveButton.set({
+                       show:"label",
+                       margin:2,
+                       toolTip :new qx.ui.tooltip.ToolTip("Save changes"),
+                       visibility : "excluded"
                });
+               buttonPane.add(this.saveButton);                
+               
+               this.cancelButton = new qx.ui.form.Button("Cancel");
+               this.cancelButton.set({
+                       show:"label",
+                       margin:2,
+                       toolTip :new qx.ui.tooltip.ToolTip("Cancel changes"),
+                       visibility : "excluded"                         
+               });
+               buttonPane.add(this.cancelButton);              
+               
+               var listener = function(){
+                       this.setGuiMode(this.initialState);
+               };
+               this.saveButton.addListener("execute", listener, this);
+               this.cancelButton.addListener("execute", listener, this);
+               
+               this.table.addListener("cellDblclick", function(cellEvent){
+                       this.setGuiMode("edit");
+               }, this);
+               
+               this.setGuiMode("clear");
+       },
+       
+       _applyGuiMode : function(guiMode, previousMode){
+               var selectionModel = this.table.getSelectionModel();
+               if(!this.usersAppletReference){
+                       var vManager = org.argeo.ria.components.ViewsManager.getInstance();
+                       this.usersAppletReference = vManager.getViewPaneById("users").getContent();
+               }
+               
+               this.saveButton.setVisibility((guiMode=="edit"?"visible":"excluded"));
+               this.cancelButton.setVisibility((guiMode=="edit"?"visible":"excluded"));
+               this.table.setEnabled((guiMode=="edit"?false:true));
+               this.toggleButton.setVisibility((guiMode=="edit"?"excluded":"visible"));                        
+               
+               if(guiMode == "filter"){
+                       if(this.usersAppletReference){
+                               this.usersAppletReference.setGuiMode(("filter"));
+                               var viewSel = this.usersAppletReference.getViewSelection();
+                               viewSel.removeListener("changeSelection", this.monitorChooserSelectionChanges, this);                   
+                       }
+                       selectionModel.addListener("changeSelection", this.selectionToFilter, this);
+                       selectionModel.setSelectionMode(qx.ui.table.selection.Model.MULTIPLE_INTERVAL_SELECTION_TOGGLE);
+                       this.selectionToFilter();                       
+               }else if(guiMode == "edit"){
+                       if(!this.usersAppletReference) return;
+                       this.initialState = previousMode;
+                       if(previousMode == "filter"){
+                               this.usersAppletReference.setGuiMode(("clear"));
+                               selectionModel.removeListener("changeSelection", this.selectionToFilter, this);
+                       }
+                       this.usersAppletReference.setGuiMode(("chooser"));
+                       this.selectionToChooser(); // Warning, to be called before calling listener!
+                       var viewSel = this.usersAppletReference.getViewSelection();
+                       viewSel.addListener("changeSelection", this.monitorChooserSelectionChanges, this);
+               }else if(guiMode == "clear"){
+                       if(this.usersAppletReference){
+                               this.usersAppletReference.setGuiMode(("clear"));
+                               var viewSel = this.usersAppletReference.getViewSelection();
+                               viewSel.removeListener("changeSelection", this.monitorChooserSelectionChanges, this);                   
+                       }
+                       this.table.setEnabled(true);
+                       selectionModel.removeListener("changeSelection", this.selectionToFilter, this);
+                       selectionModel.setSelectionMode(qx.ui.table.selection.Model.SINGLE_SELECTION);                          
+               }
+       },
+       
+       monitorChooserSelectionChanges : function(event){
+               if(!this.usersAppletReference || this.getChooserSelectionModified()) return;
+               var initialSelection = this.getChooserOriginalSelection();
+               var crtSelection = event.getTarget().getNodes();
+               if(!qx.lang.Array.equals(initialSelection.sort(), crtSelection.sort())){
+                       this.setChooserSelectionModified(true);
+                       console.log("Changed!");
+                       this.saveButton.setEnabled(true);
+               }
+       },
+       
+       selectionToFilter : function(){
+               if(!this.usersAppletReference) return;
+               var selectionModel = this.table.getSelectionModel();
+               if(!selectionModel.getSelectedCount()){
+                       this.usersAppletReference.resetHiddenRows();
+                       return;
+               }
+               this.usersAppletReference.applyFilter(this._selectionToValues(selectionModel), "roles", true);                  
+       },
+       
+       selectionToChooser : function(){
+               if(!this.usersAppletReference) return;
+               var selectionModel = this.table.getSelectionModel();
+               if(!selectionModel.getSelectedCount()){
+                       this.usersAppletReference.resetHiddenRows();
+                       return;
+               }
+               var uniqueValue = this._selectionToValues(selectionModel)[0];
+               var initSelection = this.rolesUsersStub[uniqueValue];
+               this.setChooserOriginalSelection(initSelection);
+               this.setChooserSelectionModified(false);
+               this.usersAppletReference.applySelection(initSelection, "username");
+               this.saveButton.setEnabled(false);
+       },
+       
+       _selectionToValues : function(selectionModel, viewSelection){           
+               if(viewSelection){
+                       viewSelection.setBatchMode(true);
+                       viewSelection.clear();
+               }
+               if(!selectionModel.getSelectedCount()) return [];
+               var ranges = selectionModel.getSelectedRanges();
+               var values = [];
+               for(var i=0;i<ranges.length;i++){
+                       for(var j=ranges[i].minIndex;j<=ranges[i].maxIndex;j++){                                        
+                               values.push(this.tableModel.getData()[j][0]);
+                               if(viewSelection){
+                                       viewSelection.addNode(this.tableModel.getData()[j][0]);
+                               }
+                       }
+               }
+               if(viewSelection){
+                       viewSelection.setBatchMode(false);
+               }
+               return values;
        },
        
        /**