editor: delete/insert at the edges of a span
[fnpeditor.git] / src / fnpjs / runner.js
index 2f00170..f3edd31 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')) {
@@ -31,10 +33,6 @@ var Runner = function(app, modules) {
             return bootstrappedData[moduleName];
         };
         
-        this.getTemplate = function(templateName) {
-            return _.template($('[data-template-name="' + moduleName + '.' + templateName + '"]').html().trim());
-        };
-        
         this.publish = function(eventName) {
             var eventArgs = Array.prototype.slice.call(arguments, 1);
             _.each(eventListeners, function(listenerModuleName) {
@@ -62,6 +60,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 +87,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 +103,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();
         });