wlxml: context root
[fnpeditor.git] / src / fnpjs / runner.js
1 define(['libs/jquery', 'libs/underscore', 'fnpjs/logging/logging', 'fnpjs/actions'], function($, _, logging, actions) {
2
3 'use strict';
4
5 var Runner = function(app, modules) {
6
7     function getModuleInstance(moduleName) {
8         var module = moduleInstances[moduleName] = (moduleInstances[moduleName] || modules[moduleName](new Sandbox(moduleName)));
9         return module;
10     }
11
12     var bootstrappedData = {},
13         moduleInstances = {},
14         eventListeners = [],
15         plugins = [],
16         actionDefinitions = {},
17         config,
18         actionsAppObject;
19         
20     _.each(_.keys(modules || {}), function(moduleName) {
21         if(_.contains(app.permissions[moduleName] || [], 'handleEvents')) {
22             eventListeners.push(moduleName);
23         }
24     });
25
26     
27         
28     var Sandbox = function(moduleName) {
29         this.$ = $;
30         this._ = _;
31         
32         this.getBootstrappedData = function() {
33             return bootstrappedData[moduleName];
34         };
35         
36         this.publish = function(eventName) {
37             var eventArgs = Array.prototype.slice.call(arguments, 1);
38             _.each(eventListeners, function(listenerModuleName) {
39                 var listener = moduleInstances[listenerModuleName];
40                 if(listener) {
41                     listener.handleEvent(moduleName, eventName, eventArgs);
42                 }
43             });
44         };
45         
46         var permissions = app.permissions[moduleName];
47         
48         this.getModule = _.contains(permissions, 'getModule') ? function(requestedModuleName) {
49             return getModuleInstance(requestedModuleName);
50         } : undefined;
51         
52         this.getDOM = _.contains(permissions, 'getDOM') ? function() {
53             return $(config.rootSelector);
54         } : undefined;
55
56         this.getPlugins = function() {
57             return plugins;
58         };
59
60         this.getConfig = function() {
61             return config;
62         };
63
64         this.createAction = function(fqName, config) {
65             var definition = actionDefinitions[fqName];
66             if(!definition) {
67                 throw new Error('Invalid action: ' + fqName);
68             }
69             return new actions.Action(fqName, definition, config, actionsAppObject);
70         };
71
72         this.registerKeyHandler = function(eventName, handler) {
73             $('body').on(eventName, function(e) {
74                 handler(e);
75             });
76         };
77
78         this.registerActionsAppObject = function(_actionsAppObject) {
79             actionsAppObject = _actionsAppObject;
80         };
81     };
82     
83     
84     this.setBootstrappedData = function(moduleName, data) {
85         bootstrappedData[moduleName] = data;
86     };
87
88     this.registerPlugin = function(plugin) {
89         plugins.push(plugin);
90         (plugin.actions || []).forEach(function(definition) {
91             var actionFqName = plugin.name + '.' + definition.name;
92             actionDefinitions[actionFqName] = definition;
93         });
94     };
95     
96     this.start = function(_config) {
97         config = _.extend({
98             rootSelector: 'body'
99         }, _config);
100
101
102         if(config.logging) {
103             logging.setConfig(config.logging);
104         }
105
106         _.pairs(config.plugins || {}).forEach(function(pair) {
107             var pluginName = pair[0],
108                 pluginConfig = pair[1];
109
110             plugins.some(function(plugin) {
111                 if(plugin.name === pluginName) {
112                     if(_.isFunction(plugin.config)) {
113                         plugin.config(pluginConfig);
114                     }
115                     return true; //break
116                 }
117             });
118         });
119
120         app.initModules.forEach(function(moduleName) {
121             getModuleInstance(moduleName).start();
122         });
123     };
124 };
125
126 return {
127     Runner: Runner
128 };
129
130 });