Daniel@0: "use strict"; Daniel@0: Daniel@0: App.module("GraphicsRenderingModule", function(GraphicsRenderingModule, App, Backbone, Marionette, $, _, Logger) { Daniel@0: Daniel@0: GraphicsRenderingModule.addInitializer(function(options){ Daniel@0: Daniel@0: GraphicsRenderingModule.RendererPrototypes = {}; Daniel@0: GraphicsRenderingModule.renderers = {}; Daniel@0: Daniel@0: /** Daniel@0: * Registers a new renderer Daniel@0: */ Daniel@0: GraphicsRenderingModule.registerRenderer = function(rendererDefinition) { Daniel@0: var RendererPrototype; Daniel@0: var inheritId = rendererDefinition.inherit ? rendererDefinition.inherit : ""; Daniel@0: var id = rendererDefinition.id; Daniel@0: if (!id) { Daniel@0: throw _.str.sprintf("Renderer definition does not contain an id"); Daniel@0: } Daniel@0: var rendererWithThisId = null; Daniel@0: try { Daniel@0: rendererWithThisId = GraphicsRenderingModule.getRendererById(id); Daniel@0: } catch (e) { Daniel@0: } Daniel@0: if (rendererWithThisId) { Daniel@0: throw _.str.sprintf("Renderer with id \"%s\" already exists", id); Daniel@0: } Daniel@0: Daniel@0: var prototypeOptions = {}; Daniel@0: var prototypeDefaultVegaConfig = {}; Daniel@0: if (!inheritId) { Daniel@0: RendererPrototype = Marionette.Object; Daniel@0: } else { Daniel@0: try { Daniel@0: RendererPrototype = GraphicsRenderingModule.getRendererPrototypeById(inheritId); Daniel@0: var parentRenderer = GraphicsRenderingModule.getRendererById(inheritId); Daniel@0: prototypeOptions = parentRenderer.options; Daniel@0: prototypeDefaultVegaConfig = parentRenderer.defaultVegaConfig; Daniel@0: } catch (e) { Daniel@0: throw _.str.sprintf("Renderer with id \"%s\" could not be inherited from renderer with id \"%s\" (it does not exist)", id, inheritId); Daniel@0: } Daniel@0: } Daniel@0: Daniel@0: var combinedOptions = rendererDefinition.options Daniel@0: ? _.defaults({}, rendererDefinition.options, prototypeOptions) Daniel@0: : _.clone(prototypeOptions); Daniel@0: var combinedDefaultVegaConfig = rendererDefinition.defaultVegaConfig Daniel@0: ? _.defaults({}, rendererDefinition.defaultVegaConfig, prototypeDefaultVegaConfig) Daniel@0: : _.clone(prototypeDefaultVegaConfig); Daniel@0: Daniel@0: for (var key in rendererDefinition.options) { Daniel@0: if (rendererDefinition.options.hasOwnProperty(key) && rendererDefinition.options[key] === undefined) { Daniel@0: delete combinedOptions[key]; Daniel@0: } Daniel@0: } Daniel@0: rendererDefinition.options = combinedOptions; Daniel@0: Daniel@0: for (var key in rendererDefinition.defaultVegaConfig) { Daniel@0: if (rendererDefinition.defaultVegaConfig.hasOwnProperty(key) && rendererDefinition.defaultVegaConfig[key] === undefined) { Daniel@0: delete combinedDefaultVegaConfig[key]; Daniel@0: } Daniel@0: } Daniel@0: rendererDefinition.defaultVegaConfig = combinedDefaultVegaConfig; Daniel@0: Daniel@0: var Renderer = RendererPrototype.extend(rendererDefinition); Daniel@0: GraphicsRenderingModule.RendererPrototypes[id] = Renderer; Daniel@0: GraphicsRenderingModule.renderers[id] = new Renderer(); Daniel@0: }; Daniel@0: Daniel@0: GraphicsRenderingModule.getRendererPrototypeById = function(id) { Daniel@0: var RendererPrototype = GraphicsRenderingModule.RendererPrototypes[id]; Daniel@0: if (!RendererPrototype) { Daniel@0: throw _.str.sprintf("RendererPrototype with id \"%s\" not found.", id); Daniel@0: } Daniel@0: return RendererPrototype; Daniel@0: }; Daniel@0: Daniel@0: GraphicsRenderingModule.getRendererById = function(id) { Daniel@0: var renderer = GraphicsRenderingModule.renderers[id]; Daniel@0: if (!renderer) { Daniel@0: throw _.str.sprintf("Renderer with id \"%s\" not found.", id); Daniel@0: } Daniel@0: return renderer; Daniel@0: }; Daniel@0: Daniel@0: GraphicsRenderingModule.render = function(id, $element, data, options) { Daniel@0: GraphicsRenderingModule.getRendererById(id).render($element, data, options); Daniel@0: }; Daniel@0: }); Daniel@0: }, Logger);