blob: 711d194c352f9a5a55b03272e9cad531f7521acb [file] [log] [blame]
Chetan Mehrotra58a0df62013-07-22 06:58:03 +00001//Codemirror editor
2var inputEditor
3var outputEditor
4
5function sendData(url, data) {
6 $.ajax({
7 type:"POST",
8 url:url,
9 data:data,
10// dataType:"json",
11 timeout:30000, //In millis
12 beforeSend:function () {
13 $('#ajaxSpinner').show();
14 },
15 /* error: function() {
16 $('#status').text('Update failed�try again.').slideDown('slow');
17 },*/
18 complete:function () {
19 $('#ajaxSpinner').hide();
20 },
21 success:function (data) {
22 renderData(data)
23 }
24 });
25}
26
27function renderData(data){
28 $('#code-output').show();
29 outputEditor.setValue(data)
30}
31
32function setUpCodeMirror() {
33 CodeMirror.modeURL = pluginRoot + "/res/ui/codemirror/mode/%N/%N.js";
34 inputEditor = CodeMirror.fromTextArea(document.getElementById("code"), {
35 lineNumbers:true,
36 extraKeys: {
37 'Ctrl-Q':clearOutput,
38 'Ctrl-F9':executeScript
39 }
40 });
41 outputEditor = CodeMirror.fromTextArea(document.getElementById("result"), {
42 lineNumbers:true,
43 readOnly:"nocursor"
44 });
45}
46
47function updateWithOption(opt){
48 setLangMode(inputEditor,getProp(opt,'langMode'))
49 $('[name=lang]').val(opt.val())
50}
51
52function setLangMode(editor, modeName) {
53 if(!modeName){
54 modeName = "text/plain"
55 }else{
56 CodeMirror.autoLoadMode(inputEditor, modeName);
57 }
58 editor.setOption("mode", modeName);
59}
60
61function getProp(obj,propName){
62 if(obj != null){
63 return obj.prop ? obj.prop(propName) : obj.attr(propName)
64 }
65}
66
67function setUpLangOptions() {
68 var codeLang = $('#codeLang')
69 var options = getProp(codeLang,'options');
70 codeLang.empty()
71
72 for(var i in scriptConfig){
73 var config = scriptConfig[i]
74 var opt = new Option(config.langName,config.langCode);
75 if(config.mode){
76 opt.langMode = config.mode;
77 }
78 options[options.length] = opt
79 }
80 codeLang.change(function(){
81 var opt = $(this).find(":selected");
82 updateWithOption(opt)
83 });
84
85 codeLang.find('option:eq(0)').attr('selected','selected')
86 updateWithOption($(options[0]))
87}
88
89function executeScript(){
90 inputEditor.save() //Copy the contents to textarea form field
91 sendData(pluginRoot, $("#consoleForm").serialize());
92}
93
94function clearOutput(){
95 outputEditor.setValue('')
96}
97
98$(document).ready(function () {
99 $("#executeButton").click(executeScript);
100 $('#ajaxSpinner').hide();
101 $('#code-output').hide();
102 setUpCodeMirror();
103 setUpLangOptions();
104});