Thilo Planz - Widget.Select-0.01

Documentation | Source

NAME

Widget.Select - Useful functions to manipulate HTML Select elements

SYNOPSIS

        <select id=left multiple=1 size=20>
        <option>One</option>
        <option>Two</option>
        <option>Three</option>
        <option>Four</option>
        <option>Five</option>
        <option>Six</option>
        <option>Seven</option>
        <option>Eight</option>
        </select>
        
        <button onclick='Widget.Select.moveSelectedOptionsTo("left", "right")'>move right</button>
        <button onclick='Widget.Select.moveSelectedOptionsTo("right", "left")'>move left</button>
        
        
        <select id=right multiple=1 size=20>
        </select>
        
        <button onclick='Widget.Select.moveSelectedOptionsUp("left")'>UP</button>
        
        <button onclick='Widget.Select.moveSelectedOptionsDown("left")'>DOWN</button>

DESCRIPTION

This module provides a collection of "static" functions that manipulate HTML Select form input elements. The target Select element can be passed as DOM object or DOM object ID (string).

selectAll

        Widget.Select.selectAll(elem);

Select all options.

selectNone

        Widget.Select.selectNone(elem);

Unselect all options.

invertSelection

        Widget.Select.invertSelection(elem);

Unselects all selected options and selects all other options.

moveSelectedOptionsUp

        Widget.Select.moveSelectedOptionsUp(elem);

Rearranges the order of the options within the Select element by moving all selected options up one position. The options remain selected.

moveSelectedOptionsDown

        Widget.Select.moveSelectedOptionsDown(elem);

Rearranges the order of the options within the Select element by moving all selected options down one position. The options remain selected.

moveSelectedOptionsTo

        Widget.Select.moveSelectedOptionsTo(sourceElem, targetElem);

Moves the selected options from one Select element into another Select element. The options remain selected, are placed under all options already in the target element, and are removed from their original element.

EXPORTS

Nothing.

AUTHOR

Thilo Planz <thilo@cpan.org>

SEE ALSO

The JSAN module Widget.PairedMultiSelect by Dave Rolsky also deals with (a specialised version of) Select elements.

COPYRIGHT

 COPYRIGHT
      Copyright (c) 2005 Thilo Planz .  All rights reserved.
      This module is free software; you can redistribute it and/or modify it
      under the terms of the Artistic license. 
if ( typeof Widget == "undefined" ) Widget = {};

if ( typeof Widget.Select == "undefined" ) Widget.Select = {};

Widget.Select.VERSION = '0.01';


Widget.Select.selectAll = function (source){
	if (typeof(source) != 'object') source = document.getElementById(source);
	var l = source.options.length;
    for (var j=0; j<l; j++){
    	source.options[j].selected = true;
    }
}


Widget.Select.selectNone = function(source){
	if (typeof(source) != 'object') source = document.getElementById(source);
	var l = source.options.length;
    for (var j=0; j<l; j++){
    	source.options[j].selected = false;
    }
}


Widget.Select.invertSelection =function(source){
	if (typeof(source) != 'object') source = document.getElementById(source);
	var l = source.options.length;
    for (var j=0; j<l; j++){
    	source.options[j].selected = ! source.options[j].selected;
    }
}

Widget.Select._moveOption = function(e, source, s_idx, target){
			var opt = new Option(
				e.text, e.value);
			opt.selected = e.selected;
			target.options[target.options.length] = opt;
			source.options[s_idx] = null;
}


Widget.Select.moveSelectedOptionsUp = function(source){
	if (typeof(source) != 'object') source = document.getElementById(source);
	var l = source.options.length;
    for (var j=0; j<l; j++){
	
		var e = source.options[0];
		if (e.selected){
			Widget.Select._moveOption(e, source, 0, source, l);
			continue;
		}
		
	
		while (j<l-1){
			var f= source.options[1];
			if (!f.selected) break;
			Widget.Select._moveOption(f, source, 1, source, l);
			j++;
		}
	
		Widget.Select._moveOption(e, source, 0, source, l);
	}
			
}


Widget.Select.moveSelectedOptionsDown = function(source){
	if (typeof(source) != 'object') source = document.getElementById(source);
	var l = source.options.length;
    var skip=0;
    for (var j=0; j<l; j++){
		var e = source.options[0];
		if (skip == 0){
			if (e.selected){
				for (var i=1;i<l-j; i++){
					var f = source.options[i];
					if (! f.selected){
						Widget.Select._moveOption(f, source, i, source, l);
						j++;
						break;
					}
					skip++;
					
				}
				
			}
		}else{
			skip--;
		}	
		
		Widget.Select._moveOption(e, source, 0, source, l);
		
	}
			
}



Widget.Select.moveSelectedOptionsTo = function(source, target){
	if (typeof(source) != 'object') source = document.getElementById(source);
	if (typeof(target) != 'object') target = document.getElementById(target);
	for (var i=0; i<source.options.length; i++){
		var e = source.options[i];
		if(e.selected){
			Widget.Select._moveOption(e,source, i, target, target.options.length);
			i--;
		}
	}
}

/*

*/