small refactor + fix brackets
[fnpeditor.git] / src / fnpjs / actions.js
index 8b002bf..b8aa033 100644 (file)
@@ -3,7 +3,11 @@ define(function(require) {
 'use strict';
 
 var _ = require('libs/underscore'),
 'use strict';
 
 var _ = require('libs/underscore'),
-    Backbone = require('libs/backbone');
+    Backbone = require('libs/backbone'),
+    logging = require('fnpjs/logging/logging');
+
+var logger = logging.getLogger('fnpjs.actions');
+
 
 var Action = function(fqName, definition, config, appObject) {
     this.fqName = fqName;
 
 var Action = function(fqName, definition, config, appObject) {
     this.fqName = fqName;
@@ -19,14 +23,14 @@ _.extend(Action.prototype, Backbone.Events, {
     getPluginName: function() {
         return this.fqName.split('.')[0];
     },
     getPluginName: function() {
         return this.fqName.split('.')[0];
     },
-    updateContextParam: function(contextName, value) {
+    updateParam: function(filter, value) {
         var changed = false;
         _.pairs(this.definition.params).forEach(function(pair) {
             var paramName = pair[0],
                 paramDesc = pair[1];
         var changed = false;
         _.pairs(this.definition.params).forEach(function(pair) {
             var paramName = pair[0],
                 paramDesc = pair[1];
-            if(paramDesc.type === 'context' && paramDesc.name === contextName) {
-                 this.params[paramName] = value;
-                 changed = true;
+            if(filter(paramDesc, paramName)) {
+                this.params[paramName] = value;
+                changed = true;
             }
         }.bind(this));
         if(changed) {
             }
         }.bind(this));
         if(changed) {
@@ -34,35 +38,30 @@ _.extend(Action.prototype, Backbone.Events, {
             this.trigger('paramsChanged');
         }
     },
             this.trigger('paramsChanged');
         }
     },
+    updateContextParam: function(contextName, value) {
+        this.updateParam(function(paramDesc) {
+            return paramDesc.type === 'context' && paramDesc.name === contextName;
+        }, value);
+    },
     updateKeyParam: function(keyName, toggled) {
     updateKeyParam: function(keyName, toggled) {
-        var changed = false;
-        _.pairs(this.definition.params).forEach(function(pair) {
-            var paramName = pair[0],
-                paramDesc = pair[1];
-            if(paramDesc.type === 'key' && paramDesc.key === keyName) {
-                 this.params[paramName] = toggled;
-                 changed = true;
-            }
-        }.bind(this));
-
-        if(changed) {
-            this._cache = null;
-            this.trigger('paramsChanged');
-        }
+        this.updateParam(function(paramDesc) {
+            return paramDesc.type === 'key' && paramDesc.key === keyName;
+        }, toggled);
     },
     updateWidgetParam: function(name, value) {
     },
     updateWidgetParam: function(name, value) {
-        var paramDesc = this.definition.params[name];
-        if(paramDesc.type === 'context' || paramDesc.type === 'key') {
-            throw new Error('');
-        }
-        this.params[name] = value;
-        this._cache = null;
-        this.trigger('paramsChanged');
+        this.updateParam(function(paramDesc, paramName) {
+            return !_.contains(['context', 'key'], paramDesc.type) && paramName === name;
+        }, value);
     },
     getState: function() {
         var gotState;
         if(!this._cache) {
     },
     getState: function() {
         var gotState;
         if(!this._cache) {
-            gotState = this.definition.getState.call(this, this.params);
+            try {
+                gotState = this.definition.getState.call(this, this.params);
+            } catch(e) {
+                logger.exception(e);
+                return;
+            }
             if(typeof gotState === 'boolean') {
                 gotState = {allowed: gotState};
             }
             if(typeof gotState === 'boolean') {
                 gotState = {allowed: gotState};
             }
@@ -75,8 +74,13 @@ _.extend(Action.prototype, Backbone.Events, {
     },
     execute: function() {
         var state = this.getState();
     },
     execute: function() {
         var state = this.getState();
+        
+        var callback = function(ret) {
+            this.trigger('actionExecuted', ret);
+        }.bind(this);
+
         if(state.allowed) {
         if(state.allowed) {
-            return state.execute.call(this, this.params, this.appObject);
+            return state.execute.call(this, callback, this.params, this.appObject);
         }
         throw new Error('Execution not allowed');
     }
         }
         throw new Error('Execution not allowed');
     }