summaryrefslogtreecommitdiffstats
path: root/install/ui/src/freeipa/_base/Construct_registry.js
blob: 934af6ef100530616095f7246682a0209900fd28 (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
194
195
196
/*  Authors:
 *    Petr Vobornik <pvoborni@redhat.com>
 *
 * Copyright (C) 2012 Red Hat
 * see file 'COPYING' for use and warranty information
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

define(['dojo/_base/declare',
        'dojo/_base/array',
        'dojo/_base/lang',
        './construct'
        ], function(declare, array, lang, construct) {

    /**
     * Registry for storing construction specification.
     * @class _base.Construct_registry
     */
    var Construct_registry = declare(null, {


        /**
         * Internal map for construction specifications.
         * @protected
         */
        _map: null,

        /**
         * Registers construction specification
         *
         *      // May be defined by single construction spec object:
         *      var construction_spec = {
         *          type: String,
         *          factory: Function,
         *          ctor: Function,
         *          spec: Object,
         *          pre_ops: [],
         *          post_ops: []
         *      };
         *      register(construction_spec);
         *
         *      // or by defining them separately as params:
         *      register(type, factory|ctor, spec);
         *
         * @param {string|Object} type type or construction spec
         * @param {Function} func ctor or factory function
         * @param {Object} [default_spec] default spec object for given type
         *
         * @returns {Object}
         */
        register: function(type, func, default_spec) {

            var cs, f, c;

            if (typeof type === 'object') {
                cs = type;
            } else {
                construct.is_ctor(func) ? c = func : f = func;
                cs = {
                    type: type,
                    factory: f,
                    ctor: c,
                    spec: default_spec
                };
            }

            if (!cs.pre_ops) cs.pre_ops = [];
            if (!cs.post_ops) cs.post_ops = [];

            this._check_spec(cs);

            this._map[cs.type] = cs;
            return cs;
        },

        /**
         * Makes a copy of construct specification of original type. Extends
         * it with values in supplied construct specification.
         *
         * @param {string} org_type Original type
         * @param {string} new_type New type
         * @param {Object} construct_spec Construction specification
         */
        copy: function(org_type, new_type, construct_spec) {

            var def_cs = construct_spec;
            var old_cs = this._check_get(org_type);
            var cs = construct.clone(old_cs);

            cs.type = new_type;
            if (def_cs.pre_ops) cs.pre_ops.push.apply(cs.pre_ops, def_cs.pre_ops);
            if (def_cs.post_ops) cs.post_ops.push.apply(cs.post_ops, def_cs.post_ops);
            if (def_cs.factory) cs.factory = def_cs.factory;
            if (def_cs.ctor) cs.ctor = def_cs.ctor;
            if (def_cs.spec) {
                cs.spec = cs.spec || {};
                lang.mixin(cs.spec, def_cs.spec);
            }

            this._check_spec(cs);

            this._map[cs.type] = cs;
            return cs;
        },

        /**
         * Registers pre operation.
         *
         * Purpose of pre operation is to modify spec object before build
         * operation.
         *
         * When op is Function it gets called with spec as a param and should
         * return modified spec.
         *
         * When op is Object, the object gets mixed in into spec.
         *
         * @param {string} type
         * @param {Function|Object} op
         * @param {boolean} move op to first position
         */
        register_pre_op: function(type, op, first) {

            var cs = this._check_get(type);
            if (first) cs.pre_ops.unshift(op);
            else cs.pre_ops.push(op);
        },

        /**
         * Registers post operation.
         *
         * Purpose of post operation is to modify built object.
         *
         * When op is Function it gets called with built object as a param
         * and should return modified obj.
         *
         * When op is Object, the object gets mixed in into built object. Use
         * with caution.
         *
         * @param {string} type
         * @param {Function|Object} op
         * @param {boolean} first move op to first position
         */
        register_post_op: function(type, op, first) {

            var cs = this._check_get(type);
            if (first) cs.post_ops.unshift(op);
            else cs.post_ops.push(op);
        },

        /**
         * Gets construction specification for given type.
         *
         * @param {string} string Type name
         * @returns {Object|null}
         */
        get: function(type) {
            return this._map[type] || null;
        },

        _check_get: function(type) {
            var cs = this.get(type);
            if (!cs) throw construct.no_cs_for_type_error(type);
            return cs;
        },

        _check_spec: function(spec) {
            if (typeof spec.type !== 'string' || spec.type === '') {
                throw 'Argument exception: Invalid type';
            }
            if (!lang.isArrayLike(spec.pre_ops)) {
                throw 'Argument exception: Invalid pre_ops type.';
            }
            if (!lang.isArrayLike(spec.post_ops)) {
                throw 'Argument exception: Invalid post_ops type.';
            }
        },

        constructor: function(spec) {
            this._map = {};
        }
    });

    return Construct_registry;
});