cleanup
[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) {
9     this._register = {};
10     this.BaseType = BaseType;
11 };
12
13 _.extend(ElementsRegister.prototype, {
14     createCanvasElementType: function(elementPrototype) {
15         var register = this;
16         var Constructor = function() {
17             register.BaseType.apply(this, Array.prototype.slice.call(arguments, 0));
18         };
19         Constructor.prototype = elementPrototype;
20         return Constructor;
21     },
22     register: function(params) {
23         params.klass = params.klass || '';
24         params.prototype = params.prototype || Object.create({});
25
26         this._register[params.tag] = this._register[params.tag] || {};
27         this._register[params.tag][params.klass] = this.createCanvasElementType(params.prototype);
28     },
29     getElement: function(params) {
30         params.klass = params.klass || '';
31         var Factory;
32         if(this._register[params.tag]) {
33             wlxml.getClassHierarchy(params.klass).reverse().some(function(klass) {
34                 Factory = this._register[params.tag][klass];
35                 if(Factory) {
36                     return true;
37                 }
38             }.bind(this));
39         }
40         if(!Factory) {
41             Factory = this.BaseType;
42         }
43         return Factory;
44     }
45 });
46
47
48 return ElementsRegister;
49
50 });