source: MDService2/trunk/MDService2/WebContent/scripts/mdservice_searchclause.js @ 1029

Last change on this file since 1029 was 1029, checked in by vronk, 13 years ago

changes to columns, terms (unfinished) ; .ui-context-dialog

File size: 17.6 KB
Line 
1// client-side modelling of "business-objects":
2// SearchClause, SearchClauseset,
3
4 var  element_autocomplete = new Array();
5 var  elements_hashtable =  {};
6
7 
8 //TODO use select-options or autocomplete ???
9 //var  relation_autocomplete = new Array('any','all','=','>','<');
10 //var  value_autocomplete = new Array();
11function SearchClause(index, _relation, _value ) {
12        this.index = index;
13        this.relation = _relation;     
14        this.value = _value;
15        this.negation  = false;
16       
17        this.is_category = false;
18        this.category = "";
19       
20        this.i = 0;
21        this.j = 0;
22        this.listid = "";
23        this.container = {};
24       
25        if (this.relation == "") {
26                this.relation = "=";
27        }
28}
29
30SearchClause.prototype.GetIndexInput = function(){
31        return $($($('.sc-i','#searchclauselist')[this.i]).find('.sc-j')[this.j]).find('.index-input');
32};
33
34SearchClause.prototype.GetRelInput = function(){
35        return $($($('.sc-i','#searchclauselist')[this.i]).find('.sc-j')[this.j]).find('.rel_input');
36};
37SearchClause.prototype.GetValueInput = function(){
38        return $($($('.sc-i','#searchclauselist')[this.i]).find('.sc-j')[this.j]).find('.value_input');
39};
40
41SearchClause.prototype.GetIndexSelect = function(){
42        return $($($('.sc-i','#searchclauselist')[this.i]).find('.sc-j')[this.j]).find('.index_select');
43};
44
45
46SearchClause.prototype.GetValueSelect = function(){
47        return $($($('.sc-i','#searchclauselist')[this.i]).find('.sc-j')[this.j]).find('.value_select');
48};
49
50SearchClause.prototype.render = function (rel) {
51       
52        //TODO defaults
53        //this.relation = '=';
54       
55        var x = "<div id='" + this.j + "' class='sc-j or_level' ><div id='" + this.listid + "' class='sc-wrapper focused' > " +
56        "<div class='search'><span class='index_search'><select class='index_select'/><input type='text' class='index-input autocomplete-input' /></span>" + 
57        //"<select type='text' class='rel_input' />" +
58        "<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>" + 
59        "<select class='value_select'/><input type='text' class='value_input' />" +
60         "<span class='sc_cmds'><span class='cmd cmd_sc_delete'> </span><span class='sc_id'>" + this.listid + "</span>" + 
61          "<span class='cmd cmd_add_and'> </span><span class='cmd cmd_add_or'></span>"+
62        " </span></div><div class='index-context'><table></table></div></div></div>";
63       
64        addToSClist(x, this.i, this.j, rel);   
65       
66       
67        var e_index = this.GetIndexInput();
68        //new $.GrowingInput(e_index, {max: 80});
69        var e_relation = this.GetRelInput();
70        var e_value = this.GetValueInput();
71        var index_select = this.GetIndexSelect();
72        var value_select = this.GetValueSelect();
73       
74        $(index_select).hide();
75        value_select.hide();
76        //$(e_index).closest('.sc-wrapper').find('.index-context').hide();
77       
78        //alert("focused: "  + $('.focused','#searchclauselist').size());
79        $('.sc-wrapper','#searchclauselist').each(function(){
80                $(this).removeClass("focused");
81        });
82        $(e_index).closest('.sc-wrapper').addClass("focused");
83        //alert("focused: "  + $('.focused','#searchclauselist').size());
84       
85        // set focus
86        e_index.focusin(function(){
87                $('.sc-wrapper','#searchclauselist').each(function(){
88                        $(this).removeClass("focused");
89                });
90                $(this).closest('.sc-wrapper').addClass("focused");
91        });
92        index_select.focusin(function(){
93                $('.sc-wrapper','#searchclauselist').each(function(){
94                        $(this).removeClass("focused");
95                });
96                $(this).closest('.sc-wrapper').addClass("focused");
97        });
98        e_relation.focusin(function(){
99                $('.sc-wrapper','#searchclauselist').each(function(){
100                        $(this).removeClass("focused");
101                });
102                $(this).closest('.sc-wrapper').addClass("focused");
103        });
104        e_value.focusin(function(){
105                $('.sc-wrapper','#searchclauselist').each(function(){
106                        $(this).removeClass("focused");
107                });
108                $(this).closest('.sc-wrapper').addClass("focused");
109        });
110       
111        //TODO handle keydown
112/*      e_index.keydown(function(ev){
113                var evStop = function(){ ev.stopPropagation(); ev.preventDefault(); };
114                if (ev.which === 13) evStop();
115                var evFocusNext = function(){
116                        ev.preventDefault();
117                        e_relation.focus();     
118                };
119                //notifyUser($(this).attr("value").length);
120                if ($(this).attr("value").length > 0 &&  ev.which === 32) evFocusNext(); // focus next element
121               
122        });
123       
124        e_relation.keydown(function(ev){
125                var evStop = function(){ ev.stopPropagation(); ev.preventDefault(); };
126                if (ev.which === 13) evStop();
127                var evFocusNext = function(){
128                        e_value.focus();       
129                };
130                if (ev.which === 32) evFocusNext(); // focus next element
131                notifyUser(ev.which);
132        });
133*/
134       
135        //selects for the first use
136        index_select.change(function(){
137                $(this).next().val($(this).find('option:selected').text());
138                $(this).hide();
139                $(this).next().show();
140        });
141        value_select.change(function(){
142                $(this).next().val($(this).find('option:selected').text());
143                $(this).hide();
144                $(this).next().show();
145        });
146       
147        e_index.val(this.index);
148        e_relation.val(this.relation);
149        e_value.val(this.value);
150        //update after text change
151        e_index.bind('change', function(){
152               
153                var i = $(this).closest('.sc-i').attr("id");
154                var j = $(this).closest('.sc-j').attr("id");
155                //alert("e-index-chabge;" + i + j + $(this).val());
156                searchclauseset.searchclauses[i][j].index = $(this).val();
157                notifyUser("index_sc" + i + "-" + j + " changed to:" + searchclauseset.searchclauses[i][j].index, "debug");
158                searchclauseset.searchclauses[i][j].is_category = false;
159    });
160        e_relation.change(function(){
161                var i = $(this).closest('.sc-i').attr("id");
162                var j = $(this).closest('.sc-j').attr("id");
163                //notifyUser("rel change:" + $(this).val(), 'debug');
164                searchclauseset.searchclauses[i][j].relation = $(this).val();
165               
166                searchclauseset.searchclauses[i][j].is_category = false;
167        });
168        e_value.change(function(){
169                var i = $(this).closest('.sc-i').attr("id");
170                var j = $(this).closest('.sc-j').attr("id");
171                searchclauseset.searchclauses[i][j].value = $(this).val();
172               
173                searchclauseset.searchclauses[i][j].is_category = false;
174        });
175       
176        this.initAutocomplete();
177
178        /*
179         e_value.autocompleteArray(value_autocomplete,{
180                                autoFill:true,
181                                width:90
182        });
183         */
184       
185       
186 };
187 
188 SearchClause.prototype.initAutocomplete = function(){
189       
190         if (element_autocomplete.size == 0) return;
191         
192         //autocomplete
193                function handleTermsSelection(i,j,elem){
194                       
195                        var context = $('#' + 'sc' +  i + '-' + j).find('.index-context');
196                        // fill context
197                        //$(context).remove();
198                        $(context).html(elements_hashtable[elem]);
199                       
200                        /*
201                        $(context).children('table').children().remove();
202                        for(var ii=0;ii< elements_hashtable[elem].length;ii++){
203                                var row = '<tr><td>' + elements_hashtable[elem][ii] + '</td></tr>';
204                                $(context).children('table').append(row);
205                        }
206                        */
207                       
208                        $(context).show();
209                        //notifyUser($('#' + 'sc' +  i + '-' + j).find('.index-input').val(), 'debug');
210                        //$('#' + 'sc' +  i + '-' + j).find('.index-input').focus();
211                       
212                        $('#' + 'sc' +  i + '-' + j).find('.index-input').blur(function(){
213                                $(context).hide();
214                        });
215                        $('#' + 'sc' +  i + '-' + j).find('.index-input').focusin(function(){
216                                $(context).hide();
217                        });
218                       
219                       
220                                        /*
221                        var t = '<div><table><tr><td><a href="">aaaa<a></td></tr><tr><td><a href="">bbbb<a></td></tr><table></div>';
222                        var x = '<div class="index_context"><table></table></div>';
223                        $('.focused','#searchclauselist').find('.index_search').append(x);
224
225                       
226                        for(var i=0;i< elements_hashtable[elem].length;i++){
227                                var li = '<span><a href="">' + elements_hashtable[elem][i] + '</a></span>';
228                                $('.focused','#searchclauselist').find('.index_context').append(li);
229                        }*/
230                       
231                        //$('.focused','#searchclauselist').find('.index-input').hide();
232                       
233                };
234               
235                //autocomplete
236                function findValue(e) {
237                        //if( li == null ) return alert("No match!");
238                 
239                        var sValue = e.selectValue;
240                       
241                        var i = $('.focused','#searchclauselist').closest('.sc-i').attr("id");
242                        var j = $('.focused','#searchclauselist').closest('.sc-j').attr("id");
243                        //alert(i+ ',' + j + sValue);
244                        searchclauseset.searchclauses[i][j].index = sValue;
245                       
246                        handleTermsSelection(i,j,sValue);               
247                       
248                        //$('.focused','#searchclauselist').find('.value_input').focus();
249                       
250                }
251                 
252                function selectItem(li) {
253                        findValue(li);
254                }
255
256                this.GetIndexInput().autocompleteArray(element_autocomplete,{
257                        autoFill:true,
258                        width:150,
259                        onFindValue:findValue,
260                        onItemSelect:selectItem
261                });
262
263         
264 };
265
266SearchClause.prototype.CreateSelect = function(str, element){
267        var index_select = GetIndexSelect();
268        var index_input  = GetIndexInput();
269        index_select.show();
270        var x = "<option value='" + str + "'>" + str + "</option>";
271        $(element).each(function(){
272                x = x+"<option value='" + $(this).text() + "'>" + $(this).text() + "</option>";
273        });
274        index_select.html(x);
275       
276        index_input.hide();
277       
278};
279
280SearchClause.prototype.PlainText = function(){ 
281        if (this.index.trim().length == 0 || this.value.trim().length == 0){
282                return "";
283        }
284        if (this.is_category){
285                        return "ISOCAT( " + this.category + ") " + this.relation + " " + this.value;
286        }
287        return this.index.replace(" ","_") + " " + this.relation + " " + this.value;
288};
289/*
290Searchclause.prototype.submit = function () {
291       
292        var uri = link('recordset','htmltable', this.query_uri());
293        notifyUser("submitting query:" +  uri);
294       
295        this.container.find('.result').load( uri, function() {
296                                notifyUser("result-loaded");
297                                var get = $(this).parent().find('.cmd_get');                           
298
299                                get.removeClass('cmd_get');
300                                get.addClass('cmd_up');
301                                // get.show();
302                        });
303
304}
305*/
306   
307$('.index-context td').live('click',  function(event) {
308       
309        searchclauseset.updatedata(this.textContent, false, "");
310        //$('.focused','#searchclauselist').find('.index-input').val(   this.textContent);
311        $('.focused','#searchclauselist').find('.index-context').hide();
312       
313        $('.focused','#searchclauselist').find('.value_input').focus();
314
315});
316
317
318var searchclauseset_container = $("#searchclauselist"); 
319
320var searchclauseset = { 
321        searchclauses: [],
322        container: '#searchclauselist',
323        sctext: '',
324               
325        addsearchclause: function (searchclause, rel, _i, _j){
326                var i,j;
327                       
328                if (this.searchclauses.length == 0){
329                        i = 0;
330                        this.searchclauses[i] = new Array();
331                        j = 0;
332                } else {
333                        if (rel == "or") {
334                                i = _i;//this.searchclauses.length - 1;
335                                j = this.searchclauses[i].length;//this.searchclauses[this.searchclauses.length - 1].length;
336                        } else {
337                                i = this.searchclauses.length;
338                                this.searchclauses[i] = new Array();
339                                j = 0;
340                        }
341                }
342                searchclause.i = i;
343                searchclause.j = j;
344                searchclause.listid = "sc" + i + "-" + j;
345                this.searchclauses[i][j] = searchclause;
346               
347                searchclause.render(rel);                                               
348                //$('#querylist').html(this.render());
349        },
350       
351        fillFromURL: function(url){
352        },
353       
354        clear: function() {
355                notifyUser("clear query",'debug');
356               
357                for (var i = this.searchclauses.length - 1; i > -1; i--){
358                        for (var j = this.searchclauses[i].length - 1; j > -1; j--){
359                       
360
361                                        this.searchclauses[i].splice(j, 1);
362                                        if  (j == 0){
363                                                this.searchclauses.splice(i, 1);
364                                                $('#' + 'sc' +  i + '-' + j).parent().parent().remove();
365                                                $('#' + 'sc' +  i + '-' + j).parent().remove();
366                                        } else {
367                                                $('#' + 'sc' +  i + '-' + j).parent().remove();
368                                        }               
369               
370                       
371                        }
372                }
373               
374                notifyUser(this.searchclauses.length,'debug');
375               
376                // reset focused
377                //$('.sc-wrapper','#searchclauselist').each(function(){
378                //      $(this).removeClass("focused");
379                //});
380                //$('#sc0-0').addClass("focused");
381        },
382       
383        //TODO remove only last items
384        removesearchclause: function (i, j) {
385                notifyUser("removing sc:"  + i + "," + j,'debug');
386               
387                if ((j == 0) && (i == 0)){
388                        searchclauseset.searchclauses[i][j].index = "";
389                        searchclauseset.searchclauses[i][j].is_category = false;
390                        searchclauseset.searchclauses[i][j].relation = "=";
391                        searchclauseset.searchclauses[i][j].value = "";
392                        $('#sc0-0').find('.index-input').val("");
393                        $('#sc0-0').find('.rel_input').val("=");
394                        $('#sc0-0').find('.value_input').val("");                       
395                        notifyUser("cannot remove", 'debug');
396                        return;
397                }
398                if ((j == 0) && (i < this.searchclauses.length - 1)){
399                        notifyUser("cannot remove",'debug');
400                        return;
401                }
402                if (j < this.searchclauses[i].length - 1){
403                        notifyUser("cannot remove",'debug');
404                        return;
405                }
406                this.searchclauses[i].splice(j, 1);
407                 
408                if  (j == 0){
409                        this.searchclauses.splice(i, 1);
410                        $('#' + 'sc' +  i + '-' + j).parent().parent().remove();
411                } else {
412                        $('#' + 'sc' +  i + '-' + j).parent().remove();
413                }               
414
415                notifyUser("sc removed, new sc.length:"  + this.searchclauses.length,'debug');
416                for (var j = 0; j < this.searchclauses.length; j++) {
417                        notifyUser("  new sc.or_length:"  + this.searchclauses[j].length,'debug');
418                }       
419                // reset focused
420                $('.sc-wrapper','#searchclauselist').each(function(){
421                        $(this).removeClass("focused");
422                });
423                $('#sc0-0').addClass("focused");
424               
425        },
426        /*
427        // just test
428        load: function(i,j,rel,data) {
429                if (data["triple"] == null ) {
430                         this.searchclauses[i][j].index =  data["searchClause"]["index"];
431                         this.searchclauses[i][j].relation =  data["searchClause"]["relation"]["value"];
432                         this.searchclauses[i][j].value =  data["searchClause"]["term"];
433                         
434                         $('#sc0-0','#searchclauselist').find('.index-input').val(data["searchClause"]["index"]);
435                         $('#sc0-0','#searchclauselist').find('.relation_input').val(data["searchClause"]["relation"]["value"]);
436                         $('#sc0-0','#searchclauselist').find('.value_input').val(data["searchClause"]["term"]);
437                 } else {
438                         if (data["triple"]["boolean"]["value"] == "and") {
439                                 //var sc = new SearchClause("","","");
440                                 //this.addsearchclause(sc, "and", _i, _j)
441                                 // update(leftO)
442                                 // update(rightO)
443                                 load(i,j,"",data["triple"]["leftOperand"]);
444                                 i = this.searchclauses.length;
445                                 load(i+1,j,"and",data["triple"]["rightOperand"]);
446                         } else {
447                                 load(i,j,"",data["triple"]["leftOperand"]);
448                                 j = this.searchclauses[i].length;
449                                 load(i,j,"or",data["triple"]["rightOperand"]);
450                         }
451                         
452                 }
453                 
454        },
455        */
456        // from querystring to searchclauses
457        buildfromquerystring: function(){
458                var arr = Query.simplequerystring(this.sctext);
459                var sc, screl;
460               
461                this.clear();
462               
463                //notifyUser(arr,'debug');
464                var arr_and = arr.split(" and ");
465                screl = "";
466                for( var i=0;i<arr_and.length;i++){
467                        var scstring = $.trim(arr_and[i]);
468                       
469                        if (scstring.substring(0,1) == "(" && scstring.substring(scstring.length-1) == ")") {
470                                scstring = scstring.substring(1,scstring.length-1);
471                                scstring = $.trim(scstring);
472                        }
473                       
474                        //notifyUser("i:" + scstring,'debug');
475                        var arr_or = scstring.split(" or ");
476                        if (i > 0) {screl = "and";}
477                        for( var j=0;j<arr_or.length;j++){
478                                var scstr = $.trim(arr_or[j]);
479                               
480                                // parse rel
481                                //notifyUser("j:"+scstr,'debug');
482                                var n;
483                                var scarr;
484                                var rel;
485                                n = scstr.indexOf("=");
486                                if (n > 0){
487                                        scarr = scstr.split("=");
488                                        rel = "=";
489                                }
490                                n = scstr.indexOf(" any ");
491                                if (n > 0){
492                                        scarr = scstr.split(" any ");
493                                        rel = "any";
494                                }
495                                n = scstr.indexOf(" contains ");
496                                if (n > 0){
497                                        scarr = scstr.split(" contains ");
498                                        rel = "contains";
499                                }
500                                n = scstr.indexOf("<");
501                                if (n > 0){
502                                        scarr = scstr.split("<");
503                                        rel = "<";
504                                }
505                                n = scstr.indexOf(">");
506                                if (n > 0){
507                                        scarr = scstr.split(">");
508                                        rel = ">";
509                                }
510                                n = scstr.indexOf(" all ");
511                                if (n > 0){
512                                        scarr = scstr.split(" all ");
513                                        rel = "all";
514                                }
515                                //var simplecalusetext = scstr.split("");
516                                if (j > 0) {screl = "or";}
517                                //notifyUser("scarr:" + scarr[0] + scarr[1],'debug');
518                                if (scarr == undefined) {
519                                        sc = new SearchClause('','=','');
520                                } else {
521                                        sc = new SearchClause(scarr[0],rel,scarr[1]);
522                                }
523
524                                searchclauseset.addsearchclause(sc,screl,i,j);
525                        }
526                }
527        },
528       
529        // from searchclauses to querystring
530        buildsctext: function(){
531           var uncompletequery = false;
532           var ptext = "";
533           
534                if ($('.searchtype_text','#search').size() > 0) {
535                        this.sctext = $('#query_area').val();
536                } else {
537               
538                        this.sctext = "";
539                        for (var i = 0; i < this.searchclauses.length; i++) {
540                                if ( i>0) this.sctext = this.sctext + " and ";
541                                this.sctext = this.sctext + " ( ";
542                                for (var j = 0; j < this.searchclauses[i].length; j++) {
543                                        if ( j>0) this.sctext = this.sctext + " or ";
544                                        this.sctext = this.sctext + " ( ";
545                                        ptext = this.searchclauses[i][j].PlainText();
546                                        if (ptext.length == 0){
547                                                uncompletequery = true;
548                                        }
549                                        this.sctext = this.sctext + ptext;
550                                        this.sctext = this.sctext + " ) ";
551                                }
552                                this.sctext = this.sctext + " ) ";
553                        }
554                       
555                }       
556                if (uncompletequery){
557                        this.sctext = "";
558                }
559                notifyUser(this.sctext);
560        },
561       
562        updatedata: function(term, is_category, category) {     
563                $('.focused','#searchclauselist').find('.index-input').val(term);
564               
565                var i = $('.focused','#searchclauselist').closest('.sc-i').attr("id");
566                var j = $('.focused','#searchclauselist').closest('.sc-j').attr("id");
567                searchclauseset.searchclauses[i][j].index = term.replace(" ","_");
568               
569                $('.focused','#searchclauselist').find('.value_input').focus();
570               
571                if (is_category) {
572                        searchclauseset.searchclauses[i][j].is_category = true;
573                        searchclauseset.searchclauses[i][j].category = category;
574                } else {
575                        searchclauseset.searchclauses[i][j].is_category = false;
576                }
577               
578        },
579       
580        initAutocomplete: function() {
581                notifyUser("init autocomplete",'debug');
582               
583                for (var i = this.searchclauses.length - 1; i > -1; i--){
584                        for (var j = this.searchclauses[i].length - 1; j > -1; j--){
585                                        this.searchclauses[i][j].initAutocomplete();           
586                        }
587                }       
588        }
589};
590
Note: See TracBrowser for help on using the repository browser.