2 * This file contains JS functionality required by mforms and is included automatically
6 // Namespace for the form bits and bobs
10 * Initialises the show advanced functionality and events.
11 * This should only ever happen ONCE per page.
14 * @param {object} config
16 M.form.initShowAdvanced = function(Y, config) {
17 if (M.form.showAdvanced) {
18 return M.form.showAdvanced;
20 var showAdvanced = function(config) {
21 showAdvanced.superclass.constructor.apply(this, arguments);
23 showAdvanced.prototype = {
27 initializer : function() {
28 this._advAreas = Y.all('form .advanced');
29 this._advButtons = Y.all('.showadvancedbtn');
30 if (this._advButtons.size() > 0) {
31 this._stateInput = new Y.NodeList(document.getElementsByName('mform_showadvanced_last'));
32 this._advButtons.on('click', this.switchState, this);
33 this._advButtons.set('type', 'button');
37 * Toggles between showing advanced items and hiding them.
38 * Should be fired by an event.
40 switchState : function(e) {
42 if (this._stateInput.get('value')=='1') {
43 this._stateInput.set('value', '0');
44 this._advButtons.setAttribute('value', M.str.form.showadvanced);
45 this._advAreas.addClass('hide');
47 this._stateInput.set('value', '1');
48 this._advButtons.setAttribute('value', M.str.form.hideadvanced);
49 this._advAreas.removeClass('hide');
53 // Extend it with the YUI widget fw.
54 Y.extend(showAdvanced, Y.Base, showAdvanced.prototype, {
55 NAME : 'mform-showAdvanced'
57 M.form.showAdvanced = new showAdvanced(config);
58 return M.form.showAdvanced;
62 * Initialises a manager for a forms dependencies.
63 * This should happen once per form.
65 M.form.initFormDependencies = function(Y, formid, dependencies) {
67 // If the dependencies isn't an array or object we don't want to
69 if (!Y.Lang.isArray(dependencies) && !Y.Lang.isObject(dependencies)) {
74 * Fixes an issue with YUI's processing method of form.elements property
75 * in Internet Explorer.
76 * http://yuilibrary.com/projects/yui3/ticket/2528030
78 Y.Node.ATTRS.elements = {
80 return Y.all(new Y.Array(this._node.elements, 0, true));
84 // Define the dependency manager if it hasn't already been defined.
85 M.form.dependencyManager = M.form.dependencyManager || (function(){
86 var dependencyManager = function(config) {
87 dependencyManager.superclass.constructor.apply(this, arguments);
89 dependencyManager.prototype = {
92 _nameCollections : [],
93 initializer : function(config) {
95 this._form = Y.one('#'+formid);
96 for (i in dependencies) {
97 this._depElements[i] = this.elementsByName(i);
98 if (this._depElements[i].size() == 0) {
101 this._depElements[i].each(function(node){
102 nodeName = node.get('nodeName').toUpperCase();
103 if (nodeName == 'INPUT') {
104 if (node.getAttribute('type').match(/^(button|submit|radio|checkbox)$/)) {
105 node.on('click', this.checkDependencies, this);
107 node.on('blur', this.checkDependencies, this);
109 node.on('change', this.checkDependencies, this);
110 } else if (nodeName == 'SELECT') {
111 node.on('change', this.checkDependencies, this);
113 node.on('click', this.checkDependencies, this);
114 node.on('blur', this.checkDependencies, this);
115 node.on('change', this.checkDependencies, this);
119 this._form.get('elements').each(function(input){
120 if (input.getAttribute('type')=='reset') {
121 input.on('click', function(){
123 this.checkDependencies();
128 return this.checkDependencies(null);
131 * Gets all elements in the form by thier name and returns
135 elementsByName : function(name) {
136 if (!this._nameCollections[name]) {
138 this._form.get('elements').each(function(){
139 if (this.getAttribute('name') == name) {
143 this._nameCollections[name] = new Y.NodeList(elements);
145 return this._nameCollections[name];
148 * Checks the dependencies the form has an makes any changes to the
149 * form that are required.
151 * Changes are made by functions title _dependency_{dependencytype}
152 * and more can easily be introduced by defining further functions.
154 checkDependencies : function(e) {
157 dependon, condition, value,
158 lock, hide, checkfunction, result;
159 for (dependon in dependencies) {
160 if (this._depElements[dependon].size() == 0) {
163 for (condition in dependencies[dependon]) {
164 for (value in dependencies[dependon][condition]) {
167 checkfunction = '_dependency_'+condition;
168 if (Y.Lang.isFunction(this[checkfunction])) {
169 result = this[checkfunction].apply(this, [this._depElements[dependon], value, e]);
171 result = this._dependency_default(this._depElements[dependon], value, e);
173 lock = result.lock || false;
174 hide = result.hide || false;
175 for (var ei in dependencies[dependon][condition][value]) {
176 var eltolock = dependencies[dependon][condition][value][ei];
178 tohide[eltolock] = true;
180 if (tolock[eltolock] != null) {
181 tolock[eltolock] = lock || tolock[eltolock];
183 tolock[eltolock] = lock;
189 for (var el in tolock) {
190 this._disableElement(el, tolock[el]);
191 if (tohide.propertyIsEnumerable(el)) {
192 this._hideElement(el, tohide[el]);
198 * Disabled all form elements with the given name
200 _disableElement : function(name, disabled) {
201 var els = this.elementsByName(name);
205 this.setAttribute('disabled', 'disabled');
207 this.removeAttribute('disabled');
210 // Extra code to disable a filepicker
211 if (this.getAttribute('class') == 'filepickerhidden'){
212 var pickerbuttons = form.elementsByName(name + 'choose');
213 pickerbuttons.each(function(){
214 var clientid = this.get('id').split('-')[2];
215 var filepicker = Y.one('#file_info_'+clientid);
217 this.setAttribute('disabled','disabled');
218 filepicker.addClass('disabled');
220 this.removeAttribute('disabled');
221 filepicker.removeClass('disabled');
228 * Hides all elements with the given name.
230 _hideElement : function(name, hidden) {
231 var els = this.elementsByName(name);
233 var e = els.ancestor('.fitem');
236 display : (hidden)?'none':''
241 _dependency_notchecked : function(elements, value) {
243 elements.each(function(){
244 if (this.getAttribute('type').toLowerCase()=='hidden' && !this.siblings('input[type=checkbox][name="' + this.get('name') + '"]').isEmpty()) {
245 // This is the hidden input that is part of an advcheckbox.
248 if (this.getAttribute('type').toLowerCase()=='radio' && this.get('value') != value) {
251 lock = lock || !Y.Node.getDOMNode(this).checked;
258 _dependency_checked : function(elements, value) {
260 elements.each(function(){
261 if (this.getAttribute('type').toLowerCase()=='hidden' && !this.siblings('input[type=checkbox][name="' + this.get('name') + '"]').isEmpty()) {
262 // This is the hidden input that is part of an advcheckbox.
265 if (this.getAttribute('type').toLowerCase()=='radio' && this.get('value') != value) {
268 lock = lock || Y.Node.getDOMNode(this).checked;
275 _dependency_noitemselected : function(elements, value) {
277 elements.each(function(){
278 lock = lock || this.get('selectedIndex') == -1;
285 _dependency_eq : function(elements, value) {
287 var hidden_val = false;
288 elements.each(function(){
289 if (this.getAttribute('type').toLowerCase()=='radio' && !Y.Node.getDOMNode(this).checked) {
291 } else if (this.getAttribute('type').toLowerCase() == 'hidden' && !this.siblings('input[type=checkbox][name="' + this.get('name') + '"]').isEmpty()) {
292 // This is the hidden input that is part of an advcheckbox.
293 hidden_val = (this.get('value') == value);
295 } else if (this.getAttribute('type').toLowerCase() == 'checkbox' && !Y.Node.getDOMNode(this).checked) {
296 lock = lock || hidden_val;
299 //check for filepicker status
300 if (this.getAttribute('class').toLowerCase() == 'filepickerhidden') {
301 var elementname = this.getAttribute('name');
302 if (elementname && M.form_filepicker.instances[elementname].fileadded) {
308 lock = lock || this.get('value') == value;
316 _dependency_hide : function(elements, value) {
322 _dependency_default : function(elements, value, ev) {
324 var hidden_val = false;
325 elements.each(function(){
326 if (this.getAttribute('type').toLowerCase()=='radio' && !Y.Node.getDOMNode(this).checked) {
328 } else if (this.getAttribute('type').toLowerCase() == 'hidden' && !this.siblings('input[type=checkbox][name="' + this.get('name') + '"]').isEmpty()) {
329 // This is the hidden input that is part of an advcheckbox.
330 hidden_val = (this.get('value') != value);
332 } else if (this.getAttribute('type').toLowerCase() == 'checkbox' && !Y.Node.getDOMNode(this).checked) {
333 lock = lock || hidden_val;
336 //check for filepicker status
337 if (this.getAttribute('class').toLowerCase() == 'filepickerhidden') {
338 var elementname = this.getAttribute('name');
339 if (elementname && M.form_filepicker.instances[elementname].fileadded) {
345 lock = lock || this.get('value') != value;
354 Y.extend(dependencyManager, Y.Base, dependencyManager.prototype, {
355 NAME : 'mform-dependency-manager'
358 return dependencyManager;
361 return new M.form.dependencyManager();