Initial revision
[old-projects.git] / philter / philter / include / philter.js2
1 /********************************************************************************
2 * include/philter.js : philter javascript functions
3 * ------------------
4 *
5 * This file is part of the philter distribution
6 * Copyright: See COPYING files that comes with this distribution
7 ********************************************************************************/
8
9 function text_onfocus(object,val) {
10     if(object.value == val)
11         object.value = '';
12 }
13
14 function text_onblur(object,val) {
15     if(object.value == '')
16         object.value = val;
17 }
18
19 function cleanChilds(Node, nbChilds) {
20     while(Node.childNodes.length>nbChilds)
21         Node.removeChild(Node.childNodes[nbChilds]);
22 }
23
24 /********** ORDER FORM FUNCTIONS **********/
25
26 function order_up() {
27     form = document.forms['order'];
28     select = form.elements['order[select]'];
29
30     if(select.selectedIndex>1) {
31         i = select.selectedIndex;
32         value1 = select.options[i].value;
33         text1 = select.options[i].text;
34         value2 = select.options[i-1].value;
35         text2 = select.options[i-1].text;
36
37         select.options[i] = new Option(text2,value2,false,false);
38         select.options[i-1] = new Option(text1,value1,false,false);
39         select.selectedIndex = i-1;
40
41         form.elements['order['+value1+']'].value = i-1;
42         form.elements['order['+value2+']'].value = i;
43     }
44 }
45
46 function order_dn() {
47     form = document.forms['order'];
48     select = form.elements['order[select]'];
49
50     if(select.selectedIndex>0 && select.selectedIndex<select.length-1) {
51         i = select.selectedIndex;
52         value1 = select.options[i].value;
53         text1 = select.options[i].text;
54         value2 = select.options[i+1].value;
55         text2 = select.options[i+1].text;
56
57         select.options[i] = new Option(text2,value2,false,false);
58         select.options[i+1] = new Option(text1,value1,false,false);
59         select.selectedIndex = i+1;
60
61         form.elements['order['+value1+']'].value = i+1;
62         form.elements['order['+value2+']'].value = i;
63     }
64 }
65
66 function order_submit(obj) {
67     if(obj.name == 'order[submit]')
68         obj.form.elements['order[action]'].value = 'submit';
69     if(obj.name == 'order[delete]')
70         obj.form.elements['order[action]'].value = 'delete';
71     obj.form.submit();
72 }
73
74 /********** RULE FORM GLOBALS **********/
75
76 var matches_list = new Array();
77 var matches_func = new Array();
78
79 var actions_list = new Array()
80 var actions_func = new Array();
81
82 var mail_pool = new Array();
83 var filter = new Array();
84
85 var current_rule;
86
87 var actions_i = 0;
88 var matches_i = 0;
89
90 /********** RULE.ACTION FORM FUNCTIONS **********/
91
92 function createActionSelect(data,base) {
93     var i,j;
94     var select = document.createElement("select");
95     select.name = base+'[0]';
96
97     for(i=0, j=0; i<actions_list.length; i++)
98         if(actions_list[i])
99             select.options[j++] = new Option(actions_list[i],i,false,(data==i));
100
101     select.setAttribute("onchange", "changeRow(this,actions_func)");
102
103     if(select.selectedIndex<0)
104         select.selectedIndex = 0;
105     return select;
106 }
107
108 function createActionRow(Node,data) {
109     var div = document.createElement("div");
110     div.className = 'row';
111     div.name = 'rule[actions]['+actions_i+']';
112     actions_i++;
113     Node.appendChild(div);
114
115     var del = document.createElement("input");
116     del.setAttribute("type", "submit");
117     del.setAttribute("value", "Del");
118     del.setAttribute("onclick", "parentNode.parentNode.removeChild(parentNode)");
119     div.appendChild(del);
120
121     div.appendChild(document.createTextNode(" "));
122
123     var select = createActionSelect(data[0],div.name);
124     div.appendChild(select);
125
126     actions_func[select.options[select.selectedIndex].value](div,data);
127 }
128
129 function newAction() {
130     createActionRow(document.all['actions_row'], 0);
131 }
132
133 /********** RULE.MATCH FORM FUNCTIONS **********/
134
135 function createMatchSelect(data,base) {
136     var i,j;
137     var select = document.createElement("select");
138     select.name = base+'[0]';
139
140     for(i=0, j=0; i<matches_list.length; i++)
141         if(matches_list[i])
142             select.options[j++] = new Option(matches_list[i],i,false,(data==i));
143
144     select.setAttribute("onchange", "changeRow(this,matches_func)");
145
146     if(select.selectedIndex<0)
147         select.selectedIndex = 0;
148     return select;
149 }
150
151 function createMatchRow(Node,data) {
152     var div = document.createElement("div");
153     div.className = 'row';
154     div.name = 'rule[matches]['+actions_i+']';
155     actions_i++;
156     Node.appendChild(div);
157
158     var del = document.createElement("input");
159     del.setAttribute("type", "submit");
160     del.setAttribute("value", "Del");
161     del.setAttribute("onclick", "parentNode.parentNode.removeChild(parentNode)");
162     div.appendChild(del);
163
164     div.appendChild(document.createTextNode(" "));
165
166     var select = createMatchSelect(data[0],div.name);
167     div.appendChild(select);
168
169     matches_func[select.options[select.selectedIndex].value](div,data);
170 }
171
172 function newMatch() {
173     createMatchRow(document.all['matches_row'], 0);
174 }
175
176 /********** RULE FORM FUNCTIONS **********/
177
178 function changeRow(sel, funcs) {
179     var row = sel.parentNode;
180     cleanChilds(row,3);
181     funcs[sel.options[sel.selectedIndex].value](row,0);
182 }
183
184 function createRuleForm() {
185     var i;
186     var sel = document.all['order[select]'];
187     var index = sel.options[sel.selectedIndex].value;
188
189     actions_i = matches_i = 0;
190     current_rule = filter[index];
191
192     document.all['rule[id]'].value = index;
193     document.all['rule[name]'].value = current_rule.name;
194     document.all['rule[all]'][1-current_rule.all].checked = true;
195     document.all['rule[block]'].checked = current_rule.block;
196
197     var mr = document.all['matches_row'];
198     cleanChilds(mr,0);
199     for(i=0; i<current_rule.matches.length; i++)
200         createMatchRow(mr, current_rule.matches[i]);
201     
202     var ar = document.all['actions_row'];
203     cleanChilds(ar,0);
204     for(i=0; i<current_rule.actions.length; i++)
205         createActionRow(ar, current_rule.actions[i]);
206 }
207
208 /********************************************************************************
209 * $Id$
210 * vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
211 ********************************************************************************/