/*! grafana - v3.1.0 - 2016-07-18
 * Copyright (c) 2016 Torkel Ödegaard; Licensed Apache-2.0 */

System.register(["lodash","jquery","app/core/utils/file_export","app/plugins/sdk","./transformers","./editor","./renderer"],function(a){var b,c,d,e,f,g,h,i,j=this&&this.__extends||function(a,b){function c(){this.constructor=a}for(var d in b)b.hasOwnProperty(d)&&(a[d]=b[d]);a.prototype=null===b?Object.create(b):(c.prototype=b.prototype,new c)};return{setters:[function(a){b=a},function(a){c=a},function(a){d=a},function(a){e=a},function(a){f=a},function(a){g=a},function(a){h=a}],execute:function(){i=function(a){function e(c,d,e,f){a.call(this,c,d),this.annotationsSrv=e,this.$sanitize=f,this.panelDefaults={targets:[{}],transform:"timeseries_to_columns",pageSize:null,showHeader:!0,styles:[{type:"date",pattern:"Time",dateFormat:"YYYY-MM-DD HH:mm:ss"},{unit:"short",type:"number",decimals:2,colors:["rgba(245, 54, 54, 0.9)","rgba(237, 129, 40, 0.89)","rgba(50, 172, 45, 0.97)"],colorMode:null,pattern:"/.*/",thresholds:[]}],columns:[],scroll:!0,fontSize:"100%",sort:{col:0,desc:!0}},this.pageIndex=0,void 0===this.panel.styles&&(this.panel.styles=this.panel.columns,this.panel.columns=this.panel.fields,delete this.panel.columns,delete this.panel.fields),b["default"].defaults(this.panel,this.panelDefaults),this.events.on("data-received",this.onDataReceived.bind(this)),this.events.on("data-error",this.onDataError.bind(this)),this.events.on("data-snapshot-load",this.onDataReceived.bind(this)),this.events.on("init-edit-mode",this.onInitEditMode.bind(this)),this.events.on("init-panel-actions",this.onInitPanelActions.bind(this))}return j(e,a),e.$inject=["$scope","$injector","annotationsSrv","$sanitize"],e.prototype.onInitEditMode=function(){this.addEditorTab("Options",g.tablePanelEditor,2)},e.prototype.onInitPanelActions=function(a){a.push({text:"Export CSV",click:"ctrl.exportCsv()"})},e.prototype.issueQueries=function(b){return this.pageIndex=0,"annotations"===this.panel.transform?(this.setTimeQueryStart(),this.annotationsSrv.getAnnotations(this.dashboard).then(function(a){return{data:a}})):a.prototype.issueQueries.call(this,b)},e.prototype.onDataError=function(a){this.dataRaw=[],this.render()},e.prototype.onDataReceived=function(a){this.dataRaw=a,this.pageIndex=0,this.dataRaw&&this.dataRaw.length&&("table"===this.dataRaw[0].type?this.panel.transform="table":"docs"===this.dataRaw[0].type?this.panel.transform="json":"table"!==this.panel.transform&&"json"!==this.panel.transform||(this.panel.transform="timeseries_to_rows")),this.render()},e.prototype.render=function(){return this.table=f.transformDataToTable(this.dataRaw,this.panel),this.table.sort(this.panel.sort),a.prototype.render.call(this,this.table)},e.prototype.toggleColumnSort=function(a,b){this.table.columns[this.panel.sort.col]&&(this.table.columns[this.panel.sort.col].sort=!1),this.panel.sort.col===b?this.panel.sort.desc?this.panel.sort.desc=!1:this.panel.sort.col=null:(this.panel.sort.col=b,this.panel.sort.desc=!0),this.render()},e.prototype.exportCsv=function(){d.exportTableDataToCsv(this.table)},e.prototype.link=function(a,b,d,e){function f(){var a=e.height;return n>1&&(a-=26),a-31+"px"}function g(a){var b=new h.TableRenderer(m,l,e.dashboard.isTimezoneUtc(),e.$sanitize);a.empty(),a.html(b.render(e.pageIndex))}function i(a){var b=c["default"](a.currentTarget);e.pageIndex=parseInt(b.text(),10)-1,k()}function j(a){a.empty();var b=m.pageSize||100;if(n=Math.ceil(l.rows.length/b),1!==n){for(var d=Math.max(e.pageIndex-3,0),f=Math.min(n,d+9),g=c["default"]("<ul></ul>"),h=d;h<f;h++){var i=h===e.pageIndex?"active":"",j=c["default"]('<li><a class="table-panel-page-link pointer '+i+'">'+(h+1)+"</a></li>");g.append(j)}a.append(g)}}function k(){var a=b.parents(".panel"),c=b.find(".table-panel-scroll"),d=b.find("tbody"),e=b.find(".table-panel-footer");b.css({"font-size":m.fontSize}),a.addClass("table-panel-wrapper"),g(d),j(e),c.css({"max-height":m.scroll?f():""})}var l,m=e.panel,n=0;b.on("click",".table-panel-page-link",i),a.$on("$destroy",function(){b.off("click",".table-panel-page-link")}),e.events.on("render",function(a){l=a||l,l&&k(),e.renderingCompleted()})},e.templateUrl="module.html",e}(e.MetricsPanelCtrl),a("TablePanelCtrl",i),a("PanelCtrl",i)}}});