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

System.register(["lodash","jquery","app/core/core_module"],function(a){function b(a,b){var e='<input type="text" style="display:none" class="input-mini tight-form-func-param"></input>';return{restrict:"E",template:f,scope:{part:"=",removeAction:"&",partUpdated:"&",getOptions:"&"},link:function(a,f){function g(a){var b=d["default"](this),c=b.next();c.val(n.params[a]),c.css("width",b.width()+16+"px"),b.hide(),c.show(),c.focus(),c.select();var e=c.data("typeahead");e&&(c.val(""),e.lookup())}function h(c){var e=d["default"](this),f=e.prev(),g=e.val();(""!==g||n.def.params[c].optional)&&(f.html(b.highlightVariablesAsHtml(g)),n.updateParam(e.val(),c),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(b,e,f){if(e.options||e.dynamicLookup){var g=function(b,d){return e.options?e.options:void a.$apply(function(){a.getOptions().then(function(a){var b=c["default"].map(a,function(a){return a.value});d(b)})})};b.attr("data-provide","typeahead");var i=e.options;"int"===e.type&&(i=c["default"].map(i,function(a){return a.toString()})),b.typeahead({source:g,minLength:0,items:1e3,updater:function(a){return setTimeout(function(){h.call(b[0],f)},0),a}});var j=b.data("typeahead");j.lookup=function(){this.query=this.$element.val()||"";var a=this.source(this.query,d["default"].proxy(this.process,this));return a?this.process(a):a}}}function l(){c["default"].each(o.params,function(a,f){if(!(a.optional&&n.params.length<=f)){f>0&&d["default"]("<span>, </span>").appendTo(p);var l=b.highlightVariablesAsHtml(n.params[f]),m=d["default"]('<a class="graphite-func-param-link pointer">'+l+"</a>"),o=d["default"](e);m.appendTo(p),o.appendTo(p),o.blur(c["default"].partial(h,f)),o.keyup(j),o.keypress(c["default"].partial(i,f)),m.click(c["default"].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()}}}b.$inject=["$compile","templateSrv"];var c,d,e,f;return a("queryPartEditorDirective",b),{setters:[function(a){c=a},function(a){d=a},function(a){e=a}],execute:function(){f='\n<div class="tight-form-func-controls">\n  <span class="pointer fa fa-remove" ng-click="removeActionInternal()"></span>\n</div>\n\n<a ng-click="toggleControls()" class="query-part-name">{{part.def.type}}</a>\n<span>(</span><span class="query-part-parameters"></span><span>)</span>\n',e["default"].directive("queryPartEditor",b)}}});