aboutsummaryrefslogtreecommitdiff
path: root/node_modules/typedoc/dist/lib/utils/component.js
blob: a395584b5953574e1165c33e3e2d6cff0373ce46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var _ = require("lodash");
var events_1 = require("./events");
var childMappings = [];
function Component(options) {
    return function (target) {
        var proto = target.prototype;
        if (!(proto instanceof AbstractComponent)) {
            throw new Error('The `Component` decorator can only be used with a subclass of `AbstractComponent`.');
        }
        if (options.childClass) {
            if (!(proto instanceof ChildableComponent)) {
                throw new Error('The `Component` decorator accepts the parameter `childClass` only when used with a subclass of `ChildableComponent`.');
            }
            childMappings.push({
                host: proto,
                child: options.childClass
            });
        }
        var name = options.name;
        if (name) {
            proto.componentName = name;
        }
        var internal = !!options.internal;
        if (name && !internal) {
            for (var _i = 0, childMappings_1 = childMappings; _i < childMappings_1.length; _i++) {
                var childMapping = childMappings_1[_i];
                if (!(proto instanceof childMapping.child)) {
                    continue;
                }
                var host = childMapping.host;
                var defaults = host._defaultComponents || (host._defaultComponents = {});
                defaults[name] = target;
                break;
            }
        }
    };
}
exports.Component = Component;
function Option(options) {
    return function (target, propertyKey) {
        if (!(target instanceof AbstractComponent)) {
            throw new Error('The `Option` decorator can only be used on properties within an `AbstractComponent` subclass.');
        }
        var list = target['_componentOptions'] || (target['_componentOptions'] = []);
        options.component = target['_componentName'];
        list.push(options);
        Object.defineProperty(target, propertyKey, {
            get: function () {
                return this.application.options.getValue(options.name);
            },
            enumerable: true,
            configurable: true
        });
    };
}
exports.Option = Option;
var ComponentEvent = (function (_super) {
    __extends(ComponentEvent, _super);
    function ComponentEvent(name, owner, component) {
        var _this = _super.call(this, name) || this;
        _this.owner = owner;
        _this.component = component;
        return _this;
    }
    return ComponentEvent;
}(events_1.Event));
ComponentEvent.ADDED = 'componentAdded';
ComponentEvent.REMOVED = 'componentRemoved';
exports.ComponentEvent = ComponentEvent;
var AbstractComponent = (function (_super) {
    __extends(AbstractComponent, _super);
    function AbstractComponent(owner) {
        var _this = _super.call(this) || this;
        _this._componentOwner = owner;
        _this.initialize();
        return _this;
    }
    AbstractComponent.prototype.initialize = function () { };
    AbstractComponent.prototype.bubble = function (name) {
        var args = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            args[_i - 1] = arguments[_i];
        }
        _super.prototype.trigger.apply(this, arguments);
        var owner = this.owner;
        if (owner instanceof AbstractComponent) {
            owner.bubble.apply(this._componentOwner, arguments);
        }
        return this;
    };
    AbstractComponent.prototype.getOptionDeclarations = function () {
        return this._componentOptions ? this._componentOptions.slice() : [];
    };
    Object.defineProperty(AbstractComponent.prototype, "application", {
        get: function () {
            if (this._componentOwner) {
                return this._componentOwner.application;
            }
            else {
                return null;
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(AbstractComponent.prototype, "owner", {
        get: function () {
            return this._componentOwner;
        },
        enumerable: true,
        configurable: true
    });
    return AbstractComponent;
}(events_1.EventDispatcher));
exports.AbstractComponent = AbstractComponent;
var ChildableComponent = (function (_super) {
    __extends(ChildableComponent, _super);
    function ChildableComponent(owner) {
        var _this = _super.call(this, owner) || this;
        for (var name_1 in _this._defaultComponents) {
            _this.addComponent(name_1, _this._defaultComponents[name_1]);
        }
        return _this;
    }
    ChildableComponent.prototype.getComponent = function (name) {
        if (this._componentChildren && this._componentChildren[name]) {
            return this._componentChildren[name];
        }
        else {
            return null;
        }
    };
    ChildableComponent.prototype.getComponents = function () {
        return _.values(this._componentChildren);
    };
    ChildableComponent.prototype.hasComponent = function (name) {
        return !!(this._componentChildren && this._componentChildren[name]);
    };
    ChildableComponent.prototype.addComponent = function (name, componentClass) {
        if (!this._componentChildren) {
            this._componentChildren = {};
        }
        if (this._componentChildren[name]) {
            throw new Error('The component `%s` has already been added.');
        }
        else {
            var component = typeof componentClass === 'function' ? new componentClass(this) : componentClass;
            var event_1 = new ComponentEvent(ComponentEvent.ADDED, this, component);
            this.bubble(event_1);
            this._componentChildren[name] = component;
            return component;
        }
    };
    ChildableComponent.prototype.removeComponent = function (name) {
        if (!this._componentChildren) {
            return null;
        }
        var component = this._componentChildren[name];
        if (component) {
            delete this._componentChildren[name];
            component.stopListening();
            this.bubble(new ComponentEvent(ComponentEvent.REMOVED, this, component));
            return component;
        }
        else {
            return null;
        }
    };
    ChildableComponent.prototype.removeAllComponents = function () {
        if (!this._componentChildren) {
            return;
        }
        for (var name_2 in this._componentChildren) {
            this._componentChildren[name_2].stopListening();
        }
        this._componentChildren = {};
    };
    return ChildableComponent;
}(AbstractComponent));
exports.ChildableComponent = ChildableComponent;
//# sourceMappingURL=component.js.map