summaryrefslogtreecommitdiffstats
path: root/install/ui/src/freeipa/_base/Construct_registry.js
blob: 82f0f617e3a8cbad37cb2526621081184302d23e (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
/*  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) {

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

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

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

            var spec, f, c;

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

            if (typeof spec.type !== 'string' || spec.type !== '') {
                throw 'Argument exception: Invalid type';
            }
            if (typeof spec.factory !== 'function' &&
                    typeof spec.constructor !== 'function') {
                throw 'Argument exception: No factory or constructor defined';
            }

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

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

    return Construct_registry;
});