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