summaryrefslogtreecommitdiffstats
path: root/install/ui/src/freeipa/FieldBinder.js
blob: 7ee8e58743c9e1b940286296318e4340ce0ea759 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*  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',
        './util'
       ],
       function(declare, lang, on, util) {

    /**
     * Field binder
     *
     * Binds input widget with field - defines standard communication logic
     * between widget and a field.
     *
     * Usage:
     *
     *      var binder = new FieldBinder(widget, field).bind();
     *
     *      // or
     *      var binder = new FieldBinder({
     *          field: field,
     *          widget: widget
     *      });
     *      binder.bind()
     *
     * @class FieldBinder
     */
    var FieldBinder = declare([], {

        /**
         * Field
         * @property {IPA.field}
         */
        field: null,

        /**
         * Widget
         * @property {IPA.input_widget}
         */
        widget: null,

        /**
         * Binder is enabled
         *
         * Handlers are not be called when set to false.
         *
         * @property {boolean}
         */
        enabled: true,

        /**
         * Handlers
         * @protected
         * @property {Function[]}
         */
        handlers: null,

        /**
         * Value update is in progress
         *
         * When set, binder should not react to field's nor widget's value-change
         * event.
         *
         * @property {boolean}
         */
        updating: false,

        /**
         * Bind widget with field
         *
         * Listens for field's:
         *
         * - enable-change
         * - valid-change
         * - value-change
         * - dirty-change
         * - require-change
         * - writable-change
         * - readonly-change
         * - reset
         *
         * Listens for widget's:
         *
         * - value-change
         * - undo-click
         *
         * @param {boolean} hard
         *                       Hard binding. Sets `field.widget` to `this.widget`.
         *                       This option is for backward compatibility.
         */
        bind: function(hard) {

            var field = this.field;
            var widget = this.widget;

            if (hard) field.widget = widget;

            this.handle(field, 'enable-change', this.on_field_enable_change);
            this.handle(field, 'valid-change', this.on_field_valid_change);
            this.handle(field, 'value-change', this.on_field_value_change);
            this.handle(field, 'dirty-change', this.on_field_dirty_change);
            this.handle(field, 'require-change', this.on_field_require_change);
            this.handle(field, 'writable-change', this.on_field_writable_change);
            this.handle(field, 'readonly-change', this.on_field_readonly_change);
            this.handle(field, 'acl-rights-change', this.on_field_acl_rights_change);
            this.handle(field, 'reset', this.on_field_reset);

            this.handle(widget, 'value-change', this.on_widget_value_change);
            this.handle(widget, 'undo-click', this.on_widget_undo_click);

            return this;
        },

        /**
         * Unbind all handlers
         */
        unbind: function() {

            var handler;
            while ((handler = this.handlers.pop())) {
                handler.remove();
            }
        },

        /**
         * Creates and registers the handler.
         * Handler will be called in binder context and only if
         * `this.enabled === true`.
         *
         * Do not use `on(target, type, handler)` directly.
         *
         * @param {Function} handler
         * @return {Function} context bound handler
         * @protected
         */
        handle: function(target, type, handler) {

            var _this = this;

            var hndlr = function() {
                if (_this.enabled !== true) return;
                else {
                    handler.apply(_this, Array.prototype.slice.call(arguments, 0));
                }
            };

            var reg_hndl = on(target, type, hndlr);
            this.handlers.push(reg_hndl);

            return hndlr;
        },

        /**
         * Field enable change handler
         *
         * Reflect enabled state to widget
         *
         * @protected
         */
        on_field_enable_change: function(event) {
            this.widget.set_enabled(event.enabled);
        },

        /**
         * Field valid change handler
         * @protected
         */
        on_field_valid_change: function(event) {
            this.widget.set_valid(event.result);
        },

        /**
         * Field dirty change handler
         *
         * Controls showing of widget's undo button
         *
         * @protected
         */
        on_field_dirty_change: function(event) {

            if (!this.field.undo) return;
            if (event.dirty) {
                this.widget.show_undo();
            } else {
                this.widget.hide_undo();
            }
        },

        /**
         * Field require change handler
         *
         * Updates widget's require state
         *
         * @protected
         */
        on_field_require_change: function(event) {

            this.widget.set_required(event.required);
        },

        /**
         * Field require change handler
         *
         * Updates widget's require state
         *
         * @protected
         */
        on_field_writable_change: function(event) {

            this.widget.set_writable(event.writable);
        },

        /**
         * Field require change handler
         *
         * Updates widget's require state
         *
         * @protected
         */
        on_field_readonly_change: function(event) {

            this.widget.set_read_only(event.read_only);
        },

        /**
         * Field acl rights change handler
         * @protected
         */
        on_field_acl_rights_change: function(event) {

            var readable= event.rights.indexOf('r') > -1;
            if (this.widget.set_readable) {
                this.widget.set_readable(readable);
            }
        },

        /**
         * Field reset handler
         *
         * @param {Object} event
         * @protected
         */
        on_field_reset: function(event) {
            this.copy_properties();
        },

        /**
         * Field value change handler
         * @protected
         */
        on_field_value_change: function(event) {

            if (this.updating) return;

            var format_result = util.format(this.field.ui_formatter, event.value);
            if (format_result.ok) {
                this.updating = true;
                this.widget.update(format_result.value);
                this.updating = false;
            } else {
                // this should not happen in ideal world
                window.console.warn('field format error: '+this.field.name);
            }
        },

        /**
         * Widget value change handler
         * @protected
         */
        on_widget_value_change: function(event) {

            if (this.updating) return;

            var val = this.widget.save();
            var format_result = util.parse(this.field.ui_parser, val);
            if (format_result.ok) {
                this.updating = true;
                this.field.set_value(format_result.value);
                this.updating = false;
            } else {
                this.field.set_valid(format_result);
            }
        },

        /**
         * Widget undo click handler
         * @protected
         */
        on_widget_undo_click: function(event) {

            this.field.reset();
        },

        /**
         * Copies `label`, `tooltip`, `measurement_unit`, `undo`, `writable`,
        * `read_only` from field to widget
         */
        copy_properties: function() {

            var field = this.field;
            var widget = this.widget;

            if (field.label) widget.label = field.label;
            if (field.tooltip) widget.tooltip = field.tooltip;
            if (field.measurement_unit) widget.measurement_unit = field.measurement_unit;
            widget.undo = field.undo;
            widget.set_writable(field.writable);
            widget.set_read_only(field.read_only);
            widget.set_required(field.is_required());

            return this;
        },

        constructor: function(arg1, arg2) {

            this.handlers = [];

            if (arg2) {
                this.field = arg1;
                this.widget = arg2;
            } else {
                arg1 = arg1 || {};
                this.field = arg1.field;
                this.widget = arg1.widget;
            }
        }
    });

    return FieldBinder;
});