/*! grafana - v3.0.4 - 2016-06-01
 * Copyright (c) 2016 Torkel Ödegaard; Licensed Apache-2.0 */

define(["angular","lodash","jquery"],function(a,b,c){"use strict";a.module("grafana.directives").directive("influxQueryPartEditor",["$compile","templateSrv",function(a,d){var e='<input type="text" style="display:none" class="input-mini tight-form-func-param"></input>';return{restrict:"E",templateUrl:"public/app/plugins/datasource/influxdb/partials/query_part.html",scope:{part:"=",removeAction:"&",partUpdated:"&",getOptions:"&"},link:function(a,f){function g(a){var b=c(this),d=b.next();d.val(n.params[a]),d.css("width",b.width()+16+"px"),b.hide(),d.show(),d.focus(),d.select();var e=d.data("typeahead");e&&(d.val(""),e.lookup())}function h(b){var e=c(this),f=e.prev(),g=e.val();(""!==g||n.def.params[b].optional)&&(f.html(d.highlightVariablesAsHtml(g)),n.updateParam(e.val(),b),a.$apply(a.partUpdated)),e.hide(),f.show()}function i(a,b){13===b.which&&h.call(this,a)}function j(){this.style.width=8*(3+this.value.length)+"px"}function k(d,e,f){if(e.options||e.dynamicLookup){var g=function(c,d){return e.options?e.options:void a.$apply(function(){a.getOptions().then(function(a){var c=b.map(a,function(a){return a.value});d(c)})})};d.attr("data-provide","typeahead");var i=e.options;"int"===e.type&&(i=b.map(i,function(a){return a.toString()})),d.typeahead({source:g,minLength:0,items:1e3,updater:function(a){return setTimeout(function(){h.call(d[0],f)},0),a}});var j=d.data("typeahead");j.lookup=function(){this.query=this.$element.val()||"";var a=this.source(this.query,c.proxy(this.process,this));return a?this.process(a):a}}}function l(){b.each(o.params,function(a,f){if(!(a.optional&&n.params.length<=f)){f>0&&c("<span>, </span>").appendTo(p);var l=d.highlightVariablesAsHtml(n.params[f]),m=c('<a class="graphite-func-param-link pointer">'+l+"</a>"),o=c(e);m.appendTo(p),o.appendTo(p),o.blur(b.partial(h,f)),o.keyup(j),o.keypress(b.partial(i,f)),m.click(b.partial(g,f)),k(o,a,f)}})}function m(){p.empty(),l()}var n=a.part,o=n.def,p=f.find(".query-part-parameters"),q=f.find(".tight-form-func-controls");a.toggleControls=function(){var a=f.closest(".tight-form");return f.hasClass("show-function-controls")?(f.removeClass("show-function-controls"),a.removeClass("has-open-function"),void q.hide()):(f.addClass("show-function-controls"),a.addClass("has-open-function"),void q.show())},a.removeActionInternal=function(){a.toggleControls(),a.removeAction()},m()}}}])});