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