var UserChooser=function(config){this.config=Ext.apply({},config,{only:false,URLRoot:false});if(this.config.URLRoot){URLRoot=this.config.URLRoot}this.config.url=URLRoot+"/?module=fileman&section=utils&page=user_chooser_tree";this.initTree();this.triggerField=new Ext.form.TriggerField({triggerClass:"fa-close",emptyText:"Search",onTriggerClick:this.cancelSearch.createDelegate(this),enableKeyEvents:true,width:"100%",listeners:{keyup:{buffer:500,fn:function(field,e){if(Ext.EventObject.ESC==e.getKey()){field.onTriggerClick()}else{this.reload()}}},scope:this}});this.dlg=new Ext.Window(Ext.apply({width:320,height:320,maximized:Ext.isSmallScreen,cls:"x-window-prompt",title:this.config.only=="groups"?"Select Groups":"Select Users",modal:true,layout:"border",tools:[{toolType:"search",iconCls:"fa-search",tooltip:"Search",handler:function(){this.searchBar.show();this.triggerField.focus()},scope:this},{toolType:"refresh",iconCls:"fa-refresh",tooltip:"Reload list",handler:function(){this.reload()},scope:this}],tbar:this.searchBar=new Ext.Toolbar({buttonAlign:"center",items:[this.triggerField],hidden:true}),items:{autoScroll:true,region:"center",margins:"10px 0",items:this.tree},buttons:[{cls:"fr-btn-primary",text:"Ok",handler:this.doCallback,scope:this},{style:"margin-left:10px",text:"Cancel",handler:function(){this.dlg.hide()},scope:this}]},config&&config.windowCfg?config.windowCfg:{}))};UserChooser.prototype={initTree:function(){this.tree=new Ext.tree.TreePanel({animate:true,containerScroll:true,rootVisible:false,autoScroll:true,bodyStyle:"padding-bottom:15px",cls:"user-chooser-tree",selModel:new Ext.tree.MultiSelectionModel,listeners:{beforeclick:function(node){if(node.id=="add-guest-user"){this.dlg.hide();this.config.addGuest()}if(node.attributes.noSelect){return false}},scope:this}});this.TreeLoader=Ext.extend(Ext.tree.TreeLoader,{dataUrl:this.config.url,nodeParameter:null,listeners:{load:function(){this.tree.ownerCt.el.unmask()},loadexception:function(){this.tree.ownerCt.el.unmask()},beforeload:function(loader,node){this.tree.ownerCt.el.mask("Loading...");var p={allow_all:this.config.allowAll?1:0,showSelf:this.config.showSelf?1:0,addGuest:this.config.addGuest?1:0};var searchKey=this.triggerField.getRawValue();if(searchKey){p.search=searchKey}if(node.attributes.gid){p.gid=node.attributes.gid}if(this.config.only){p.only=this.config.only}loader.baseParams=p},scope:this}});this.rootNode=new Ext.tree.AsyncTreeNode({loader:new this.TreeLoader({applyLoader:true})});this.tree.setRootNode(this.rootNode)},cancelSearch:function(){if(this.triggerField.getRawValue()!=""){this.triggerField.setValue("");this.reload()}this.searchBar.hide()},show:function(el,callback,scope){this.dlg.show(el);this.searchBar.hide();var task=new Ext.util.DelayedTask(function(){},this);task.delay(500);this.callback=callback;this.scope=scope},clearChecked:function(){this.tree.getSelectionModel().clearSelections()},doCallback:function(){var data={users:[],groups:[]};Ext.each(this.tree.getSelectionModel().getSelectedNodes(),function(node){var type=node.attributes.uid?"users":"groups";var id=node.attributes.uid?node.attributes.uid:node.attributes.gid;if(type=="groups"&&id=="-"&&!this.config.allowAll){}else{data[type].push(node.attributes)}},this);if(this.scope){this.callback.createDelegate(this.scope,[data]).call()}else{this.callback(data)}this.cancelSearch();this.dlg.hide()},reload:function(){this.rootNode.loaded=false;this.rootNode.collapse();this.rootNode.expand()}};