Project

General

Profile

Patch #18357 » select_list_move.js

Filou Centrinov, 2014-11-17 03:03

 
1
var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
2

    
3
function addOption(theSel, theText, theValue) {
4
  var newOpt = new Option(theText, theValue);
5
  var selLength = theSel.length;
6
  theSel.options[selLength] = newOpt;
7
}
8

    
9
function swapOptions(theSel, index1, index2) {
10
  var text, value, selected;
11
  text = theSel.options[index1].text;
12
  value = theSel.options[index1].value;
13
  selected = theSel.options[index1].selected;
14
  theSel.options[index1].text = theSel.options[index2].text;
15
  theSel.options[index1].value = theSel.options[index2].value;
16
  theSel.options[index1].selected = theSel.options[index2].selected;
17
  theSel.options[index2].text = text;
18
  theSel.options[index2].value = value;
19
  theSel.options[index2].selected = selected;
20
}
21

    
22
function deleteOption(theSel, theIndex) {
23
  var selLength = theSel.length;
24
  if (selLength > 0) {
25
    theSel.options[theIndex] = null;
26
  }
27
}
28

    
29
function moveOptions(theSelFrom, theSelTo) {
30
  var selLength = theSelFrom.length;
31
  var selectedText = new Array();
32
  var selectedValues = new Array();
33
  var selectedCount = 0;
34
  var i;
35
  for (i = selLength - 1; i >= 0; i--) {
36
    if (theSelFrom.options[i].selected) {
37
      selectedText[selectedCount] = theSelFrom.options[i].text;
38
      selectedValues[selectedCount] = theSelFrom.options[i].value;
39
      deleteOption(theSelFrom, i);
40
      selectedCount++;
41
    }
42
  }
43
  for (i = selectedCount - 1; i >= 0; i--) {
44
    addOption(theSelTo, selectedText[i], selectedValues[i]);
45
  }
46
  if (NS4) history.go(0);
47
}
48

    
49
function moveOptionUp(theSel) {
50
  var indexTop = 0;
51
  for(var s=0; s<theSel.length; s++) {
52
    if (theSel.options[s].selected) {
53
      if (s > indexTop) {
54
        swapOptions(theSel, s-1, s);
55
      }
56
      indexTop++;
57
    }
58
  }
59
}
60

    
61
function moveOptionTop(theSel) {
62
  var indexTop = 0;
63
  for(var s=0; s<theSel.length; s++) {
64
    if (theSel.options[s].selected) {
65
      if (s > indexTop) {
66
        for (var i=s; i>indexTop; i--) {
67
          swapOptions(theSel, i-1, i);
68
        }
69
      }
70
      indexTop++;
71
    }
72
  }
73
}
74

    
75
function moveOptionDown(theSel) {
76
  var indexBottom = theSel.length - 1;
77
  for(var s=indexBottom; s>=0; s--) {
78
    if (theSel.options[s].selected) {
79
      if (s < indexBottom) {
80
        swapOptions(theSel, s+1, s);
81
      }
82
      indexBottom--;
83
    }
84
  }
85
}
86

    
87
function moveOptionBottom(theSel) {
88
  var indexBottom = theSel.length - 1;
89
  for(var s=indexBottom; s>=0; s--) {
90
    if (theSel.options[s].selected) {
91
      if (s < indexBottom) {
92
        for (i=s; i<indexBottom; i++) {
93
          swapOptions(theSel, i+1, i);
94
        }
95
      }
96
      indexBottom--;
97
    }
98
  }
99
}
100

    
101
// OK
102
function selectAllOptions(id) {
103
  var select = $('#'+id);
104
  select.children('option').attr('selected', true);
105
}
(1-1/2)