(function(){u8.Crm.Widgets.QueryResultSnapshotPresenter=function(e,t){this.Widget(e,u8.tools.extend(true,{},u8.Crm.Widgets.QueryResultSnapshotPresenter.defaults,t));this.resultSet=null;this.snapshot=new u8.Crm.QueryResultSnapshot(this.snapshot);this._idGrid=this.id+"_grid";if(!this.dataProvider){this.dataProvider=new u8.Crm.QueryResultSnapshotDataProvider({snapshot:this.snapshot,__owner:this})}if(!this.resultSet){this.resultSet=new u8.Crm.ResultSet({dataProvider:this.dataProvider,autoRefresh:true,moreRowsExist:true,__owner:this})}this._grid=null;if(typeof this.commandDelegates=="undefined"){this.commandDelegates=[u8.services.queries.snapshots.createCommandDelegate({snapshot:this.snapshot,resultSet:this.resultSet,elWindow:$w(this.element)})]}this.commandBindings={Delete:"Crud/Delete","RecordSet/Fill":{important:true,execute:this._onCommandRecordSetFill.bind(this)}};this._create()};u8.Crm.Widgets.QueryResultSnapshotPresenter.makeClass({name:"QueryResultSnapshotPresenter",deriveFrom:u8.Base.Widgets.Widget,events:["SelectionChange"]});u8.Crm.Widgets.QueryResultSnapshotPresenter.defaults={autoExecute:true,queryNotFoundTextId:"TXT_XSelResultsText_1",snapshot:{maxRows:20},grid:{pageSize:20,header:{headerName:"ShowQueryResultSet",headerGroupName:"Special"}}};$.extend(u8.Crm.Widgets.QueryResultSnapshotPresenter.prototype,{create:function(e){Function.chain(this._loadResources,this._load,function(e){e(this.autoExecute?null:"end")},this.update.curry(null)).call(this,e)},dispose:function(){if(this._isDisposed){return}if(this.dataProvider&&this.dataProvider.__owner==this){this.dataProvider.dispose()}if(this.resultSet&&this.resultSet.__owner==this){this.resultSet.dispose()}this.Widget_dispose()},setHeader:function(e){this._header=e;if(!this._header){return}e.parameters={1:this.snapshot.name};e.view={showCollapsed:true};e.setCollapsed(this.formCollapsed)},update:function(e,t){if(!u8.isValidRecordUid(this.snapshot.uid)){t();return}if(!e){if(this._grid){return this._grid.update({page:true})}e={from:0,to:this.resultSet.getNumberOfRows()-1}}if(typeof e.from=="undefined"){e.from=0}if(!e.to){e.to=this.snapshot.maxRows-1}if(e.to<=0){e.to=this._grid?Math.max(this._grid.getPageSize()-1,0):0}if(typeof e.filter!="undefined"){this.snapshot.filter=e.filter}if(e.pageSize){this._grid.setPageSize(e.pageSize)}e.reload=true;this.resultSet.load(e,t)},getResultGrid:function(){return this._grid},_updateHeader:function(){if(!this._header){return}this._header.parameters={1:this.snapshot.name};this._header.update({caption:true})},_loadResources:function(e){var t=this;var s=["search_text","XSelResultsText","net_ListIconLabels","XSelDefineText","XSelFilter_Text","standard_header_texts"];if(t.displayTextId){s.push(u8.services.texts.textIdToTextGroupName(t.displayTextId))}if(t.queryNotFoundTextId){s.push(u8.services.texts.textIdToTextGroupName(t.queryNotFoundTextId))}u8.using({texts:s},function(){if(t.displayTextId){t.displayText=u8.services.texts.get(t.displayTextId)}if(t.queryNotFoundTextId){t.queryNotFoundText=u8.services.texts.get(t.queryNotFoundTextId)}}.chain(e))},_load:function(e){var t=this;this.snapshot.load(s);function s(s,i){if(i.error&&t.queryNotFoundText){var r=u8.format(t.queryNotFoundText,{QUERYNAME:t.snapshot.name});if(r){u8.services.notification.error(r)}}t._updateHeader();e()}},_create:function(){this.$(this.element).html("<div id='"+this._idGrid+"'></div>");if(this.header){this.setHeader(this.header)}this._createGrid()},_createGrid:function(){var e={resultSet:this.resultSet,selectionMode:"single",canEdit:false,useDefaultActionOnDblClick:false,showQuickViewAction:false,showMiniDetails:false,parent:this,onSelectionChange:this._onGridSelectionChange.bind(this)};u8.tools.extend(e,this.grid);this._grid=new u8.Crm.Widgets.ResultSetGrid(this.$("#"+this._idGrid).get(0),e)},_onGridSelectionChange:function(e,t){this.raise("SelectionChange",t)},_onCommandRecordSetFill:function(e,t,s){var i=s.recordSet;if(s.name!=="all"){return}s.handled=true;s.pending=true;var r={};var o=this.snapshot.clone();o.filter=null;o.maxRows=-1;o.skipRows=0;o.execute(r,a);var n=s;function a(e,t){t.resultSet.toRecordSet(n.recordSet);n.recordSet.remove("Q3");if(typeof n.proceed=="function"){n.proceed(i)}}}})})();