2 * This file contains JS functionality required by mforms and is included automatically
6 // Namespace for the form bits and bobs
9 if (typeof M.form.dependencyManager === 'undefined') {
10 var dependencyManager = function() {
11 dependencyManager.superclass.constructor.apply(this, arguments);
13 Y.extend(dependencyManager, Y.Base, {
17 _nameCollections: null,
20 initializer: function() {
21 // Setup initial values for complex properties.
26 // Setup event handlers.
27 Y.Object.each(this.get('dependencies'), function(value, i) {
28 var elements = this.elementsByName(i);
29 elements.each(function(node) {
30 var nodeName = node.get('nodeName').toUpperCase();
31 if (nodeName == 'INPUT') {
32 if (node.getAttribute('type').match(/^(button|submit|radio|checkbox)$/)) {
33 node.on('click', this.updateEventDependencies, this);
35 node.on('blur', this.updateEventDependencies, this);
37 node.on('change', this.updateEventDependencies, this);
38 } else if (nodeName == 'SELECT') {
39 node.on('change', this.updateEventDependencies, this);
41 node.on('click', this.updateEventDependencies, this);
42 node.on('blur', this.updateEventDependencies, this);
43 node.on('change', this.updateEventDependencies, this);
48 // Handle the reset button.
49 this.get('form').get('elements').each(function(input) {
50 if (input.getAttribute('type') == 'reset') {
51 input.on('click', function() {
52 this.get('form').reset();
53 this.updateAllDependencies();
58 this.updateAllDependencies();
62 * Initializes the mapping from element name to YUI NodeList
64 initElementsByName: function() {
67 // Collect element names.
68 Y.Object.each(this.get('dependencies'), function(conditions, i) {
69 names[i] = new Y.NodeList();
70 for (var condition in conditions) {
71 for (var value in conditions[condition]) {
72 for (var ei in conditions[condition][value]) {
73 names[conditions[condition][value][ei]] = new Y.NodeList();
79 // Locate elements for each name.
80 this.get('form').get('elements').each(function(node) {
81 var name = node.getAttribute('name');
82 if (({}).hasOwnProperty.call(names, name)) {
83 names[name].push(node);
86 this._nameCollections = names;
90 * Gets all elements in the form by their name and returns
93 * @param {String} name The form element name.
94 * @return {Y.NodeList}
96 elementsByName: function(name) {
97 if (!this._nameCollections) {
98 this.initElementsByName();
100 if (!({}).hasOwnProperty.call(this._nameCollections, name)) {
101 return new Y.NodeList();
103 return this._nameCollections[name];
107 * Checks the dependencies the form has an makes any changes to the
108 * form that are required.
110 * Changes are made by functions title _dependency{Dependencytype}
111 * and more can easily be introduced by defining further functions.
113 * @param {EventFacade | null} e The event, if any.
114 * @param {String} dependon The form element name to check dependencies against.
117 checkDependencies: function(e, dependon) {
118 var dependencies = this.get('dependencies'),
121 condition, value, lock, hide,
122 checkfunction, result, elements;
123 if (!({}).hasOwnProperty.call(dependencies, dependon)) {
126 elements = this.elementsByName(dependon);
127 for (condition in dependencies[dependon]) {
128 for (value in dependencies[dependon][condition]) {
129 checkfunction = '_dependency' + condition[0].toUpperCase() + condition.slice(1);
130 if (Y.Lang.isFunction(this[checkfunction])) {
131 result = this[checkfunction].apply(this, [elements, value, e]);
133 result = this._dependencyDefault(elements, value, e);
135 lock = result.lock || false;
136 hide = result.hide || false;
137 for (var ei in dependencies[dependon][condition][value]) {
138 var eltolock = dependencies[dependon][condition][value][ei];
139 if (({}).hasOwnProperty.call(tohide, eltolock)) {
140 tohide[eltolock] = tohide[eltolock] || hide;
142 tohide[eltolock] = hide;
145 if (({}).hasOwnProperty.call(tolock, eltolock)) {
146 tolock[eltolock] = tolock[eltolock] || lock;
148 tolock[eltolock] = lock;
154 for (var el in tolock) {
155 var needsupdate = false;
156 if (!({}).hasOwnProperty.call(this._locks, el)) {
157 this._locks[el] = {};
159 if (({}).hasOwnProperty.call(tolock, el) && tolock[el]) {
160 if (!({}).hasOwnProperty.call(this._locks[el], dependon) || this._locks[el][dependon]) {
161 this._locks[el][dependon] = true;
164 } else if (({}).hasOwnProperty.call(this._locks[el], dependon) && this._locks[el][dependon]) {
165 delete this._locks[el][dependon];
169 if (!({}).hasOwnProperty.call(this._hides, el)) {
170 this._hides[el] = {};
172 if (({}).hasOwnProperty.call(tohide, el) && tohide[el]) {
173 if (!({}).hasOwnProperty.call(this._hides[el], dependon) || this._hides[el][dependon]) {
174 this._hides[el][dependon] = true;
177 } else if (({}).hasOwnProperty.call(this._hides[el], dependon) && this._hides[el][dependon]) {
178 delete this._hides[el][dependon];
183 this._dirty[el] = true;
190 * Update all dependencies in form
192 updateAllDependencies: function() {
193 Y.Object.each(this.get('dependencies'), function(value, name) {
194 this.checkDependencies(null, name);
200 * Update dependencies associated with event
202 * @param {Event} e The event.
204 updateEventDependencies: function(e) {
205 var el = e.target.getAttribute('name');
206 this.checkDependencies(e, el);
210 * Flush pending changes to the form
212 updateForm: function() {
214 for (el in this._dirty) {
215 if (({}).hasOwnProperty.call(this._locks, el)) {
216 this._disableElement(el, !Y.Object.isEmpty(this._locks[el]));
218 if (({}).hasOwnProperty.call(this._hides, el)) {
219 this._hideElement(el, !Y.Object.isEmpty(this._hides[el]));
226 * Disables or enables all form elements with the given name
228 * @param {String} name The form element name.
229 * @param {Boolean} disabled True to disable, false to enable.
231 _disableElement: function(name, disabled) {
232 var els = this.elementsByName(name);
233 var filepicker = this.isFilePicker(name);
234 els.each(function(node) {
236 node.setAttribute('disabled', 'disabled');
238 node.removeAttribute('disabled');
241 // Extra code to disable filepicker or filemanager form elements
243 var fitem = node.ancestor('.fitem');
246 fitem.addClass('disabled');
248 fitem.removeClass('disabled');
255 * Hides or shows all form elements with the given name.
257 * @param {String} name The form element name.
258 * @param {Boolean} hidden True to hide, false to show.
260 _hideElement: function(name, hidden) {
261 var els = this.elementsByName(name);
262 els.each(function(node) {
263 var e = node.ancestor('.fitem');
266 display: (hidden) ? 'none' : ''
272 * Is the form element inside a filepicker or filemanager?
274 * @param {String} el The form element name.
277 isFilePicker: function(el) {
278 if (!this._fileinputs) {
280 var selector = '.fitem [data-fieldtype="filepicker"] input,.fitem [data-fieldtype="filemanager"] input';
281 var els = this.get('form').all(selector);
282 els.each(function(node) {
283 fileinputs[node.getAttribute('name')] = true;
285 this._fileinputs = fileinputs;
288 if (({}).hasOwnProperty.call(this._fileinputs, el)) {
289 return this._fileinputs[el] || false;
294 _dependencyNotchecked: function(elements, value) {
296 elements.each(function() {
297 if (this.getAttribute('type').toLowerCase() == 'hidden' &&
298 !this.siblings('input[type=checkbox][name="' + this.get('name') + '"]').isEmpty()) {
299 // This is the hidden input that is part of an advcheckbox.
302 if (this.getAttribute('type').toLowerCase() == 'radio' && this.get('value') != value) {
305 lock = lock || !Y.Node.getDOMNode(this).checked;
312 _dependencyChecked: function(elements, value) {
314 elements.each(function() {
315 if (this.getAttribute('type').toLowerCase() == 'hidden' &&
316 !this.siblings('input[type=checkbox][name="' + this.get('name') + '"]').isEmpty()) {
317 // This is the hidden input that is part of an advcheckbox.
320 if (this.getAttribute('type').toLowerCase() == 'radio' && this.get('value') != value) {
323 lock = lock || Y.Node.getDOMNode(this).checked;
330 _dependencyNoitemselected: function(elements, value) {
332 elements.each(function() {
333 lock = lock || this.get('selectedIndex') == -1;
340 _dependencyEq: function(elements, value) {
342 var hiddenVal = false;
343 var options, v, selected, values;
344 elements.each(function() {
345 if (this.getAttribute('type').toLowerCase() == 'radio' && !Y.Node.getDOMNode(this).checked) {
347 } else if (this.getAttribute('type').toLowerCase() == 'hidden' &&
348 !this.siblings('input[type=checkbox][name="' + this.get('name') + '"]').isEmpty()) {
349 // This is the hidden input that is part of an advcheckbox.
350 hiddenVal = (this.get('value') == value);
352 } else if (this.getAttribute('type').toLowerCase() == 'checkbox' && !Y.Node.getDOMNode(this).checked) {
353 lock = lock || hiddenVal;
356 if (this.getAttribute('class').toLowerCase() == 'filepickerhidden') {
357 // Check for filepicker status.
358 var elementname = this.getAttribute('name');
359 if (elementname && M.form_filepicker.instances[elementname].fileadded) {
364 } else if (this.get('nodeName').toUpperCase() === 'SELECT' && this.get('multiple') === true) {
365 // Multiple selects can have one or more value assigned. A pipe (|) is used as a value separator
366 // when multiple values have to be selected at the same time.
367 values = value.split('|');
369 options = this.get('options');
370 options.each(function() {
371 if (this.get('selected')) {
372 selected[selected.length] = this.get('value');
375 if (selected.length > 0 && selected.length === values.length) {
376 for (var i in selected) {
378 if (values.indexOf(v) > -1) {
389 lock = lock || this.get('value') == value;
398 * Lock the given field if the field value is in the given set of values.
400 * @param {Array} elements
401 * @param {String} values Single value or pipe (|) separated values when multiple
402 * @returns {{lock: boolean, hide: boolean}}
405 _dependencyIn: function(elements, values) {
406 // A pipe (|) is used as a value separator
407 // when multiple values have to be passed on at the same time.
408 values = values.split('|');
410 var hiddenVal = false;
411 var options, v, selected, value;
412 elements.each(function() {
413 if (this.getAttribute('type').toLowerCase() == 'radio' && !Y.Node.getDOMNode(this).checked) {
415 } else if (this.getAttribute('type').toLowerCase() == 'hidden' &&
416 !this.siblings('input[type=checkbox][name="' + this.get('name') + '"]').isEmpty()) {
417 // This is the hidden input that is part of an advcheckbox.
418 hiddenVal = (values.indexOf(this.get('value')) > -1);
420 } else if (this.getAttribute('type').toLowerCase() == 'checkbox' && !Y.Node.getDOMNode(this).checked) {
421 lock = lock || hiddenVal;
424 if (this.getAttribute('class').toLowerCase() == 'filepickerhidden') {
425 // Check for filepicker status.
426 var elementname = this.getAttribute('name');
427 if (elementname && M.form_filepicker.instances[elementname].fileadded) {
432 } else if (this.get('nodeName').toUpperCase() === 'SELECT' && this.get('multiple') === true) {
433 // Multiple selects can have one or more value assigned.
435 options = this.get('options');
436 options.each(function() {
437 if (this.get('selected')) {
438 selected[selected.length] = this.get('value');
441 if (selected.length > 0 && selected.length === values.length) {
442 for (var i in selected) {
444 if (values.indexOf(v) > -1) {
455 value = this.get('value');
456 lock = lock || (values.indexOf(value) > -1);
464 _dependencyHide: function(elements, value) {
470 _dependencyDefault: function(elements, value, ev) {
475 elements.each(function() {
477 if (this.getAttribute('type').toLowerCase() == 'radio' && !Y.Node.getDOMNode(this).checked) {
479 } else if (this.getAttribute('type').toLowerCase() == 'hidden' &&
480 !this.siblings('input[type=checkbox][name="' + this.get('name') + '"]').isEmpty()) {
481 // This is the hidden input that is part of an advcheckbox.
482 hiddenVal = (this.get('value') != value);
484 } else if (this.getAttribute('type').toLowerCase() == 'checkbox' && !Y.Node.getDOMNode(this).checked) {
485 lock = lock || hiddenVal;
488 // Check for filepicker status.
489 if (this.getAttribute('class').toLowerCase() == 'filepickerhidden') {
490 var elementname = this.getAttribute('name');
491 if (elementname && M.form_filepicker.instances[elementname].fileadded) {
496 } else if (this.get('nodeName').toUpperCase() === 'SELECT' && this.get('multiple') === true) {
497 // Multiple selects can have one or more value assigned. A pipe (|) is used as a value separator
498 // when multiple values have to be selected at the same time.
499 values = value.split('|');
501 this.get('options').each(function() {
502 if (this.get('selected')) {
503 selected[selected.length] = this.get('value');
506 if (selected.length > 0 && selected.length === values.length) {
507 for (var i in selected) {
508 if (values.indexOf(selected[i]) > -1) {
519 lock = lock || this.get('value') != value;
528 NAME: 'mform-dependency-manager',
531 setter: function(value) {
532 return Y.one('#' + value);
543 M.form.dependencyManager = dependencyManager;
547 * Stores a list of the dependencyManager for each form on the page.
549 M.form.dependencyManagers = {};
552 * Initialises a manager for a forms dependencies.
553 * This should happen once per form.
555 * @param {YUI} Y YUI3 instance
556 * @param {String} formid ID of the form
557 * @param {Array} dependencies array
558 * @return {M.form.dependencyManager}
560 M.form.initFormDependencies = function(Y, formid, dependencies) {
562 // If the dependencies isn't an array or object we don't want to
564 if (!Y.Lang.isArray(dependencies) && !Y.Lang.isObject(dependencies)) {
569 * Fixes an issue with YUI's processing method of form.elements property
570 * in Internet Explorer.
571 * http://yuilibrary.com/projects/yui3/ticket/2528030
573 Y.Node.ATTRS.elements = {
575 return Y.all(new Y.Array(this._node.elements, 0, true));
579 M.form.dependencyManagers[formid] = new M.form.dependencyManager({form: formid, dependencies: dependencies});
580 return M.form.dependencyManagers[formid];
584 * Update the state of a form. You need to call this after, for example, changing
585 * the state of some of the form input elements in your own code, in order that
586 * things like the disableIf state of elements can be updated.
588 * @param {String} formid ID of the form
590 M.form.updateFormState = function(formid) {
591 if (formid in M.form.dependencyManagers) {
592 M.form.dependencyManagers[formid].updateAllDependencies();