fnpjs: actions
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 18 Feb 2014 11:43:47 +0000 (12:43 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 23 Apr 2014 11:05:03 +0000 (13:05 +0200)
src/fnpjs/actions.js [new file with mode: 0644]
src/fnpjs/runner.js

diff --git a/src/fnpjs/actions.js b/src/fnpjs/actions.js
new file mode 100644 (file)
index 0000000..8b002bf
--- /dev/null
@@ -0,0 +1,90 @@
+define(function(require) {
+    
+'use strict';
+
+var _ = require('libs/underscore'),
+    Backbone = require('libs/backbone');
+
+var Action = function(fqName, definition, config, appObject) {
+    this.fqName = fqName;
+    this.definition = definition;
+    this.config = config;
+    this._cache = null;
+    this.appObject = appObject;
+
+    this.params = {};
+};
+
+_.extend(Action.prototype, Backbone.Events, {
+    getPluginName: function() {
+        return this.fqName.split('.')[0];
+    },
+    updateContextParam: function(contextName, value) {
+        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;
+            }
+        }.bind(this));
+        if(changed) {
+            this._cache = null;
+            this.trigger('paramsChanged');
+        }
+    },
+    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');
+        }
+    },
+    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');
+    },
+    getState: function() {
+        var gotState;
+        if(!this._cache) {
+            gotState = this.definition.getState.call(this, this.params);
+            if(typeof gotState === 'boolean') {
+                gotState = {allowed: gotState};
+            }
+            this._cache = _.extend({}, this.definition.stateDefaults || {}, gotState);
+        }
+        if(this._cache === false) {
+            this._cache = {allowed: false};
+        }
+        return this._cache;
+    },
+    execute: function() {
+        var state = this.getState();
+        if(state.allowed) {
+            return state.execute.call(this, this.params, this.appObject);
+        }
+        throw new Error('Execution not allowed');
+    }
+});
+
+
+return {
+    Action: Action
+};
+
+});
index 2f00170..6c28a61 100644 (file)
@@ -1,4 +1,4 @@
-define(['libs/jquery', 'libs/underscore', 'fnpjs/logging/logging'], function($, _, logging) {
+define(['libs/jquery', 'libs/underscore', 'fnpjs/logging/logging', 'fnpjs/actions'], function($, _, logging, actions) {
 
 'use strict';
 
@@ -13,7 +13,9 @@ var Runner = function(app, modules) {
         moduleInstances = {},
         eventListeners = [],
         plugins = [],
-        config;
+        actionDefinitions = {},
+        config,
+        actionsAppObject;
         
     _.each(_.keys(modules || {}), function(moduleName) {
         if(_.contains(app.permissions[moduleName] || [], 'handleEvents')) {
@@ -62,6 +64,24 @@ var Runner = function(app, modules) {
         this.getConfig = function() {
             return config;
         };
+
+        this.createAction = function(fqName, config) {
+            var definition = actionDefinitions[fqName];
+            if(!definition) {
+                throw new Error('Invalid action: ' + fqName);
+            }
+            return new actions.Action(fqName, definition, config, actionsAppObject);
+        };
+
+        this.registerKeyHandler = function(eventName, handler) {
+            $('body').on(eventName, function(e) {
+                handler(e);
+            });
+        };
+
+        this.registerActionsAppObject = function(_actionsAppObject) {
+            actionsAppObject = _actionsAppObject;
+        };
     };
     
     
@@ -71,6 +91,10 @@ var Runner = function(app, modules) {
 
     this.registerPlugin = function(plugin) {
         plugins.push(plugin);
+        (plugin.actions || []).forEach(function(definition) {
+            var actionFqName = plugin.name + '.' + definition.name;
+            actionDefinitions[actionFqName] = definition;
+        });
     };
     
     this.start = function(_config) {
@@ -83,6 +107,20 @@ var Runner = function(app, modules) {
             logging.setConfig(config.logging);
         }
 
+        _.pairs(config.plugins || {}).forEach(function(pair) {
+            var pluginName = pair[0],
+                pluginConfig = pair[1];
+
+            plugins.some(function(plugin) {
+                if(plugin.name === pluginName) {
+                    if(_.isFunction(plugin.config)) {
+                        plugin.config(pluginConfig);
+                    }
+                    return true; //break
+                }
+            });
+        });
+
         app.initModules.forEach(function(moduleName) {
             getModuleInstance(moduleName).start();
         });