// ------------------------------------------------------------------------------------------ // Switch Behavior // ------------------------------------------------------------------------------------------ if(wFORMS) { // Component properties wFORMS.classNamePrefix_switch = "switch"; wFORMS.className_switchIsOn = "swtchIsOn"; // used to keep track of the switch state on buttons and links (where the checked attribute is not available) wFORMS.className_switchIsOff = "swtchIsOff"; wFORMS.classNamePrefix_offState = "offstate"; wFORMS.classNamePrefix_onState = "onstate"; wFORMS.switchScopeRootTag = ""; // deprecated. wFORMS.switchTriggers = {}; // associative multi-dimensional array (switchname->element Ids) wFORMS.switchTargets = {}; // associative multi-dimensional array (switchname->element Ids) wFORMS.behaviors['switch'] = { // ------------------------------------------------------------------------------------------ // evaluate: check if the behavior applies to the given node. Adds event handlers if appropriate // ------------------------------------------------------------------------------------------ evaluate: function(node) { // Handle Switch Triggers // add event handles and populate the wFORMS.switchTriggers // associative array (switchname->element Ids) // ------------------------------------------------------------------------------------------ if (wFORMS.helpers.hasClassPrefix(node, wFORMS.classNamePrefix_switch)) { if(!node.id) node.id = wFORMS.helpers.randomId(); //wFORMS.debug('switch/evaluate: '+ node.className + ' ' + node.tagName); // Go through each class (one element can have more than one switch trigger). var switchNames = wFORMS.behaviors['switch'].getSwitchNames(node); for(var i=0; i < switchNames.length; i++) { if(!wFORMS.switchTriggers[switchNames[i]]) wFORMS.switchTriggers[switchNames[i]] = new Array(); if(!wFORMS.switchTriggers[switchNames[i]][node.id]) wFORMS.switchTriggers[switchNames[i]].push(node.id); //wFORMS.debug('switch/evaluate: [trigger] '+ switchNames[i] + ' ' + node.id,3); } switch(node.tagName.toUpperCase()) { case "OPTION": // get the SELECT element var selectNode = node.parentNode; while(selectNode && selectNode.tagName.toUpperCase() != 'SELECT') { var selectNode = selectNode.parentNode; } if(!selectNode) { alert('Error: invalid markup in SELECT field ?'); return false; } // invalid markup if(!selectNode.id) selectNode.id = wFORMS.helpers.randomId(); // Make sure we have only one event handler for the select. if(!selectNode.getAttribute('rel') || selectNode.getAttribute('rel').indexOf('wfHandled')==-1) { //wFORMS.debug('switch/add event: '+ selectNode.className + ' ' + selectNode.tagName); selectNode.setAttribute('rel', (selectNode.getAttribute('rel')||"") + ' wfHandled'); wFORMS.helpers.addEvent(selectNode, 'change', wFORMS.behaviors['switch'].run); } break; case "INPUT": if(node.type && node.type.toLowerCase() == 'radio') { // Add the onclick event on radio inputs of the same group var formElement = node.form; for (var j=0; j element ids) // ------------------------------------------------------------------------------------------ if (wFORMS.helpers.hasClassPrefix(node, wFORMS.classNamePrefix_offState) || wFORMS.helpers.hasClassPrefix(node, wFORMS.classNamePrefix_onState)) { if(!node.id) node.id = wFORMS.helpers.randomId(); // Go through each class (one element can be the target of more than one switch). var switchNames = wFORMS.behaviors['switch'].getSwitchNames(node); for(var i=0; i < switchNames.length; i++) { if(!wFORMS.switchTargets[switchNames[i]]) wFORMS.switchTargets[switchNames[i]] = new Array(); if(!wFORMS.switchTargets[switchNames[i]][node.id]) wFORMS.switchTargets[switchNames[i]].push(node.id); //wFORMS.debug('switch/evaluate: [target] '+ switchNames[i],3); } } if(node.tagName && node.tagName.toUpperCase()=='FORM') { // function to be called when all behaviors for this form have been applied //wFORMS.debug('switch/push init'); wFORMS.onLoadComplete.push(wFORMS.behaviors['switch'].init); } }, // ------------------------------------------------------------------------------------------ // init: executed once evaluate has been applied to all elements // ------------------------------------------------------------------------------------------ init: function() { // go through all switch triggers and activate those who are already ON //wFORMS.debug('switch/init: '+ (wFORMS.switchTriggers.length)); for(var switchName in wFORMS.switchTriggers) { // go through all triggers for the current switch for(var i=0; i< wFORMS.switchTriggers[switchName].length; i++) { var element = document.getElementById(wFORMS.switchTriggers[switchName][i]); //wFORMS.debug('switch/init: ' + element + ' ' + switchName , 5); if(wFORMS.behaviors['switch'].isTriggerOn(element,switchName)) { // if it's a select option, get the select element if(element.tagName.toUpperCase()=='OPTION') { var element = element.parentNode; while(element && element.tagName.toUpperCase() != 'SELECT') { var element = element.parentNode; } } // run the trigger wFORMS.behaviors['switch'].run(element); } } } }, // ------------------------------------------------------------------------------------------ // run: executed when the behavior is activated // ------------------------------------------------------------------------------------------ run: function(e) { var element = wFORMS.helpers.getSourceElement(e); if(!element) element = e; //wFORMS.debug('switch/run: ' + element.id , 5); var switches_ON = new Array(); var switches_OFF = new Array(); // Get list of triggered switches (some ON, some OFF) switch(element.tagName.toUpperCase()) { case 'SELECT': for(var i=0;i