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.getTemplate = function(templateName) {
37 return _.template($('[data-template-name="' + moduleName + '.' + templateName + '"]').html().trim());
40 this.publish = function(eventName) {
41 var eventArgs = Array.prototype.slice.call(arguments, 1);
42 _.each(eventListeners, function(listenerModuleName) {
43 var listener = moduleInstances[listenerModuleName];
45 listener.handleEvent(moduleName, eventName, eventArgs);
50 var permissions = app.permissions[moduleName];
52 this.getModule = _.contains(permissions, 'getModule') ? function(requestedModuleName) {
53 return getModuleInstance(requestedModuleName);
56 this.getDOM = _.contains(permissions, 'getDOM') ? function() {
57 return $(config.rootSelector);
60 this.getPlugins = function() {
64 this.getConfig = function() {
68 this.createAction = function(fqName, config) {
69 var definition = actionDefinitions[fqName];
71 throw new Error('Invalid action: ' + fqName);
73 return new actions.Action(fqName, definition, config, actionsAppObject);
76 this.registerKeyHandler = function(eventName, handler) {
77 $('body').on(eventName, function(e) {
82 this.registerActionsAppObject = function(_actionsAppObject) {
83 actionsAppObject = _actionsAppObject;
88 this.setBootstrappedData = function(moduleName, data) {
89 bootstrappedData[moduleName] = data;
92 this.registerPlugin = function(plugin) {
94 (plugin.actions || []).forEach(function(definition) {
95 var actionFqName = plugin.name + '.' + definition.name;
96 actionDefinitions[actionFqName] = definition;
100 this.start = function(_config) {
107 logging.setConfig(config.logging);
110 _.pairs(config.plugins || {}).forEach(function(pair) {
111 var pluginName = pair[0],
112 pluginConfig = pair[1];
114 plugins.some(function(plugin) {
115 if(plugin.name === pluginName) {
116 if(_.isFunction(plugin.config)) {
117 plugin.config(pluginConfig);
124 app.initModules.forEach(function(moduleName) {
125 getModuleInstance(moduleName).start();