(function(){"use strict";var e=["<div id='${id}_grid'></div>","<div id='${id}_noResponsesMessage' style='display:none' class='u8-campaigndesigner-noResponsesInResponsesSelector'></div>"].join("");u8.Crm.Widgets.ActivityResponseSelector=function(e,s){this.Widget(e,$.extend({},u8.Crm.Widgets.ActivityResponseSelector.defaults,s));$assert(this.responsesState,"Argument 'responsesState' must not be null or empty.");this._isCreated=false;this._grid=null;u8.using({texts:["Campaign_Designer"]},this._create.bind(this))};u8.Crm.Widgets.ActivityResponseSelector.makeClass({name:"ActivityResponseSelector",deriveFrom:u8.Base.Widgets.Widget,events:["Create"]});u8.Crm.Widgets.ActivityResponseSelector.defaults={isModified:false,isSaved:false,listHeight:150};$.extend(u8.Crm.Widgets.ActivityResponseSelector.prototype,{getValue:function(){return this.responsesState},setValue:function(e){this.responsesState=e},_setModified:function(e){this.isModified=e;if(e){this.isSaved=false}},verify:function(){return true},dispose:function(){if(this._isDisposed){return}this.responsesState=null;this._grid=null;this.Widget_dispose()},_create:function(){this.$(this.element).utml({template:e,itemOptions:{parent:this},data:this},function(){this.focus();this._createGrid();u8.tools.createdWhenReady(this)}.bind(this))},_createGrid:function(){if(!this._grid){var e=new u8.Base.Widgets.GridModel({hasRowCheck:true,columns:[{type:"Text",visible:false,dataKey:"responseText"},{type:"Text",dataKey:"displayText"}],selectionMode:"multi"});var s=this.$("#"+this.id+"_grid").on("keyup.activityResponseSelector",this._onGridKeyUp.bind(this));this._grid=u8.widget("Grid",s,{header:false,parent:this,height:this.listHeight,allowSorting:false,gridLayout:"fit",model:e,showColumnLabels:false,onRowCheck:this._onRowCheck.bind(this)})}this._updateGrid()},_updateGrid:function(){if(!this._grid){return}var e=this._grid.model;e.setData(this.responsesState.responseUsages,{createRows:true});var s=e.getRowCount();for(var t=0;t<s;t++){var i=e.getRow(t);var n=e.getRowData(t);i.isChecked=n.selected;if(n.isLocked===true){i.setDisabled(t)}}e.update();if(s===0){var o=this.$("#"+this.id+"_noResponsesMessage");var r=this.$("#"+this.id+"_grid");o.text($t("TXT_Campaign_Designer_195"));o.show();r.hide()}},_getSelectedResponses:function(){var e=this._grid.getModel();var s=e.getSelectedRows();return(s||[]).map(function(s){return(e.getRowData(s.dataKey)||{}).responseText})},_onGridKeyUp:function(e){var s=$.ui.keyCode;switch(e.keyCode){case s.SPACE:if(e.target.className.indexOf("u8-checkbox")>-1){return}this._toggleResponses(this._getSelectedResponses());this._keepSelection(this._updateGrid,this);break;default:break}},_keepSelection:function(e,s){var t=this._grid;var i=t.getModel();var n=this._getSelectedResponses();e.call(s);var o=n.map(function(e){var s=i.data.indexWhere(u8._.eqish("responseText",e));return s===-1?undefined:s}).clean();i.setSelection(o.length?o:0);t.focus()},_onRowCheck:function(e,s){if(e){e.model.setSelection(s.rowIndex)}this.responsesState.responseUsages[s.rowIndex].selected=s.isChecked},_toggleResponses:function(e){for(var s=0;s<this.responsesState.responseUsages.length;s++){var t=$.inArray(this.responsesState.responseUsages[s].responseText,e);if(t>=0){this.responsesState.responseUsages[s].selected=!this.responsesState.responseUsages[s].selected}}}})})();