/*! grafana - v4.5.1 - 2017-09-19
 * Copyright (c) 2017 Torkel Ödegaard; Licensed Apache-2.0 */

System.register(["lodash","moment","app/core/utils/kbn"],function(a,b){"use strict";var c,d,e,f;b&&b.id;return{setters:[function(a){c=a},function(a){d=a},function(a){e=a}],execute:function(){f=function(){function a(a,b,c,d,e){this.panel=a,this.table=b,this.isUtc=c,this.sanitize=d,this.templateSrv=e,this.initColumns()}return a.prototype.setTable=function(a){this.table=a,this.initColumns()},a.prototype.initColumns=function(){this.formatters=[],this.colorState={};for(var a=0;a<this.table.columns.length;a++){var b=this.table.columns[a];b.title=b.text;for(var c=0;c<this.panel.styles.length;c++){var d=this.panel.styles[c],f=e.default.stringToJsRegex(d.pattern);if(b.text.match(f)){b.style=d,d.alias&&(b.title=b.text.replace(f,d.alias));break}}this.formatters[a]=this.createColumnFormatter(b)}},a.prototype.getColorForValue=function(a,b){if(!b.thresholds)return null;for(var d=b.thresholds.length;d>0;d--)if(a>=b.thresholds[d-1])return b.colors[d];return c.default.first(b.colors)},a.prototype.defaultCellFormatter=function(a,b){return null===a||void 0===a||void 0===a?"":(c.default.isArray(a)&&(a=a.join(", ")),b&&b.sanitize?this.sanitize(a):c.default.escape(a))},a.prototype.createColumnFormatter=function(a){var b=this;if(!a.style)return this.defaultCellFormatter;if("hidden"===a.style.type)return function(a){};if("date"===a.style.type)return function(e){if(void 0===e||null===e)return"-";c.default.isArray(e)&&(e=e[0]);var f=d.default(e);return b.isUtc&&(f=f.utc()),f.format(a.style.dateFormat)};if("number"===a.style.type){var f=e.default.valueFormats[a.unit||a.style.unit];return function(d){return null===d||void 0===d?"-":c.default.isString(d)?b.defaultCellFormatter(d,a.style):(a.style.colorMode&&(b.colorState[a.style.colorMode]=b.getColorForValue(d,a.style)),f(d,a.style.decimals,null))}}return function(c){return b.defaultCellFormatter(c,a.style)}},a.prototype.renderRowVariables=function(a){for(var b,c={},d=this.table.rows[a],e=0;e<d.length;e++)b="__cell_"+e,c[b]={value:d[e]};return c},a.prototype.formatColumnValue=function(a,b){return this.formatters[a]?this.formatters[a](b):b},a.prototype.renderCell=function(a,b,c,d){void 0===d&&(d=!1),c=this.formatColumnValue(a,c);var e=this.table.columns[a],f="",g=[],h="";this.colorState.cell?(f=' style="background-color:'+this.colorState.cell+';color: white"',this.colorState.cell=null):this.colorState.value&&(f=' style="color:'+this.colorState.value+'"',this.colorState.value=null);var i="";if(d&&(i='<div class="table-panel-width-hack">'+this.table.columns[a].title+"</div>"),void 0===c?(f=' style="display:none;"',e.hidden=!0):e.hidden=!1,e.style&&e.style.preserveFormat&&g.push("table-panel-cell-pre"),e.style&&e.style.link){var j=this.renderRowVariables(b);j.__cell={value:c};var k=this.templateSrv.replace(e.style.linkUrl,j),l=this.templateSrv.replace(e.style.linkTooltip,j),m=e.style.linkTargetBlank?"_blank":"";g.push("table-panel-cell-link"),i+='\n        <a href="'+k+'" target="'+m+'" data-link-tooltip data-original-title="'+l+'" data-placement="right">\n          '+c+"\n        </a>\n      "}else i+=c;return e.filterable&&(g.push("table-panel-cell-filterable"),i+='\n        <a class="table-panel-filter-link" data-link-tooltip data-original-title="Filter out value" data-placement="bottom"\n           data-row="'+b+'" data-column="'+a+'" data-operator="!=">\n          <i class="fa fa-search-minus"></i>\n        </a>\n        <a class="table-panel-filter-link" data-link-tooltip data-original-title="Filter for value" data-placement="bottom"\n           data-row="'+b+'" data-column="'+a+'" data-operator="=">\n          <i class="fa fa-search-plus"></i>\n        </a>'),g.length&&(h=' class="'+g.join(" ")+'"'),i="<td"+h+f+">"+i+"</td>"},a.prototype.render=function(a){for(var b=this.panel.pageSize||100,c=a*b,d=Math.min(c+b,this.table.rows.length),e="",f=c;f<d;f++){for(var g=this.table.rows[f],h="",i="",j=0;j<this.table.columns.length;j++)h+=this.renderCell(j,f,g[j],f===c);this.colorState.row&&(i=' style="background-color:'+this.colorState.row+';color: white"',this.colorState.row=null),e+="<tr "+i+">"+h+"</tr>"}return e},a.prototype.render_values=function(){for(var a=[],b=0;b<this.table.rows.length;b++){for(var c=this.table.rows[b],d=[],e=0;e<this.table.columns.length;e++)d.push(this.formatColumnValue(e,c[e]));a.push(d)}return{columns:this.table.columns,rows:a}},a}(),a("TableRenderer",f)}}});