| 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.name = 'rule[actions]['+actions_i+']'; |
| 124 | actions_i++; |
| 125 | Node.appendChild(div); |
| 126 | |
| 127 | var del = document.createElement("input"); |
| 128 | del.setAttribute("type", "submit"); |
| 129 | del.setAttribute("value", "Del"); |
| 130 | del.onclick = function () { del.parentNode.parentNode.removeChild(del.parentNode); }; |
| 131 | div.appendChild(del); |
| 132 | |
| 133 | div.appendChild(document.createTextNode(" ")); |
| 134 | |
| 135 | var select = createActionSelect(data[0],div.name); |
| 136 | div.appendChild(select); |
| 137 | |
| 138 | actions_func[select.options[select.selectedIndex].value](div,data); |
| 139 | } |
| 140 | |
| 141 | function newAction() { |
| 142 | createActionRow(getElement('actionsRow'), [-1]); |
| 143 | } |
| 144 | |
| 145 | /********** RULE.MATCH FORM FUNCTIONS **********/ |
| 146 | |
| 147 | function createMatchSelect(data,base) { |
| 148 | var i,j; |
| 149 | var select = document.createElement("select"); |
| 150 | var sel = 0; |
| 151 | select.name = base+'[0]'; |
| 152 | |
| 153 | for(i=0, j=0; i<matches_list.length; i++) { |
| 154 | if(data==i) sel = j; |
| 155 | if(matches_list[i]) select.options[j++] = new Option(matches_list[i],i,false,(data==i)); |
| 156 | } |
| 157 | |
| 158 | select.selectedIndex = sel; |
| 159 | select.onchange = function () { changeRow(this,matches_func); }; |
| 160 | |
| 161 | return select; |
| 162 | } |
| 163 | |
| 164 | function createMatchRow(Node,data) { |
| 165 | var div = document.createElement("div"); |
| 166 | div.name = 'rule[matches]['+actions_i+']'; |
| 167 | actions_i++; |
| 168 | Node.appendChild(div); |
| 169 | |
| 170 | var del = document.createElement("input"); |
| 171 | del.setAttribute("type", "submit"); |
| 172 | del.setAttribute("value", "Del"); |
| 173 | del.onclick = function () { del.parentNode.parentNode.removeChild(del.parentNode); }; |
| 174 | div.appendChild(del); |
| 175 | |
| 176 | div.appendChild(document.createTextNode(" ")); |
| 177 | |
| 178 | var select = createMatchSelect(data[0],div.name); |
| 179 | div.appendChild(select); |
| 180 | |
| 181 | matches_func[select.options[select.selectedIndex].value](div,data); |
| 182 | } |
| 183 | |
| 184 | function newMatch() { |
| 185 | createMatchRow(getElement('matchesRow'), [-1]); |
| 186 | } |
| 187 | |
| 188 | /********** RULE FORM FUNCTIONS **********/ |
| 189 | |
| 190 | function changeRow(sel, funcs) { |
| 191 | var row = sel.parentNode; |
| 192 | cleanChilds(row,3); |
| 193 | funcs[sel.options[sel.selectedIndex].value](row,0); |
| 194 | } |
| 195 | |
| 196 | function createRuleForm() { |
| 197 | var i; |
| 198 | var sel = getElement('orderSelect'); |
| 199 | var index = sel.options[sel.selectedIndex].value; |
| 200 | |
| 201 | actions_i = matches_i = 0; |
| 202 | current_rule = filter[index]; |
| 203 | |
| 204 | getElement('ruleId').value = index; |
| 205 | getElement('ruleName').value = current_rule.name; |
| 206 | var c = (current_rule.all ? '1' : '0'); |
| 207 | getElement('ruleAll'+c).checked = true; |
| 208 | getElement('ruleBlock').checked = current_rule.block; |
| 209 | |
| 210 | var mr = getElement('matchesRow'); |
| 211 | cleanChilds(mr,0); |
| 212 | for(i=0; i<current_rule.matches.length; i++) |
| 213 | createMatchRow(mr, current_rule.matches[i]); |
| 214 | |
| 215 | var ar = getElement('actionsRow'); |
| 216 | cleanChilds(ar,0); |
| 217 | for(i=0; i<current_rule.actions.length; i++) |
| 218 | createActionRow(ar, current_rule.actions[i]); |
| 219 | } |
| 220 | |
| 221 | /******************************************************************************** |
| 222 | * $Id$ |
| 223 | * vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100: |
| 224 | ********************************************************************************/ |