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