2
* © Copyright 2007, 2008 IntraHealth International, Inc.
4
* This File is part of I2CE
6
* I2CE is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 3 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
* FormWorm.js -- a javascript mini-library to handle validation and submissions of forms with multiple
21
* actions and options depending on the action.
24
* @author Carl Leitner <litlfred@ibiblio.org>
25
* @copyright Copyright © 2007, 2008 IntraHealth International, Inc.
26
* This file is part of I2CE. I2CE is free software; you can redistribute it and/or modify it under
27
* the terms of the GNU General Public License as published by the Free Software Foundation; either
28
* version 3 of the License, or (at your option) any later version. I2CE is distributed in the hope
29
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
30
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have
31
* received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
35
* This incorporates a few ideas/code of http://forum.mootools.net/topic.php?id=3300
40
var SelectUpdate = new Class( {
44
valNodes: new Object(),
48
show_function : false,
49
hide_function : false,
50
show_function_by_val : false,
51
hide_function_by_val : false,
54
prefix : 'select_update'
57
initialize: function(select_id,options ) {
58
this.select_id = select_id;
59
this.setOptions(options);
63
window.addEvent('load',function(e) {
70
this.select = $(this.select_id);
74
if ($type(this.options.show_function) === 'string') {
75
this.options.show_function = eval(this.options.show_function);
77
if ($type(this.options.show_function) === 'function') {
78
this.options.show_function.bind(this);
80
if ($type(this.options.show_function_by_val) === 'string') {
81
this.options.show_function_by_val = eval(this.options.show_function_by_val);
83
if ($type(this.options.show_function_by_val) === 'function') {
84
this.options.show_function_by_val.bind(this);
86
if ($type(this.options.hide_function) === 'string') {
87
this.options.hide_function = eval(this.options.show_function);
89
if ($type(this.options.hide_function) === 'function') {
90
this.options.hide_function.bind(this);
92
if ($type(this.options.hide_function_by_val) === 'string') {
93
this.options.hide_function_by_val = eval(this.options.hide_function_by_val);
95
if ($type(this.options.hide_function_by_vall) === 'function') {
96
this.options.hide_function_by_val.bind(this);
99
this.select.addEvent('change', function(e) {
100
var new_vals = this.select.getSelected();
101
this.hide(this.current, new_vals);
102
this.show(this.current, new_vals);
103
this.current = new_vals;
105
var new_vals = this.select.getSelected();
106
if (this.options.hide_on_init) {
107
this.hide(this.current, new_vals);
109
if (this.options.show_on_init) {
110
this.show(this.current, new_vals);
112
this.current = new_vals;
115
show: function(old_vals, new_vals, force) {
120
new_vals = this.select.getSelected();
123
old_vals = this.current;
125
if (this.options.show_class || this.options.hide_class) {
126
this.select.getElements('option').each(function (option) {
127
valNode = this.getValNode(option.getAttribute('value'));
131
if (this.nodeWithin(option,new_vals)) {
132
//th current options is among the newly selected options. we don't need to try to remove the show class
133
if (this.options.show_class) {
134
valNode.addClass(this.options.show_class);
136
if (this.options.hide_class) {
137
valNode.removeClass(this.options.hide_class);
140
if (this.options.show_class) {
141
valNode.removeClass(this.options.show_class);
143
if (this.options.hide_class) {
144
valNode.addClass(this.options.hide_class);
149
if ($type(this.options.show_function) === 'function') {
150
this.options.show_function(this.select);
152
if (this.options.show_function_by_val || this.options.show_class) {
153
new_vals.each (function(option) { //run through newly selected options
154
if (force !== true && this.nodeWithin(option,old_vals)) {
155
//the newly selected option was already selected we don't need to do the showingg stuff
158
valNode = this.getValNode(option.getProperty('value'));
162
if (this.options.show_class) {
163
valNode.addClass(this.options.show_class);
165
if (this.options.show_function_by_val) {
166
this.options.show_function_by_val(valNode);
172
nodeWithin: function (node,nodes) {
173
return nodes.some(function(c_node) { return $type(c_node) === 'element' && c_node.isSameNode(node);});
178
hide: function(old_vals,new_vals, force) {
183
new_vals = this.select.getSelected();
186
old_vals = this.current;
188
if ($type(this.options.hide_function) === 'function' ) {
189
this.options.hide_function(this.select);
191
if (this.options.hide_function_by_val || this.options.hide_class) {
192
old_vals.each (function(option) { //run through the old selected options,
193
if (force !== true && this.nodeWithin(option,new_vals)) {
194
//old option remains selected. we don't need to do the hiding stuff
197
valNode = this.getValNode(option.getProperty('value'));
201
if (this.options.hide_class) {
202
valNode.addClass(this.options.hide_class);
204
if (this.options.hide_function_by_val) {
205
this.options.hide_function_by_val(valNode);
211
getValId:function(val) {
213
return this.options.prefix + ':' + this.select_id + ':'+ val;
215
return this.options.prefix + ':' + this.select_id ;
219
getValNode:function (val) {
220
return $(this.getValId(val));
227
SelectUpdate.implement(new Options);
b'\\ No newline at end of file'