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 = {},
21 _.each(_.keys(modules || {}), function(moduleName) {
22 if(_.contains(app.permissions[moduleName] || [], 'handleEvents')) {
23 eventListeners.push(moduleName);
29 var Sandbox = function(moduleName) {
33 this.getBootstrappedData = function() {
34 return bootstrappedData[moduleName];
37 this.publish = function(eventName) {
38 var eventArgs = Array.prototype.slice.call(arguments, 1);
39 _.each(eventListeners, function(listenerModuleName) {
40 var listener = moduleInstances[listenerModuleName];
42 listener.handleEvent(moduleName, eventName, eventArgs);
47 var permissions = app.permissions[moduleName];
49 this.getModule = _.contains(permissions, 'getModule') ? function(requestedModuleName) {
50 return getModuleInstance(requestedModuleName);
53 this.getDOM = _.contains(permissions, 'getDOM') ? function() {
54 return $(config.rootSelector);
57 this.getPlugins = function() {
61 this.getConfig = function() {
65 this.createAction = function(fqName, config) {
66 var definition = actionDefinitions[fqName];
68 throw new Error('Invalid action: ' + fqName);
70 return new actions.Action(fqName, definition, config, actionsAppObject);
73 this.registerKeyHandler = function(eventName, handler) {
74 $('body').on(eventName, function(e) {
79 this.registerActionsAppObject = function(_actionsAppObject) {
80 actionsAppObject = _actionsAppObject;
83 this.showContextMenu = function(menu, coors) {
84 if(currentContextMenu) {
85 currentContextMenu.close();
87 currentContextMenu = menu;
88 $(config.rootSelector).append(menu.dom);
89 menu.dom.css({top: coors.y, left: coors.x});
94 this.setBootstrappedData = function(moduleName, data) {
95 bootstrappedData[moduleName] = data;
98 this.registerPlugin = function(plugin) {
100 (plugin.actions || []).forEach(function(definition) {
101 var actionFqName = plugin.name + '.' + definition.name;
102 actionDefinitions[actionFqName] = definition;
106 this.start = function(_config) {
113 logging.setConfig(config.logging);
116 _.pairs(config.plugins || {}).forEach(function(pair) {
117 var pluginName = pair[0],
118 pluginConfig = pair[1];
120 plugins.some(function(plugin) {
121 if(plugin.name === pluginName) {
122 if(_.isFunction(plugin.config)) {
123 plugin.config(pluginConfig);
130 app.initModules.forEach(function(moduleName) {
131 getModuleInstance(moduleName).start();
134 $(config.rootSelector)[0].addEventListener('click', function(e) {
135 if(currentContextMenu && !currentContextMenu.dom[0].contains(e.target)) {
136 currentContextMenu.close();
137 currentContextMenu = null;