1 | 'use strict';
|
2 |
|
3 | var INHERIT = require('inherit'),
|
4 | ASSERT = require('assert'),
|
5 | LOGGER = require('./logger'),
|
6 |
|
7 | registry = {},
|
8 | cache = {};
|
9 |
|
10 | module.exports = {
|
11 |
|
12 | |
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | _decl: function(nodeName, objectOrBaseName, objectOrStatic, staticObject) {
|
23 |
|
24 | var explicitBase = typeof objectOrBaseName === 'string',
|
25 | baseName = explicitBase? objectOrBaseName : nodeName,
|
26 | staticObj = typeof objectOrBaseName !== 'string'? objectOrStatic: staticObject,
|
27 | obj = explicitBase? objectOrStatic : objectOrBaseName,
|
28 | base = baseName;
|
29 |
|
30 | if (typeof baseName === 'string') {
|
31 | base = baseName === nodeName? cache[baseName] : this.getNodeClass(baseName);
|
32 | }
|
33 |
|
34 | if (typeof objectOrStatic === 'function') {
|
35 | cache[nodeName] = objectOrStatic;
|
36 | } else {
|
37 | cache[nodeName] = base? INHERIT(base, obj, staticObj) : INHERIT(obj, staticObj);
|
38 | }
|
39 |
|
40 | return cache;
|
41 |
|
42 | },
|
43 |
|
44 | _inheritChain: function(nodeName) {
|
45 |
|
46 | var stack = registry[nodeName];
|
47 |
|
48 | ASSERT.ok(Array.isArray(stack), 'definition for class ' + nodeName + ' is not found in the registry');
|
49 |
|
50 | for(var i = 0; i < stack.length; i++) {
|
51 | this._decl.apply(this, stack[i]);
|
52 | }
|
53 |
|
54 | return cache[nodeName];
|
55 |
|
56 | },
|
57 |
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | decl: function(nodeName, objectOrBaseName, objectOrStatic, staticObject) {
|
67 |
|
68 | cache = {};
|
69 |
|
70 | var stack = registry[nodeName] || [];
|
71 | stack.push(Array.prototype.slice.call(arguments, 0));
|
72 |
|
73 | registry[nodeName] = stack;
|
74 |
|
75 | },
|
76 |
|
77 | getRegistry: function(node) {
|
78 | return node? registry[node] : registry;
|
79 | },
|
80 |
|
81 |
|
82 | getNodeClass: function(nodeName){
|
83 | return cache[nodeName] || this._inheritChain(nodeName);
|
84 | }
|
85 |
|
86 | };
|