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

System.register(["lodash","app/core/core_module","app/core/config","ace"],function(a,b){"use strict";function c(a,b,c){void 0===c&&(c=null);var d=j,e="ace/"+a+"/"+b,g=a+"-"+b,h=g+".js";f.default.includes(q,g)&&(d=r),c&&(d=c+"/"),"snippets"===a&&(h=a+"/"+b+".js"),i.default.config.setModuleUrl(e,d+h)}function d(a,b,d){function e(b){var d="ace/mode/"+b;c("mode",b,a.datasource.meta.baseUrl||null),c("snippets",b,a.datasource.meta.baseUrl||null),z.setMode(d),i.default.config.loadModule("ace/ext/language_tools",function(b){y.setOptions({enableBasicAutocompletion:!0,enableLiveAutocompletion:!0,enableSnippets:!0}),a.getCompleter()&&(y.completers=y.completers.slice(),y.completers.push(a.getCompleter()))})}function f(a){c("theme",a);var d="ace/theme/"+a;i.default.config.loadModule(d,function(e){var f=h.default.bootData.user.lightTheme,g=a;f&&e.isDark?g=l:f||e.isDark||(g=k),c("theme",g),d="ace/theme/"+g,y.setTheme(d),b.addClass("gf-code-editor--theme-loaded")})}function g(a){y.setValue(a),y.clearSelection()}var j=h.default.bootData.user.lightTheme,q=j?l:k,r=d.mode||m,s=d.maxLines||n,t=void 0!==d.showGutter,u=d.theme||q,v=d.tabSize||o,w=d.behavioursEnabled?"true"===d.behavioursEnabled:p,x=b.get(0),y=i.default.edit(x),z=y.getSession(),A={maxLines:s,showGutter:t,tabSize:v,behavioursEnabled:w,highlightActiveLine:!1,showPrintMargin:!1,autoScrollEditorIntoView:!0};y.setOptions(A),y.$blockScrolling=1/0,y.renderer.setScrollMargin(15,15),y.renderer.setPadding(10),f(u),e(r),g(a.content),b.addClass("gf-code-editor");var B=b.find("textarea");B.addClass("gf-form-input"),a.codeEditorFocus&&setTimeout(function(){B.focus();var a=B[0];if(a.setSelectionRange){var b=2*B.val().length;a.setSelectionRange(b,b)}},100),z.on("change",function(b){a.$apply(function(){var b=y.getValue();a.content=b})}),a.$watch("content",function(b,c){var d=y.getValue();b!==d&&b!==c&&a.$$postDigest(function(){g(b)})}),y.on("blur",function(){a.onChange()}),a.$on("$destroy",function(){y.destroy()}),y.commands.addCommand({name:"executeQuery",bindKey:{win:"Ctrl-Enter",mac:"Command-Enter"},exec:function(){a.onChange()}})}function e(){return{restrict:"E",template:s,scope:{content:"=",datasource:"=",codeEditorFocus:"<",onChange:"&",getCompleter:"&"},link:d}}b&&b.id;a("codeEditorDirective",e);var f,g,h,i,j,k,l,m,n,o,p,q,r,s;return{setters:[function(a){f=a},function(a){g=a},function(a){h=a},function(a){i=a}],execute:function(){j="public/vendor/npm/ace-builds/src-noconflict/",k="grafana-dark",l="textmate",m="text",n=10,o=2,p=!0,q=["theme-grafana-dark"],r="public/app/core/components/code_editor/",c("ext","language_tools"),c("mode","text"),c("snippets","text"),s="<div></div>",g.default.directive("codeEditor",e)}}});