import rt 3.6.4
[freeside.git] / rt / html / NoAuth / js / list.js
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %# 
3 %# COPYRIGHT:
4 %#  
5 %# This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC 
6 %#                                          <jesse@bestpractical.com>
7 %# 
8 %# (Except where explicitly superseded by other copyright notices)
9 %# 
10 %# 
11 %# LICENSE:
12 %# 
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
16 %# from www.gnu.org.
17 %# 
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 %# General Public License for more details.
22 %# 
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 %# 02110-1301 or visit their web page on the internet at
27 %# http://www.gnu.org/copyleft/gpl.html.
28 %# 
29 %# 
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %# 
32 %# (The following paragraph is not intended to limit the rights granted
33 %# to you to modify and distribute this software under the terms of
34 %# the GNU General Public License and is only of importance to you if
35 %# you choose to contribute your changes and enhancements to the
36 %# community by submitting them to Best Practical Solutions, LLC.)
37 %# 
38 %# By intentionally submitting any modifications, corrections or
39 %# derivatives to this work, or any other work intended for use with
40 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 %# you are the copyright holder for those contributions and you grant
42 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 %# royalty-free, perpetual, license to use, copy, create derivative
44 %# works based on those contributions, and sublicense and distribute
45 %# those contributions and any derivatives thereof.
46 %# 
47 %# END BPS TAGGED BLOCK }}}
48 /* by TKirby, released under GPL */
49 /* Define the "list" Class */
50 Class("list").define({
51  name : null,
52  xml  : null,
53  sels : null,
54  list : function (src, esrc, name) { this.init(src, esrc, name); },
55  read : function () {
56      var i              = 0;
57      if(this.xml.readyState!=4) { setTimeout(this.name+".read()", 100); }
58      else if(this.xml.status!=200) alert("Document not available.");
59      else {
60          var doc        = this.xml.responseXML;
61          var nNode      = null;
62          if(doc.childNodes[0].nodeName=="parseerror") alert("Parse Error.");
63          doc            = doc.getElementsByTagName("list")[0];
64          for(i=0;i<doc.childNodes.length;i++) {
65              if(doc.childNodes[i].childNodes.length>0) {
66                  nNode  = document.createElement("option");
67                  nNode.appendChild(document.createTextNode(doc.childNodes[i].childNodes[0].nodeValue));
68                  this.sels[0].appendChild(nNode);
69              }
70          }
71      }
72  },
73      
74  init : function (src,esrc,name) {
75      if(!src) return;
76      this.name          = name;
77      this.sels          = new Array();
78      var i                      = 0;
79      for(i=0;i<src.childNodes.length;i++) {
80          if(src.childNodes[i].nodeName=="select" || src.childNodes[i].nodeName=="SELECT") {
81              this.sels.push(src.childNodes[i]);
82          } 
83
84          if((src.childNodes[i].nodeName=="input" || src.childNodes[i].nodeName=="INPUT")
85             && (src.childNodes[i].name=="fromjs")) {
86              src.childNodes[i].value = 1;
87          }
88
89          if((src.childNodes[i].nodeName=="input" || src.childNodes[i].nodeName=="INPUT")
90             && (src.childNodes[i].type=="submit" || src.childNodes[i].type=="SUBMIT")) {
91
92              if (src.childNodes[i].name.indexOf("Save") < 0) {
93                  var tmp        = document.createElement("input");
94                  tmp.type       = "button";
95                  tmp.name       = src.childNodes[i].name;
96                  tmp.value      = src.childNodes[i].value;
97                  src.replaceChild(tmp,src.childNodes[i]);
98              }
99
100              if(src.childNodes[i].name=="add")
101                  src.childNodes[i].onclick = new Function(this.name+".add();");
102              if(src.childNodes[i].name=="remove") 
103                  src.childNodes[i].onclick = new Function(this.name+".remove();");
104              if(src.childNodes[i].name=="moveup") 
105                  src.childNodes[i].onclick = new Function(this.name+".moveup();");
106              if(src.childNodes[i].name=="movedown") 
107                  src.childNodes[i].onclick = new Function(this.name+".movedown();");
108          } 
109      }
110      if (esrc) {
111          this.xml       = (window.navigator.appName!="Microsoft Internet Explorer"
112                            ?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP"));
113          this.xml.open("GET", esrc);
114          this.xml.send("");
115          setTimeout(this.name+".read()", 100);
116      }
117  },
118      
119  add : function() {
120      var i, j   = 0;
121      var dNode  = null;
122      for(i=0;i<this.sels[0].length;i++) if(this.sels[0][i].selected) {
123          for(j=0;j<this.sels[1].length;j++) if(this.sels[1][j].value==this.sels[0][i].value) break;
124          if(j==this.sels[1].length) dNode       = this.sels[0][i].cloneNode(true), 
125                                         this.sels[1].appendChild(dNode);
126      }
127  },
128
129  moveup : function() { this.move(-1); },
130  movedown : function() { this.move(1); },
131  move : function(v) {
132   var i         = 0;
133   if(v<0) for(i=0;i<this.sels[1].length;i++) this.moveOne(v, i);
134   else if(v>0) for(i=this.sels[1].length-1;i>=0;i--)this.moveOne(v, i);
135  },
136
137  moveOne : function(v, i) {
138   var ins       = v + i;
139   if(ins<0 || ins>=this.sels[1].length) return;
140   if(this.sels[1][ins].selected) return;
141   if(this.sels[1][i].selected) {
142    Node         = this.sels[1][i];
143    this.sels[1].removeChild(Node);
144    this.sels[1].insertBefore(Node, this.sels[1][ins]);
145   }
146  },
147
148  remove : function() {
149   var i         = 0;
150   for(i=this.sels[1].length-1;i>=0;i--) if(this.sels[1][i].selected) 
151    this.sels[1].removeChild(this.sels[1][i]);
152  },
153
154  selectAll: function() {
155   var i         = 0;
156   for(i=0;i<this.sels[0].length;i++) this.sels[0][i].selected = false;
157   for(i=0;i<this.sels[1].length;i++) this.sels[1][i].selected = true;
158  }
159 });