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