Mercurial > bins
comparison templates.swigs/search.js @ 0:a84c32f131df 1.1.29
Import vendor version
author | Peter Gervai <grin@grin.hu> |
---|---|
date | Wed, 15 Oct 2008 23:28:56 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a84c32f131df |
---|---|
1 | |
2 /* contains all the data about the pictures (set after the page is | |
3 loaded) */ | |
4 var search_data; | |
5 | |
6 function get_query_variable(variable) { | |
7 var query = window.location.search.substring(1); | |
8 var vars = query.split("&"); | |
9 for (var i=0;i<vars.length;i++) { | |
10 var pair = vars[i].split("="); | |
11 if (pair[0] == variable) { | |
12 return pair[1]; | |
13 } | |
14 } | |
15 return ''; | |
16 } | |
17 | |
18 /* Remove the loading message and get the search data | |
19 */ | |
20 function page_loaded() { | |
21 search_data = sd; | |
22 | |
23 if (document.getElementById){ | |
24 document.getElementById('loading').style.visibility='hidden'; | |
25 } else { | |
26 if (document.layers){ //NS4 | |
27 document.loading.visibility = 'hidden'; | |
28 } else { //IE4 | |
29 document.all.loading.style.visibility = 'hidden'; | |
30 } | |
31 } | |
32 search_string = decodeURIComponent(get_query_variable('q')); | |
33 set_search_string(search_string); | |
34 var word_search = get_query_variable('w'); | |
35 if (word_search=='1') { | |
36 word_search = true; | |
37 } else { | |
38 word_search = false; | |
39 } | |
40 document.getElementById('search_word').checked = word_search; | |
41 perform_search(search_string, word_search); | |
42 | |
43 if (document.layers) { | |
44 document.captureEvents(Event.KEYPRESS); | |
45 } | |
46 } | |
47 | |
48 /* Return a copy of str, translating all occurrences of each character | |
49 in from to the corresponding character in to | |
50 */ | |
51 String.prototype.strtr = function(from, to) | |
52 { | |
53 var s, p, c1, c2, c3; | |
54 if(! this.length || from.length != to.length) return; | |
55 | |
56 s = this; | |
57 for(var i=0; i < this.length; i++) { | |
58 c1 = this.substr(i,1); | |
59 for(var j=0; j<from.length; j++) { | |
60 c2 = from.substr(j,1); | |
61 c3 = to.substr(j,1); | |
62 if(c1 == c2) { | |
63 p = new RegExp(c2,'gi'); | |
64 s = s.replace(p,c3); | |
65 } | |
66 } | |
67 } | |
68 return s; | |
69 }; | |
70 | |
71 /* Return a copy of str, removing all accents in it | |
72 */ | |
73 function remove_accents(string) { | |
74 return string.strtr( | |
75 "ÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ", | |
76 "AAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy"); | |
77 } | |
78 | |
79 | |
80 /* set the string in the query input | |
81 */ | |
82 function set_search_string(search_string) { | |
83 var input = document.getElementById('search_string'); | |
84 input.value = search_string; | |
85 } | |
86 | |
87 /* return the string entered by the user, normalized | |
88 */ | |
89 function get_search_string() { | |
90 var input = document.getElementById('search_string'); | |
91 var search_string = remove_accents(input.value.toLowerCase()); | |
92 | |
93 return search_string; | |
94 } | |
95 | |
96 /* return a list of keywords from the string entered by user | |
97 */ | |
98 function get_keywords(search_string, word_search) { | |
99 if (! search_string ) { | |
100 return new Array; | |
101 } | |
102 keywords = search_string.split(/[^a-z0-9]+/i); | |
103 | |
104 var result = new Array; | |
105 for (var i=0; i<keywords.length; i++) { | |
106 if (keywords[i]) { | |
107 if (word_search) { | |
108 result[result.length] = " " + keywords[i] + " "; | |
109 } else { | |
110 result[result.length] = keywords[i]; | |
111 } | |
112 } | |
113 } | |
114 return result; | |
115 } | |
116 | |
117 /* replace a %parameter% in the template by its value | |
118 */ | |
119 function set_value(template, parameter, value) { | |
120 re = new RegExp("%"+parameter+"%", "g"); | |
121 return template.replace(re, value) | |
122 } | |
123 | |
124 /* display one found result in the page | |
125 */ | |
126 function display_result(results, image) { | |
127 result = result_html; | |
128 result = set_value(result, 'url', image.url); | |
129 result = set_value(result, 'thumb_url', image.thumb_url); | |
130 result = set_value(result, 'album_url', image.album_url); | |
131 result = set_value(result, 'album_title', image.album_title); | |
132 result = set_value(result, 'title', image.title); | |
133 result = set_value(result, 'thumb_width', image.width); | |
134 result = set_value(result, 'thumb_height', image.height); | |
135 | |
136 results.innerHTML += result; | |
137 } | |
138 | |
139 /* update the status string on the page | |
140 */ | |
141 function update_status(status, found) { | |
142 status.firstChild.nodeValue = "Search in progress: " + found.toString() +" found"; | |
143 } | |
144 | |
145 /* return true if all keywords (array of string) match the string s | |
146 */ | |
147 function match(keywords, s){ | |
148 for (var i=0 ; i<keywords.length ; i++) { | |
149 if (keywords[i] && s.indexOf(keywords[i]) == -1) { | |
150 return false; | |
151 } | |
152 } | |
153 return true; | |
154 } | |
155 /* perform the search when the page is loaded | |
156 */ | |
157 function perform_search(search_string, word_search) { | |
158 | |
159 if (! (document.getElementById || document.createElement)){ | |
160 alert("<TMPL_VAR NAME=STRING_NODOM>"); | |
161 return; | |
162 } | |
163 | |
164 var limit = <TMPL_VAR NAME=SEARCH_LIMIT>; | |
165 var found = 0; | |
166 var keywords = get_keywords(search_string, word_search); | |
167 if (keywords.length == 0) { | |
168 return; | |
169 } | |
170 var results = document.getElementById('results'); | |
171 results.innerHTML = ""; | |
172 var status = document.getElementById('status'); | |
173 | |
174 update_status(status, found); | |
175 | |
176 for (var i=0 ; i<search_data.length; i++) { | |
177 image = search_data[i]; | |
178 if (match(keywords, image.search_string)) { | |
179 if (found >= limit) { | |
180 var status_string = "<TMPL_VAR NAME=STRING_TOOMANYRESULTS>"; | |
181 // WTF ? Not sprintf in javascript ? OK, so I code dirty | |
182 status_string = status_string.replace(/%d/, found.toString()); | |
183 status.firstChild.nodeValue = status_string; | |
184 return; | |
185 } | |
186 | |
187 update_status(status, found++); | |
188 display_result(results, image) | |
189 } | |
190 } | |
191 var status_string = "<TMPL_VAR NAME=STRING_SEARCHCOMPLETED>"; | |
192 // For dirty hacking remark, see above comment | |
193 status_string = status_string.replace(/%d/, found.toString()); | |
194 status.firstChild.nodeValue = status_string; | |
195 return; | |
196 } | |
197 | |
198 /* if user has pressed return in the input field, validate the search | |
199 */ | |
200 function process_key(e) { | |
201 if (window.event) { | |
202 if (window.event.type == "keypress" & window.event.keyCode == 13) | |
203 reload_page(); | |
204 } | |
205 if (e) { | |
206 if (e.type == "keypress" & e.keyCode == 13) { | |
207 reload_page(); | |
208 } | |
209 } | |
210 } | |
211 | |
212 /* Reload the page when the user click the search button | |
213 */ | |
214 function reload_page() { | |
215 var search_string = get_search_string(); | |
216 var word_search = document.getElementById('search_word').checked; | |
217 if (search_string) { | |
218 search_string = encodeURIComponent(search_string) | |
219 if (word_search) { | |
220 search_string += "&w=1"; | |
221 } | |
222 location.href = location.pathname + "?q=" + search_string; | |
223 } | |
224 } |