Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/**
2
 * This file contains JS functionality required by mforms and is included automatically
3
 * when required.
4
 */
5
 
6
// Namespace for the form bits and bobs
7
M.form = M.form || {};
8
 
9
if (typeof M.form.dependencyManager === 'undefined') {
10
    var dependencyManager = function() {
11
        dependencyManager.superclass.constructor.apply(this, arguments);
12
    };
13
    Y.extend(dependencyManager, Y.Base, {
14
        _locks: null,
15
        _hides: null,
16
        _dirty: null,
17
        _nameCollections: null,
18
        _fileinputs: null,
11 efrain 19
        _staticElements: null,
20
        _editors: null,
21
        _editorNameSuffix: '[text]',
1 efrain 22
 
23
        initializer: function() {
24
            // Setup initial values for complex properties.
25
            this._locks = {};
26
            this._hides = {};
27
            this._dirty = {};
28
 
29
            // Setup event handlers.
30
            Y.Object.each(this.get('dependencies'), function(value, i) {
31
                var elements = this.elementsByName(i);
32
                elements.each(function(node) {
33
                    var nodeName = node.get('nodeName').toUpperCase();
34
                    if (nodeName == 'INPUT') {
35
                        if (node.getAttribute('type').match(/^(button|submit|radio|checkbox)$/)) {
36
                            node.on('click', this.updateEventDependencies, this);
37
                        } else {
38
                            node.on('blur', this.updateEventDependencies, this);
39
                        }
40
                        node.on('change', this.updateEventDependencies, this);
41
                    } else if (nodeName == 'SELECT') {
42
                        node.on('change', this.updateEventDependencies, this);
43
                    } else {
44
                        node.on('click', this.updateEventDependencies, this);
45
                        node.on('blur', this.updateEventDependencies, this);
46
                        node.on('change', this.updateEventDependencies, this);
47
                    }
48
                }, this);
49
            }, this);
50
 
51
            // Handle the reset button.
52
            this.get('form').get('elements').each(function(input) {
53
                if (input.getAttribute('type') == 'reset') {
54
                    input.on('click', function() {
55
                        this.get('form').reset();
56
                        this.updateAllDependencies();
57
                    }, this);
58
                }
59
            }, this);
60
 
61
            this.updateAllDependencies();
62
        },
63
 
64
        /**
65
         * Initializes the mapping from element name to YUI NodeList
66
         */
67
        initElementsByName: function() {
68
            var names = {}; // Form elements with a given name.
69
            var allnames = {}; // Form elements AND outer elements for groups with a given name.
70
 
71
            // Collect element names.
72
            Y.Object.each(this.get('dependencies'), function(conditions, i) {
73
                names[i] = new Y.NodeList();
74
                allnames[i] = new Y.NodeList();
75
                for (var condition in conditions) {
76
                    for (var value in conditions[condition]) {
77
                        for (var hide in conditions[condition][value]) {
78
                            for (var ei in conditions[condition][value][hide]) {
79
                                names[conditions[condition][value][hide][ei]] = new Y.NodeList();
80
                                allnames[conditions[condition][value][hide][ei]] = new Y.NodeList();
81
                            }
82
                        }
83
                    }
84
                }
85
            });
86
 
87
            // Locate elements for each name.
88
            this.get('form').get('elements').each(function(node) {
89
                var name = node.getAttribute('name');
90
                if (({}).hasOwnProperty.call(names, name)) {
91
                    names[name].push(node);
92
                    allnames[name].push(node);
11 efrain 93
                } else if (this.isEditor(name)) {
94
                    // If this is an editor, we need to remove the suffix.
95
                    name = name.replace(this._editorNameSuffix, '');
96
                    if (({}).hasOwnProperty.call(names, name)) {
97
                        names[name].push(node);
98
                        allnames[name].push(node);
99
                    }
1 efrain 100
                }
11 efrain 101
            }, this);
1 efrain 102
            // Locate any groups with the given name.
103
            this.get('form').all('.fitem').each(function(node) {
104
                var name = node.getData('groupname');
105
                if (name && ({}).hasOwnProperty.call(allnames, name)) {
106
                    allnames[name].push(node);
107
                }
108
            });
11 efrain 109
            // Locate any static elements for each name.
110
            this.get('form').all('.form-control-static').each(function(node) {
111
                var name = node.getData('name');
112
                if (({}).hasOwnProperty.call(allnames, name)) {
113
                    names[name].push(node);
114
                    allnames[name].push(node);
115
                }
116
            });
1 efrain 117
            this._nameCollections = {names: names, allnames: allnames};
118
        },
119
 
120
        /**
121
         * Gets all elements in the form by their name and returns
122
         * a YUI NodeList
123
         *
124
         * @param {String} name The form element name.
125
         * @param {Boolean} includeGroups (optional - default false) Should the outer element for groups be included?
126
         * @return {Y.NodeList}
127
         */
128
        elementsByName: function(name, includeGroups) {
129
            if (includeGroups === undefined) {
130
                includeGroups = false;
131
            }
132
            var collection = (includeGroups ? 'allnames' : 'names');
133
 
134
            if (!this._nameCollections) {
135
                this.initElementsByName();
136
            }
137
            if (!({}).hasOwnProperty.call(this._nameCollections[collection], name)) {
138
                return new Y.NodeList();
139
            }
140
            return this._nameCollections[collection][name];
141
        },
142
 
143
        /**
144
         * Checks the dependencies the form has an makes any changes to the
145
         * form that are required.
146
         *
147
         * Changes are made by functions title _dependency{Dependencytype}
148
         * and more can easily be introduced by defining further functions.
149
         *
150
         * @param {EventFacade | null} e The event, if any.
151
         * @param {String} dependon The form element name to check dependencies against.
152
         * @return {Boolean}
153
         */
154
        checkDependencies: function(e, dependon) {
155
            var dependencies = this.get('dependencies'),
156
                tohide = {},
157
                tolock = {},
158
                condition, value, isHide, lock, hide,
159
                checkfunction, result, elements;
160
            if (!({}).hasOwnProperty.call(dependencies, dependon)) {
161
                return true;
162
            }
163
            elements = this.elementsByName(dependon);
164
            for (condition in dependencies[dependon]) {
165
                for (value in dependencies[dependon][condition]) {
166
                    for (isHide in dependencies[dependon][condition][value]) {
167
                        checkfunction = '_dependency' + condition[0].toUpperCase() + condition.slice(1);
168
                        if (Y.Lang.isFunction(this[checkfunction])) {
169
                            result = this[checkfunction].apply(this, [elements, value, (isHide === "1"), e]);
170
                        } else {
171
                            result = this._dependencyDefault(elements, value, (isHide === "1"), e);
172
                        }
173
                        lock = result.lock || false;
174
                        hide = result.hide || false;
175
                        for (var ei in dependencies[dependon][condition][value][isHide]) {
176
                            var eltolock = dependencies[dependon][condition][value][isHide][ei];
177
                            if (({}).hasOwnProperty.call(tohide, eltolock)) {
178
                                tohide[eltolock] = tohide[eltolock] || hide;
179
                            } else {
180
                                tohide[eltolock] = hide;
181
                            }
182
 
183
                            if (({}).hasOwnProperty.call(tolock, eltolock)) {
184
                                tolock[eltolock] = tolock[eltolock] || lock;
185
                            } else {
186
                                tolock[eltolock] = lock;
187
                            }
188
                        }
189
                    }
190
                }
191
            }
192
 
193
            for (var el in tolock) {
194
                var needsupdate = false;
195
                if (!({}).hasOwnProperty.call(this._locks, el)) {
196
                    this._locks[el] = {};
197
                }
198
                if (({}).hasOwnProperty.call(tolock, el) && tolock[el]) {
199
                    if (!({}).hasOwnProperty.call(this._locks[el], dependon) || this._locks[el][dependon]) {
200
                        this._locks[el][dependon] = true;
201
                        needsupdate = true;
202
                    }
203
                } else if (({}).hasOwnProperty.call(this._locks[el], dependon) && this._locks[el][dependon]) {
204
                    delete this._locks[el][dependon];
205
                    needsupdate = true;
206
                }
207
 
208
                if (!({}).hasOwnProperty.call(this._hides, el)) {
209
                    this._hides[el] = {};
210
                }
211
                if (({}).hasOwnProperty.call(tohide, el) && tohide[el]) {
212
                    if (!({}).hasOwnProperty.call(this._hides[el], dependon) || this._hides[el][dependon]) {
213
                        this._hides[el][dependon] = true;
214
                        needsupdate = true;
215
                    }
216
                } else if (({}).hasOwnProperty.call(this._hides[el], dependon) && this._hides[el][dependon]) {
217
                    delete this._hides[el][dependon];
218
                    needsupdate = true;
219
                }
220
 
221
                if (needsupdate) {
222
                    this._dirty[el] = true;
223
                }
224
            }
225
 
226
            return true;
227
        },
228
        /**
229
         * Update all dependencies in form
230
         */
231
        updateAllDependencies: function() {
232
            Y.Object.each(this.get('dependencies'), function(value, name) {
233
                this.checkDependencies(null, name);
234
            }, this);
235
 
236
            this.updateForm();
237
        },
238
        /**
239
         * Update dependencies associated with event
240
         *
241
         * @param {Event} e The event.
242
         */
243
        updateEventDependencies: function(e) {
244
            var el = e.target.getAttribute('name');
245
            this.checkDependencies(e, el);
246
            this.updateForm();
247
        },
248
        /**
249
         * Flush pending changes to the form
250
         */
251
        updateForm: function() {
252
            var el;
253
            for (el in this._dirty) {
254
                if (({}).hasOwnProperty.call(this._locks, el)) {
255
                    this._disableElement(el, !Y.Object.isEmpty(this._locks[el]));
256
                }
257
                if (({}).hasOwnProperty.call(this._hides, el)) {
258
                    this._hideElement(el, !Y.Object.isEmpty(this._hides[el]));
259
                }
260
            }
261
 
262
            this._dirty = {};
263
        },
264
        /**
265
         * Disables or enables all form elements with the given name
266
         *
267
         * @param {String} name The form element name.
268
         * @param {Boolean} disabled True to disable, false to enable.
269
         */
270
        _disableElement: function(name, disabled) {
11 efrain 271
            const els = this.elementsByName(name),
1 efrain 272
                filepicker = this.isFilePicker(name),
11 efrain 273
                editors = this.get('form').all('.fitem [data-fieldtype="editor"] textarea[name="' + name + '[text]"]'),
274
                staticElement = this.isStaticElement(name);
1 efrain 275
 
276
            els.each(function(node) {
11 efrain 277
                const fitem = node.ancestor('.fitem');
1 efrain 278
                if (disabled) {
279
                    node.setAttribute('disabled', 'disabled');
280
                } else {
281
                    node.removeAttribute('disabled');
282
                }
11 efrain 283
                // Enable/Disable static elements if exist.
284
                if (staticElement) {
285
                    const disabledNonTextElements = 'INPUT,SELECT,TEXTAREA,BUTTON,A';
286
                    if (disabled) {
287
                        // Mute the text inside the current static element.
288
                        fitem.addClass('text-muted');
289
                        // Disabled non-text elements in the static if exist.
290
                        fitem.all(disabledNonTextElements).each(function(disabledElement) {
291
                            if (disabledElement.get('tagName').toUpperCase() === "A") {
292
                                disabledElement.addClass('disabled');
293
                            } else {
294
                                disabledElement.setAttribute('disabled', 'disabled');
295
                            }
296
                        });
297
                    } else {
298
                        // Unmute the text inside the current static element.
299
                        fitem.removeClass('text-muted');
300
                        // Enabled non-text elements in the static if exist.
301
                        fitem.all(disabledNonTextElements).each(function(disabledElement) {
302
                            if (disabledElement.get('tagName').toUpperCase() === "A") {
303
                                disabledElement.removeClass('disabled');
304
                            } else {
305
                                disabledElement.removeAttribute('disabled', 'disabled');
306
                            }
307
                        });
308
                    }
309
                }
1 efrain 310
                // Extra code to disable filepicker or filemanager form elements
311
                if (filepicker) {
312
                    if (fitem) {
313
                        if (disabled) {
314
                            fitem.addClass('disabled');
315
                        } else {
316
                            fitem.removeClass('disabled');
317
                        }
318
                    }
319
                }
320
            });
321
            editors.each(function(editor) {
322
                if (disabled) {
323
                    editor.setAttribute('readonly', 'readonly');
324
                } else {
325
                    editor.removeAttribute('readonly', 'readonly');
326
                }
327
                editor.getDOMNode().dispatchEvent(new Event('form:editorUpdated'));
328
            });
329
        },
330
        /**
331
         * Hides or shows all form elements with the given name.
332
         *
333
         * @param {String} name The form element name.
334
         * @param {Boolean} hidden True to hide, false to show.
335
         */
336
        _hideElement: function(name, hidden) {
337
            var els = this.elementsByName(name, true);
338
            els.each(function(node) {
339
                var e = node.ancestor('.fitem', true);
340
                var label = null,
341
                    id = null;
342
                if (e) {
343
                    // Cope with differences between clean and boost themes.
344
                    if (e.hasClass('fitem_fgroup')) {
345
                        // Items within groups are not wrapped in div.fitem in theme_clean, so
346
                        // we need to hide the input, not the div.fitem.
347
                        e = node;
348
                    }
349
 
350
                    if (hidden) {
351
                        e.setAttribute('hidden', 'hidden');
352
                    } else {
353
                        e.removeAttribute('hidden');
354
                    }
355
                    e.setStyles({
356
                        display: (hidden) ? 'none' : ''
357
                    });
358
 
359
                    // Hide/unhide the label as well.
360
                    id = node.get('id');
361
                    if (id) {
362
                        label = Y.all('label[for="' + id + '"]');
363
                        if (label) {
364
                            if (hidden) {
365
                                label.setAttribute('hidden', 'hidden');
366
                            } else {
367
                                label.removeAttribute('hidden');
368
                            }
369
                            label.setStyles({
370
                                display: (hidden) ? 'none' : ''
371
                            });
372
                        }
373
                    }
374
                }
375
            });
376
        },
377
        /**
378
         * Is the form element inside a filepicker or filemanager?
379
         *
380
         * @param {String} el The form element name.
381
         * @return {Boolean}
382
         */
383
        isFilePicker: function(el) {
384
            if (!this._fileinputs) {
385
                var fileinputs = {};
386
                var selector = '.fitem [data-fieldtype="filepicker"] input,.fitem [data-fieldtype="filemanager"] input';
387
                // Include a selector where the filemanager input is nested in a group.
388
                selector += ',.fitem [data-fieldtype="group"] input[id*="filemanager"]';
389
                var els = this.get('form').all(selector);
390
                els.each(function(node) {
391
                    fileinputs[node.getAttribute('name')] = true;
392
                });
393
                this._fileinputs = fileinputs;
394
            }
395
 
396
            if (({}).hasOwnProperty.call(this._fileinputs, el)) {
397
                return this._fileinputs[el] || false;
398
            }
399
 
400
            return false;
401
        },
11 efrain 402
        /**
403
         * Checks if a form element with the given name is static.
404
         *
405
         * @param {string} el - The name of the form element to check.
406
         * @returns {boolean} - Returns true if the form element is static, otherwise false.
407
         */
408
        isStaticElement: function(el) {
409
            if (!this._staticElements) {
410
                const staticElements = {};
411
                const els = this.get('form').all('.fitem [data-fieldtype="static"] .form-control-static');
412
                els.each(function(node) {
413
                    if (node.getData('name') === el) {
414
                        staticElements[node.getData('name')] = true;
415
                    }
416
                });
417
                this._staticElements = staticElements;
418
            }
419
            if (({}).hasOwnProperty.call(this._staticElements, el)) {
420
                return this._staticElements[el] || false;
421
            }
422
            return false;
423
        },
1 efrain 424
        _dependencyNotchecked: function(elements, value, isHide) {
425
            var lock = false;
426
            elements.each(function() {
427
                if (this.getAttribute('type').toLowerCase() == 'hidden' &&
428
                        !this.siblings('input[type=checkbox][name="' + this.get('name') + '"]').isEmpty()) {
429
                    // This is the hidden input that is part of an advcheckbox.
430
                    return;
431
                }
432
                if (this.getAttribute('type').toLowerCase() == 'radio' && this.get('value') != value) {
433
                    return;
434
                }
435
                lock = lock || !Y.Node.getDOMNode(this).checked;
436
            });
437
            return {
438
                lock: lock,
439
                hide: isHide ? lock : false
440
            };
441
        },
442
        _dependencyChecked: function(elements, value, isHide) {
443
            var lock = false;
444
            elements.each(function() {
445
                if (this.getAttribute('type').toLowerCase() == 'hidden' &&
446
                        !this.siblings('input[type=checkbox][name="' + this.get('name') + '"]').isEmpty()) {
447
                    // This is the hidden input that is part of an advcheckbox.
448
                    return;
449
                }
450
                if (this.getAttribute('type').toLowerCase() == 'radio' && this.get('value') != value) {
451
                    return;
452
                }
453
                lock = lock || Y.Node.getDOMNode(this).checked;
454
            });
455
            return {
456
                lock: lock,
457
                hide: isHide ? lock : false
458
            };
459
        },
460
        _dependencyNoitemselected: function(elements, value, isHide) {
461
            var lock = false;
462
            elements.each(function() {
463
                lock = lock || this.get('selectedIndex') == -1;
464
            });
465
            return {
466
                lock: lock,
467
                hide: isHide ? lock : false
468
            };
469
        },
470
        _dependencyEq: function(elements, value, isHide) {
471
            var lock = false;
472
            var hiddenVal = false;
473
            var options, v, selected, values;
474
            elements.each(function() {
475
                if (this.getAttribute('type').toLowerCase() == 'radio' && !Y.Node.getDOMNode(this).checked) {
476
                    return;
477
                } else if (this.getAttribute('type').toLowerCase() == 'hidden' &&
478
                        !this.siblings('input[type=checkbox][name="' + this.get('name') + '"]').isEmpty()) {
479
                    // This is the hidden input that is part of an advcheckbox.
480
                    hiddenVal = (this.get('value') == value);
481
                    return;
482
                } else if (this.getAttribute('type').toLowerCase() == 'checkbox' && !Y.Node.getDOMNode(this).checked) {
483
                    lock = lock || hiddenVal;
484
                    return;
485
                }
486
                if (this.getAttribute('class').toLowerCase() == 'filepickerhidden') {
487
                    // Check for filepicker status.
488
                    var elementname = this.getAttribute('name');
489
                    if (elementname && M.form_filepicker.instances[elementname].fileadded) {
490
                        lock = false;
491
                    } else {
492
                        lock = true;
493
                    }
494
                } else if (this.get('nodeName').toUpperCase() === 'SELECT' && this.get('multiple') === true) {
495
                    // Multiple selects can have one or more value assigned. A pipe (|) is used as a value separator
496
                    // when multiple values have to be selected at the same time.
497
                    values = value.split('|');
498
                    selected = [];
499
                    options = this.get('options');
500
                    options.each(function() {
501
                        if (this.get('selected')) {
502
                            selected[selected.length] = this.get('value');
503
                        }
504
                    });
505
                    if (selected.length > 0 && selected.length === values.length) {
506
                        for (var i in selected) {
507
                            v = selected[i];
508
                            if (values.indexOf(v) > -1) {
509
                                lock = true;
510
                            } else {
511
                                lock = false;
512
                                return;
513
                            }
514
                        }
515
                    } else {
516
                        lock = false;
517
                    }
518
                } else {
519
                    lock = lock || this.get('value') == value;
520
                }
521
            });
522
            return {
523
                lock: lock,
524
                hide: isHide ? lock : false
525
            };
526
        },
527
        /**
528
         * Lock the given field if the field value is in the given set of values.
529
         *
530
         * @param {Array} elements
531
         * @param {String} values Single value or pipe (|) separated values when multiple
532
         * @returns {{lock: boolean, hide: boolean}}
533
         * @private
534
         */
535
        _dependencyIn: function(elements, values, isHide) {
536
            // A pipe (|) is used as a value separator
537
            // when multiple values have to be passed on at the same time.
538
            values = values.split('|');
539
            var lock = false;
540
            var hiddenVal = false;
541
            var options, v, selected, value;
542
            elements.each(function() {
543
                if (this.getAttribute('type').toLowerCase() == 'radio' && !Y.Node.getDOMNode(this).checked) {
544
                    return;
545
                } else if (this.getAttribute('type').toLowerCase() == 'hidden' &&
546
                        !this.siblings('input[type=checkbox][name="' + this.get('name') + '"]').isEmpty()) {
547
                    // This is the hidden input that is part of an advcheckbox.
548
                    hiddenVal = (values.indexOf(this.get('value')) > -1);
549
                    return;
550
                } else if (this.getAttribute('type').toLowerCase() == 'checkbox' && !Y.Node.getDOMNode(this).checked) {
551
                    lock = lock || hiddenVal;
552
                    return;
553
                }
554
                if (this.getAttribute('class').toLowerCase() == 'filepickerhidden') {
555
                    // Check for filepicker status.
556
                    var elementname = this.getAttribute('name');
557
                    if (elementname && M.form_filepicker.instances[elementname].fileadded) {
558
                        lock = false;
559
                    } else {
560
                        lock = true;
561
                    }
562
                } else if (this.get('nodeName').toUpperCase() === 'SELECT' && this.get('multiple') === true) {
563
                    // Multiple selects can have one or more value assigned.
564
                    selected = [];
565
                    options = this.get('options');
566
                    options.each(function() {
567
                        if (this.get('selected')) {
568
                            selected[selected.length] = this.get('value');
569
                        }
570
                    });
571
                    if (selected.length > 0 && selected.length === values.length) {
572
                        for (var i in selected) {
573
                            v = selected[i];
574
                            if (values.indexOf(v) > -1) {
575
                                lock = true;
576
                            } else {
577
                                lock = false;
578
                                return;
579
                            }
580
                        }
581
                    } else {
582
                        lock = false;
583
                    }
584
                } else {
585
                    value = this.get('value');
586
                    lock = lock || (values.indexOf(value) > -1);
587
                }
588
            });
589
            return {
590
                lock: lock,
591
                hide: isHide ? lock : false
592
            };
593
        },
594
        _dependencyHide: function(elements, value) {
595
            return {
596
                lock: false,
597
                hide: true
598
            };
599
        },
600
        _dependencyDefault: function(elements, value, isHide) {
601
            var lock = false,
602
                hiddenVal = false,
603
                values
604
                ;
605
            elements.each(function() {
606
                var selected;
607
                if (this.getAttribute('type').toLowerCase() == 'radio' && !Y.Node.getDOMNode(this).checked) {
608
                    return;
609
                } else if (this.getAttribute('type').toLowerCase() == 'hidden' &&
610
                        !this.siblings('input[type=checkbox][name="' + this.get('name') + '"]').isEmpty()) {
611
                    // This is the hidden input that is part of an advcheckbox.
612
                    hiddenVal = (this.get('value') != value);
613
                    return;
614
                } else if (this.getAttribute('type').toLowerCase() == 'checkbox' && !Y.Node.getDOMNode(this).checked) {
615
                    lock = lock || hiddenVal;
616
                    return;
617
                }
618
                // Check for filepicker status.
619
                if (this.getAttribute('class').toLowerCase() == 'filepickerhidden') {
620
                    var elementname = this.getAttribute('name');
621
                    if (elementname && M.form_filepicker.instances[elementname].fileadded) {
622
                        lock = true;
623
                    } else {
624
                        lock = false;
625
                    }
626
                } else if (this.get('nodeName').toUpperCase() === 'SELECT' && this.get('multiple') === true) {
627
                    // Multiple selects can have one or more value assigned. A pipe (|) is used as a value separator
628
                    // when multiple values have to be selected at the same time.
629
                    values = value.split('|');
630
                    selected = [];
631
                    this.get('options').each(function() {
632
                        if (this.get('selected')) {
633
                            selected[selected.length] = this.get('value');
634
                        }
635
                    });
636
                    if (selected.length > 0 && selected.length === values.length) {
637
                        for (var i in selected) {
638
                            if (values.indexOf(selected[i]) > -1) {
639
                                lock = false;
640
                            } else {
641
                                lock = true;
642
                                return;
643
                            }
644
                        }
645
                    } else {
646
                        lock = true;
647
                    }
648
                } else {
649
                    lock = lock || this.get('value') != value;
650
                }
651
            });
652
            return {
653
                lock: lock,
654
                hide: isHide ? lock : false
655
            };
11 efrain 656
        },
657
        /**
658
         * Is the form element an editor?
659
         *
660
         * @param {String} el The form element name.
661
         * @return {Boolean}
662
         */
663
        isEditor: function(el) {
664
            if (!this._editors) {
665
                let editors = {};
666
                const selector = '.fitem [data-fieldtype="editor"] textarea';
667
                const els = this.get('form').all(selector);
668
                els.each(function(node) {
669
                    editors[node.getAttribute('name')] = true;
670
                });
671
                this._editors = editors;
672
            }
673
 
674
            return this._editors[el] || false;
675
        },
1 efrain 676
    }, {
677
        NAME: 'mform-dependency-manager',
678
        ATTRS: {
679
            form: {
680
                setter: function(value) {
681
                    return Y.one('#' + value);
682
                },
683
                value: null
684
            },
685
 
686
            dependencies: {
687
                value: {}
688
            }
689
        }
690
    });
691
 
692
    M.form.dependencyManager = dependencyManager;
693
}
694
 
