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

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