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

define(["lodash","jquery","../core_module"],function(a,b,c){"use strict";c["default"].directive("metricSegment",["$compile","$sce",function(c,d){var e='<input type="text" data-provide="typeahead"  class="gf-form-input input-medium" spellcheck="false" style="display:none"></input>',f='<a class="gf-form-label" ng-class="segment.cssClass" tabindex="1" give-focus="segment.focus" ng-bind-html="segment.html"></a>',g='<a class="gf-form-input gf-form-input--dropdown" ng-class="segment.cssClass" tabindex="1" give-focus="segment.focus" ng-bind-html="segment.html"></a>';return{scope:{segment:"=",getOptions:"&",onChange:"&"},link:function(h,i,j){var k=b(e),l=b("select"===j.styleMode?g:f),m=h.segment,n=null,o=null,p=!0;k.appendTo(i),l.appendTo(i),h.updateVariableValue=function(b){""!==b&&m.value!==b&&h.$apply(function(){var c=a.findWhere(h.altSegments,{value:b});c?(m.value=c.value,m.html=c.html,m.fake=!1,m.expandable=c.expandable):"false"!==m.custom&&(m.value=b,m.html=d.trustAsHtml(b),m.expandable=!0,m.fake=!1),h.onChange()})},h.switchToLink=function(a){p&&!a||(clearTimeout(o),o=null,p=!0,k.hide(),l.show(),h.updateVariableValue(k.val()))},h.inputBlur=function(){o=setTimeout(h.switchToLink,200)},h.source=function(b,c){return n?n:void h.$apply(function(){h.getOptions().then(function(b){h.altSegments=b,n=a.map(h.altSegments,function(a){return a.value}),"false"!==m.custom&&(m.fake||a.indexOf(n,m.value)!==-1||n.unshift(m.value)),c(n)})})},h.updater=function(a){return a===m.value?(clearTimeout(o),k.focus(),a):(k.val(a),h.switchToLink(!0),a)},h.matcher=function(a){var b=this.query;"/"===b[0]&&(b=b.substring(1)),"/"===b[b.length-1]&&(b=b.substring(0,b.length-1));try{return a.toLowerCase().match(b)}catch(c){return!1}},k.attr("data-provide","typeahead"),k.typeahead({source:h.source,minLength:0,items:1e4,updater:h.updater,matcher:h.matcher});var q=k.data("typeahead");q.lookup=function(){this.query=this.$element.val()||"";var a=this.source(this.query,b.proxy(this.process,this));return a?this.process(a):a},l.keydown(function(a){40!==a.keyCode&&13!==a.keyCode||l.click()}),l.click(function(){n=null,k.css("width",l.width()+16+"px"),l.hide(),k.show(),k.focus(),p=!1;var a=k.data("typeahead");a&&(k.val(""),a.lookup())}),k.blur(h.inputBlur),c(i.contents())(h)}}}]),c["default"].directive("metricSegmentModel",["uiSegmentSrv","$q",function(b,c){return{template:'<metric-segment segment="segment" get-options="getOptionsInternal()" on-change="onSegmentChange()"></metric-segment>',restrict:"E",scope:{property:"=",options:"=",getOptions:"&",onChange:"&"},link:{pre:function(d,e,f){d.valueToSegment=function(c){var e=a.findWhere(d.options,{value:c}),g={cssClass:f.cssClass,custom:f.custom,value:e?e.text:c};return b.newSegment(g)},d.getOptionsInternal=function(){if(d.options){var e=a.map(d.options,function(a){return b.newSegment({value:a.text})});return c.when(e)}return d.getOptions()},d.onSegmentChange=function(){if(d.options){var b=a.findWhere(d.options,{text:d.segment.value});b&&b.value!==d.property?d.property=b.value:"false"!==f.custom&&(d.property=d.segment.value)}else d.property=d.segment.value;d.$$postDigest(function(){d.$apply(function(){d.onChange()})})},d.segment=d.valueToSegment(d.property)}}}}])});