1 | // client-side oportunistic modelling of "business-objects": |
---|
2 | // Query, Queryset, Component? |
---|
3 | var page_record_count = 10; |
---|
4 | var formatquerystring_len = 25; |
---|
5 | |
---|
6 | function Collection(index, name){ |
---|
7 | this.index = index; |
---|
8 | this.name = name; |
---|
9 | }; |
---|
10 | var collections = []; |
---|
11 | |
---|
12 | |
---|
13 | function Query(collection, columns, query , squery) { |
---|
14 | this.collection = collection; // [] |
---|
15 | this.columns = columns; // string |
---|
16 | this.query = query; //sctext |
---|
17 | this.squery = squery; |
---|
18 | this.listid = ""; |
---|
19 | this.container = {}; |
---|
20 | // page |
---|
21 | this.startItem = 1; |
---|
22 | this.maximumItems = parseInt(this.startItem) + page_record_count - 1; |
---|
23 | |
---|
24 | // repository |
---|
25 | this.repository = 1; |
---|
26 | this.options = null; |
---|
27 | this.time_created = null; |
---|
28 | |
---|
29 | this.format = 'htmltable'; |
---|
30 | this.columns_widget = null; |
---|
31 | }; |
---|
32 | |
---|
33 | Query.prototype.load = function(json) { |
---|
34 | //var collection_text = Query.collectiontext(json); |
---|
35 | |
---|
36 | var columns_text = ""; |
---|
37 | if (json.columns != "null"){ |
---|
38 | $.each(json.columns,function(i,item){ |
---|
39 | columns_text = columns_text + "," + item; |
---|
40 | }); |
---|
41 | if (columns_text.length > 1) { |
---|
42 | columns_text = columns_text.substring(1); |
---|
43 | } |
---|
44 | } |
---|
45 | this.columns = columns_text; |
---|
46 | |
---|
47 | var collection = []; |
---|
48 | if (json.collections != "null"){ |
---|
49 | $.each(json.collections,function(i,item){ |
---|
50 | var coll = new Collection(item.index, item.name); |
---|
51 | collection[i] = coll; |
---|
52 | }); |
---|
53 | } |
---|
54 | this.collection = collection; |
---|
55 | if (json["querystring"] == "[NULL]"){ |
---|
56 | this.query = null; |
---|
57 | } else { |
---|
58 | this.query = json["querystring"]; |
---|
59 | } |
---|
60 | if (json["squerystring"] == "[NULL]"){ |
---|
61 | this.squery = null; |
---|
62 | } else { |
---|
63 | this.squery = json["squerystring"]; |
---|
64 | } |
---|
65 | this.columns = columns_text; |
---|
66 | |
---|
67 | var options_text = ""; |
---|
68 | if (json.options == undefined) { |
---|
69 | this.options = null; |
---|
70 | } else { |
---|
71 | if (json.options != "null"){ |
---|
72 | $.each(json.options,function(i,item){ |
---|
73 | options_text = options_text + "," + item; |
---|
74 | }); |
---|
75 | if (options_text.length > 1) { |
---|
76 | options_text = options_text.substring(1); |
---|
77 | } |
---|
78 | this.options = options_text; |
---|
79 | } else { |
---|
80 | this.options = null; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | if (json.time == undefined){ |
---|
85 | this.time_created = null; |
---|
86 | } else { |
---|
87 | this.time_created = json.time; |
---|
88 | } |
---|
89 | }; |
---|
90 | |
---|
91 | Query.prototype.getcolumnstext = function (){ |
---|
92 | |
---|
93 | if (this.columns_widget == null ) { |
---|
94 | return this.columns; |
---|
95 | } |
---|
96 | return this.columns_widget.getColumnsListText(); |
---|
97 | }; |
---|
98 | |
---|
99 | Query.prototype.updatecolumnstext = function (list) { |
---|
100 | var columns_text = ""; |
---|
101 | $(list).each(function(index){ |
---|
102 | if (index > 0) { |
---|
103 | columns_text = columns_text + ","; |
---|
104 | } |
---|
105 | columns_text = columns_text + $(this).text(); |
---|
106 | }); |
---|
107 | this.columns = columns_text; |
---|
108 | }; |
---|
109 | |
---|
110 | /** special handling for special characters: double escaping (escape the %-sign) |
---|
111 | * to survive the %-encoding through the request (and parsing) down to the transformation in XCQL2XPath.xsl |
---|
112 | * it's: whitespace, and single and double-quotes (unified to double quotes %22) |
---|
113 | */ |
---|
114 | |
---|
115 | Query.prototype.query_uri = function () { |
---|
116 | var params = "?"; |
---|
117 | if (this.query != null) { |
---|
118 | params = params + "q=" + escape(this.query) + "&"; |
---|
119 | } |
---|
120 | if (this.squery != null) { |
---|
121 | escaped_sq =escape(this.squery).replace(/%20/g,"%2520").replace(/\%2[27]/g,"%2522") ; |
---|
122 | escaped_sq = escaped_sq.replace(/%7C/g,"%257C").replace(/\+/g,"%2B"); |
---|
123 | params = params + "squery=" + escaped_sq + "&"; |
---|
124 | } |
---|
125 | params = params + "collection=" + this.getcollectiontext("index") + "&columns=" + this.getcolumnstext() + "&startItem=" + this.startItem + "&maximumItems=" + this.maximumItems + "&repository=" + this.repository; |
---|
126 | if (this.options != null) { |
---|
127 | params = params + "&options=withSummary"; |
---|
128 | } |
---|
129 | |
---|
130 | return params; |
---|
131 | |
---|
132 | //return $.param(this.query); |
---|
133 | }; |
---|
134 | |
---|
135 | Query.prototype.query_json = function () { |
---|
136 | var jsonq = {"squerystring":this.squery, |
---|
137 | "querystring":this.query, |
---|
138 | "collections" : this.getcollectiontext("index"), |
---|
139 | "columns" : this.columns, |
---|
140 | "startItem" : this.startItem, |
---|
141 | "maximumItems" : this.maximumItems}; |
---|
142 | return JSON.stringify(jsonq); |
---|
143 | }; |
---|
144 | /* |
---|
145 | Query.prototype.getcolumnstext = function(what) { |
---|
146 | var columns_text = ""; |
---|
147 | for (var i = 0; i < this.columns.length; i++) { |
---|
148 | if (what == "index") { |
---|
149 | columns_text = columns_text + "," + this.columns[i]; |
---|
150 | } else { |
---|
151 | columns_text = columns_text + "," + this.columns[i]; |
---|
152 | } |
---|
153 | } |
---|
154 | if (columns_text.length > 1) { |
---|
155 | columns_text = columns_text.substring(1); |
---|
156 | } |
---|
157 | return columns_text; |
---|
158 | }; |
---|
159 | */ |
---|
160 | |
---|
161 | Query.prototype.getcollectiontext = function(what) { |
---|
162 | var collection_text = ""; |
---|
163 | for (var i = 0; i < this.collection.length; i++) { |
---|
164 | if (what == "index") { |
---|
165 | collection_text = collection_text + "," + this.collection[i].index; |
---|
166 | } else { |
---|
167 | collection_text = collection_text + "," + this.collection[i].name; |
---|
168 | } |
---|
169 | } |
---|
170 | if (collection_text.length > 1) { |
---|
171 | collection_text = collection_text.substring(1); |
---|
172 | } |
---|
173 | return collection_text; |
---|
174 | }; |
---|
175 | Query.prototype.getcollectionindextext = function() { |
---|
176 | var collection_text = ""; |
---|
177 | for (var i = 0; i < this.collection.length; i++) { |
---|
178 | collection_text = collection_text + "," + this.collection[i].index; |
---|
179 | } |
---|
180 | if (collection_text.length > 1) { |
---|
181 | collection_text = collection_text.substring(1); |
---|
182 | } |
---|
183 | return collection_text; |
---|
184 | }; |
---|
185 | |
---|
186 | Query.collectiontext = function(json) { |
---|
187 | var collection_text = ""; |
---|
188 | if (json.collections != "null"){ |
---|
189 | $.each(json.collections,function(i,item){ |
---|
190 | collection_text = collection_text + "," + item.name; |
---|
191 | }); |
---|
192 | if (collection_text.length > 1) { |
---|
193 | collection_text = collection_text.substring(1); |
---|
194 | } |
---|
195 | } |
---|
196 | return collection_text; |
---|
197 | }; |
---|
198 | |
---|
199 | Query.columnstext = function(json) { |
---|
200 | var columns_text = ""; |
---|
201 | if (json.columns != "null"){ |
---|
202 | $.each(json.columns,function(i,item){ |
---|
203 | columns_text = columns_text + "," + item; |
---|
204 | }); |
---|
205 | if (columns_text.length > 1) { |
---|
206 | columns_text = columns_text.substring(1); |
---|
207 | } |
---|
208 | } |
---|
209 | return columns_text; |
---|
210 | }; |
---|
211 | |
---|
212 | Query.optionstext = function(json) { |
---|
213 | var options_text = ""; |
---|
214 | if (json.options == undefined){ |
---|
215 | return ""; |
---|
216 | } |
---|
217 | if (json.options != "null"){ |
---|
218 | $.each(json.options,function(i,item){ |
---|
219 | options_text = options_text + "," + item; |
---|
220 | }); |
---|
221 | if (options_text.length > 1) { |
---|
222 | options_text = options_text.substring(1); |
---|
223 | } |
---|
224 | } |
---|
225 | return options_text; |
---|
226 | }; |
---|
227 | |
---|
228 | Query.fullformatstring = function (json) { |
---|
229 | var qs = json.querystring; |
---|
230 | var sqs = json.squerystring; |
---|
231 | |
---|
232 | if (qs == "[NULL]") { |
---|
233 | qs = null; |
---|
234 | } |
---|
235 | if (sqs == "[NULL]") { |
---|
236 | sqs = null; |
---|
237 | } |
---|
238 | var collection_text = Query.collectiontext(json); |
---|
239 | |
---|
240 | var full_str = ""; |
---|
241 | var len = formatquerystring_len; |
---|
242 | |
---|
243 | if (sqs != null) { |
---|
244 | if (sqs.length > len){ |
---|
245 | full_str = sqs.substring(0,len) + "..| "; |
---|
246 | } else { |
---|
247 | sqs = sqs + " "; |
---|
248 | full_str = sqs.substring(0,len) + " | "; |
---|
249 | } |
---|
250 | } |
---|
251 | if (qs != null) { |
---|
252 | qs = Query.simplequerystring(qs); |
---|
253 | if (qs.length > len){ |
---|
254 | full_str = full_str + qs.substring(0,len) + "..| "; |
---|
255 | } else { |
---|
256 | qs = qs + " "; |
---|
257 | full_str = full_str + qs.substring(0,len) + " | "; |
---|
258 | } |
---|
259 | } |
---|
260 | if (collection_text.length > (len - 6)) { |
---|
261 | full_str = full_str + collection_text.substring(0,len); |
---|
262 | } else { |
---|
263 | full_str = full_str + collection_text; |
---|
264 | } |
---|
265 | |
---|
266 | return full_str; |
---|
267 | }; |
---|
268 | |
---|
269 | Query.queryliststring = function(squery, query) { |
---|
270 | var s_squery = ""; |
---|
271 | var s_query = ""; |
---|
272 | var str = ""; |
---|
273 | |
---|
274 | if (squery != null){ |
---|
275 | s_squery = squery; |
---|
276 | } |
---|
277 | if (query != null){ |
---|
278 | s_query = query; |
---|
279 | } |
---|
280 | if (s_squery.length != 0 && s_query.length != 0){ |
---|
281 | str = squery + " & (" + Query.simplequerystring(query) + ")"; |
---|
282 | } else if (s_squery.length != 0) { |
---|
283 | str = squery; |
---|
284 | } else { |
---|
285 | str = Query.simplequerystring(query); |
---|
286 | } |
---|
287 | return str; |
---|
288 | }; |
---|
289 | |
---|
290 | Query.simplequerystring = function (querystring) { |
---|
291 | |
---|
292 | if (querystring == null){ |
---|
293 | return ""; |
---|
294 | } |
---|
295 | querystring = Url.decode(querystring); |
---|
296 | |
---|
297 | var arr_and = querystring.split(" and "); |
---|
298 | var simple_form = ""; |
---|
299 | var simple_form_all = ""; |
---|
300 | var rel = ""; |
---|
301 | |
---|
302 | for( var i=0;i<arr_and.length;i++){ |
---|
303 | arr_and[i] = $.trim(arr_and[i]); |
---|
304 | var arr_or = arr_and[i].split(" or "); |
---|
305 | simple_form = ""; |
---|
306 | for( var j=0;j<arr_or.length;j++){ |
---|
307 | arr_or[j] = $.trim(arr_or[j]); |
---|
308 | while (arr_or[j].substring(0,1) == "(" ) { |
---|
309 | arr_or[j] = arr_or[j].substring(1,arr_or[j].length); |
---|
310 | arr_or[j] = $.trim(arr_or[j]); |
---|
311 | } |
---|
312 | while ( arr_or[j].substring(arr_or[j].length-1) == ")"){ |
---|
313 | arr_or[j] = arr_or[j].substring(0,arr_or[j].length-1); |
---|
314 | arr_or[j] = $.trim(arr_or[j]); |
---|
315 | } |
---|
316 | if (j > 0) { |
---|
317 | rel = " or "; |
---|
318 | } else { |
---|
319 | rel = ""; |
---|
320 | } |
---|
321 | simple_form = simple_form + rel + arr_or[j]; |
---|
322 | } |
---|
323 | if (arr_or.length > 1){ |
---|
324 | simple_form = "(" + simple_form + ") "; |
---|
325 | } |
---|
326 | if (i > 0) { |
---|
327 | rel = " and "; |
---|
328 | } else { |
---|
329 | rel = ""; |
---|
330 | } |
---|
331 | simple_form_all = simple_form_all + rel + simple_form; |
---|
332 | |
---|
333 | } |
---|
334 | |
---|
335 | |
---|
336 | //notifyUser("querystring:" + querystring, 'debug'); |
---|
337 | //notifyUser("simplequerystring:" + simple_form_all, 'debug'); |
---|
338 | return simple_form_all; |
---|
339 | }; |
---|
340 | |
---|
341 | Query.prototype.render = function () { |
---|
342 | var x = "<div id='" + this.listid + "' class='query_wrapper ui-widget' ><div class='query_header ui-widget-header ui-state-default ui-corner-top'>" + |
---|
343 | "<span class='cmd cmd_get'></span><span class='cmd cmd_save'></span><span class='cmd cmd_del'> </span>" + |
---|
344 | "<span class='query_id'>" + this.listid + "</span>: <span class='query'>" + |
---|
345 | this.getcollectiontext("name") + ":" + Query.queryliststring(this.squery, this.query) + ", repository:" + this.repository.toString() + "</span>" + |
---|
346 | "<a class='cmd cmd_xml' target='_blank' href='" + this.link("xml") + "'>xml</a> <a class='cmd cmd_link' target='_blank' href='" + this.link("fullpage") + "' >link</a></div>" + |
---|
347 | // <span class='cmd cmd-xml'></span><span class='cmd cmd-page'></span><span class='cmd cmd-link'></span></div>" + |
---|
348 | //"<div class='ui-context-dialog columns-wrapper'><div class='query-columns'></div><span class='cmd cmd_del'></span></div><div class='result ui-widget-content ui-corner-bottom'></div>"; |
---|
349 | "<div class='result ui-widget-content ui-corner-bottom'></div>"; |
---|
350 | addToQuerylist(x); |
---|
351 | |
---|
352 | this.container = $('#' + this.listid ); |
---|
353 | |
---|
354 | // this.columns_widget = new ListWidget($(this.container).find('.query-columns')); |
---|
355 | //$(this.container).find('.query-columns').append(this.columns_widget); |
---|
356 | |
---|
357 | //this.columns_widget.load(this.columns.split(',')); |
---|
358 | //this.columns_widget.initAutocomplete(element_autocomplete); |
---|
359 | //$(this.container).find('.columns-wrapper').hide(); |
---|
360 | |
---|
361 | notifyUser("DEBUG: setting up removing query:" + $(this).closest('.query_wrapper').attr('id')); |
---|
362 | $(this.container).children('.query_header').find('.cmd_del').click(function(event) { |
---|
363 | notifyUser("DEBUG: removing query:" + $(this).closest('.query_wrapper').attr('id')); |
---|
364 | queryset.removequery($(this).closest('.query_wrapper').attr('id')); |
---|
365 | }); |
---|
366 | |
---|
367 | /* |
---|
368 | $(this.container).children('.columns-wrapper').children('.cmd_del').live('click', function(event) { |
---|
369 | $(this).parent().hide(); |
---|
370 | }); |
---|
371 | */ |
---|
372 | createTooltip(this.container); |
---|
373 | }; |
---|
374 | |
---|
375 | Query.prototype.open = function (type) { |
---|
376 | if (type != null) |
---|
377 | window.open(this.link(type)); |
---|
378 | else |
---|
379 | window.open(this.link()); |
---|
380 | }; |
---|
381 | |
---|
382 | Query.prototype.link = function (type) { |
---|
383 | var uri=""; |
---|
384 | if (type=="fullpage") { |
---|
385 | //var jsonq = {"squerystring":this.squery, "querystring":this.query, "searchclauses":"null" , "collections" : this.json_coll, "columns" : this.json_cols}; |
---|
386 | //uri = "?query=" + JSON.stringify(jsonq) + "&startItem=" + this.startItem + "&maximumItems=" + this.maximumItems; |
---|
387 | //uri = this.query_json(); |
---|
388 | uri = link('base',this.query_uri()); |
---|
389 | } else { |
---|
390 | uri = link('recordset',type, this.query_uri()); |
---|
391 | } |
---|
392 | return uri; |
---|
393 | }; |
---|
394 | |
---|
395 | Query.prototype.link_obsoleted = function () { |
---|
396 | |
---|
397 | // JSON conversion |
---|
398 | /* |
---|
399 | if (this.query.length == 0){ |
---|
400 | xqstring = "[NULL]"; |
---|
401 | } else { |
---|
402 | xqstring = this.query; |
---|
403 | } |
---|
404 | if (this.squery.length == 0){ |
---|
405 | xsquery = "[NULL]"; |
---|
406 | } else { |
---|
407 | xsquery = this.squery; |
---|
408 | } |
---|
409 | */ |
---|
410 | var jsonq = {"squerystring":this.squery, "querystring":this.query, "searchclauses":"null" , "collections" : this.json_coll, "columns" : this.json_cols}; |
---|
411 | |
---|
412 | var uri = "?query=" + JSON.stringify(jsonq) + "&startItem=" + this.startItem + "&maximumItems=" + this.maximumItems; |
---|
413 | return uri; |
---|
414 | }; |
---|
415 | |
---|
416 | Query.prototype.submit = function () { |
---|
417 | |
---|
418 | |
---|
419 | var uri = link('recordset',this.format, this.query_uri()); |
---|
420 | |
---|
421 | notifyUser("submitting query:" + uri); |
---|
422 | this.container.find('.result').load( uri, function() { |
---|
423 | notifyUser("result-loaded",'debug'); |
---|
424 | |
---|
425 | var get = $(this).parent().find('.cmd_get'); |
---|
426 | |
---|
427 | get.removeClass('cmd_get'); |
---|
428 | get.addClass('cmd_up'); |
---|
429 | // get.show(); |
---|
430 | |
---|
431 | var result_header = $(this).children('.result-header'); |
---|
432 | |
---|
433 | var q_header; |
---|
434 | q_header = $(this).parent().find('.query_header'); |
---|
435 | q_header.append(result_header); |
---|
436 | q_header.find('.cmd_reload').click(function() { |
---|
437 | // reload from columns-wrapper |
---|
438 | if ($(this).parent().attr('class').indexOf("columns-wrapper") > 0) { |
---|
439 | $(this).closest('.columns-wrapper').hide(); |
---|
440 | } |
---|
441 | var qid = $(this).closest('.query_wrapper').attr("id"); |
---|
442 | queryset.resubmit(qid); |
---|
443 | }); |
---|
444 | q_header.find('.cmd_prev').click(function() { |
---|
445 | var qid = $(this).closest('.query_wrapper').attr("id"); |
---|
446 | queryset.next(qid,-1); |
---|
447 | }); |
---|
448 | q_header.find('.cmd_next').click(function() { |
---|
449 | var qid = $(this).closest('.query_wrapper').attr("id"); |
---|
450 | queryset.next(qid,1); |
---|
451 | }); |
---|
452 | |
---|
453 | q_header.find('.value-format').change(function(){ |
---|
454 | var qid = $(this).closest('.query_wrapper').attr("id"); |
---|
455 | var q = queryset.getQuery(qid); |
---|
456 | q.format = $(this).find('option:selected').text().trim(); |
---|
457 | queryset.resubmit(qid); |
---|
458 | |
---|
459 | }); |
---|
460 | q_header.find('.cmd_columns').live('click', function(event) { |
---|
461 | notifyUser("DEBUG: submit() .cmd_columns"); |
---|
462 | $(this).closest('.query_wrapper').find('.columns-wrapper').toggle(); |
---|
463 | }); |
---|
464 | |
---|
465 | q_header.find('.cmd_add').click(function(){ |
---|
466 | |
---|
467 | query_wrapper_add_column($(this)); |
---|
468 | }); |
---|
469 | |
---|
470 | q_header.find('.columns-wrapper').hide(); |
---|
471 | q_header.find('.terms-tree').treeTable({initialState:"collapsed"}); |
---|
472 | createTooltip($(this)); |
---|
473 | }); |
---|
474 | |
---|
475 | }; |
---|
476 | |
---|
477 | Query.prototype.resubmit = function () { |
---|
478 | |
---|
479 | var uri = link('recordset',this.format, this.query_uri()); |
---|
480 | var qid = this.listid; |
---|
481 | var q_uri = this.query_uri(); |
---|
482 | |
---|
483 | notifyUser("resubmitting query:" + uri); |
---|
484 | |
---|
485 | var get = $('#' + qid ).find('.cmd_up'); |
---|
486 | if (get.length == 0) { |
---|
487 | get = $('#' + qid ).find('.cmd_down'); |
---|
488 | } |
---|
489 | get.addClass('cmd_get'); |
---|
490 | get.removeClass('cmd_up'); |
---|
491 | get.removeClass('cmd_down'); |
---|
492 | // get.show(); |
---|
493 | |
---|
494 | $('#' + qid ).children('.result').children().remove(); |
---|
495 | $('#' + qid ).find('.result').load( uri, function() { |
---|
496 | // update link-hrefs |
---|
497 | var qid = $(this).closest('.query_wrapper').attr("id"); |
---|
498 | notifyUser("result-loaded ID:" + qid,'debug'); |
---|
499 | |
---|
500 | var q = queryset.getQuery(qid); |
---|
501 | $('#' + qid ).find(".cmd_link").attr("href", q.link("fullpage")); |
---|
502 | |
---|
503 | var get = $(this).parent().find('.cmd_get'); |
---|
504 | get.removeClass('cmd_get'); |
---|
505 | get.addClass('cmd_up'); |
---|
506 | // get.show(); |
---|
507 | |
---|
508 | var result_header = $(this).children('.result-header'); |
---|
509 | |
---|
510 | var q_header; |
---|
511 | q_header = $(this).parent().find('.query_header'); |
---|
512 | q_header.children('.result-header').remove(); |
---|
513 | |
---|
514 | q_header.append(result_header); |
---|
515 | q_header.find('.cmd_reload').click(function() { |
---|
516 | // reload from columns-wrapper |
---|
517 | if ($(this).parent().attr('class').indexOf("columns-wrapper") > 0) { |
---|
518 | $(this).closest('.columns-wrapper').hide(); |
---|
519 | } |
---|
520 | var qid = $(this).closest('.query_wrapper').attr("id"); |
---|
521 | queryset.resubmit(qid); |
---|
522 | }); |
---|
523 | q_header.find('.cmd_prev').click(function() { |
---|
524 | var qid = $(this).closest('.query_wrapper').attr("id"); |
---|
525 | queryset.next(qid,-1); |
---|
526 | }); |
---|
527 | q_header.find('.cmd_next').click(function() { |
---|
528 | var qid = $(this).closest('.query_wrapper').attr("id"); |
---|
529 | queryset.next(qid,1); |
---|
530 | }); |
---|
531 | q_header.find('.value-format').change(function(){ |
---|
532 | var qid = $(this).closest('.query_wrapper').attr("id"); |
---|
533 | var q = queryset.getQuery(qid); |
---|
534 | q.format = $(this).find('option:selected').text().trim(); |
---|
535 | queryset.resubmit(qid); |
---|
536 | |
---|
537 | }); |
---|
538 | q_header.find('.cmd_columns').live('click', function(event) { |
---|
539 | notifyUser("DEBUG: resubmit() .cmd_columns"); |
---|
540 | $(this).closest('.query_wrapper').find('.columns-wrapper').toggle(); |
---|
541 | }); |
---|
542 | |
---|
543 | q_header.find('.cmd_add').click(function(){ |
---|
544 | query_wrapper_add_column($(this)); |
---|
545 | }); |
---|
546 | |
---|
547 | q_header.find('.columns-wrapper').hide(); |
---|
548 | q_header.find('.terms-tree').treeTable({initialState:"collapsed"}); |
---|
549 | }); |
---|
550 | |
---|
551 | }; |
---|
552 | |
---|
553 | |
---|
554 | |
---|
555 | var queryset_container = $("#querylist"); |
---|
556 | |
---|
557 | var queryset = { queries: [], |
---|
558 | container: '#querylist', |
---|
559 | |
---|
560 | addquery: function (query){ |
---|
561 | |
---|
562 | this.queries[this.queries.length] = query; |
---|
563 | query.listid = "q" + this.queries.length; |
---|
564 | query.render(); |
---|
565 | query.submit(); |
---|
566 | //$('#querylist').html(this.render()); |
---|
567 | }, |
---|
568 | |
---|
569 | removequery: function (qid) { |
---|
570 | notifyUser("removing query:" + qid); |
---|
571 | |
---|
572 | for (var i = 0; i < this.queries.length; i++) { |
---|
573 | if (this.queries[i].listid == qid) { |
---|
574 | this.queries.splice(i, 1); |
---|
575 | } |
---|
576 | } |
---|
577 | $('#' + qid).remove(); |
---|
578 | notifyUser("query removed, new queries.length:" + this.queries.length); |
---|
579 | |
---|
580 | }, |
---|
581 | |
---|
582 | getsquery: function(qid) { |
---|
583 | var qstring = ""; |
---|
584 | |
---|
585 | for (var i = 0; i < this.queries.length; i++) { |
---|
586 | if (this.queries[i].listid == qid) { |
---|
587 | qstring = this.queries[i].squery; |
---|
588 | } |
---|
589 | } |
---|
590 | if (qstring == null){ |
---|
591 | qstring = ""; |
---|
592 | } |
---|
593 | return qstring; |
---|
594 | }, |
---|
595 | getquerystring: function(qid) { |
---|
596 | var qstring = ""; |
---|
597 | |
---|
598 | for (var i = 0; i < this.queries.length; i++) { |
---|
599 | if (this.queries[i].listid == qid) { |
---|
600 | qstring = this.queries[i].query; |
---|
601 | } |
---|
602 | } |
---|
603 | if (qstring == null){ |
---|
604 | qstring = ""; |
---|
605 | } |
---|
606 | |
---|
607 | return qstring; |
---|
608 | }, |
---|
609 | getcollections: function(qid) { |
---|
610 | var coll = ""; |
---|
611 | var json_coll, json_temp; |
---|
612 | |
---|
613 | for (var i = 0; i < this.queries.length; i++) { |
---|
614 | if (this.queries[i].listid == qid) { |
---|
615 | coll = this.queries[i].collection; |
---|
616 | } |
---|
617 | } |
---|
618 | if (coll.length == 0) { |
---|
619 | json_coll = {}; |
---|
620 | json_coll = "null"; |
---|
621 | } else { |
---|
622 | json_coll = [{}]; |
---|
623 | for (var i = 0; i < coll.length; i++) { |
---|
624 | json_coll[i] = {"index" : coll[i].index, "name" :coll[i].name}; |
---|
625 | } |
---|
626 | } |
---|
627 | return json_coll; |
---|
628 | }, |
---|
629 | |
---|
630 | getcolumns: function(qid) { |
---|
631 | var cols = ""; |
---|
632 | var json_cols, json_temp; |
---|
633 | |
---|
634 | for (var i = 0; i < this.queries.length; i++) { |
---|
635 | if (this.queries[i].listid == qid) { |
---|
636 | cols = this.queries[i].columns; |
---|
637 | } |
---|
638 | } |
---|
639 | |
---|
640 | if (cols == "") { |
---|
641 | json_cols = {}; |
---|
642 | json_cols = "null"; |
---|
643 | } else { |
---|
644 | var pos = cols.indexOf(",", 0); |
---|
645 | var i = 0; |
---|
646 | |
---|
647 | json_cols = [{}]; |
---|
648 | while (pos > -1){ |
---|
649 | if (pos > -1){ |
---|
650 | json_cols[i] = cols.substring(0,pos); |
---|
651 | } else { |
---|
652 | json_cols[i] = cols; |
---|
653 | } |
---|
654 | cols = cols.substring(pos+1); |
---|
655 | pos = cols.indexOf(",", 0); |
---|
656 | i = i+1; |
---|
657 | } |
---|
658 | |
---|
659 | json_cols[i] = cols; |
---|
660 | |
---|
661 | |
---|
662 | } |
---|
663 | return json_cols; |
---|
664 | }, |
---|
665 | getoptions: function(qid) { |
---|
666 | var opts = null; |
---|
667 | var json_opts; |
---|
668 | |
---|
669 | for (var i = 0; i < this.queries.length; i++) { |
---|
670 | if (this.queries[i].listid == qid) { |
---|
671 | opts = this.queries[i].options; |
---|
672 | } |
---|
673 | } |
---|
674 | |
---|
675 | if (opts != null) { |
---|
676 | json_opts = [{}]; |
---|
677 | json_opts[0] = opts; |
---|
678 | } |
---|
679 | return json_opts; |
---|
680 | }, |
---|
681 | resubmit: function(qid){ |
---|
682 | var query = queryset.queries[qid.substring(1)-1]; |
---|
683 | |
---|
684 | query.repository = parseInt(getSelectedRepository()); |
---|
685 | query.startItem = $('#' + qid ).find('.start_record').val(); |
---|
686 | query.maximumItems = $('#' + qid ).find('.maximum_records').val(); |
---|
687 | query.resubmit(); |
---|
688 | |
---|
689 | }, |
---|
690 | |
---|
691 | getQuery: function(qid){ |
---|
692 | var query = queryset.queries[qid.substring(1)-1]; |
---|
693 | return query; |
---|
694 | }, |
---|
695 | |
---|
696 | next: function(qid,pages){ |
---|
697 | var query = queryset.queries[qid.substring(1)-1]; |
---|
698 | var start = 0; |
---|
699 | var num = 0; |
---|
700 | var max_value = $('#' + qid ).find('.result-header').attr("max_value"); |
---|
701 | |
---|
702 | if (parseInt(query.startItem) + pages * page_record_count >= 1){ |
---|
703 | start = parseInt(query.startItem) + pages * page_record_count ; |
---|
704 | } else if (parseInt(query.startItem) + pages * page_record_count + page_record_count - 1 >= 1){ |
---|
705 | start = 1 ; |
---|
706 | } |
---|
707 | if (start > 0){ |
---|
708 | if (start + page_record_count - 1 <= max_value) { |
---|
709 | num = page_record_count; |
---|
710 | } else if (start <= max_value){ |
---|
711 | num = max_value - start; |
---|
712 | } |
---|
713 | |
---|
714 | if (num > 0){ |
---|
715 | query.startItem = start; |
---|
716 | query.maximumItems = num; |
---|
717 | query.resubmit(); |
---|
718 | } |
---|
719 | } |
---|
720 | |
---|
721 | //$('#' + qid ).find('.start_record').val(query.startItem); |
---|
722 | //$('#' + qid ).find('.maximum_records').val(query.maximumItems); |
---|
723 | //var max_results = |
---|
724 | |
---|
725 | } |
---|
726 | |
---|
727 | |
---|
728 | }; |
---|
729 | |
---|
730 | $('#querylist .cmd_columns').live('click',function(){ |
---|
731 | notifyUser("DEBUG: #querylist.cmd_columns"); |
---|
732 | $(this).closest('.query_wrapper').find('.columns-wrapper').toggle(); |
---|
733 | }); |
---|
734 | |
---|
735 | |
---|
736 | $('.cmd_del').click(function(){ |
---|
737 | |
---|
738 | if ($(this).parent().parent().attr("class") == "query-columnslist"){ |
---|
739 | var list = $(this).closest('.query-columns').children('.query-columnslist'); |
---|
740 | var qid = $(this).closest('.query_wrapper').attr("id"); |
---|
741 | var q = queryset.getQuery(qid); |
---|
742 | |
---|
743 | $(this).parent().remove(); |
---|
744 | q.updatecolumnstext($(list).children()); |
---|
745 | |
---|
746 | } |
---|
747 | if ($(this).parent().attr('class') == 'columns-wrapper'){ |
---|
748 | $(this).parent().hide(); |
---|
749 | } |
---|
750 | }); |
---|
751 | |
---|
752 | function query_wrapper_add_column(elem){ |
---|
753 | var col = '<li>' + $(elem).next().text() + '<span class="cmd cmd_del" /></li>'; |
---|
754 | $(elem).closest('.query-columns').find('.query-columnslist').append(col); |
---|
755 | |
---|
756 | // add to query columnslist // columns_widget not used |
---|
757 | var qid = $(elem).closest('.query_wrapper').attr("id"); |
---|
758 | var q = queryset.getQuery(qid); |
---|
759 | //q.columns_widget.add($(this).text()); |
---|
760 | q.updatecolumnstext($(elem).closest('.query-columns').children('.query-columnslist').children()); |
---|
761 | /* |
---|
762 | var columns_text = ""; |
---|
763 | $(elem).closest('.query-columns').children('.query-columnslist').children().each(function(index){ |
---|
764 | if (index > 0) { |
---|
765 | columns_text = columns_text + ","; |
---|
766 | } |
---|
767 | columns_text = columns_text + $(this).text(); |
---|
768 | }); |
---|
769 | q.columns = columns_text; |
---|
770 | */ |
---|
771 | } |
---|