|
| 1 | +/*jslint browser: true */ |
| 2 | +/*global ace, jQuery */ |
1 | 3 | /** |
2 | 4 | * PHP Console |
3 | 5 | * |
|
11 | 13 | * |
12 | 14 | * Source on Github http://github.com/Seldaek/php-console |
13 | 15 | */ |
14 | | -(function(require, $, ace) { |
| 16 | +(function (require, $, ace) { |
15 | 17 | "use strict"; |
16 | 18 |
|
17 | | - var updateStatusBar, prepareClippyButton, refreshKrumoState, handleSubmit, initializeAce, |
| 19 | + var updateStatusBar, prepareClippyButton, refreshKrumoState, handleSubmit, initializeAce, handleAjaxError, |
18 | 20 | options, editor; |
19 | 21 | options = { |
20 | 22 | tabsize: 4, |
|
24 | 26 | /** |
25 | 27 | * updates the text of the status bar |
26 | 28 | */ |
27 | | - updateStatusBar = function(e) { |
| 29 | + updateStatusBar = function (e) { |
28 | 30 | var cursor_position = editor.getCursorPosition(); |
29 | | - $('.statusbar .position').text('Line: ' + (1+cursor_position.row) + ', Column: ' + cursor_position.column); |
| 31 | + $('.statusbar .position').text('Line: ' + (1 + cursor_position.row) + ', Column: ' + cursor_position.column); |
30 | 32 | }; |
31 | 33 |
|
32 | 34 | /** |
33 | 35 | * prepares a clippy button for clipboard access |
34 | 36 | */ |
35 | | - prepareClippyButton = function(e) { |
| 37 | + prepareClippyButton = function (e) { |
36 | 38 | var selection = editor.getSession().doc.getTextRange(editor.getSelectionRange()); |
37 | 39 | if (!selection) { |
38 | 40 | $('.statusbar .copy').hide(); |
|
46 | 48 | /** |
47 | 49 | * adds a toggle button to expand/collapse all krumo sub-trees at once |
48 | 50 | */ |
49 | | - refreshKrumoState = function() { |
| 51 | + refreshKrumoState = function () { |
50 | 52 | if ($('.krumo-expand').length > 0) { |
51 | 53 | $('<a class="expand" href="#">Toggle all</a>') |
52 | | - .click(function(e) { |
53 | | - $('div.krumo-element.krumo-expand').each(function(idx, el) { |
| 54 | + .click(function (e) { |
| 55 | + $('div.krumo-element.krumo-expand').each(function (idx, el) { |
54 | 56 | window.krumo.toggle(el); |
55 | 57 | }); |
56 | 58 | e.preventDefault(); |
|
62 | 64 | /** |
63 | 65 | * does an async request to eval the php code and displays the result |
64 | 66 | */ |
65 | | - handleSubmit = function(e) { |
| 67 | + handleSubmit = function (e) { |
66 | 68 | e.preventDefault(); |
67 | 69 | $('div.output').html('<img src="loader.gif" class="loader" alt="" /> Loading ...'); |
68 | 70 |
|
69 | | - $.post('?js=1', { code: editor.getSession().getValue() }, function(res) { |
| 71 | + // store session |
| 72 | + if (window.localStorage) { |
| 73 | + localStorage.setItem('phpCode', editor.getSession().getValue()); |
| 74 | + } |
| 75 | + |
| 76 | + // eval server-side |
| 77 | + $.post('?js=1', { code: editor.getSession().getValue() }, function (res) { |
70 | 78 | if (res.match(/#end-php-console-output#$/)) { |
71 | | - $('div.output').html(res.substring(0, res.length-24)); |
| 79 | + $('div.output').html(res.substring(0, res.length - 24)); |
72 | 80 | } else { |
73 | 81 | $('div.output').html(res + "<br /><br /><em>Script ended unexpectedly.</em>"); |
74 | 82 | } |
75 | 83 | refreshKrumoState(); |
76 | 84 | }); |
77 | 85 | }; |
78 | | - |
79 | | - handleAjaxError = function(event, jqxhr, settings, exception) { |
| 86 | + |
| 87 | + handleAjaxError = function (event, jqxhr, settings, exception) { |
80 | 88 | $('div.output').html("<em>Error occured while posting your code.</em>"); |
81 | 89 | refreshKrumoState(); |
82 | 90 | }; |
83 | 91 |
|
84 | | - initializeAce = function() { |
85 | | - var PhpMode, code; |
| 92 | + initializeAce = function () { |
| 93 | + var PhpMode, code, storedCode; |
86 | 94 |
|
87 | 95 | code = $('#' + options.editor).text(); |
88 | | - $('#' + options.editor).replaceWith('<div id="'+options.editor+'" class="'+options.editor+'"></div>'); |
| 96 | + |
| 97 | + // reload last session |
| 98 | + if (window.localStorage && code.match(/(<\?php)?\s*/)) { |
| 99 | + storedCode = localStorage.getItem('phpCode'); |
| 100 | + if (storedCode) { |
| 101 | + code = storedCode; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + $('#' + options.editor).replaceWith('<div id="' + options.editor + '" class="' + options.editor + '"></div>'); |
89 | 106 | $('#' + options.editor).text(code); |
90 | 107 |
|
91 | 108 | editor = ace.edit(options.editor); |
92 | 109 |
|
93 | 110 | editor.focus(); |
94 | | - editor.gotoLine(3,0); |
| 111 | + editor.gotoLine(3, 0); |
95 | 112 |
|
96 | 113 | // set mode |
97 | 114 | PhpMode = require("ace/mode/php").Mode; |
|
107 | 124 | editor.getSession().selection.on('changeSelection', prepareClippyButton); |
108 | 125 | } |
109 | 126 |
|
| 127 | + // reset button |
| 128 | + if (window.localStorage) { |
| 129 | + $('.statusbar .reset').on('click', function (e) { |
| 130 | + editor.getSession().setValue('<?php\n\n'); |
| 131 | + editor.focus(); |
| 132 | + editor.gotoLine(3, 0); |
| 133 | + window.localStorage.setItem('phpCode', ''); |
| 134 | + e.preventDefault(); |
| 135 | + }); |
| 136 | + } |
| 137 | + |
110 | 138 | // commands |
111 | 139 | editor.commands.addCommand({ |
112 | 140 | name: 'submitForm', |
113 | 141 | bindKey: { |
114 | 142 | win: 'Ctrl-Return|Alt-Return', |
115 | 143 | mac: 'Command-Return|Alt-Return' |
116 | 144 | }, |
117 | | - exec: function(editor) { |
| 145 | + exec: function (editor) { |
118 | 146 | $('form').submit(); |
119 | 147 | } |
120 | 148 | }); |
121 | 149 | }; |
122 | 150 |
|
123 | | - $.console = function(settings) { |
| 151 | + $.console = function (settings) { |
124 | 152 | $.extend(options, settings); |
125 | 153 |
|
126 | | - $(function() { |
| 154 | + $(function () { |
127 | 155 | $(document).ready(initializeAce); |
128 | 156 | $(document).ajaxError(handleAjaxError); |
129 | 157 |
|
|
0 commit comments