source: MDService2/branches/MDService_simple3/WebContent/scripts/mdservice_searchclause.js @ 1918

Last change on this file since 1918 was 1918, checked in by gaba, 12 years ago

autocomplete - explain.xml

File size: 31.1 KB
Line 
1/**
2 * @fileOverview  This file contains model for building searchclausesets used in user queries.
3 * Consists of two parts:
4 * - SearchClause model [index,relation, value], with basic functionality -
5 * creation, render, autocomplete.
6 * - Searchclauseset - container of actually used searchclauses in app
7 * with basic functionality for building container - add, remove, clear
8 * and conversion functions - conversions from-to querystring
9 *
10 * @author
11 * @version
12 */
13
14// client-side modelling of "business-objects":
15// SearchClause, SearchClauseset,
16
17 
18 //TODO use select-options or autocomplete ???
19 //var  relation_autocomplete = new Array('any','all','=','>','<');
20 //var  value_autocomplete = new Array();
21
22/**
23 * SearchClause model [index,relation, value], with basic functionality -
24 * creation, render, autocomplete.
25 * @constructor
26*/ 
27function SearchClause(index, relation, value ) {
28        this.index = index;
29        this.relation = relation;       
30        this.value = value;
31        this.negation  = false;
32       
33        this.is_category = false;
34        this.category = "";
35       
36        this.i = 0;
37        this.j = 0;
38        this.listid = "";
39        this.container = {};
40       
41        if (this.relation == "") {
42                this.relation = "=";
43        }
44       
45        this.is_complex = true;
46}
47
48SearchClause.prototype.GetIndexInput = function(){
49        return $($($('.sc-i','#searchclauselist')[this.i]).find('.sc-j')[this.j]).find('.index-input');
50};
51
52SearchClause.prototype.GetRelInput = function(){
53        return $($($('.sc-i','#searchclauselist')[this.i]).find('.sc-j')[this.j]).find('.rel_input');
54};
55SearchClause.prototype.GetValueInput = function(){
56        return $($($('.sc-i','#searchclauselist')[this.i]).find('.sc-j')[this.j]).find('.value-input');
57};
58
59SearchClause.prototype.GetIndexSelect = function(){
60        return $($($('.sc-i','#searchclauselist')[this.i]).find('.sc-j')[this.j]).find('.index_select');
61};
62SearchClause.prototype.GetIndexInputTermset = function(){
63        return $($($('.sc-i','#searchclauselist')[this.i]).find('.sc-j')[this.j]).find('.index-input-termset');
64};
65SearchClause.prototype.GetIndexInput = function(){
66        return $($($('.sc-i','#searchclauselist')[this.i]).find('.sc-j')[this.j]).find('.index-input');
67};
68
69
70SearchClause.prototype.GetValueSelect = function(){
71        return $($($('.sc-i','#searchclauselist')[this.i]).find('.sc-j')[this.j]).find('.value_select');
72};
73
74SearchClause.prototype.render = function (rel) {
75       
76        //TODO defaults
77        //this.relation = '=';
78        //var index_detail = "";
79        //if ((this.i == 0) && (this.j == 0)) {
80        //      index_detail = "<span name='detail_index' class='cmd cmd_detail'></span>";
81        //}
82        var x = "<div id='" + this.j + "' class='sc-j or_level' ><div id='" + this.listid + "' class='sc-wrapper focused' > " +
83        "<div class='search'><span class='cmd cmd_sc_autocomplete cmd_sca_explain'></span><span class='index-search'><select class='index_select'/><input type='text' class='index-input-termset autocomplete-input' /><input type='text' class='index-input autocomplete-input' /></span>" + 
84        //index_detail +
85        //"<select type='text' class='rel_input' />" +
86        "<select class='rel_input'><option value='='>=</option><option value='>'>></option><option value='<'><</option><option value='any'>any</option><option value='contains'>contains</option><option value='all'>all</option></select>" + 
87        "<select class='value_select'/><input type='text' class='value-input' /><span name='detail_value' class='cmd cmd_detail'></span>" +
88         "<span class='sc_cmds'><span class='cmd cmd_sc_delete'> </span><span class='sc_id'>" + this.listid + "</span>" + 
89          "<span class='cmd cmd_add_and'> </span><span class='cmd cmd_add_or'></span>"+
90        " </span></div><div class='index-context'><table></table></div></div></div>";
91       
92        addToSClist(x, this.i, this.j, rel);   
93       
94       
95        var e_index = this.GetIndexInput();
96        //new $.GrowingInput(e_index, {max: 80});
97        var e_relation = this.GetRelInput();
98        var e_value = this.GetValueInput();
99        var index_select = this.GetIndexSelect();
100        //var index_input_termset = this.GetIndexInputTermset();
101        var value_select = this.GetValueSelect();
102       
103        $(index_select).hide();
104        //$(index_input_term).hide();
105        this.GetIndexInput().hide();
106        value_select.hide();
107        //$(e_index).closest('.sc-wrapper').find('.index-context').hide();
108       
109        //alert("focused: "  + $('.focused','#searchclauselist').size());
110        $('.sc-wrapper','#searchclauselist').each(function(){
111                $(this).removeClass("focused");
112        });
113        $(e_index).closest('.sc-wrapper').addClass("focused");
114        //alert("focused: "  + $('.focused','#searchclauselist').size());
115       
116        // set focus
117        e_index.focusin(function(){
118                $('.sc-wrapper','#searchclauselist').each(function(){
119                        $(this).removeClass("focused");
120                });
121                $(this).closest('.sc-wrapper').addClass("focused");
122        });
123        index_select.focusin(function(){
124                $('.sc-wrapper','#searchclauselist').each(function(){
125                        $(this).removeClass("focused");
126                });
127                $(this).closest('.sc-wrapper').addClass("focused");
128        });
129        e_relation.focusin(function(){
130                $('.sc-wrapper','#searchclauselist').each(function(){
131                        $(this).removeClass("focused");
132                });
133                $(this).closest('.sc-wrapper').addClass("focused");
134        });
135        e_value.focusin(function(){
136                $('.sc-wrapper','#searchclauselist').each(function(){
137                        $(this).removeClass("focused");
138                });
139                $(this).closest('.sc-wrapper').addClass("focused");
140        });
141        $($($('.sc-i','#searchclauselist')[this.i]).find('.sc-j')[this.j]).find('.cmd_detail').click(function(){
142                $('.sc-wrapper','#searchclauselist').each(function(){
143                        $(this).removeClass("focused");
144                });
145                $(this).closest('.sc-wrapper').addClass("focused");
146        });
147       
148        //TODO handle keydown
149/*      e_index.keydown(function(ev){
150                var evStop = function(){ ev.stopPropagation(); ev.preventDefault(); };
151                if (ev.which === 13) evStop();
152                var evFocusNext = function(){
153                        ev.preventDefault();
154                        e_relation.focus();     
155                };
156                //notifyUser($(this).attr("value").length);
157                if ($(this).attr("value").length > 0 &&  ev.which === 32) evFocusNext(); // focus next element
158               
159        });
160       
161        e_relation.keydown(function(ev){
162                var evStop = function(){ ev.stopPropagation(); ev.preventDefault(); };
163                if (ev.which === 13) evStop();
164                var evFocusNext = function(){
165                        e_value.focus();       
166                };
167                if (ev.which === 32) evFocusNext(); // focus next element
168                notifyUser(ev.which);
169        });
170*/
171       
172        //selects for the first use
173        index_select.change(function(){
174                $(this).next().val($(this).find('option:selected').text());
175                $(this).hide();
176                $(this).next().show();
177        });
178        value_select.change(function(){
179                $(this).next().val($(this).find('option:selected').text());
180                $(this).hide();
181                $(this).next().show();
182        });
183       
184        e_index.val(this.index);
185        e_relation.val(this.relation);
186        e_value.val(this.value);
187        //update after text change
188        e_index.bind('change', function(){
189               
190                var i = $(this).closest('.sc-i').attr("id");
191                var j = $(this).closest('.sc-j').attr("id");
192                //alert("e-index-chabge;" + i + j + $(this).val());
193                searchclauseset.searchclauses[i][j].index = $(this).val();
194                notifyUser("index_sc" + i + "-" + j + " changed to:" + searchclauseset.searchclauses[i][j].index, "debug");
195                searchclauseset.searchclauses[i][j].is_category = false;
196    });
197        e_relation.change(function(){
198                var i = $(this).closest('.sc-i').attr("id");
199                var j = $(this).closest('.sc-j').attr("id");
200                //notifyUser("rel change:" + $(this).val(), 'debug');
201                searchclauseset.searchclauses[i][j].relation = $(this).val();
202               
203                searchclauseset.searchclauses[i][j].is_category = false;
204        });
205        e_value.change(function(){
206                var i = $(this).closest('.sc-i').attr("id");
207                var j = $(this).closest('.sc-j').attr("id");
208                searchclauseset.searchclauses[i][j].value = $(this).val();
209               
210                searchclauseset.searchclauses[i][j].is_category = false;
211        });
212       
213        try{
214                this.initAutocomplete(false);
215                this.initAutocomplete(true);
216        } catch (e){
217                notifyUser("autocomplete failed: " + e.message);
218        }
219       
220 };
221 
222
223 SearchClause.prototype.initAutocomplete = function(is_explain){
224       
225         if (is_explain){
226                        if (element_autocomplete_explain.length == 0) return;
227                         
228                        $(this.GetIndexInputTermset()).autocomplete({
229                                        source: element_autocomplete_explain,
230                                        autoFill:true,
231                                        width:150,
232                                        //onFindValue:findValueTermset,
233                                        //onItemSelect:selectItemTermset,
234                                        minLength: 0,
235                                        matchContains:true,
236                                        delay: 0,
237                                        select: function( event, ui ) {
238                                                $(this).data("item", ui.item);
239                                                selectItemTermsetExplain(ui.item);
240                                        }
241                                }).data( "autocomplete" )._renderItem = function( ul, item ) { 
242                                        var t, re;
243                                        if (this.term == ""){
244                                                t = item.value;
245                                        } else {
246                                                re = new RegExp(this.term + "+") ; 
247                                            t = item.value.replace(re,"<span style='font-weight:bold;color:Blue;'>" + 
248                                                    this.term + 
249                                                    "</span>"); 
250                                        }
251                                       
252                                    return $( "<li></li>" ) 
253                                        .data( "item.autocomplete", item ) 
254                                        .append( "<a>" + t + "</a>" ) 
255                                        .appendTo( ul ); 
256                        }; 
257
258         } else {
259                        if (element_autocomplete.length == 0) return;
260                         
261                        $(this.GetIndexInputTermset()).autocomplete({
262                                        source: element_autocomplete,
263                                        autoFill:true,
264                                        width:150,
265                                        //onFindValue:findValueTermset,
266                                        //onItemSelect:selectItemTermset,
267                                        minLength: 0,
268                                        matchContains:true,
269                                        delay: 0,
270                                        select: function( event, ui ) {
271                                                $(this).data("item", ui.item);
272                                                selectItemTermset(ui.item);
273                                        }
274                                }).data( "autocomplete" )._renderItem = function( ul, item ) { 
275                                        var t, re;
276                                        if (this.term == ""){
277                                                t = item.value;
278                                        } else {
279                                                re = new RegExp(this.term + "+") ; 
280                                            t = item.value.replace(re,"<span style='font-weight:bold;color:Blue;'>" + 
281                                                    this.term + 
282                                                    "</span>"); 
283                                        }
284                                       
285                                    return $( "<li></li>" ) 
286                                        .data( "item.autocomplete", item ) 
287                                        .append( "<a>" + t + "</a>" ) 
288                                        .appendTo( ul ); 
289                        }; 
290
291         }
292        $('body').find('.ac_results').css({'z-index' : '1000'});
293         
294 };
295
296
297SearchClause.prototype.CreateSelect = function(str, element){
298        var index_select = GetIndexSelect();
299        var index_input  = GetIndexInput();
300        index_select.show();
301        var x = "<option value='" + str + "'>" + str + "</option>";
302        $(element).each(function(){
303                x = x+"<option value='" + $(this).text() + "'>" + $(this).text() + "</option>";
304        });
305        index_select.html(x);
306       
307        index_input.hide();
308       
309};
310
311SearchClause.prototype.PlainText = function(){ 
312        if (this.index.trim().length == 0 || this.value.trim().length == 0){
313                return "";
314        }
315        if (this.is_category){
316                        return "ISOCAT( " + this.category + ") " + this.relation + " " + this.value;
317        }
318        return this.index.trim().replace(" ","_") + " " + this.relation + " " + this.value;
319};
320
321/** special handling for special characters: double escaping (escape the %-sign)
322 * to survive the %-encoding through the request (and parsing) down to the transformation in XCQL2XPath.xsl
323 * it's: whitespace, and single and double-quotes (unified to double quotes %22)
324*/ 
325SearchClause.prototype.escape = function(){
326        var escape_string = "";
327        var escape_value_string = "";
328       
329        escape_value_string =escape(this.value).replace(/%20/g,"%2520").replace(/\%2[27]/g,"%2522").replace(/%7C/g,"%257C").replace(/\+/g,"%2B"); 
330
331        if (this.is_complex) {
332                escape_string = escape_string + escape(this.index.trim().replace(" ","_") + " " + this.relation + " ") + escape_value_string;
333        } else {
334                // case more simple words in search
335                escape_string = escape_value_string;
336        }       
337        return escape_string;
338       
339};
340
341SearchClause.createFromData = function(scstr){
342        var n;
343        var scarr;
344        var rel = "";
345        n = scstr.indexOf("=");
346        if (n > 0){
347                scarr = scstr.split("=");
348                rel = "=";
349        }
350        n = scstr.indexOf(" any ");
351        if (n > 0){
352                scarr = scstr.split(" any ");
353                rel = "any";
354        }
355        n = scstr.indexOf(" contains ");
356        if (n > 0){
357                scarr = scstr.split(" contains ");
358                rel = "contains";
359        }
360        n = scstr.indexOf("<");
361        if (n > 0){
362                scarr = scstr.split("<");
363                rel = "<";
364        }
365        n = scstr.indexOf(">");
366        if (n > 0){
367                scarr = scstr.split(">");
368                rel = ">";
369        }
370        n = scstr.indexOf(" all ");
371        if (n > 0){
372                scarr = scstr.split(" all ");
373                rel = "all";
374        }
375        //notifyUser("scarr:" + scarr[0] + scarr[1],'debug');
376       
377        if (scarr == undefined) {
378                //this.issimpletext = true;
379                sc = new SearchClause('*','any',scstr);
380                sc.is_complex = false;
381        } else {
382                //this.issimpletext = false;
383                sc = new SearchClause(scarr[0],rel,scarr[1]);
384                sc.is_complex = true;
385        }
386
387        return sc;
388};
389/*
390Searchclause.prototype.submit = function () {
391       
392        var uri = link('recordset','htmltable', this.query_uri());
393        notifyUser("submitting query:" +  uri);
394       
395        this.container.find('.result').load( uri, function() {
396                                notifyUser("result-loaded");
397                                var get = $(this).parent().find('.cmd_get');                           
398
399                                get.removeClass('cmd_get');
400                                get.addClass('cmd_up');
401                                // get.show();
402                        });
403
404}
405*/
406   
407$('.index-context td').live('click',  function(event) {
408       
409        searchclauseset.updatedata(this.textContent, false, "");
410        //$('.focused','#searchclauselist').find('.index-input').val(   this.textContent);
411        $('.focused','#searchclauselist').find('.index-context').hide();
412       
413        $('.focused','#searchclauselist').find('.value-input').focus();
414
415});
416
417
418var searchclauseset_container = $("#searchclauselist"); 
419
420/**
421 * container of actually used Searchclauses in the query-input
422 * with basic functionality for building container - add, remove, clear
423 * and conversion functions - conversions from-to querystring
424 * @constructor
425 */
426
427var searchclauseset = {
428                /**  @field */
429        searchclauses: [],
430        container: '#searchclauselist',
431        sctext: '',
432               
433        addsearchclause: function (searchclause, rel, _i, _j){
434                var i,j;
435                       
436                if (this.searchclauses.length == 0){
437                        i = 0;
438                        this.searchclauses[i] = new Array();
439                        j = 0;
440                } else {
441                        if (rel == "or") {
442                                i = _i;//this.searchclauses.length - 1;
443                                j = this.searchclauses[i].length;//this.searchclauses[this.searchclauses.length - 1].length;
444                        } else {
445                                i = this.searchclauses.length;
446                                this.searchclauses[i] = new Array();
447                                j = 0;
448                        }
449                }
450                searchclause.i = i;
451                searchclause.j = j;
452                searchclause.listid = "sc" + i + "-" + j;
453                this.searchclauses[i][j] = searchclause;
454               
455                searchclause.render(rel);                                               
456                //$('#querylist').html(this.render());
457        },
458       
459        fillFromURL: function(url){
460        },
461       
462        clear: function() {
463                notifyUser("clear query",'debug');
464               
465                for (var i = this.searchclauses.length - 1; i > -1; i--){
466                        for (var j = this.searchclauses[i].length - 1; j > -1; j--){
467                       
468
469                                        this.searchclauses[i].splice(j, 1);
470                                        if  (j == 0){
471                                                this.searchclauses.splice(i, 1);
472                                                $('#' + 'sc' +  i + '-' + j).parent().parent().remove();
473                                                $('#' + 'sc' +  i + '-' + j).parent().remove();
474                                        } else {
475                                                $('#' + 'sc' +  i + '-' + j).parent().remove();
476                                        }               
477               
478                       
479                        }
480                }
481               
482                notifyUser(this.searchclauses.length,'debug');
483               
484                // reset focused
485                //$('.sc-wrapper','#searchclauselist').each(function(){
486                //      $(this).removeClass("focused");
487                //});
488                //$('#sc0-0').addClass("focused");
489        },
490       
491        //TODO remove only last items
492        removesearchclause: function (i, j) {
493                notifyUser("removing sc:"  + i + "," + j,'debug');
494               
495                if ((j == 0) && (i == 0)){
496                        searchclauseset.searchclauses[i][j].index = "";
497                        searchclauseset.searchclauses[i][j].is_category = false;
498                        searchclauseset.searchclauses[i][j].relation = "=";
499                        searchclauseset.searchclauses[i][j].value = "";
500                        $('#sc0-0').find('.index-input').val("");
501                        $('#sc0-0').find('.index-input-termset').val("");
502                        $('#sc0-0').find('.rel_input').val("=");
503                        $('#sc0-0').find('.value-input').val("");                       
504                        notifyUser("cannot remove", 'debug');
505                        return;
506                }
507                if ((j == 0) && (i < this.searchclauses.length - 1)){
508                        notifyUser("cannot remove",'debug');
509                        return;
510                }
511                if (j < this.searchclauses[i].length - 1){
512                        notifyUser("cannot remove",'debug');
513                        return;
514                }
515                this.searchclauses[i].splice(j, 1);
516                 
517                if  (j == 0){
518                        this.searchclauses.splice(i, 1);
519                        $('#' + 'sc' +  i + '-' + j).parent().parent().remove();
520                } else {
521                        $('#' + 'sc' +  i + '-' + j).parent().remove();
522                }               
523
524                notifyUser("sc removed, new sc.length:"  + this.searchclauses.length,'debug');
525                for (var j = 0; j < this.searchclauses.length; j++) {
526                        notifyUser("  new sc.or_length:"  + this.searchclauses[j].length,'debug');
527                }       
528                // reset focused
529                $('.sc-wrapper','#searchclauselist').each(function(){
530                        $(this).removeClass("focused");
531                });
532                $('#sc0-0').addClass("focused");
533               
534        },
535        /*
536        // just test
537        load: function(i,j,rel,data) {
538                if (data["triple"] == null ) {
539                         this.searchclauses[i][j].index =  data["searchClause"]["index"];
540                         this.searchclauses[i][j].relation =  data["searchClause"]["relation"]["value"];
541                         this.searchclauses[i][j].value =  data["searchClause"]["term"];
542                         
543                         $('#sc0-0','#searchclauselist').find('.index-input').val(data["searchClause"]["index"]);
544                         $('#sc0-0','#searchclauselist').find('.relation_input').val(data["searchClause"]["relation"]["value"]);
545                         $('#sc0-0','#searchclauselist').find('.value-input').val(data["searchClause"]["term"]);
546                 } else {
547                         if (data["triple"]["boolean"]["value"] == "and") {
548                                 //var sc = new SearchClause("","","");
549                                 //this.addsearchclause(sc, "and", _i, _j)
550                                 // update(leftO)
551                                 // update(rightO)
552                                 load(i,j,"",data["triple"]["leftOperand"]);
553                                 i = this.searchclauses.length;
554                                 load(i+1,j,"and",data["triple"]["rightOperand"]);
555                         } else {
556                                 load(i,j,"",data["triple"]["leftOperand"]);
557                                 j = this.searchclauses[i].length;
558                                 load(i,j,"or",data["triple"]["rightOperand"]);
559                         }
560                         
561                 }
562                 
563        },
564        */
565        // from querystring to searchclauses
566        buildfromquerystring: function(){
567                var arr = Query.simplequerystring(this.sctext);
568                var sc, screl;
569               
570                this.clear();
571               
572                //notifyUser(arr,'debug');
573                var arr_and = arr.split(" and ");
574                screl = "";
575                for( var i=0;i<arr_and.length;i++){
576                        var scstring = $.trim(arr_and[i]);
577                       
578                        if (scstring.substring(0,1) == "(" && scstring.substring(scstring.length-1) == ")") {
579                                scstring = scstring.substring(1,scstring.length-1);
580                                scstring = $.trim(scstring);
581                        }
582                       
583                        //notifyUser("i:" + scstring,'debug');
584                        var arr_or = scstring.split(" or ");
585                        if (i > 0) {screl = "and";}
586                        for( var j=0;j<arr_or.length;j++){
587                                if (j > 0) {screl = "or";}
588                               
589                                sc = SearchClause.createFromData($.trim(arr_or[j]));
590                                this.addsearchclause(sc,screl,i,j);                             
591                        }
592                }
593        },
594       
595       
596        buildsctext: function(){
597           var uncompletequery = false;
598           var ptext = "";
599           
600                if ($('.searchtype_text','#search').size() > 0) {
601                        this.sctext = $('#query_area').val();
602                } else {
603               
604                        this.sctext = "";
605                        for (var i = 0; i < this.searchclauses.length; i++) {
606                                if ( i>0) this.sctext = this.sctext + " and ";
607                                if (this.searchclauses.length> 1) this.sctext = this.sctext + " ( ";
608                                for (var j = 0; j < this.searchclauses[i].length; j++) {
609                                        if ( j>0) this.sctext = this.sctext + " or ";
610                                        if (this.searchclauses[i].length > 1) this.sctext = this.sctext + " ( ";
611                                        ptext = this.searchclauses[i][j].PlainText();
612                                        if (ptext.length == 0){
613                                                uncompletequery = true;
614                                        }
615                                        this.sctext = this.sctext + ptext;
616                                        if (this.searchclauses[i].length > 1) this.sctext = this.sctext + " ) ";
617                                }
618                                if (this.searchclauses.length> 1) this.sctext = this.sctext + " ) ";
619                        }
620                       
621                }       
622                if ((i == 1 && j == 1) && (this.sctext.substring(0,6) == '* any ')) {
623                        this.sctext  = this.sctext.replace('* any ','');
624                }
625                if (uncompletequery){
626                        this.sctext = "";
627                }
628                notifyUser(this.sctext);
629                return this.sctext;
630               
631        },
632       
633        updatedata: function(term, is_category, category, value) {       
634               
635                var i = $('.focused','#searchclauselist').closest('.sc-i').attr("id");
636                var j = $('.focused','#searchclauselist').closest('.sc-j').attr("id");
637               
638                if (term != undefined){
639                        $('.focused','#searchclauselist').find('.index-input').val(term);
640                        searchclauseset.searchclauses[i][j].index = term.replace(" ","_");
641                }
642               
643                if (value == undefined){
644                        $('.focused','#searchclauselist').find('.value-input').focus();
645                } else {
646                        $('.focused','#searchclauselist').find('.value-input').val(value);
647                        searchclauseset.searchclauses[i][j].value = value;
648                }
649               
650                if (is_category) {
651                        searchclauseset.searchclauses[i][j].is_category = true;
652                        searchclauseset.searchclauses[i][j].category = category;
653                } else {
654                        searchclauseset.searchclauses[i][j].is_category = false;
655                }
656               
657        },
658       
659        initAutocomplete: function(is_explain) {
660                notifyUser("init autocomplete",'debug');
661               
662                for (var i = this.searchclauses.length - 1; i > -1; i--){
663                        for (var j = this.searchclauses[i].length - 1; j > -1; j--){
664                                        this.searchclauses[i][j].initAutocomplete(is_explain);         
665                        }
666                }       
667        }
668};
669
670function escapequerystring(querystring){
671        var arr = Query.simplequerystring(querystring);
672        var sc, screl;
673        var escape_string = "";
674        var add_bracket = false;
675       
676        //notifyUser(arr,'debug');
677        var arr_and = arr.split(" and ");
678        screl = "";
679        for( var i=0;i<arr_and.length;i++){
680                var scstring = $.trim(arr_and[i]);
681               
682                add_bracket = false;
683                if (scstring.substring(0,1) == "(" && scstring.substring(scstring.length-1) == ")") {
684                        scstring = scstring.substring(1,scstring.length-1);
685                        scstring = $.trim(scstring);
686                        escape_string = escape_string + "(";
687                        add_bracket = true;
688                }
689               
690                //notifyUser("i:" + scstring,'debug');
691                var arr_or = scstring.split(" or ");
692                if (i > 0) {screl = "and";}
693               
694                for( var j=0;j<arr_or.length;j++){
695                        var scstr = $.trim(arr_or[j]);         
696                        if (j > 0) {screl = "or";}
697                       
698                        sc = SearchClause.createFromData(scstr);
699                        if (screl != ""){
700                                escape_string  = escape_string + "%20" + screl + "%20";
701                        }
702                        escape_string = escape_string + sc.escape();
703                }
704                if (add_bracket){
705                        escape_string = escape_string + ")";
706                }
707        }
708        return escape_string;
709}
710
711
712
713//autocomplete
714       
715function findValueValue(e) {
716        var i = $('.focused','#searchclauselist').closest('.sc-i').attr("id");
717        var j = $('.focused','#searchclauselist').closest('.sc-j').attr("id");
718        //alert(i+ ',' + j + sValue);
719        var termsetitem = $('#' + 'sc' +  i + '-' + j).find('.value-input').data('item');
720        var termset = "";
721        if (termsetitem != null) termset = termsetitem.key;
722        searchclauseset.searchclauses[i][j].value = e.value;
723       
724}
725function selectItemValue(li) {
726        findValueValue(li);
727}
728
729function initValueAutocomplete(i,j,index, term_index){
730       
731        $('#' + 'sc' +  i + '-' + j).find('.value-input').autocomplete({
732                source: element_autocomplete[index].data[term_index].values,
733                autoFill:true,
734                width:150,
735                minLength: 0,
736                matchContains:true,
737                delay: 0,
738                select: function( event, ui ) {
739                        $(this).data("item", ui.item);
740                        selectItemValue(ui.item);
741                }
742        }).data( "autocomplete" )._renderItem = function( ul, item ) { 
743                var t, re;
744                if (this.term == ""){
745                        t = item.value;
746                } else {
747                        re = new RegExp(this.term + "+") ; 
748                    t = item.value.replace(re,"<span style='font-weight:bold;color:Blue;'>" + 
749                            this.term + 
750                            "</span>"); 
751                }
752               
753            return $( "<li></li>" ) 
754                .data( "item.autocomplete", item ) 
755                .append( "<a>" + t + "</a>" ) 
756                .appendTo( ul ); 
757        };
758
759       
760        // reset the index value
761        $('#' + 'sc' +  i + '-' + j).find('.value-input').val("");
762        searchclauseset.searchclauses[i][j].value = "";
763        $('#' + 'sc' +  i + '-' + j).find('.value-input').show();
764        $('body').find('.ac_results').css({'z-index' : '1000'});
765       
766       
767        $('.focused','#searchclauselist').find('.value-input').focus();
768
769       
770};
771function initValueAutocompleteExplain(i,j,index, term_index){
772       
773        // reset the index value
774        $('#' + 'sc' +  i + '-' + j).find('.value-input').val("");
775        searchclauseset.searchclauses[i][j].value = "";
776        $('#' + 'sc' +  i + '-' + j).find('.value-input').show();
777        $('body').find('.ac_results').css({'z-index' : '1000'});
778       
779       
780        $('.focused','#searchclauselist').find('.value-input').focus();
781
782       
783};
784
785function findValueTerm(e) {
786       
787        //var sValue = e.selectValue;
788       
789        var i = $('.focused','#searchclauselist').closest('.sc-i').attr("id");
790        var j = $('.focused','#searchclauselist').closest('.sc-j').attr("id");
791        //alert(i+ ',' + j + sValue);
792        var sClause = $('#' + 'sc' +  i + '-' + j);
793        var context = "";
794        var termsetitem = $(sClause).find('.index-input-termset').data('item');
795        var termset = "";
796        if (termsetitem != null) termset = termsetitem.key;
797        var str = e.id;
798        str = str.substring(str.lastIndexOf('/')+1, str.length);
799        var termsetindex = termset + ":" +  str;
800        searchclauseset.searchclauses[i][j].index = termsetindex;
801       
802        //handleTermsSelection(i,j,sValue);     
803        //find element value
804
805        var index;
806        $.each(element_autocomplete,function(i, item){
807                if (this.key == termset){
808                        index = i;
809                        return;
810                }
811        });
812        var term_index;
813       
814        $.each(element_autocomplete[index].data,function(i, item){
815                if (this.id == e.id){
816                        term_index = i;
817                        return;
818                }
819        });
820        if (element_autocomplete[index].data[term_index].values.length == 0){
821                jQuery.get(link('fcs','?operation=scan&scanClause' + termsetindex + '&x-context=' + getSelectedRepository()),function(data){
822                $(data).find("sru\\:term").each(function(i){
823                        element_autocomplete[index].data[term_index].values[i] = {label:$(this).find('sru\\:value').text()};
824                });
825                initValueAutocomplete(i,j, index, term_index);
826                });
827        } else {
828                initValueAutocomplete(i,j, index, term_index);
829        }       
830}
831function findValueTermExplain(e) {
832       
833        //var sValue = e.selectValue;
834       
835        var i = $('.focused','#searchclauselist').closest('.sc-i').attr("id");
836        var j = $('.focused','#searchclauselist').closest('.sc-j').attr("id");
837        //alert(i+ ',' + j + sValue);
838        var sClause = $('#' + 'sc' +  i + '-' + j);
839        var context = "";
840        var termsetitem = $(sClause).find('.index-input-termset').data('item');
841        var termset = "";
842        if (termsetitem != null) termset = termsetitem.key;
843        var str = e.id;
844        str = str.substring(str.lastIndexOf('/')+1, str.length);
845        var termsetindex = termset + ":" +  str;
846        searchclauseset.searchclauses[i][j].index = termsetindex;
847       
848        //find element value
849
850        var index;
851        $.each(element_autocomplete_explain,function(i, item){
852                if (this.key == termset){
853                        index = i;
854                        return;
855                }
856        });
857        var term_index;
858       
859        $.each(element_autocomplete_explain[index].data,function(i, item){
860                if (this.id == e.id){
861                        term_index = i;
862                        return;
863                }
864        });
865        if (element_autocomplete_explain[index].data[term_index].values.length == 0){
866                jQuery.get(link('fcs','?operation=scan&scanClause' + termsetindex + '&x-context=' + getSelectedRepository()),function(data){
867                $(data).find("sru\\:term").each(function(i){
868                        element_autocomplete[index].data[term_index].values[i] = {label:$(this).find('sru\\:value').text()};
869                });
870                initValueAutocompleteExplain(i,j, index, term_index);
871                });
872        } else {
873                initValueAutocompleteExplain(i,j, index, term_index);
874        }       
875}
876 
877function selectItemTerm(li) {
878        findValueTerm(li);
879}
880function selectItemTermExplain(li) {
881        findValueTermExplain(li);
882}
883
884function initTermsetAutocomplete(i,j,index){
885
886        $('#' + 'sc' +  i + '-' + j).find('.index-input').autocomplete({
887                source: element_autocomplete[index].data,
888                autoFill:true,
889                width:150,
890                minLength: 0,
891                matchContains:true,
892                delay: 0,
893                select: function( event, ui ) {
894                        $(this).data("item", ui.item);
895                        selectItemTerm(ui.item);
896                        }
897        }).data( "autocomplete" )._renderItem = function( ul, item ) { 
898                var t, re;
899                if (this.term == ""){
900                        t = item.value;
901                } else {
902                        re = new RegExp(this.term + "+") ; 
903                    t = item.value.replace(re,"<span style='font-weight:bold;color:Blue;'>" + 
904                            this.term + 
905                            "</span>"); 
906                }
907       
908                return $( "<li></li>" ) 
909                    .data( "item.autocomplete", item ) 
910                    .append( "<a>" + t + "</a>" ) 
911                    .appendTo( ul ); 
912        };
913       
914       
915                // reset the index value
916        $('#' + 'sc' +  i + '-' + j).find('.index-input').val("");
917        searchclauseset.searchclauses[i][j].index = "";
918        $('#' + 'sc' +  i + '-' + j).find('.value-input').val("");
919        searchclauseset.searchclauses[i][j].value = "";
920        $('#' + 'sc' +  i + '-' + j).find('.index-input').show();
921        $('body').find('.ac_results').css({'z-index' : '1000'});
922       
923       
924        $('.focused','#searchclauselist').find('.index-input').focus();
925};
926function initTermsetAutocompleteExplain(i,j,index){
927
928        $('#' + 'sc' +  i + '-' + j).find('.index-input').autocomplete({
929                source: element_autocomplete_explain[index].data,
930                autoFill:true,
931                width:150,
932                minLength: 0,
933                matchContains:true,
934                delay: 0,
935                select: function( event, ui ) {
936                        $(this).data("item", ui.item);
937                        selectItemTermExplain(ui.item);
938                        }
939        }).data( "autocomplete" )._renderItem = function( ul, item ) { 
940                var t, re;
941                if (this.term == ""){
942                        t = item.value;
943                } else {
944                        re = new RegExp(this.term + "+") ; 
945                    t = item.value.replace(re,"<span style='font-weight:bold;color:Blue;'>" + 
946                            this.term + 
947                            "</span>"); 
948                }
949       
950                return $( "<li></li>" ) 
951                    .data( "item.autocomplete", item ) 
952                    .append( "<a>" + t + "</a>" ) 
953                    .appendTo( ul ); 
954        };
955       
956       
957                // reset the index value
958                $('#' + 'sc' +  i + '-' + j).find('.index-input').val("");
959                searchclauseset.searchclauses[i][j].index = "";
960                $('#' + 'sc' +  i + '-' + j).find('.index-input').show();
961                $('body').find('.ac_results').css({'z-index' : '1000'});
962       
963       
964                $('.focused','#searchclauselist').find('.index-input').focus();
965};
966
967
968function findValueTermset(elem) {
969        //var sValue = e.selectValue;
970
971        var i = $('.focused','#searchclauselist').closest('.sc-i').attr("id");
972        var j = $('.focused','#searchclauselist').closest('.sc-j').attr("id");
973
974        //searchclauseset.searchclauses[i][j].index = sValue;   
975        //handleTermsetSelection(i,j,sValue);           
976
977        //find element
978        var index;
979        $.each(element_autocomplete,function(i, item){
980                if (this.key == elem.key){
981                        index = i;
982                        return;
983                }
984        });
985
986        if (element_autocomplete[index].data.length == 0){
987                jQuery.get(link('smc','list/?context=' + element_autocomplete[index].key),function(data){
988                        $(data).find('Term').each(function(i){
989                                element_autocomplete[index].data[i] = {label:$(this).text(), value:$(this).text(), id:$(this).attr("concept-id"),values:[] };
990                        });
991                        initTermsetAutocomplete(i,j, index);
992                });
993        } else {
994                initTermsetAutocomplete(i,j, index);
995        }
996}
997function findValueTermsetExplain(elem) {
998       
999        var i = $('.focused','#searchclauselist').closest('.sc-i').attr("id");
1000        var j = $('.focused','#searchclauselist').closest('.sc-j').attr("id");
1001
1002                //find element
1003        var index;
1004        $.each(element_autocomplete_explain,function(i, item){
1005                if (this.key == elem.key){
1006                        index = i;
1007                        return;
1008                }
1009        });
1010
1011        if (element_autocomplete_explain[index].data.length == 0){
1012                jQuery.get(link('fcs','?operation=explain&x-context=' + getSelectedRepository()),function(data){
1013                        $(data).find('indexInfo').children('index').children('map').children('name[set='+ element_autocomplete_explain[index].key +']').each(function(i){
1014                                element_autocomplete_explain[index].data[i] = {label:$(this).text(), value:$(this).text(), id:$(this).text(),values:[] };
1015                        });
1016                        initTermsetAutocompleteExplain(i,j, index);
1017                });
1018        } else {
1019                initTermsetAutocompleteExplain(i,j, index);
1020        }
1021
1022}
1023
1024
1025function selectItemTermset(li) {
1026        findValueTermset(li);
1027        }
1028function selectItemTermsetExplain(li) {
1029        findValueTermsetExplain(li);
1030        }
1031
Note: See TracBrowser for help on using the repository browser.