1 | /* file: rbCriteriaBuilder.js - v1.1.2 | minified on 2011/05/15 with http://jscompress.com/ */ |
---|
2 | function criteriaBuilder(container) |
---|
3 | {var itemsId={group:'iCbGroup',item:'iCbItem',container:container},counters={group:0,item:0},options={textAND:'AND',textOR:'OR',textNoCriteria:'There is no criteria ! At least, one criteria is required to do search...',textHint:'',textSomethingWrong:'An error has occured on the server-side',textCaddieUpdated:'Caddie was updated',classGroup:'',classItem:'',classOperator:'',classHelper:'helper',opacity:0.8,onEdit:null,onDelete:null,onRequestSuccess:null,onRequestError:null,onGetPageSuccess:null,onGetPageError:null,helpEditUrl:'',helpDeleteUrl:'',helpMove:'',helpSwitchCondition:'',ajaxUrl:''},extraData=new Array();if(arguments.length==2) |
---|
4 | {if(typeof arguments[1]=='object') |
---|
5 | {options=jQuery.extend(options,arguments[1]);}} |
---|
6 | this.doAction=function(fct) |
---|
7 | {switch(fct) |
---|
8 | {case'add':if(arguments.length==3) |
---|
9 | {if(typeof arguments[1]=='string') |
---|
10 | {addItem(arguments[1],arguments[2]);}} |
---|
11 | break;case'delete':if(arguments.length==2) |
---|
12 | {if(typeof arguments[1]=='string') |
---|
13 | {deleteItem(arguments[1]);}} |
---|
14 | break;case'edit':if(arguments.length==4) |
---|
15 | {if(typeof arguments[1]=='string'&&typeof arguments[2]=='string') |
---|
16 | {editItem(arguments[1],arguments[2],arguments[3]);}} |
---|
17 | break;case'get':return(getItems());break;case'getExtraData':if(arguments.length==2) |
---|
18 | {return(getExtraData(arguments[1]));} |
---|
19 | else |
---|
20 | {return(null);} |
---|
21 | break;case'clear':clearItems();break;case'send':sendRequest();break;case'getPage':if(arguments.length==4) |
---|
22 | {getPage(arguments[1],arguments[2],arguments[3]);} |
---|
23 | break;case'setOptions':if(arguments.length==2) |
---|
24 | {return(setOptions(arguments[1]));} |
---|
25 | break;}};var addGroup=function(itemId) |
---|
26 | {counters.group++;var content="<li id='"+itemsId.group+counters.group+"' class='cbGroup cbSortable cbOpAND "+options.classGroup+" cbItemUnique'>";content+="<ul></ul></li>";$('#'+itemId).wrap(content);content="<div class='cbSortHandle' style='display:none;'>";content+="<div class='cbItemButtons' style='float:left;'>";content+="<div class='iconMove' id='iImgMoveItem"+counters.item+"' title=\""+options.helpMove+"\"></div>";content+="<div class='iconSwitchCondition' id='iImgSwitchCItem"+counters.item+"' title=\""+options.helpSwitchCondition+"\"></div>";content+="</div>";content+="<div id='"+itemsId.group+counters.group+"OpAND' class='"+options.classOperator+"' style='display:none;'>"+options.textAND+"</div>";content+="<div id='"+itemsId.group+counters.group+"OpOR' class='"+options.classOperator+"' style='display:none;'>"+options.textOR+"</div>";content+="</div>";$("#"+itemsId.group+counters.group).prepend(content);$('#'+itemsId.group+counters.group+'OpOR, #'+itemsId.group+counters.group+'OpAND, #'+itemsId.group+counters.group+' div.iconSwitchCondition ').bind('click',itemsId.group+counters.group,onSwitchOperator);applyNested();};var removeGroup=function(groupId) |
---|
27 | {$('#'+groupId).remove();};var addItem=function(itemContent,data) |
---|
28 | {counters.item++;var content="<li id='"+itemsId.item+counters.item+"' class='cbItem cbSortable "+options.classItem+"'>";content+="<div class='cbItemButtons' style='float:right;'>";if(options.onEdit!=null&&jQuery.isFunction(options.onEdit))content+="<div class='iconEdit' id='iImgEdit"+counters.item+"' title=\""+options.helpEdit+"\"></div>";if(options.onDelete!=null&&jQuery.isFunction(options.onDelete))content+="<div class='iconDelete' id='iImgDelete"+counters.item+"' title=\""+options.helpDelete+"\"></div>";content+="</div><div class='cbSortHandle'>";content+="<div class='cbItemButtons' style='float:left;'> <div class='iconMove' id='iImgMoveItem"+counters.item+"' title=\""+options.helpMove+"\"></div></div>";content+="<div class='itemContent'>"+itemContent+"</div></div></li>";$('#'+itemsId.container).append(content);addGroup(itemsId.item+counters.item);if(options.onEdit!=null)$('#iImgEdit'+counters.item).bind('click',itemsId.item+counters.item,options.onEdit);if(options.onDelete!=null)$('#iImgDelete'+counters.item).bind('click',itemsId.item+counters.item,options.onDelete);extraData[counters.item]=data;};var deleteItem=function(itemId) |
---|
29 | {if($('#'+itemId).length!=0) |
---|
30 | {$('#'+itemId).remove();var re=/[0-9]*$/;extraData[eval(re.exec(itemId)[0])]=null;manage();}};var editItem=function(itemId,content,data) |
---|
31 | {if($('#'+itemId).length!=0) |
---|
32 | {$('#'+itemId+' .itemContent').html(content);var re=/[0-9]*$/;extraData[eval(re.exec(itemId)[0])]=data;}};var clearItems=function() |
---|
33 | {$('#'+itemsId.container).NestedSortableDestroy();$('#'+itemsId.container).html("");counters.item=0;counters.group=0;extraData=new Array();};var serializeData=function(prefix,value) |
---|
34 | {var returned='';if(typeof value=='object') |
---|
35 | {for(var key in value) |
---|
36 | {if(typeof value[key]=='object') |
---|
37 | {returned+=serializeData(prefix+'['+key+']',value[key]);} |
---|
38 | else if(typeof value[key]=='string'||typeof value[key]=='number'||typeof value[key]=='boolean') |
---|
39 | {returned+='&'+prefix+'['+key+']='+value[key];}}} |
---|
40 | else if(typeof value=='string'||typeof value=='number'||typeof value=='boolean') |
---|
41 | {returned+='&'+prefix+'='+value;} |
---|
42 | return(returned);};var getItems=function() |
---|
43 | {var serialized=jQuery.iNestedSortable.serialize(itemsId.container)['hash'],tmp=Array();for(i=0;i<extraData.length;i++) |
---|
44 | {if(extraData[i]!=null) |
---|
45 | {serialized+=serializeData('extraData['+i+']',extraData[i]);}} |
---|
46 | $('#'+itemsId.container+' .cbGroup').each(function() |
---|
47 | {re=/[0-9]*$/;serialized+='&operator['+re.exec(this.id)[0]+']=';if($(this).hasClass('cbOpOR')) |
---|
48 | {serialized+='OR';} |
---|
49 | else |
---|
50 | {serialized+='AND';}});return(serialized);};var getExtraData=function(itemId) |
---|
51 | {var re=/[0-9]*$/;extraDataNumber=re.exec(itemId)[0];return(extraData[extraDataNumber]);};var setOptions=function(optionsToSet) |
---|
52 | {options=jQuery.extend(options,optionsToSet);};var displayOperator=function(groupId,visible) |
---|
53 | {if($('#'+groupId).hasClass('cbOpAND')) |
---|
54 | {if(visible) |
---|
55 | {$('#'+groupId+'OpAND').css('display','block');} |
---|
56 | else |
---|
57 | {$('#'+groupId+'OpAND').css('display','none');}} |
---|
58 | else |
---|
59 | {if(visible) |
---|
60 | {$('#'+groupId+'OpOR').css('display','block');} |
---|
61 | else |
---|
62 | {$('#'+groupId+'OpOR').css('display','none');}} |
---|
63 | if(visible) |
---|
64 | {$('#'+groupId).children('div.cbSortHandle').css('display','block');} |
---|
65 | else |
---|
66 | {$('#'+groupId).children('div.cbSortHandle').css('display','none');}};var manage=function() |
---|
67 | {$('#'+itemsId.container+' li').each(function() |
---|
68 | {if($(this).hasClass('cbGroup')) |
---|
69 | {if($('#'+this.id+' li.cbItem').length==0) |
---|
70 | {removeGroup(this.id);} |
---|
71 | else if($('#'+this.id+' li.cbItem').length==1) |
---|
72 | {$('#'+this.id).addClass('cbItemUnique').removeClass('cbItemMultiple');displayOperator(this.id,false);} |
---|
73 | else |
---|
74 | {$('#'+this.id).removeClass('cbItemUnique').addClass('cbItemMultiple');displayOperator(this.id,true);}} |
---|
75 | else if($(this).hasClass('cbItem')) |
---|
76 | {if($(this).parent().get(0).id==itemsId.container) |
---|
77 | {addGroup(this.id);}}});};var applyNested=function() |
---|
78 | {$('#'+itemsId.container).NestedSortableDestroy();$('#'+itemsId.container).NestedSortable({accept:'cbSortable',noNestingClass:'cbItem',opacity:options.opacity,helperclass:options.classHelper,serializeRegExp:/.*/i,autoScroll:true,handle:'.cbSortHandle:first',ghosting:false,nestingPxSpace:15,currentNestingClass:'cbItemOverGroup',onChange:function(serialized){manage();}});};onSwitchOperator=function(event) |
---|
79 | {var groupId=event.data;if($('#'+groupId).hasClass('cbOpAND')) |
---|
80 | {$('#'+groupId).removeClass('cbOpAND').addClass('cbOpOR');$('#'+groupId+'OpAND').css('display','none');$('#'+groupId+'OpOR').css('display','block');} |
---|
81 | else |
---|
82 | {$('#'+groupId).removeClass('cbOpOR').addClass('cbOpAND');$('#'+groupId+'OpAND').css('display','block');$('#'+groupId+'OpOR').css('display','none');}};var sendRequest=function() |
---|
83 | {if(extraData.length==0) |
---|
84 | {alert(options.textNoCriteria);return(false);} |
---|
85 | var datas=encodeURI('ajaxfct=public.rbuilder.searchExecute&requestName='+itemsId.container+'&'+getItems());$.ajax({type:"POST",url:options.ajaxUrl,async:true,data:datas,success:function(msg) |
---|
86 | {if(options.onRequestSuccess!=null&&jQuery.isFunction(options.onRequestSuccess))options.onRequestSuccess(msg);},error:function(msg) |
---|
87 | {if(options.onRequestError!=null&&jQuery.isFunction(options.onRequestError))options.onRequestError(msg);}});};var getPage=function(requestNumber,pageNumber,numberPerPage) |
---|
88 | {$.ajax({type:"POST",url:options.ajaxUrl,async:true,data:{ajaxfct:'public.rbuilder.searchGetPage',page:pageNumber,requestNumber:requestNumber,numPerPage:numberPerPage},success:function(msg) |
---|
89 | {if(options.onGetPageSuccess!=null&&jQuery.isFunction(options.onGetPageSuccess))options.onGetPageSuccess(msg);},error:function(msg) |
---|
90 | {if(options.onGetPageError!=null&&jQuery.isFunction(options.onGetPageError))options.onGetPageError(msg);}});};applyNested();};criteriaBuilder.makeExtendedData=function(owner,data) |
---|
91 | {return({owner:owner,param:data});} |
---|