(function(){u8.Base.Widgets.JobStatus=function(t,e){this.Widget(t,$.extend({},u8.Base.Widgets.JobStatus.defaults,e));$assert(this.executionId,"Failed to create job status widget! Missing property 'executionId'.");u8.using({texts:["net_jobmanager_texts"],templates:[{name:"u8-jobstatus",url:"~/base/jobstatus.utml.htm"}]},this._create.bind(this))};u8.Base.Widgets.JobStatus.defaults={autoRefreshInterval:3500,theme:{css:{root:"u8-jobstatus",label:"u8-jobstatus-label",progress:"u8-jobstatus-progress",tasks:"u8-jobstatus-tasks",taskStatus:{Executing:"u8-anim-rotation"}},image:{taskStatus:{Void:"u8-tasks-open-xs.png",Todo:"u8-tasks-open-xs.png",Executing:"u8-progresswheel-s.png",Complete:"u8-tasks-completed-xs.png",Failed:"u8-tasks-failed-xs.png"}}}};u8.Base.Widgets.JobStatus.makeClass({name:"JobStatus",deriveFrom:u8.Base.Widgets.Widget,events:["Create","StatusChange"]});$.extend(u8.Base.Widgets.JobStatus.prototype,{dispose:function(){if(this._isDisposed){return}this._clearRefreshTimer();this.Widget_dispose()},setExecutionId:function(t){if(t==this.executionId){return}this._clearRefreshTimer();this.executionId=t;this._update()},getExecutionId:function(){return this.executionId},_create:function(){this._createModel();Function.chain(this._createView,this._update).call(this,function(){this.raise("Create")})},_createModel:function(){this._model=u8.create("Model",{properties:{text:"",percentComplete:0}});var t=($t("TXT_net_jobmanager_texts_4")||"Tasks,State").split(",");var e=[{dataKey:"name",width:"80%"},{dataKey:"status",width:"20%",cellRenderer:this._imageRenderer.bind(this)}];e.each(function(e,s){e.label=t[s]});this._gridModel=u8.create("GridModel",{columns:e})},_createView:function(t){var e={id:this.id,headerOptions:{id:this.id+"_header",fixedText:"Job"},labelOptions:{bindings:"#text==>@text"},rangeInputOptions:{bindings:"#percentComplete==>@value",showValue:false,showCurrentValue:true,readonly:true,normalizer:{type:"PercentageNormalizer",options:{postfix:"%",decimalDigits:0}}},gridOptions:{model:this._gridModel,allowColumnReordering:false,allowColumnResizing:false,allowSorting:false,showEmptyGrid:false,header:null},css:this.theme.css,showLabel:this.showStatusText!==false,showHeader:this.showHeader!==false};this.$(this.element).empty().addClass(this.theme.css.root).utml({name:"u8-jobstatus",itemOptions:{parent:this,context:this._model},data:e},t)},_update:function(t){u8.services.jobs.getJob(this.executionId,function(e,s){this._updateJob(s.job);return t&&t()}.bind(this))},_updateJob:function(t){if(!t){return}this.job=t;var e=this.children.get(this.id+"_header");if(e){e.attr({fixedText:t.name})}var s=false;var i=0;var a="";if(t.status==u8.Base.Jobs.JobStatus.queued){a=$t("TXT_net_jobmanager_texts_0");s=true}else if(t.status==u8.Base.Jobs.JobStatus.executing){i=t.percentComplete||0;a=$t("TXT_net_jobmanager_texts_5");s=true}else if(t.status==u8.Base.Jobs.JobStatus.failed){i=100;a=$t("TXT_net_jobmanager_texts_1")}else if(t.status==u8.Base.Jobs.JobStatus.complete){i=100}this._model.set("percentComplete",i);this._model.set("text",a);var o=t.tasks;var n=o?o.indexWhere(u8._.eq("status","Executing")):-1;this._gridModel.setData(o,{createRows:true,rowFn:function(t){return t===n?{isSelected:true}:{}}});this._gridModel.update({from:0,to:this._gridModel.getRowCount()-1});if(t.exception){var r=t.exception;u8.log.error("Job #"+t.executionId+" failed with an exception.\n"+r.text+"\n\nStackTrace:\n"+(r.stackTrace||""))}this._setStatus(t.status);if(s&&this.autoRefreshInterval){this._timer=setTimeout(this._update.bind(this),this.autoRefreshInterval)}},_imageRenderer:function(t,e){var s=this.$(e.elCell).empty();var i=e.cellData;var a=this.theme.image.taskStatus[i];var o=this.theme.css.taskStatus[i];var n=this.$("<img/>").appendTo(s);if(a){n.image(a)}if(o){n.addClass(o)}},_clearRefreshTimer:function(){if(this._timer){clearTimeout(this._timer);this._timer=null}},_setStatus:function(t){if(this.status==t){return}this.status=t;this.raise("StatusChange",{})}})})();