summaryrefslogtreecommitdiffstats
path: root/install/ui/src/freeipa/FormMixin.js
blob: ef594733a1365679c6fe3a6a122b59d93daf94e8 (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
197
198
199
200
201
202
203
204
/*  Authors:
 *    Petr Vobornik <pvoborni@redhat.com>
 *
 * Copyright (C) 2013 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/lang',
        'dojo/on',
        './builder',
        './field',
        './ordered-map'
       ],
       function(declare, lang, on, builder, field_mod, ordered_map) {

    /**
     * Form mixin
     *
     * Manages fields and related logic.
     *
     * Expects that this mixin will be mixed in a class which will implement
     * `Stateful`.
     *
     * @class FormMixin
     */
    var FormMixin = declare([], {

        /**
         * Some field is dirty
         * @property {boolean}
         */
        dirty: null,

        /**
         * Fields
         * @property {ordered_map}
         */
        fields: null,

        /**
         * Builds fields on add if not already built
         *
         * @property {field.field_builder}
         */
        field_builder: null,

        /**
         * Raised when `dirty` state changes
         * @event dirty-change
         */

        /**
         * Raised after fields reset
         * @event reset
         */

        /**
         * Get field by name
         * @param {string} name
         */
        get_field: function(name) {
            return this.fields.get(name);
        },

        /**
         * Get all fields
         * @return {Array.<IPA.field>}
         */
        get_fields: function() {
            return this.fields.values;
        },

        /**
         * Add field
         * @param {IPA.field|Object|String} field
         *                           Field or field spec
         */
        add_field: function(field) {
            field.container = this;
            var built = this.field_builder.build_field(field);
            this.register_field_listeners(built);
            this.fields.put(field.name, built);
            return built;
        },

        /**
         * Add multiple fields
         * @param {Array} fields
         */
        add_fields: function(fields) {

            if (!fields) return [];

            var built = [];
            for (var i=0; i<fields.length; i++) {
                var f = this.add_field(fields[i]);
                built.push(f);
            }
            return built;
        },

        /**
         * Registers listeners for field events
         * @param {IPA.field} field
         * @protected
         */
        register_field_listeners: function(field) {

            on(field, 'dirty-change', lang.hitch(this, this.on_field_dirty_change));
        },

        /**
         * Field's dirty-change handler
         * @param {Object} event
         * @protected
         * @fires dirty-change
         */
        on_field_dirty_change: function(event) {

            var old = this.dirty;

            if (event.dirty) {
                this.dirty = true;
            } else {
                this.dirty = this.is_dirty();
            }

            if (old !== this.dirty) {
                this.emit('dirty-change', { source: this, dirty: this.dirty });
            }
        },

        /**
         * Perform check if any field is dirty
         *
         * @return {boolean}
         *                  - true: some field is dirty
         *                  - false: all field's aren't dirty
         */
        is_dirty: function() {
            var fields = this.get_fields();
            for (var i=0; i<fields.length; i++) {
                if (fields[i].enabled && fields[i].dirty) {
                    return true;
                }
            }
            return false;
        },

        /**
         * Reset all fields
         * @fires reset
         */
        reset: function() {

            var fields = this.get_fields();
            for (var i=0; i<fields.length; i++) {
                var field = fields[i];
                field.reset();
            }

            this.emit('reset', { source: this });
        },

        /**
         * Validate all fields
         * @return {boolean} true when all fields are valid
         */
        validate: function() {
            var valid = true;
            var fields = this.get_fields();
            for (var i=0; i<fields.length; i++) {
                var field = fields[i];
                valid = field.validate() && field.validate_required() && valid;
            }
            return valid;
        },

        /** Constructor */
        constructor: function(spec) {

            this.fields = ordered_map();
            var builder_spec = spec.field_builder || field_mod.field_builder;
            this.field_builder = builder.build(null, builder_spec);
            this.dirty = false;
        }
    });

    return FormMixin;
});