Daniel@0
|
1 "use strict";
|
Daniel@0
|
2
|
Daniel@0
|
3 App.module("GraphicsRenderingModule", function(GraphicsRenderingModule, App, Backbone, Marionette, $, _, Logger) {
|
Daniel@0
|
4
|
Daniel@0
|
5 GraphicsRenderingModule.addInitializer(function(options){
|
Daniel@0
|
6
|
Daniel@0
|
7 GraphicsRenderingModule.RendererPrototypes = {};
|
Daniel@0
|
8 GraphicsRenderingModule.renderers = {};
|
Daniel@0
|
9
|
Daniel@0
|
10 /**
|
Daniel@0
|
11 * Registers a new renderer
|
Daniel@0
|
12 */
|
Daniel@0
|
13 GraphicsRenderingModule.registerRenderer = function(rendererDefinition) {
|
Daniel@0
|
14 var RendererPrototype;
|
Daniel@0
|
15 var inheritId = rendererDefinition.inherit ? rendererDefinition.inherit : "";
|
Daniel@0
|
16 var id = rendererDefinition.id;
|
Daniel@0
|
17 if (!id) {
|
Daniel@0
|
18 throw _.str.sprintf("Renderer definition does not contain an id");
|
Daniel@0
|
19 }
|
Daniel@0
|
20 var rendererWithThisId = null;
|
Daniel@0
|
21 try {
|
Daniel@0
|
22 rendererWithThisId = GraphicsRenderingModule.getRendererById(id);
|
Daniel@0
|
23 } catch (e) {
|
Daniel@0
|
24 }
|
Daniel@0
|
25 if (rendererWithThisId) {
|
Daniel@0
|
26 throw _.str.sprintf("Renderer with id \"%s\" already exists", id);
|
Daniel@0
|
27 }
|
Daniel@0
|
28
|
Daniel@0
|
29 var prototypeOptions = {};
|
Daniel@0
|
30 var prototypeDefaultVegaConfig = {};
|
Daniel@0
|
31 if (!inheritId) {
|
Daniel@0
|
32 RendererPrototype = Marionette.Object;
|
Daniel@0
|
33 } else {
|
Daniel@0
|
34 try {
|
Daniel@0
|
35 RendererPrototype = GraphicsRenderingModule.getRendererPrototypeById(inheritId);
|
Daniel@0
|
36 var parentRenderer = GraphicsRenderingModule.getRendererById(inheritId);
|
Daniel@0
|
37 prototypeOptions = parentRenderer.options;
|
Daniel@0
|
38 prototypeDefaultVegaConfig = parentRenderer.defaultVegaConfig;
|
Daniel@0
|
39 } catch (e) {
|
Daniel@0
|
40 throw _.str.sprintf("Renderer with id \"%s\" could not be inherited from renderer with id \"%s\" (it does not exist)", id, inheritId);
|
Daniel@0
|
41 }
|
Daniel@0
|
42 }
|
Daniel@0
|
43
|
Daniel@0
|
44 var combinedOptions = rendererDefinition.options
|
Daniel@0
|
45 ? _.defaults({}, rendererDefinition.options, prototypeOptions)
|
Daniel@0
|
46 : _.clone(prototypeOptions);
|
Daniel@0
|
47 var combinedDefaultVegaConfig = rendererDefinition.defaultVegaConfig
|
Daniel@0
|
48 ? _.defaults({}, rendererDefinition.defaultVegaConfig, prototypeDefaultVegaConfig)
|
Daniel@0
|
49 : _.clone(prototypeDefaultVegaConfig);
|
Daniel@0
|
50
|
Daniel@0
|
51 for (var key in rendererDefinition.options) {
|
Daniel@0
|
52 if (rendererDefinition.options.hasOwnProperty(key) && rendererDefinition.options[key] === undefined) {
|
Daniel@0
|
53 delete combinedOptions[key];
|
Daniel@0
|
54 }
|
Daniel@0
|
55 }
|
Daniel@0
|
56 rendererDefinition.options = combinedOptions;
|
Daniel@0
|
57
|
Daniel@0
|
58 for (var key in rendererDefinition.defaultVegaConfig) {
|
Daniel@0
|
59 if (rendererDefinition.defaultVegaConfig.hasOwnProperty(key) && rendererDefinition.defaultVegaConfig[key] === undefined) {
|
Daniel@0
|
60 delete combinedDefaultVegaConfig[key];
|
Daniel@0
|
61 }
|
Daniel@0
|
62 }
|
Daniel@0
|
63 rendererDefinition.defaultVegaConfig = combinedDefaultVegaConfig;
|
Daniel@0
|
64
|
Daniel@0
|
65 var Renderer = RendererPrototype.extend(rendererDefinition);
|
Daniel@0
|
66 GraphicsRenderingModule.RendererPrototypes[id] = Renderer;
|
Daniel@0
|
67 GraphicsRenderingModule.renderers[id] = new Renderer();
|
Daniel@0
|
68 };
|
Daniel@0
|
69
|
Daniel@0
|
70 GraphicsRenderingModule.getRendererPrototypeById = function(id) {
|
Daniel@0
|
71 var RendererPrototype = GraphicsRenderingModule.RendererPrototypes[id];
|
Daniel@0
|
72 if (!RendererPrototype) {
|
Daniel@0
|
73 throw _.str.sprintf("RendererPrototype with id \"%s\" not found.", id);
|
Daniel@0
|
74 }
|
Daniel@0
|
75 return RendererPrototype;
|
Daniel@0
|
76 };
|
Daniel@0
|
77
|
Daniel@0
|
78 GraphicsRenderingModule.getRendererById = function(id) {
|
Daniel@0
|
79 var renderer = GraphicsRenderingModule.renderers[id];
|
Daniel@0
|
80 if (!renderer) {
|
Daniel@0
|
81 throw _.str.sprintf("Renderer with id \"%s\" not found.", id);
|
Daniel@0
|
82 }
|
Daniel@0
|
83 return renderer;
|
Daniel@0
|
84 };
|
Daniel@0
|
85
|
Daniel@0
|
86 GraphicsRenderingModule.render = function(id, $element, data, options) {
|
Daniel@0
|
87 GraphicsRenderingModule.getRendererById(id).render($element, data, options);
|
Daniel@0
|
88 };
|
Daniel@0
|
89 });
|
Daniel@0
|
90 }, Logger);
|