annotate public/javascripts/select_list_move.js @ 1613:90bed4e10cc8 deploy

Download file link
author Chris Cannam
date Wed, 30 Aug 2017 17:24:37 +0100
parents dffacf8a6908
children
rev   line source
Chris@1115 1 var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
Chris@1115 2
Chris@1464 3 function addOption(theSel, theText, theValue) {
Chris@1115 4 var newOpt = new Option(theText, theValue);
Chris@1115 5 var selLength = theSel.length;
Chris@1115 6 theSel.options[selLength] = newOpt;
Chris@1115 7 }
Chris@1115 8
Chris@1464 9 function swapOptions(theSel, index1, index2) {
Chris@1115 10 var text, value;
Chris@1115 11 text = theSel.options[index1].text;
Chris@1115 12 value = theSel.options[index1].value;
Chris@1115 13 theSel.options[index1].text = theSel.options[index2].text;
Chris@1115 14 theSel.options[index1].value = theSel.options[index2].value;
Chris@1115 15 theSel.options[index2].text = text;
Chris@1115 16 theSel.options[index2].value = value;
Chris@1115 17 }
Chris@1115 18
Chris@1464 19 function deleteOption(theSel, theIndex) {
Chris@1115 20 var selLength = theSel.length;
Chris@1464 21 if (selLength > 0) {
Chris@1115 22 theSel.options[theIndex] = null;
Chris@1115 23 }
Chris@1115 24 }
Chris@1115 25
Chris@1464 26 function moveOptions(theSelFrom, theSelTo) {
Chris@1115 27 var selLength = theSelFrom.length;
Chris@1115 28 var selectedText = new Array();
Chris@1115 29 var selectedValues = new Array();
Chris@1115 30 var selectedCount = 0;
Chris@1115 31 var i;
Chris@1464 32 for (i = selLength - 1; i >= 0; i--) {
Chris@1464 33 if (theSelFrom.options[i].selected) {
Chris@1115 34 selectedText[selectedCount] = theSelFrom.options[i].text;
Chris@1115 35 selectedValues[selectedCount] = theSelFrom.options[i].value;
Chris@1115 36 deleteOption(theSelFrom, i);
Chris@1115 37 selectedCount++;
Chris@1115 38 }
Chris@1115 39 }
Chris@1464 40 for (i = selectedCount - 1; i >= 0; i--) {
Chris@1115 41 addOption(theSelTo, selectedText[i], selectedValues[i]);
Chris@1115 42 }
Chris@1464 43 if (NS4) history.go(0);
Chris@1115 44 }
Chris@1115 45
Chris@1115 46 function moveOptionUp(theSel) {
Chris@1115 47 var index = theSel.selectedIndex;
Chris@1115 48 if (index > 0) {
Chris@1115 49 swapOptions(theSel, index-1, index);
Chris@1115 50 theSel.selectedIndex = index-1;
Chris@1115 51 }
Chris@1115 52 }
Chris@1115 53
Chris@1517 54 function moveOptionTop(theSel) {
Chris@1517 55 var index = theSel.selectedIndex;
Chris@1517 56
Chris@1517 57 if (index > 0) {
Chris@1517 58 for (i=index; i>0; i--) {
Chris@1517 59 swapOptions(theSel, i-1, i);
Chris@1517 60 }
Chris@1517 61 theSel.selectedIndex = 0;
Chris@1517 62 }
Chris@1517 63 }
Chris@1517 64
Chris@1115 65 function moveOptionDown(theSel) {
Chris@1115 66 var index = theSel.selectedIndex;
Chris@1115 67 if (index < theSel.length - 1) {
Chris@1115 68 swapOptions(theSel, index, index+1);
Chris@1115 69 theSel.selectedIndex = index+1;
Chris@1115 70 }
Chris@1115 71 }
Chris@1115 72
Chris@1517 73 function moveOptionBottom(theSel) {
Chris@1517 74 var index = theSel.selectedIndex;
Chris@1517 75 var indexTop = theSel.length - 1;
Chris@1517 76 if (index < theSel.length - 1) {
Chris@1517 77 for (i=index; i<indexTop; i++) {
Chris@1517 78 swapOptions(theSel, i+1, i);
Chris@1517 79 }
Chris@1517 80 theSel.selectedIndex = indexTop;
Chris@1517 81 }
Chris@1517 82 }
Chris@1517 83
Chris@1115 84 // OK
Chris@1464 85 function selectAllOptions(id) {
Chris@1464 86 var select = $('#'+id);
Chris@1115 87 select.children('option').attr('selected', true);
Chris@1115 88 }