|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var codeMirrorDefaultHeight = 10000; |
| 4 | +var myCode1Mirror = CodeMirror.fromTextArea( |
| 5 | + document.getElementById('ocamlcode#1'), |
| 6 | + { |
| 7 | + mode:'text/x-ocaml', |
| 8 | + lineNumbers: true, |
| 9 | + lineWrapping: true, |
| 10 | + styleActiveLine:true, |
| 11 | + theme: "monokai", |
| 12 | + matchBrackets: true, |
| 13 | + autoCloseBrackets: true |
| 14 | + } ); |
| 15 | +var jsCode1Mirror = CodeMirror.fromTextArea( |
| 16 | + document.getElementById('jscode#1'), |
| 17 | + { mode:'javascript', |
| 18 | + lineNumbers:true, |
| 19 | + readOnly: true, |
| 20 | + lineWrapping: true, |
| 21 | + theme: "monokai", |
| 22 | + }); |
| 23 | + |
| 24 | +var outputMirror = CodeMirror.fromTextArea( |
| 25 | + document.getElementById('output'), |
| 26 | + { |
| 27 | + mode : 'javascript', |
| 28 | + readOnly: true, |
| 29 | + lineWrapping: true, |
| 30 | + lineNumbers: true, |
| 31 | + theme: "monokai" |
| 32 | + } |
| 33 | +); |
| 34 | +var errorMirror = CodeMirror.fromTextArea( |
| 35 | + document.getElementById('error'), |
| 36 | + { |
| 37 | + readOnly: true, |
| 38 | + lineWrapping: true, |
| 39 | + lineNumbers : true, |
| 40 | + theme: "monokai" |
| 41 | + } |
| 42 | +); |
| 43 | +var PROMPT = "> " ; |
| 44 | +var log_output = PROMPT; |
| 45 | +var ERR_OUTPUT = "Warnings: " |
| 46 | +var err_output = ERR_OUTPUT; |
| 47 | + |
| 48 | +function reset_log_output (){ log_output = PROMPT;} |
| 49 | +function reset_error_output(){ err_output = ERR_OUTPUT;} |
| 50 | +function get_log_output(){ |
| 51 | + var old = log_output; |
| 52 | + reset_log_output(); |
| 53 | + return old; |
| 54 | +} |
| 55 | +function get_error_output(){ |
| 56 | + var old = err_output; |
| 57 | + reset_error_output(); |
| 58 | + return old; |
| 59 | +} |
| 60 | +var compile_code ; |
| 61 | +var evalButton = document.getElementById('option-eval'); |
| 62 | +var shakeButton = document.getElementById('option-non-export'); |
| 63 | + |
| 64 | +function shouldEval(){ |
| 65 | + return evalButton.checked; |
| 66 | +} |
| 67 | +function onEvalButtonChange(){ |
| 68 | + if(!shouldEval()){ |
| 69 | + outputMirror.setValue(PROMPT); |
| 70 | + } else { |
| 71 | + onEditChanges(); |
| 72 | + } |
| 73 | +} |
| 74 | +evalButton.addEventListener('change', onEvalButtonChange); |
| 75 | + |
| 76 | +function onShakeButtonChange(){ |
| 77 | + if(shakeButton.checked){ |
| 78 | + compile_code = ocaml.shake_compile; |
| 79 | + }else{ |
| 80 | + compile_code = ocaml.compile; |
| 81 | + } |
| 82 | + onEditChanges(); |
| 83 | +} |
| 84 | + |
| 85 | +shakeButton.addEventListener('change', onShakeButtonChange); |
| 86 | +var original_log = console.log; |
| 87 | +var original_err = console.error; |
| 88 | + |
| 89 | +/** |
| 90 | + * TODO: simulate commonjs in browser |
| 91 | + * */ |
| 92 | +var exports = window; |
| 93 | + |
| 94 | +function redirect() { log_output = log_output + Array.prototype.slice.apply(arguments).join(' ') + "\n"}; |
| 95 | + |
| 96 | +function redirect_err() { |
| 97 | + err_output = err_output + Array.prototype.slice.apply(arguments).join(' ') + "\n" |
| 98 | +}; |
| 99 | + |
| 100 | +myCode1Mirror.setSize(null,codeMirrorDefaultHeight); |
| 101 | +outputMirror.setSize(null,50); |
| 102 | +outputMirror.setValue(PROMPT + 'Hello BuckleScript!'); |
| 103 | +errorMirror.setSize(null,50); |
| 104 | +errorMirror.setValue(ERR_OUTPUT); |
| 105 | + |
| 106 | +var sourceLocation = "" |
| 107 | +if (typeof window.location !== "undefined"){ |
| 108 | + sourceLocation = "\n//# sourceURL=" + window.location.href + "/repl.js" |
| 109 | +} |
| 110 | + |
| 111 | +function evalCode(js){ |
| 112 | + console.log = redirect; |
| 113 | + try { |
| 114 | + window.eval(js + sourceLocation); |
| 115 | + outputMirror.setValue(get_log_output()); |
| 116 | + console.log = original_log; |
| 117 | + } |
| 118 | + catch(e){ |
| 119 | + outputMirror.setValue(get_log_output() + "\n" + e); |
| 120 | + console.log = original_log; |
| 121 | + } |
| 122 | + |
| 123 | +} |
| 124 | + |
| 125 | +function createExample(name){ |
| 126 | + var li = document.createElement('li'); |
| 127 | + var a = document.createElement('a') |
| 128 | + a.setAttribute('href', '#' + name); |
| 129 | + a.setAttribute('data-key', name); |
| 130 | + a.appendChild(document.createTextNode(name)) |
| 131 | + li.appendChild(a) |
| 132 | + return li |
| 133 | +} |
| 134 | + |
| 135 | +var examplesDropdown = document.getElementById("examplesDropdown") |
| 136 | +var examplesDataSet ; |
| 137 | + |
| 138 | + |
| 139 | +//Event handler for examples dropdown |
| 140 | +$('#examplesDropdown').click(clickHandler); |
| 141 | + |
| 142 | +function switchExample(id){ |
| 143 | + var filename = ""; |
| 144 | + var example = examplesDataSet [id]; |
| 145 | + if (example){ |
| 146 | + changeEvalButton(example.eval) |
| 147 | + filename = "examples/" + example.file |
| 148 | + } |
| 149 | + //make ajax request |
| 150 | + $ |
| 151 | + .ajax({ |
| 152 | + url: filename, |
| 153 | + cache: true |
| 154 | + }) |
| 155 | + .done(function (response) { |
| 156 | + myCode1Mirror.setValue(response); |
| 157 | + }); |
| 158 | + |
| 159 | + //update dropdown label |
| 160 | + $('#examplesLabel').html(id + ' <span class="caret"></span>'); |
| 161 | + |
| 162 | +} |
| 163 | + |
| 164 | +function clickHandler(e) { |
| 165 | + var id = e.target.getAttribute('data-key'); |
| 166 | + switchExample(id) |
| 167 | +} |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | +function onEditChanges(cm, change) { |
| 172 | + if(typeof compile_code === 'undefined'){ |
| 173 | + console.log('init....'); |
| 174 | + compile_code = ocaml.compile; |
| 175 | + } |
| 176 | + console.error = redirect_err; |
| 177 | + var raw = compile_code(myCode1Mirror.getValue()); |
| 178 | + errorMirror.setValue(get_error_output()); |
| 179 | + console.error = original_err; |
| 180 | + console.log(raw); |
| 181 | + var rsp = JSON.parse(raw); // can we save this from parsing? |
| 182 | + if (rsp.js_code !== undefined) { |
| 183 | + jsCode1Mirror.setValue(rsp.js_code); |
| 184 | + // eval |
| 185 | + if(shouldEval()) { |
| 186 | + evalCode(rsp.js_code) |
| 187 | + } |
| 188 | + } else { |
| 189 | + jsCode1Mirror.setValue(rsp.js_error_msg); |
| 190 | + |
| 191 | + } |
| 192 | + |
| 193 | +} |
| 194 | +myCode1Mirror.on("changes", onEditChanges); |
| 195 | + |
| 196 | +jsCode1Mirror.setSize(null,codeMirrorDefaultHeight); |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | +//checks or unchecks the eval button |
| 201 | +function changeEvalButton(bool) { |
| 202 | + $('#option-eval').prop('checked', bool); |
| 203 | + onEvalButtonChange(); |
| 204 | +} |
| 205 | + |
| 206 | +//creates a gist from OCaml code |
| 207 | +$('#share').click(function (e) { |
| 208 | + var state = $(this).button('loading'); |
| 209 | + var request = |
| 210 | + { |
| 211 | + "description": "BuckleScript Gist", |
| 212 | + "public": true, |
| 213 | + "files": { |
| 214 | + "gist.ml": { |
| 215 | + "content": myCode1Mirror.getValue() |
| 216 | + } |
| 217 | + } |
| 218 | + }; |
| 219 | + |
| 220 | + $ |
| 221 | + .ajax({ url:'https://api.github.com/gists', |
| 222 | + type: 'POST', |
| 223 | + data: JSON.stringify(request) |
| 224 | + }) |
| 225 | + .done(function (response) { |
| 226 | + state.button('reset'); |
| 227 | + $('#shareModal').modal('show'); |
| 228 | + var url = 'https://bloomberg.github.io/bucklescript/js-demo/?gist=' + response.id; |
| 229 | + $('#shareModalBody').html('<a href=' + '"' + url + '"' + 'target="_blank"' + '>' + url + '</a>'); |
| 230 | + }) |
| 231 | + .error(function (err) { |
| 232 | + state.button('reset'); |
| 233 | + $('#shareModal').modal('show'); |
| 234 | + $('#shareModalBody').text('Sorry! Currently GitHub\'s API limits the number of requests we can send per hour. Please try again later.'); |
| 235 | + }) |
| 236 | +}); |
| 237 | + |
| 238 | +//copy link to clipboard |
| 239 | +var copy = new Clipboard('#copyButton'); |
| 240 | +copy.on('success', function(e) { |
| 241 | + e.clearSelection(); |
| 242 | + $('#copyGlyph').attr('class', 'glyphicon glyphicon-ok'); |
| 243 | +}); |
| 244 | + |
| 245 | +//reset clipboard icon when modal is closed |
| 246 | +$('#shareModal').on('hidden.bs.modal', function (e) { |
| 247 | + $('#copyGlyph').attr('class', 'glyphicon glyphicon-copy'); |
| 248 | +}); |
0 commit comments