editor: minor refactoring
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / elementsRegister.js
1 define(function(require) {
2     
3 'use strict';
4 var _ = require('libs/underscore'),
5     wlxml = require('wlxml/wlxml');
6
7
8 var ElementsRegister = function(BaseType, NullType) {
9     this._register = {};
10     this.BaseType = BaseType;
11     this.NullType = NullType;
12 };
13
14 _.extend(ElementsRegister.prototype, {
15     createCanvasElementType: function(elementPrototype) {
16         var register = this;
17         var Constructor = function() {
18             register.BaseType.apply(this, Array.prototype.slice.call(arguments, 0));
19         };
20         Constructor.prototype = elementPrototype;
21         return Constructor;
22     },
23     register: function(params) {
24         params.klass = params.klass || '';
25         params.prototype = params.prototype || this.NullType;
26
27         this._register[params.tag] = this._register[params.tag] || {};
28         this._register[params.tag][params.klass] = this.createCanvasElementType(params.prototype);
29     },
30     getElement: function(params) {
31         params.klass = params.klass || '';
32         var Factory;
33         if(this._register[params.tag]) {
34             wlxml.getClassHierarchy(params.klass).reverse().some(function(klass) {
35                 Factory = this._register[params.tag][klass];
36                 if(Factory) {
37                     return true;
38                 }
39             }.bind(this));
40         }
41         if(!Factory) {
42             Factory = this.BaseType;
43         }
44         return Factory;
45     }
46 });
47
48
49 return ElementsRegister;
50
51 });