Validate the code as submitted, not as it is in the database
[L-seed.git] / web / js / communication.js
index 584be1f..2c1ea6e 100644 (file)
@@ -22,6 +22,8 @@ Lseed.MessageCommands = {
 };
 
 Lseed.Communication = function() {
+       
+       this.Username = '';
 
        // ----- Framework -----
        
@@ -31,7 +33,7 @@ Lseed.Communication = function() {
                var params = {};
                Ext.apply(params, opts, { cmd: cmd });
                Ext.Ajax.request({
-                       url: 'php/communication.php',
+                       url: 'php/Communication.php',
                        success: this.handleResponse.createDelegate(this)
                        ,failure: function(response, opts) {
                                Ext.MessageBox.alert("Fehler", "Es konnte keine Verbindung zum Server hergestellt werden.")
@@ -103,6 +105,9 @@ Lseed.Communication = function() {
                                break;
                        default:
                                this.loadTab(content, contentname);
+                               if (contentname == "editplant") {
+                                       editor.EditCallback();
+                               }
                                break;
                }
        };
@@ -192,6 +197,8 @@ Lseed.Communication = function() {
                        var pw = cmpPw.getValue();
                        pw = MD5(pw);
                        Ext.MessageBox.wait("Authentifiziere.", "Wird geladen...");
+                               
+                       this.Username = user;
                        
                        this.sendMessage(Lseed.MessageCommands.RPC, { func: 'Auth', user: user, pw: pw });
                        this.hideLoginDialog();
@@ -299,6 +306,8 @@ Lseed.Communication = function() {
                        if (pw == pwRepeat) {
                                pw = MD5(pw);
                                Ext.MessageBox.wait("Registriere.", "Wird geladen...");
+                               
+                               this.Username = user;
 
                                this.sendMessage(Lseed.MessageCommands.RPC, { func: 'Register', user: user, pw: pw });
                                this.hideRegisterDialog();
@@ -371,7 +380,20 @@ Lseed.Communication = function() {
                        cmp.removeAll();
                        cmp.add(elem);
                } else {
-                       console.error("Lseed.Communication.clearNavigation: 'navTree' does not exist.");
+                       console.error("Lseed.Communication.closeAllTabs: 'contentTabPanel' does not exist.");
+               }
+       };
+       
+       this.closeTab = function(tabname) {
+               var cmp = Ext.getCmp('contentTabPanel');
+               if (cmp) {
+                       var tab = this.getTab(tabname);
+                       if (tab) {
+                               console.error("Lseed.Communication.closeTab: '"+tabname+"' does not exist.");
+                       }
+                       cmp.remove(tab);
+               } else {
+                       console.error("Lseed.Communication.closeTab: 'contentTabPanel' does not exist.");
                }
        };
        
@@ -451,6 +473,24 @@ Lseed.Communication = function() {
                        console.error("Lseed.Communication.stopWaitingForPage: loadPageProgressbar is not defined.");
                }
        };
+       
+       // === Season Managerment ===
+       
+       this.GetSeasonList = function() {
+               Ext.MessageBox.wait("Saisons werden geladen.", "Wird geladen...");
+               
+               this.sendMessage(Lseed.MessageCommands.RPC, { func: 'GetSeasonList' });
+       };
+       
+       this.GetSeasonListCallback = function(data) {
+               var grid = Ext.getCmp("seasonlistgrid");
+               if (grid) {
+                       grid.store.loadData(data.list);
+               } else {
+                       console.error("Lseed.Communication.GetSeasonList: 'seasonlistgrid' could not be found.");
+               }
+       }
+       
 
        this.Init = function(editor) {
                Ext.MessageBox.wait("Programmeinstellungen werden geladen.", "Wird geladen...");
@@ -459,10 +499,12 @@ Lseed.Communication = function() {
                this.AddCallback("Auth", this.AuthCallback.createDelegate(this));
                this.AddCallback("Register", this.RegisterCallback.createDelegate(this));
                this.AddCallback("GetPlantList", this.GetPlantListCallback.createDelegate(this));
+               this.AddCallback("GetSeasonList", this.GetSeasonListCallback.createDelegate(this));
                
                this.AddCallback("SavePlant", editor.SaveCallback.createDelegate(editor));
                this.AddCallback("DeletePlant", editor.DeleteCallback.createDelegate(editor));
-               this.AddCallback("ValidatePlant", editor.CheckSyntaxCallback.createDelegate(editor));
+               this.AddCallback("ValidatePlant", editor.HandleSyntaxCheckAnswerForEditor.createDelegate(editor));
+               this.AddCallback("ActivatePlant", editor.ActivateCallback.createDelegate(editor));
                
                this.sendMessage(Lseed.MessageCommands.RPC, {func: 'IsLoggedIn'});
        };
@@ -479,7 +521,7 @@ Lseed.Editor = function() {
                
                communication.sendMessage(Lseed.MessageCommands.RPC, { 
                        func: 'SavePlant'
-                       ,name: plant.data.Name
+                       ,plant: plant.data.Name
                        ,code: plant.data.Code
                });
        };
@@ -492,6 +534,39 @@ Lseed.Editor = function() {
                }
        };
        
+       this.Activate = function(plant) {
+               console.log(arguments);
+               communication.sendMessage(Lseed.MessageCommands.RPC, { 
+                       func: 'ActivatePlant'
+                       ,plant: plant.data.Name
+                       ,user: communication.Username
+               });
+       };
+       
+       this.ActivateCallback = function(data) {
+               if (data.success) {
+                       communication.showMessage("Erfolgreich aktiviert.", "info");
+                       communication.GetPlantList();
+               } else {
+                       communication.showMessage(data.msg, "error");
+               }
+       };
+       
+       this.Edit = function(plant) {
+               communication.sendMessage(Lseed.MessageCommands.ContentRequest, {content: 'editplant'});
+               this.LastEditPlant = plant;
+       };
+       this.EditCallback = function() {
+               var cmp = Ext.getCmp("editplantdefinitionnamefield");
+               if (cmp) {
+                       cmp.setValue(this.LastEditPlant.data.Name);
+               }
+               var cmp = Ext.getCmp("editplantdefinitioneditor");
+               if (cmp) {
+                       cmp.setValue(this.LastEditPlant.data.Code);
+               }
+       };
+       
        this.Test = function(plant, callback) {
                communication.showMessage("Diese Funktion ist leider momentan nicht verf├╝gbar", "error");
        };
@@ -499,25 +574,52 @@ Lseed.Editor = function() {
        this.TestCallback = function() {
        };
        
-       this.CheckSyntaxCallback = null;
        this.CheckSyntax = function(plant, callback) {
-               this.CheckSyntaxCallback = callback;
+               if (typeof callback != 'undefined' && callback != null) {
+                       this.CheckSyntaxCallback = callback;
+               }
                
                communication.sendMessage(Lseed.MessageCommands.RPC, { 
                        func: 'ValidatePlant'
+                       ,plant: plant.data.Name
                        ,code: plant.data.Code
                });
        };
        
-       this.CheckSyntaxCallback = function(data) {
-               if (!data.valid) {
-                       if (this.CheckSyntaxCallback != null) {
-                               this.CheckSyntaxCallback(data);
+       this.HandleSyntaxCheckAnswerForEditor = function(data) {
+               if (data.success || data.valid) {
+                       Ext.MessageBox.show({
+                               title:'Valide'
+                               ,msg: "Alles Super."
+                               ,buttons: Ext.Msg.OK
+                               ,icon: Ext.MessageBox.Info
+                       });
+               } else {
+                       // This is a hack for now:
+                       var pdEditor = Ext.getCmp("plantdefinitioneditor");
+                       if (!pdEditor) {
+                               var pdEditor = Ext.getCmp("editplantdefinitioneditor");
+                       }
+                       if (pdEditor) {
+                               communication.stopWaitingForPage();
+                               Ext.MessageBox.show({
+                                       title:'Fehler'
+                                       ,msg: data.msg
+                                       ,buttons: Ext.Msg.OK
+                                       ,fn: function() {
+                                               if (typeof data.line != "undefined") {
+                                                       var index = editor.GetStartFromField(pdEditor, data.line-1, data.column);
+                                                       if (index != -1) {
+                                                               pdEditor.selectText(index-1, index);
+                                                       }
+                                               }
+                                       }
+                                       ,icon: Ext.MessageBox.ERROR
+                               });
                        } else {
-                               console.error("Lseed.Editor.CheckSyntaxCallback: no Callback given.");
+                               console.error("Lseed.Editor.CheckSyntaxCallback_Callback: 'plantdefinitioneditor' Could not be found.");
+                               alert("Es trat ein Fehler auf...sowas.");
                        }
-               } else {
-                       console.info("Syntax is Valid.");
                }
        };
        
@@ -561,5 +663,22 @@ Lseed.Plant = Ext.data.Record.create([{
 }, {
        name: 'Code'
        ,type: 'string'
+}, {
+       name: 'IsActive'
+       ,type: 'boolean'
+}]);
+
+Lseed.Season = Ext.data.Record.create([{
+       name: 'ID'
+       ,type: 'int'
+}, {
+       name: 'User'
+       ,type: 'string'
+}, {
+       name: 'IsRunning'
+       ,type: 'boolean'
+}, {
+       name: 'Score'
+       ,type: 'float'
 }]);