
Ext.namespace("Ext.ux.application.controllers.recherche");Ext.ux.application.controllers.recherche=Ext.extend(Ext.util.Observable,{constructor:function(config,viewCmp){this.viewCmp=viewCmp;this.store=new Ext.data.JsonStore({url:config.url,root:"recherche",idProperty:"categorie",fields:['categorie','results','type','url'],baseParams:{view:Ext.value(config.viewMode,"liste")},listeners:{scope:this,load:this.onReceivedResults}});Ext.ux.application.controllers.recherche.superclass.constructor.call(this,config);},onReceivedResults:function(store,records){this.viewCmp.showPreRecherche(records);},initLocalDB:function(){if(!this.localDB){this.localDB=openDatabase("INA_DB","1.0","Sauvegarde des recherches",200000);this.localDB.transaction(function(tx){tx.executeSql("CREATE TABLE IF NOT EXISTS Searchs (search TEXT PRIMARY KEY)");});}},storeInLocalDB:function(search){if(!window.openDatabase)
return;this.initLocalDB();this.localDB.transaction(function(tx){tx.executeSql("INSERT INTO Searchs (search) VALUES('"+search+"')");});},selectInLocalDB:function(query){if(!window.openDatabase)
return false;this.initLocalDB();var results=[];this.localDB.transaction(function(tx){tx.executeSql("SELECT DISTINCT search FROM Searchs WHERE search LIKE ?",[query+"%"],function(tx,sqlResults){for(var iter=0;iter<sqlResults.rows.length;iter++)
results.push(sqlResults.rows.item(iter).search);});});return results;}});Ext.namespace("Ext.ux.application.views.recherche");Ext.ux.application.views.recherche=Ext.extend(Ext.form.TwinTriggerField,{mode:"local",store:["a","b","c"],enableKeyEvents:true,validationEvent:false,validateOnBlur:false,trigger1Class:'x-form-clear-trigger',trigger2Class:'x-form-search-trigger',trigger3Class:'x-form-search-advanced-trigger',hideTrigger1:true,width:180,store:new Ext.data.Store(),hasSearch:false,paramName:'query',listeners:{keyup:function(f,e){if(e.getKey()!=e.ENTER)
this.onTrigger1Click(true);},specialkey:function(f,e){if(e.getKey()==e.ENTER)
this.onTrigger2Click();}},constructor:function(config){this.controller=new Ext.ux.application.controllers.recherche(config,this);Ext.ux.application.views.recherche.superclass.constructor.call(this,Ext.apply({store:this.controller.store},config));},initComponent:function(){Ext.ux.application.views.recherche.superclass.initComponent.call(this);this.triggerConfig.cn.push({tag:"img",src:Ext.BLANK_IMAGE_URL,alt:"",cls:"x-form-trigger "+this.trigger3Class});},onTrigger1Click:function(dontRemoveText){if(dontRemoveText!==true)
this.el.dom.value='';this.triggers[0].hide();this.removePreRecherche();},onTrigger2Click:function(){var v=this.getRawValue();if(v.length<1)
return;this.store.baseParams=this.store.baseParams||{};this.store.baseParams[this.paramName]=v;this.store.reload();this.triggers[0].show();this.controller.storeInLocalDB(v);},onTrigger3Click:function(){window.location.href=this.urlAdvancedSearch+"simple/"+this.getValue();},showPreRecherche:function(records){this.preRechercheCmp=new Ext.Panel({renderTo:Ext.getBody(),border:false,cls:"ux-fenetre-resultats",style:{position:"absolute"},x:this.el.getX()-40,y:this.el.getY()+this.el.getHeight(),width:this.width+40});Ext.each(records,function(record){var categoriePanel={border:false,items:[]};switch(record.get("type")){case"list-tpl":Ext.apply(categoriePanel,{collapsible:true,title:record.id});Ext.each(record.get("results"),function(result){categoriePanel.items.push({baseCls:'x-plain',layout:"table",cls:"ux-recherche-item",defaults:{type:"box",border:false},layoutConfig:{columns:2},items:[{cellCls:"ux-recherche-item-titre",html:result.titre},{rowspan:2,xtype:"button",cls:"ux-button ux-recherche-item-button",text:result.buttonLabel,handler:(function(url){window.location=url}).createCallback(result.buttonLink)},{cellCls:"ux-recherche-item-content",html:result.content}]});},this);break;case"link":Ext.apply(categoriePanel,{collapsible:false,title:'<a href="'+record.get("url")+'">'+record.get("categorie")+'</a>'});break;default:}
this.preRechercheCmp.add(categoriePanel)},this);this.preRechercheCmp.doLayout();},removePreRecherche:function(){if(this.preRechercheCmp){this.preRechercheCmp.collapse();this.preRechercheCmp.on("collapse",function(p){p.destroy();})}}});Ext.reg("ux-recherche",Ext.ux.application.views.recherche);

