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);
36 * Toggles between showing advanced items and hiding them.
37 * Should be fired by an event.
39 switchState : function(e) {
41 if (this._stateInput.get('value')=='1') {
42 this._stateInput.set('value', '0');
43 this._advButtons.setAttribute('value', M.str.form.showadvanced);
44 this._advAreas.addClass('hide');
46 this._stateInput.set('value', '1');
47 this._advButtons.setAttribute('value', M.str.form.hideadvanced);
48 this._advAreas.removeClass('hide');
52 // Extend it with the YUI widget fw.
53 Y.extend(showAdvanced, Y.Base, showAdvanced.prototype, {
54 NAME : 'mform-showAdvanced'
56 M.form.showAdvanced = new showAdvanced(config);
57 return M.form.showAdvanced;
61 * Initialises a manager for a forms dependencies.
62 * This should happen once per form.
64 M.form.initFormDependencies = function(Y, formid, dependencies) {
66 // If the dependencies isn't an array or object we don't want to
68 if (!Y.Lang.isArray(dependencies) && !Y.Lang.isObject(dependencies)) {
73 * Fixes an issue with YUI's processing method of form.elements property
74 * in Internet Explorer.
75 * http://yuilibrary.com/projects/yui3/ticket/2528030
77 Y.Node.ATTRS.elements = {
79 return Y.all(new Y.Array(this._node.elements, 0, true));
83 // Define the dependency manager if it hasn't already been defined.
84 M.form.dependencyManager = M.form.dependencyManager || (function(){
85 var dependencyManager = function(config) {
86 dependencyManager.superclass.constructor.apply(this, arguments);
88 dependencyManager.prototype = {
91 _nameCollections : [],
92 initializer : function(config) {
94 this._form = Y.one('#'+formid);
95 for (i in dependencies) {
96 this._depElements[i] = this.elementsByName(i);
97 if (this._depElements[i].size() == 0) {
100 this._depElements[i].each(function(node){
101 nodeName = node.get('nodeName').toUpperCase();
102 if (nodeName == 'INPUT') {
103 if (node.getAttribute('type').match(/^(button|submit|radio|checkbox)$/)) {
104 node.on('click', this.checkDependencies, this);
106 node.on('blur', this.checkDependencies, this);
108 node.on('change', this.checkDependencies, this);
109 } else if (nodeName == 'SELECT') {
110 node.on('change', this.checkDependencies, this);
112 node.on('click', this.checkDependencies, this);
113 node.on('blur', this.checkDependencies, this);
114 node.on('change', this.checkDependencies, this);
118 this._form.get('elements').each(function(input){
119 if (input.getAttribute('type')=='reset') {
120 input.on('click', function(){
122 this.checkDependencies();
127 return this.checkDependencies(null);
130 * Gets all elements in the form by thier name and returns
134 elementsByName : function(name) {
135 if (!this._nameCollections[name]) {
137 this._form.get('elements').each(function(){
138 if (this.getAttribute('name') == name) {
142 this._nameCollections[name] = new Y.NodeList(elements);
144 return this._nameCollections[name];
147 * Checks the dependencies the form has an makes any changes to the
148 * form that are required.
150 * Changes are made by functions title _dependency_{dependencytype}
151 * and more can easily be introduced by defining further functions.
153 checkDependencies : function(e) {
156 dependon, condition, value,
157 lock, hide, checkfunction, result;
158 for (dependon in dependencies) {
159 if (this._depElements[dependon].size() == 0) {
162 for (condition in dependencies[dependon]) {
163 for (value in dependencies[dependon][condition]) {
166 checkfunction = '_dependency_'+condition;
167 if (Y.Lang.isFunction(this[checkfunction])) {
168 result = this[checkfunction].apply(this, [this._depElements[dependon], value, e]);
170 result = this._dependency_default(this._depElements[dependon], value, e);
172 lock = result.lock || false;
173 hide = result.hide || false;
174 for (var ei in dependencies[dependon][condition][value]) {
175 var eltolock = dependencies[dependon][condition][value][ei];
177 tohide[eltolock] = true;
179 if (tolock[eltolock] != null) {
180 tolock[eltolock] = lock || tolock[eltolock];
182 tolock[eltolock] = lock;
188 for (var el in tolock) {
189 this._disableElement(el, tolock[el]);
190 if (tohide.propertyIsEnumerable(el)) {
191 this._hideElement(el, tohide[el]);
197 * Disabled all form elements with the given name
199 _disableElement : function(name, disabled) {
200 var els = this.elementsByName(name);
204 this.setAttribute('disabled', 'disabled');
206 this.removeAttribute('disabled');
209 // Extra code to disable a filepicker
210 if (this.getAttribute('class') == 'filepickerhidden'){
211 var pickerbuttons = form.elementsByName(name + 'choose');
212 pickerbuttons.each(function(){
214 this.setAttribute('disabled','disabled');
216 this.removeAttribute('disabled');
223 * Hides all elements with the given name.
225 _hideElement : function(name, hidden) {
226 var els = this.elementsByName(name);
228 var e = els.ancestor('.fitem');
231 display : (hidden)?'none':''
236 _dependency_notchecked : function(elements, value) {
238 elements.each(function(){
239 if (this.getAttribute('type').toLowerCase()=='radio' && this.get('value') != value) {
242 lock = lock || !Y.Node.getDOMNode(this).checked;
249 _dependency_checked : function(elements, value) {
251 elements.each(function(){
252 if (this.getAttribute('type').toLowerCase()=='radio' && this.get('value') != value) {
255 lock = lock || Y.Node.getDOMNode(this).checked;
262 _dependency_noitemselected : function(elements, value) {
264 elements.each(function(){
265 lock = lock || this.get('selectedIndex') == -1;
272 _dependency_eq : function(elements, value) {
274 elements.each(function(){
275 if (this.getAttribute('type').toLowerCase()=='radio' && !Y.Node.getDOMNode(this).checked) {
277 } else if (this.getAttribute('type').toLowerCase() == 'checkbox' && !Y.Node.getDOMNode(this).checked) {
280 lock = lock || this.get('value') == value;
287 _dependency_hide : function(elements, value) {
293 _dependency_default : function(elements, value, ev) {
295 elements.each(function(){
296 if (this.getAttribute('type').toLowerCase()=='radio' && !Y.Node.getDOMNode(this).checked) {
298 } else if (this.getAttribute('type').toLowerCase() == 'checkbox' && !Y.Node.getDOMNode(this).checked) {
301 lock = lock || this.get('value') != value;
309 Y.extend(dependencyManager, Y.Base, dependencyManager.prototype, {
310 NAME : 'mform-dependency-manager'
313 return dependencyManager;
316 return new M.form.dependencyManager();