|
|
@ -15,134 +15,134 @@ |
|
|
|
* Source on Github http://github.com/MightyPork/php-sandbox
|
|
|
|
* Source on Github http://github.com/MightyPork/php-sandbox
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
(function (require, $, ace) { |
|
|
|
(function (require, $, ace) { |
|
|
|
"use strict"; |
|
|
|
"use strict"; |
|
|
|
|
|
|
|
|
|
|
|
var updateStatusBar, handleSubmit, initializeAce, handleAjaxError, options, editor; |
|
|
|
var updateStatusBar, handleSubmit, initializeAce, handleAjaxError, options, editor; |
|
|
|
|
|
|
|
|
|
|
|
options = { |
|
|
|
options = { |
|
|
|
tabsize: 4, |
|
|
|
tabsize: 4, |
|
|
|
editor: 'editor' |
|
|
|
editor: 'editor' |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* updates the text of the status bar |
|
|
|
* updates the text of the status bar |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
updateStatusBar = function (e) { |
|
|
|
updateStatusBar = function (e) { |
|
|
|
var cursor_position = editor.getCursorPosition(); |
|
|
|
var cursor_position = editor.getCursorPosition(); |
|
|
|
$('.statusbar .position').text('Line: ' + (1 + cursor_position.row) + ', Column: ' + cursor_position.column); |
|
|
|
$('.statusbar .position').text('Line: ' + (1 + cursor_position.row) + ', Column: ' + cursor_position.column); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* does an async request to eval the php code and displays the result |
|
|
|
* does an async request to eval the php code and displays the result |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
handleSubmit = function (e) { |
|
|
|
handleSubmit = function (e) { |
|
|
|
e.preventDefault(); |
|
|
|
e.preventDefault(); |
|
|
|
//$('div.output').html('<img src="loader.gif" class="loader" alt="" /> Loading ...'); /* loader sucks */
|
|
|
|
//$('div.output').html('<img src="loader.gif" class="loader" alt="" /> Loading ...'); /* loader sucks */
|
|
|
|
|
|
|
|
|
|
|
|
// store session
|
|
|
|
// store session
|
|
|
|
if (window.localStorage) { |
|
|
|
if (window.localStorage) { |
|
|
|
localStorage.setItem('phpCode', editor.getSession().getValue()); |
|
|
|
localStorage.setItem('phpCode', editor.getSession().getValue()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// eval server-side
|
|
|
|
// eval server-side
|
|
|
|
$.post('?js=1', { code: editor.getSession().getValue() }, function (res, status, jqXHR) { |
|
|
|
$.post('?js=1', {code: editor.getSession().getValue()}, function (res, status, jqXHR) { |
|
|
|
var mem = jqXHR.getResponseHeader("X-Memory-Usage") || "", |
|
|
|
var mem = jqXHR.getResponseHeader("X-Memory-Usage") || "", |
|
|
|
rendertime = jqXHR.getResponseHeader("X-Rendertime") || ""; |
|
|
|
rendertime = jqXHR.getResponseHeader("X-Rendertime") || ""; |
|
|
|
|
|
|
|
|
|
|
|
if (mem || rendertime) { |
|
|
|
if (mem || rendertime) { |
|
|
|
$('.statusbar .runtime-info').text('Memory usage: '+ mem + ' MB, Rendertime: ' + rendertime + 'ms'); |
|
|
|
$('.statusbar .runtime-info').text('Memory usage: ' + mem + ' MB, Rendertime: ' + rendertime + 'ms'); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$('.statusbar .runtime-info').text(''); |
|
|
|
$('.statusbar .runtime-info').text(''); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (res.match(/#end-php-console-output#$/)) { |
|
|
|
if (res.match(/#end-php-console-output#$/)) { |
|
|
|
var result = res.substring(0, res.length - 24); |
|
|
|
var result = res.substring(0, res.length - 24); |
|
|
|
$('div.output').text(result); // or html?
|
|
|
|
$('div.output').text(result); // or html?
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$('div.output').text(res + "\n\n*** Script ended unexpectedly. ***"); |
|
|
|
$('div.output').text(res + "\n\n*** Script ended unexpectedly. ***"); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
handleAjaxError = function (event, jqxhr, settings, exception) { |
|
|
|
handleAjaxError = function (event, jqxhr, settings, exception) { |
|
|
|
$('div.output').html("<em>Error occured while posting your code.</em>"); |
|
|
|
$('div.output').html("<em>Error occured while posting your code.</em>" + jqxhr.responseText); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
initializeAce = function () { |
|
|
|
initializeAce = function () { |
|
|
|
var PhpMode, code, storedCode; |
|
|
|
var PhpMode, code, storedCode; |
|
|
|
|
|
|
|
|
|
|
|
code = $('#' + options.editor).text(); |
|
|
|
code = $('#' + options.editor).text(); |
|
|
|
|
|
|
|
|
|
|
|
// reload last session
|
|
|
|
// reload last session
|
|
|
|
if (window.localStorage && code.match(/(<\?php)?\s*/)) { |
|
|
|
if (window.localStorage && code.match(/(<\?php)?\s*/)) { |
|
|
|
storedCode = localStorage.getItem('phpCode'); |
|
|
|
storedCode = localStorage.getItem('phpCode'); |
|
|
|
if (storedCode) { |
|
|
|
if (storedCode) { |
|
|
|
code = storedCode; |
|
|
|
code = storedCode; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$('#' + options.editor).replaceWith('<div id="' + options.editor + '" class="' + options.editor + '"></div>'); |
|
|
|
$('#' + options.editor).replaceWith('<div id="' + options.editor + '" class="' + options.editor + '"></div>'); |
|
|
|
$('#' + options.editor).text(code); |
|
|
|
$('#' + options.editor).text(code); |
|
|
|
|
|
|
|
|
|
|
|
editor = ace.edit(options.editor); |
|
|
|
editor = ace.edit(options.editor); |
|
|
|
|
|
|
|
|
|
|
|
editor.focus(); |
|
|
|
editor.focus(); |
|
|
|
editor.gotoLine(3, 0); |
|
|
|
editor.gotoLine(3, 0); |
|
|
|
editor.setTheme("ace/theme/monokai"); |
|
|
|
editor.setTheme("ace/theme/monokai"); |
|
|
|
editor.setOptions({ |
|
|
|
editor.setOptions({ |
|
|
|
showPrintMargin: false, |
|
|
|
showPrintMargin: false, |
|
|
|
fontSize: '18px', |
|
|
|
fontSize: '18px', |
|
|
|
enableBasicAutocompletion: true, |
|
|
|
enableBasicAutocompletion: true, |
|
|
|
fontFamily: 'Source Code Pro' |
|
|
|
fontFamily: 'Source Code Pro' |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// set mode
|
|
|
|
// set mode
|
|
|
|
PhpMode = require("ace/mode/php").Mode; |
|
|
|
PhpMode = require("ace/mode/php").Mode; |
|
|
|
editor.getSession().setMode(new PhpMode()); |
|
|
|
editor.getSession().setMode(new PhpMode()); |
|
|
|
|
|
|
|
|
|
|
|
// tab size
|
|
|
|
// tab size
|
|
|
|
if (options.tabsize) { |
|
|
|
if (options.tabsize) { |
|
|
|
editor.getSession().setTabSize(options.tabsize); |
|
|
|
editor.getSession().setTabSize(options.tabsize); |
|
|
|
editor.getSession().setUseSoftTabs(true); |
|
|
|
editor.getSession().setUseSoftTabs(true); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
editor.getSession().setUseSoftTabs(false); |
|
|
|
editor.getSession().setUseSoftTabs(false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// events
|
|
|
|
// events
|
|
|
|
editor.getSession().selection.on('changeCursor', updateStatusBar); |
|
|
|
editor.getSession().selection.on('changeCursor', updateStatusBar); |
|
|
|
|
|
|
|
|
|
|
|
// reset button
|
|
|
|
// reset button
|
|
|
|
if (window.localStorage) { |
|
|
|
if (window.localStorage) { |
|
|
|
$('.statusbar .reset').on('click', function (e) { |
|
|
|
$('.statusbar .reset').on('click', function (e) { |
|
|
|
editor.getSession().setValue('<?php\n\n'); |
|
|
|
editor.getSession().setValue('<?php\n\n'); |
|
|
|
editor.focus(); |
|
|
|
editor.focus(); |
|
|
|
editor.gotoLine(3, 0); |
|
|
|
editor.gotoLine(3, 0); |
|
|
|
window.localStorage.setItem('phpCode', ''); |
|
|
|
window.localStorage.setItem('phpCode', ''); |
|
|
|
e.preventDefault(); |
|
|
|
e.preventDefault(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// commands
|
|
|
|
// commands
|
|
|
|
editor.commands.addCommand({ |
|
|
|
editor.commands.addCommand({ |
|
|
|
name: 'submitForm', |
|
|
|
name: 'submitForm', |
|
|
|
bindKey: { |
|
|
|
bindKey: { |
|
|
|
win: 'Ctrl-Return|Alt-Return', |
|
|
|
win: 'Ctrl-Return|Alt-Return', |
|
|
|
mac: 'Command-Return|Alt-Return' |
|
|
|
mac: 'Command-Return|Alt-Return' |
|
|
|
}, |
|
|
|
}, |
|
|
|
exec: function (editor) { |
|
|
|
exec: function (editor) { |
|
|
|
$('form').submit(); |
|
|
|
$('form').submit(); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
$.console = function (settings) { |
|
|
|
$.console = function (settings) { |
|
|
|
$.extend(options, settings); |
|
|
|
$.extend(options, settings); |
|
|
|
|
|
|
|
|
|
|
|
$(function () { |
|
|
|
$(function () { |
|
|
|
$(document).ready(initializeAce); |
|
|
|
$(document).ready(initializeAce); |
|
|
|
$(document).ajaxError(handleAjaxError); |
|
|
|
$(document).ajaxError(handleAjaxError); |
|
|
|
|
|
|
|
|
|
|
|
$('form').submit(handleSubmit); |
|
|
|
$('form').submit(handleSubmit); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
}; |
|
|
|
}(ace.require, jQuery, ace)); |
|
|
|
}(ace.require, jQuery, ace)); |
|
|
|