1 define(['libs/jquery', 'libs/underscore', 'fnpjs/logging/logging', 'fnpjs/actions'], function($, _, logging, actions) {
5 var Runner = function(app, modules) {
7 function getModuleInstance(moduleName) {
8 var module = moduleInstances[moduleName] = (moduleInstances[moduleName] || modules[moduleName](new Sandbox(moduleName)));
12 var bootstrappedData = {},
16 actionDefinitions = {},
20 _.each(_.keys(modules || {}), function(moduleName) {
21 if(_.contains(app.permissions[moduleName] || [], 'handleEvents')) {
22 eventListeners.push(moduleName);
28 var Sandbox = function(moduleName) {
32 this.getBootstrappedData = function() {
33 return bootstrappedData[moduleName];
36 this.publish = function(eventName) {
37 var eventArgs = Array.prototype.slice.call(arguments, 1);
38 _.each(eventListeners, function(listenerModuleName) {
39 var listener = moduleInstances[listenerModuleName];
41 listener.handleEvent(moduleName, eventName, eventArgs);
46 var permissions = app.permissions[moduleName];
48 this.getModule = _.contains(permissions, 'getModule') ? function(requestedModuleName) {
49 return getModuleInstance(requestedModuleName);
52 this.getDOM = _.contains(permissions, 'getDOM') ? function() {
53 return $(config.rootSelector);
56 this.getPlugins = function() {
60 this.getConfig = function() {
64 this.createAction = function(fqName, config) {
65 var definition = actionDefinitions[fqName];
67 throw new Error('Invalid action: ' + fqName);
69 return new actions.Action(fqName, definition, config, actionsAppObject);
72 this.registerKeyHandler = function(eventName, handler) {
73 $('body').on(eventName, function(e) {
78 this.registerActionsAppObject = function(_actionsAppObject) {
79 actionsAppObject = _actionsAppObject;
84 this.setBootstrappedData = function(moduleName, data) {
85 bootstrappedData[moduleName] = data;
88 this.registerPlugin = function(plugin) {
90 (plugin.actions || []).forEach(function(definition) {
91 var actionFqName = plugin.name + '.' + definition.name;
92 actionDefinitions[actionFqName] = definition;
96 this.start = function(_config) {
103 logging.setConfig(config.logging);
106 _.pairs(config.plugins || {}).forEach(function(pair) {
107 var pluginName = pair[0],
108 pluginConfig = pair[1];
110 plugins.some(function(plugin) {
111 if(plugin.name === pluginName) {
112 if(_.isFunction(plugin.config)) {
113 plugin.config(pluginConfig);
120 app.initModules.forEach(function(moduleName) {
121 getModuleInstance(moduleName).start();