Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-09-25-01-46-57
[civicrm-core.git] / templates / CRM / common / Filter.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.4 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2013 |
6 +--------------------------------------------------------------------+
7 | This file is a part of CiviCRM. |
8 | |
9 | CiviCRM is free software; you can copy, modify, and distribute it |
10 | under the terms of the GNU Affero General Public License |
11 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
12 | |
13 | CiviCRM is distributed in the hope that it will be useful, but |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
16 | See the GNU Affero General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Affero General Public |
19 | License and the CiviCRM Licensing Exception along |
20 | with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25 *}
26 {literal}
27 var stregexp = new RegExp;
28
29 function initFilter( id ) {
30 //build the array
31 filterArray = new Array();
32 filterArray = {/literal}{$tokens}{literal};
33
34 tempArray = new Array();
35 remvdArray = new Array();
36
37 //get select object
38 selObj = document.getElementById("token"+ id);
39
40 //rebuild the list
41 buildOptions(filterArray);
42
43 //clear the input box
44 document.getElementById("filter"+id).value = "";
45
46 //clear the last typed value
47 lastVal = "";
48 }
49
50 function filter( ob, id ) {
51 str = ob.value;
52 //if the length of str is 0, keep original array as option
53 if ( str.length == 0 ) {
54 buildOptions(filterArray);
55 remvdArray.length = 0;
56 } else {
57 //clear tempArray
58 tempArray.length = 0;
59
60 //set up temporary array
61 for ( i = 0; i < selObj.options.length; i++ ) {
62 tempArray[selObj.options[i].value] = selObj.options[i].text;
63 }
64 //escape the special character
65 str = str.replace(/([\\"'()\]\[])/g, "\\$1");
66
67 //case-insensitive regexp
68 stregexp = new RegExp( str, "i" );
69
70 //remove appropriate item(s)
71 if ( lastVal.length < str.length ) {
72 for ( j = selObj.options.length-1; j > -1; j-- ) {
73 if ( selObj.options[j].text.match( stregexp ) == null ) {
74 //delete unwanted option
75 delete tempArray[selObj.options[j].value];
76 }
77 }
78 } else {
79 //add appropriate item(s)
80 //if a removed item matches the new pattern, add it to the list of names
81 for ( key in remvdArray) {
82 tempName = remvdArray[key].toString();
83 if ( tempName.match(stregexp) != null ) {
84 tempArray[key] = tempName;
85 }
86 }
87
88 //sort the names array
89 tempArray.sort();
90 }
91
92 //build the new select list
93 buildOptions(tempArray);
94 }
95
96 //remember the last value on which we narrowed
97 lastVal = str;
98 }
99
100 function buildOptions( arrayName ) {
101 //clear the select list
102 selObj.options.length = 0;
103 //to select only valid tokens in tokens list
104 var tokenRegx = new RegExp (/{(\w+\.\w+)}/);
105 var i = 0;
106 for ( script in arrayName ) {
107 if ( script.match(tokenRegx) != null ) {
108 var option = new Option( arrayName[script], script );
109 selObj.options[i] = option;
110 i++;
111 }
112 }
113 buildRemvd();
114 }
115
116 function buildRemvd( ) {
117 //clear the removed items array
118 remvdArray.length = 0;
119 var remToken = null;
120 //build the removed items array
121 for ( key in filterArray ) {
122 //for filtering tokens
123 remToken = filterArray[key].toString();
124 if ( remToken.match(stregexp) == null ) {
125 //remember which item was removed
126 remvdArray[key] = filterArray[key];
127 }
128 }
129 }
130
131 function getMatches(id) {
132 if ( selObj.options.length == 1 ) {
133 document.getElementById("match"+id).innerHTML = "{/literal}{ts escape='js'}1 match{/ts}{literal}";
134 } else {
135 document.getElementById("match"+id).innerHTML = selObj.options.length +"&nbsp;{/literal}{ts escape='js'}matches{/ts}{literal}";
136 }
137 }
138 {/literal}