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