695
/**
696
 * Stores a list of the dependencyManager for each form on the page.
697
 */
698
M.form.dependencyManagers = {};
699
 
700
/**
701
 * Initialises a manager for a forms dependencies.
702
 * This should happen once per form.
703
 *
704
 * @param {YUI} Y YUI3 instance
705
 * @param {String} formid ID of the form
706
 * @param {Array} dependencies array
707
 * @return {M.form.dependencyManager}
708
 */
709
M.form.initFormDependencies = function(Y, formid, dependencies) {
710
 
711
    // If the dependencies isn't an array or object we don't want to
712
    // know about it
713
    if (!Y.Lang.isArray(dependencies) && !Y.Lang.isObject(dependencies)) {
714
        return false;
715
    }
716
 
717
    /**
718
     * Fixes an issue with YUI's processing method of form.elements property
719
     * in Internet Explorer.
720
     *     http://yuilibrary.com/projects/yui3/ticket/2528030
721
     */
722
    Y.Node.ATTRS.elements = {
723
        getter: function() {
724
            return Y.all(new Y.Array(this._node.elements, 0, true));
725
        }
726
    };
727
 
728
    M.form.dependencyManagers[formid] = new M.form.dependencyManager({form: formid, dependencies: dependencies});
729
    return M.form.dependencyManagers[formid];
730
};
731
 
732
/**
733
 * Update the state of a form. You need to call this after, for example, changing
734
 * the state of some of the form input elements in your own code, in order that
735
 * things like the disableIf state of elements can be updated.
736
 *
737
 * @param {String} formid ID of the form
738
 */
739
M.form.updateFormState = function(formid) {
740
    if (formid in M.form.dependencyManagers) {
741
        M.form.dependencyManagers[formid].updateAllDependencies();
742
    }
743
};