Matt Godbolt | 8610ec9 | 2016-03-28 08:22:49 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012-2016, Matt Godbolt |
Gabriel Devillers | ac2338f | 2016-07-04 16:54:38 +0200 | [diff] [blame] | 2 | // |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer in the |
| 12 | // documentation and/or other materials provided with the distribution. |
| 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 18 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 24 | // POSSIBILITY OF SUCH DAMAGE. |
| 25 | |
| 26 | function parseLines(lines, callback) { |
Matt Godbolt | 3f05562 | 2012-09-20 22:14:28 -0500 | [diff] [blame] | 27 | var re = /^\/tmp\/[^:]+:([0-9]+)(:([0-9]+))?:\s+(.*)/; |
Matt Godbolt | c60cad2 | 2014-11-26 08:24:40 -0600 | [diff] [blame] | 28 | $.each(lines.split('\n'), function (_, line) { |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 29 | line = line.trim(); |
Matt Godbolt | c60cad2 | 2014-11-26 08:24:40 -0600 | [diff] [blame] | 30 | if (line !== "") { |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 31 | var match = line.match(re); |
| 32 | if (match) { |
Matt Godbolt | 6dd6168 | 2013-02-06 17:28:01 -0600 | [diff] [blame] | 33 | callback(parseInt(match[1]), match[4].trim()); |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 34 | } else { |
| 35 | callback(null, line); |
| 36 | } |
| 37 | } |
| 38 | }); |
| 39 | } |
Matt Godbolt | 3075f96 | 2013-02-04 21:25:57 -0600 | [diff] [blame] | 40 | |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 41 | // Function called on the editor's window, or on a slot's window |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 42 | function clearBackground(cm) { |
| 43 | for (var i = 0; i < cm.lineCount(); ++i) { |
Matt Godbolt | 091bf37 | 2013-02-04 22:18:37 -0600 | [diff] [blame] | 44 | cm.removeLineClass(i, "background", null); |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 45 | } |
| 46 | } |
Matt Godbolt | 3075f96 | 2013-02-04 21:25:57 -0600 | [diff] [blame] | 47 | |
| 48 | const NumRainbowColours = 12; |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 49 | |
Gabriel Devillers | ac2338f | 2016-07-04 16:54:38 +0200 | [diff] [blame] | 50 | // This function is called in function initialise in static/gcc.js |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 51 | function Compiler(domRoot, origFilters, windowLocalPrefix, |
Gabriel Devillers | 2e057e5 | 2016-07-29 11:39:05 +0200 | [diff] [blame] | 52 | onEditorChangeCallback, lang, compilers, defaultCompiler) { |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 53 | console.log("[TRACE] Entering function Compiler()"); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 54 | // Global array that will contain the slots. |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 55 | var slots = []; |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 56 | var Slot = function (id) { |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 57 | // this id will be used in the DOM, ex : class="slot"+id |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 58 | // the getAvailableId could be placed here but that could |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 59 | // lead to confusion if the Slot is not pushed in slots |
| 60 | this.id = id; |
| 61 | this.asmCodeMirror = null; |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 62 | this.currentAssembly = null; |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 63 | // lastRequest contains the previous request in the slot |
| 64 | // it can be used to prevent useless re-compilation |
| 65 | // if no parameters were really changed (e.g. spaces moved) |
| 66 | this.lastRequest = null; |
Gabriel Devillers | 60c96cd | 2016-07-25 14:30:34 +0200 | [diff] [blame] | 67 | this.pendingDiffs = []; |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 68 | this.node = null; // Will be initialized in slotDomCtor |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 69 | this.description = function () { |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 70 | var options = this.node.find('.compiler-options').val(); |
| 71 | var compiler = currentCompiler(this); |
| 72 | return compiler.name + " " + options; |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 73 | }; |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 74 | this.shortDescription = function () { |
Gabriel Devillers | 68379dd | 2016-08-04 15:43:12 +0200 | [diff] [blame] | 75 | var description = this.description(); |
| 76 | if (description.length >= 19) { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 77 | return description.substring(0, 13) + "[...]" |
Gabriel Devillers | 68379dd | 2016-08-04 15:43:12 +0200 | [diff] [blame] | 78 | } else { |
| 79 | return description; |
| 80 | } |
| 81 | } |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 82 | }; |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 83 | |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 84 | var diffs = []; |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 85 | var Diff = function (id) { |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 86 | this.id = id; |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 87 | this.beforeSlot = null; |
| 88 | this.afterSlot = null; |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 89 | this.currentDiff = null; |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 90 | this.asmCodeMirror = null; |
Gabriel Devillers | 269615a | 2016-07-20 18:18:55 +0200 | [diff] [blame] | 91 | this.zones = null; |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 92 | |
Gabriel Devillers | 3205e56 | 2016-07-25 15:57:15 +0200 | [diff] [blame] | 93 | // used only if the editor's code is modified, |
| 94 | // to prevent two diff generation by waiting for the second one. |
| 95 | this.remainingTriggers = 2; |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 96 | this.node = null; // Will be initialized in diffDomCtor |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 97 | }; |
Gabriel Devillers | 60c96cd | 2016-07-25 14:30:34 +0200 | [diff] [blame] | 98 | |
| 99 | function contains(array, object) { |
| 100 | for (var i = 0; i < array.length; i++) { |
| 101 | if (array[i] === object) { |
| 102 | return true; |
| 103 | } |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | // adds diff to the set (array) slot.pendingDiffs |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 109 | function addToPendings(slot, diff) { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 110 | if (!contains(slot.pendingDiffs, diff)) { |
Gabriel Devillers | 60c96cd | 2016-07-25 14:30:34 +0200 | [diff] [blame] | 111 | slot.pendingDiffs.push(diff); |
| 112 | } |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 115 | // returns the smallest Natural that is not used as an id |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 116 | // (suppose that ids are Naturals, but any other way of getting |
| 117 | // an unique id usable in HTML's classes should work) |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 118 | function getAvailableId(array) { |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 119 | if (array.length == 0) return 0; |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 120 | var usedIds = []; |
| 121 | var i; |
| 122 | for (i = 0; i < array.length; i++) { |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 123 | usedIds.push(array[i].id) |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 124 | } |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 125 | usedIds.sort(); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 126 | var prev = -1; |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 127 | for (i = 0; i < usedIds.length; i++) { |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 128 | if (usedIds[i] - prev > 1) return prev + 1; |
| 129 | prev = usedIds[i]; |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 130 | } |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 131 | return usedIds.length; |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 134 | |
Gabriel Devillers | 6d94cab | 2016-07-29 15:44:31 +0200 | [diff] [blame] | 135 | // setSetting('leaderSlot', null); // TODO : remove ? |
| 136 | var leaderSlot = null; // is set up correctly at the end of this function. |
| 137 | function isLeader(slot) { |
| 138 | return (slot == leaderSlot); |
| 139 | } |
| 140 | |
Matt Godbolt | f4e76d2 | 2015-01-22 07:22:25 -0600 | [diff] [blame] | 141 | var compilersById = {}; |
| 142 | var compilersByAlias = {}; |
Gabriel Devillers | ac2338f | 2016-07-04 16:54:38 +0200 | [diff] [blame] | 143 | |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 144 | var pendingTimeoutInEditor = null; |
Gabriel Devillers | ac2338f | 2016-07-04 16:54:38 +0200 | [diff] [blame] | 145 | |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 146 | var cppEditor = null; |
Gabriel Devillers | ac2338f | 2016-07-04 16:54:38 +0200 | [diff] [blame] | 147 | |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 148 | // default filters |
Matt Godbolt | b8fca03 | 2015-11-28 13:46:39 -0600 | [diff] [blame] | 149 | var filters_ = $.extend({}, origFilters); |
Gabriel Devillers | 3f62aa6 | 2016-06-23 13:38:03 +0200 | [diff] [blame] | 150 | var ignoreChanges = true; // Horrible hack to avoid onEditorChange doing anything on first starting, ie before we've set anything up. |
Matt Godbolt | b8fca03 | 2015-11-28 13:46:39 -0600 | [diff] [blame] | 151 | |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 152 | function setCompilerById(compilerId, slot) { |
Gabriel Devillers | f6a5dd1 | 2016-08-02 14:08:46 +0200 | [diff] [blame] | 153 | var compilerNode = slot.node.find('.compiler'); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 154 | compilerNode.text(compilersById[compilerId].name); |
| 155 | compilerNode.attr('data', compilerId); |
Matt Godbolt | 3b39141 | 2016-03-27 12:04:52 -0500 | [diff] [blame] | 156 | } |
| 157 | |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 158 | function currentCompilerId(slot) { |
Gabriel Devillers | f6a5dd1 | 2016-08-02 14:08:46 +0200 | [diff] [blame] | 159 | return slot.node.find('.compiler').attr('data'); |
Matt Godbolt | 3b39141 | 2016-03-27 12:04:52 -0500 | [diff] [blame] | 160 | } |
| 161 | |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 162 | function currentCompiler(slot) { |
| 163 | return compilersById[currentCompilerId(slot)]; |
Matt Godbolt | b8fca03 | 2015-11-28 13:46:39 -0600 | [diff] [blame] | 164 | } |
| 165 | |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 166 | // set the autocompile button on static/index.html |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 167 | var autocompile = $('.autocompile'); |
| 168 | autocompile.click(function (e) { |
| 169 | autocompile.toggleClass('active'); |
Gabriel Devillers | 3f62aa6 | 2016-06-23 13:38:03 +0200 | [diff] [blame] | 170 | onEditorChange(); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 171 | setSetting('autocompile', autocompile.hasClass('active')); |
| 172 | }); |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 173 | autocompile.toggleClass('active', getSetting("autocompile") !== "false"); |
Matt Godbolt | 15d5b93 | 2016-03-27 22:41:18 -0500 | [diff] [blame] | 174 | |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 175 | // handle filter options that are specific to a compiler |
Matt Godbolt | b8fca03 | 2015-11-28 13:46:39 -0600 | [diff] [blame] | 176 | function patchUpFilters(filters) { |
| 177 | filters = $.extend({}, filters); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 178 | // TODO : correct the fact that filters should be slot specific !! |
| 179 | var compiler = currentCompiler(slots[0]); |
Matt Godbolt | b8fca03 | 2015-11-28 13:46:39 -0600 | [diff] [blame] | 180 | var compilerSupportsBinary = compiler ? compiler.supportsBinary : true; |
| 181 | if (filters.binary && !(OPTIONS.supportsBinary && compilerSupportsBinary)) { |
| 182 | filters.binary = false; |
| 183 | } |
| 184 | return filters; |
| 185 | } |
Matt Godbolt | 91c8965 | 2015-02-06 21:57:58 -0600 | [diff] [blame] | 186 | |
Matt Godbolt | 986252d | 2015-02-20 08:25:55 -0600 | [diff] [blame] | 187 | var cmMode; |
| 188 | switch (lang.toLowerCase()) { |
| 189 | default: |
| 190 | cmMode = "text/x-c++src"; |
| 191 | break; |
Ola Jeppsson | b761960 | 2015-08-08 11:33:11 +0000 | [diff] [blame] | 192 | case "c": |
| 193 | cmMode = "text/x-c"; |
| 194 | break; |
Matt Godbolt | 986252d | 2015-02-20 08:25:55 -0600 | [diff] [blame] | 195 | case "rust": |
| 196 | cmMode = "text/x-rustsrc"; |
| 197 | break; |
| 198 | case "d": |
| 199 | cmMode = "text/x-d"; |
| 200 | break; |
| 201 | case "go": |
| 202 | cmMode = "text/x-go"; |
| 203 | break; |
Matt Godbolt | 91c8965 | 2015-02-06 21:57:58 -0600 | [diff] [blame] | 204 | } |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 205 | |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 206 | // Set up the editor's window |
| 207 | // [0] is mandatory here because domRoot.find() returns an array of all |
| 208 | // matching elements. It is not a problem for jquery which applies it's |
| 209 | // functions to all elements, whereas CodeMirror works on a single DOM node. |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 210 | cppEditor = CodeMirror.fromTextArea(domRoot.find(".editor textarea")[0], { |
| 211 | lineNumbers: true, |
Matt Godbolt | 091bf37 | 2013-02-04 22:18:37 -0600 | [diff] [blame] | 212 | matchBrackets: true, |
| 213 | useCPP: true, |
Matt Godbolt | 217c41d | 2014-04-30 23:26:08 -0500 | [diff] [blame] | 214 | mode: cmMode |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 215 | }); |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 216 | |
Matt Godbolt | 28b6608 | 2016-06-11 08:29:32 -0500 | [diff] [blame] | 217 | // With reference to "fix typing '#' in emacs mode" |
| 218 | // https://github.com/mattgodbolt/gcc-explorer/pull/131 |
Arvid Gerstmann | 2cd6ff7 | 2016-06-11 01:54:42 +0200 | [diff] [blame] | 219 | cppEditor.setOption("extraKeys", { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 220 | "Alt-F": false |
Arvid Gerstmann | 2cd6ff7 | 2016-06-11 01:54:42 +0200 | [diff] [blame] | 221 | }); |
Matt Godbolt | 15d5b93 | 2016-03-27 22:41:18 -0500 | [diff] [blame] | 222 | cppEditor.on("change", function () { |
| 223 | if ($('.autocompile').hasClass('active')) { |
Gabriel Devillers | 3f62aa6 | 2016-06-23 13:38:03 +0200 | [diff] [blame] | 224 | onEditorChange(); |
Matt Godbolt | 15d5b93 | 2016-03-27 22:41:18 -0500 | [diff] [blame] | 225 | } |
| 226 | }); |
Gabriel Devillers | ac2338f | 2016-07-04 16:54:38 +0200 | [diff] [blame] | 227 | |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 228 | // Load/Save/Remove setting from the browser |
Matt Godbolt | 35156ff | 2012-07-09 06:56:11 -0500 | [diff] [blame] | 229 | function getSetting(name) { |
| 230 | return window.localStorage[windowLocalPrefix + "." + name]; |
| 231 | } |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 232 | |
Matt Godbolt | 35156ff | 2012-07-09 06:56:11 -0500 | [diff] [blame] | 233 | function setSetting(name, value) { |
| 234 | window.localStorage[windowLocalPrefix + "." + name] = value; |
| 235 | } |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 236 | |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 237 | function removeSetting(name) { |
| 238 | // source: http://stackoverflow.com/questions/9943220/how-to-delete-a-localstorage-item-when-the-browser-window-tab-is-closed |
| 239 | window.localStorage.removeItem(windowLocalPrefix + "." + name); |
| 240 | } |
| 241 | |
Matt Godbolt | 91c8965 | 2015-02-06 21:57:58 -0600 | [diff] [blame] | 242 | var codeText = getSetting('code'); |
| 243 | if (!codeText) codeText = $(".template.lang." + lang.replace(/[^a-zA-Z]/g, '').toLowerCase()).text(); |
| 244 | if (codeText) cppEditor.setValue(codeText); |
Gabriel Devillers | ac2338f | 2016-07-04 16:54:38 +0200 | [diff] [blame] | 245 | |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 246 | ignoreChanges = false; |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 247 | |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 248 | // auxiliary function to onCompileResponse(), |
| 249 | // used to display the compiler error messages in the editor's window |
Matt Godbolt | 43231b8 | 2013-02-05 06:26:59 -0600 | [diff] [blame] | 250 | function makeErrNode(text) { |
Matt Godbolt | 6dd6168 | 2013-02-06 17:28:01 -0600 | [diff] [blame] | 251 | var clazz = "error"; |
| 252 | if (text.match(/^warning/)) clazz = "warning"; |
| 253 | if (text.match(/^note/)) clazz = "note"; |
| 254 | var node = $('<div class="' + clazz + ' inline-msg"><span class="icon">!!</span><span class="msg"></span></div>'); |
Matt Godbolt | 43231b8 | 2013-02-05 06:26:59 -0600 | [diff] [blame] | 255 | node.find(".msg").text(text); |
| 256 | return node[0]; |
Matt Godbolt | 091bf37 | 2013-02-04 22:18:37 -0600 | [diff] [blame] | 257 | } |
| 258 | |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 259 | // keep track of error widgets to delete them if need be |
Matt Godbolt | 43231b8 | 2013-02-05 06:26:59 -0600 | [diff] [blame] | 260 | var errorWidgets = []; |
Matt Godbolt | c60cad2 | 2014-11-26 08:24:40 -0600 | [diff] [blame] | 261 | |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 262 | // Compilation's callback |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 263 | function onCompileResponse(slot, request, data) { |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 264 | var leaderSlot = getSetting('leaderSlot'); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 265 | |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 266 | console.log("[CALLBACK] onCompileResponse() with slot = " + slot.id + ", leaderSlot = ", +leaderSlot); |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 267 | var stdout = data.stdout || ""; |
| 268 | var stderr = data.stderr || ""; |
Matt Godbolt | c60cad2 | 2014-11-26 08:24:40 -0600 | [diff] [blame] | 269 | if (data.code === 0) { |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 270 | stdout += "Compiled ok in slot " + slot.id + "\n"; |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 271 | } else { |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 272 | stderr += "Compilation failed in slot " + slot.id + "\n"; |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 273 | } |
Matt Godbolt | b10029c | 2013-02-06 06:19:41 -0600 | [diff] [blame] | 274 | if (_gaq) { |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 275 | // TODO : to modify to handle the slots ? |
Matt Godbolt | c9cbcd9 | 2013-02-07 13:35:28 -0600 | [diff] [blame] | 276 | _gaq.push(['_trackEvent', 'Compile', request.compiler, request.options, data.code]); |
| 277 | _gaq.push(['_trackTiming', 'Compile', 'Timing', new Date() - request.timestamp]); |
Matt Godbolt | b10029c | 2013-02-06 06:19:41 -0600 | [diff] [blame] | 278 | } |
Matt Godbolt | 889cf27 | 2016-08-16 06:59:46 -0500 | [diff] [blame] | 279 | var outputRoot = slot.node.find('.result .output'); |
| 280 | outputRoot.find(':visible').remove(); |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 281 | // only show in Editor messages comming from the leaderSlot |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 282 | var i; |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 283 | if (slot.id == leaderSlot) { |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 284 | for (i = 0; i < errorWidgets.length; ++i) |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 285 | cppEditor.removeLineWidget(errorWidgets[i]); |
| 286 | errorWidgets.length = 0; |
| 287 | } |
Matt Godbolt | 746072c | 2015-02-21 12:10:51 -0600 | [diff] [blame] | 288 | var numLines = 0; |
Matt Godbolt | c60cad2 | 2014-11-26 08:24:40 -0600 | [diff] [blame] | 289 | parseLines(stderr + stdout, function (lineNum, msg) { |
Matt Godbolt | 746072c | 2015-02-21 12:10:51 -0600 | [diff] [blame] | 290 | if (numLines > 50) return; |
| 291 | if (numLines === 50) { |
| 292 | lineNum = null; |
| 293 | msg = "Too many output lines...truncated"; |
| 294 | } |
| 295 | numLines++; |
Matt Godbolt | 889cf27 | 2016-08-16 06:59:46 -0500 | [diff] [blame] | 296 | var elem = outputRoot.find('.template').clone().appendTo(outputRoot).removeClass('template'); |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 297 | if (lineNum) { |
Matt Godbolt | 480b130 | 2016-08-07 20:47:28 -0500 | [diff] [blame] | 298 | // only show messages coming from the leaderSlot in the Editor |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 299 | if (slot.id == leaderSlot) { |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 300 | errorWidgets.push(cppEditor.addLineWidget(lineNum - 1, makeErrNode(msg), { |
| 301 | coverGutter: false, noHScroll: true |
| 302 | })); |
| 303 | } |
Gabriel Devillers | 68379dd | 2016-08-04 15:43:12 +0200 | [diff] [blame] | 304 | elem.html($('<a href="#">').text(lineNum + " : " + msg)).click(function (e) { |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 305 | cppEditor.setSelection({line: lineNum - 1, ch: 0}, {line: lineNum, ch: 0}); |
Gabriel Devillers | 68379dd | 2016-08-04 15:43:12 +0200 | [diff] [blame] | 306 | |
| 307 | // do not bring user to the top of index.html |
| 308 | // http://stackoverflow.com/questions/3252730 |
| 309 | e.preventDefault(); |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 310 | return false; |
| 311 | }); |
| 312 | } else { |
| 313 | elem.text(msg); |
| 314 | } |
| 315 | }); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 316 | slot.currentAssembly = data.asm || fakeAsm("[no output]"); |
| 317 | updateAsm(slot); |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 318 | for (i = 0; i < slot.pendingDiffs.length; i++) { |
Gabriel Devillers | 3205e56 | 2016-07-25 15:57:15 +0200 | [diff] [blame] | 319 | onDiffChange(slot.pendingDiffs[i], request.fromEditor); |
Gabriel Devillers | 60c96cd | 2016-07-25 14:30:34 +0200 | [diff] [blame] | 320 | } |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 321 | } |
| 322 | |
Matt Godbolt | 480b130 | 2016-08-07 20:47:28 -0500 | [diff] [blame] | 323 | function numberUsedLines() { |
| 324 | var result = {source: {}, pending: false, asm: {}}; |
| 325 | _.each(slots, function (slot) { |
| 326 | if (slot.currentAssembly) { |
| 327 | slot.currentAssembly.forEach(function (x) { |
| 328 | if (x.source) result.source[x.source - 1] = true; |
| 329 | }); |
| 330 | } |
Matt Godbolt | c60cad2 | 2014-11-26 08:24:40 -0600 | [diff] [blame] | 331 | }); |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 332 | var ordinal = 0; |
Matt Godbolt | 480b130 | 2016-08-07 20:47:28 -0500 | [diff] [blame] | 333 | _.each(result.source, function (v, k) { |
| 334 | result.source[k] = ordinal++; |
Matt Godbolt | c60cad2 | 2014-11-26 08:24:40 -0600 | [diff] [blame] | 335 | }); |
Matt Godbolt | 480b130 | 2016-08-07 20:47:28 -0500 | [diff] [blame] | 336 | _.each(slots, function (slot) { |
| 337 | var asmLines = {}; |
| 338 | if (slot.currentAssembly) { |
| 339 | slot.currentAssembly.forEach(function (x, index) { |
| 340 | if (x.source) asmLines[index] = result.source[x.source - 1]; |
| 341 | if (x.fake) result.pending = true; |
| 342 | }); |
| 343 | } |
| 344 | result.asm[slot.id] = asmLines; |
Matt Godbolt | c60cad2 | 2014-11-26 08:24:40 -0600 | [diff] [blame] | 345 | }); |
Matt Godbolt | 480b130 | 2016-08-07 20:47:28 -0500 | [diff] [blame] | 346 | return result; |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 347 | } |
Matt Godbolt | 480b130 | 2016-08-07 20:47:28 -0500 | [diff] [blame] | 348 | |
| 349 | var colourise = function colourise() { |
| 350 | var numberedLines = numberUsedLines(); |
| 351 | cppEditor.operation(function () { |
| 352 | clearBackground(cppEditor); |
| 353 | if (numberedLines.pending) return; // don't colourise until all results are in |
| 354 | _.each(numberedLines.source, function (ordinal, line) { |
| 355 | cppEditor.addLineClass(parseInt(line), |
| 356 | "background", "rainbow-" + (ordinal % NumRainbowColours)); |
| 357 | }); |
| 358 | }); |
| 359 | // colourise the assembly in slots |
| 360 | _.each(slots, function (slot) { |
| 361 | slot.asmCodeMirror.operation(function () { |
| 362 | clearBackground(slot.asmCodeMirror); |
| 363 | if (numberedLines.pending) return; // don't colourise until all results are in |
| 364 | _.each(numberedLines.asm[slot.id], function (ordinal, line) { |
| 365 | slot.asmCodeMirror.addLineClass(parseInt(line), |
| 366 | "background", "rainbow-" + (ordinal % NumRainbowColours)); |
| 367 | }); |
| 368 | }); |
| 369 | }); |
| 370 | }; |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 371 | |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 372 | function updateAsm(slot, forceUpdate) { |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 373 | console.log("[CALLBACK] updateAsm() with slot = " + slot.id + ", forceUpdate = " + forceUpdate); |
| 374 | if (!slot.currentAssembly) return; |
| 375 | var hashedUpdate = JSON.stringify(slot.currentAssembly); |
Matt Godbolt | 480b130 | 2016-08-07 20:47:28 -0500 | [diff] [blame] | 376 | if (!forceUpdate && JSON.stringify(slot.lastUpdatedAsm) == hashedUpdate) { |
Matt Godbolt | c60cad2 | 2014-11-26 08:24:40 -0600 | [diff] [blame] | 377 | return; |
| 378 | } |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 379 | slot.lastUpdatedAsm = hashedUpdate; |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 380 | |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 381 | var asmText = $.map(slot.currentAssembly, function (x) { |
Matt Godbolt | c60cad2 | 2014-11-26 08:24:40 -0600 | [diff] [blame] | 382 | return x.text; |
| 383 | }).join("\n"); |
Matt Godbolt | c60cad2 | 2014-11-26 08:24:40 -0600 | [diff] [blame] | 384 | |
Matt Godbolt | b8fca03 | 2015-11-28 13:46:39 -0600 | [diff] [blame] | 385 | var filters = currentFilters(); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 386 | slot.asmCodeMirror.operation(function () { |
| 387 | slot.asmCodeMirror.setValue(asmText); |
| 388 | clearBackground(slot.asmCodeMirror); |
Matt Godbolt | b8dac7e | 2015-11-23 22:26:00 -0600 | [diff] [blame] | 389 | var addrToAddrDiv = {}; |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 390 | $.each(slot.currentAssembly, function (line, obj) { |
Matt Godbolt | 0f95490 | 2015-11-23 18:16:34 -0600 | [diff] [blame] | 391 | var address = obj.address ? obj.address.toString(16) : ""; |
Matt Godbolt | b8dac7e | 2015-11-23 22:26:00 -0600 | [diff] [blame] | 392 | var div = $("<div class='address cm-number'>" + address + "</div>"); |
| 393 | addrToAddrDiv[address] = {div: div, line: line}; |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 394 | slot.asmCodeMirror.setGutterMarker(line, 'address', div[0]); |
Matt Godbolt | b8dac7e | 2015-11-23 22:26:00 -0600 | [diff] [blame] | 395 | }); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 396 | $.each(slot.currentAssembly, function (line, obj) { |
Matt Godbolt | 0f95490 | 2015-11-23 18:16:34 -0600 | [diff] [blame] | 397 | var opcodes = $("<div class='opcodes'></div>"); |
| 398 | if (obj.opcodes) { |
| 399 | var title = []; |
| 400 | $.each(obj.opcodes, function (_, op) { |
| 401 | var opcodeNum = "00" + op.toString(16); |
| 402 | opcodeNum = opcodeNum.substr(opcodeNum.length - 2); |
| 403 | title.push(opcodeNum); |
| 404 | var opcode = $("<span class='opcode'>" + opcodeNum + "</span>"); |
| 405 | opcodes.append(opcode); |
| 406 | }); |
| 407 | opcodes.attr('title', title.join(" ")); |
| 408 | } |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 409 | slot.asmCodeMirror.setGutterMarker(line, 'opcodes', opcodes[0]); |
Matt Godbolt | b8dac7e | 2015-11-23 22:26:00 -0600 | [diff] [blame] | 410 | if (obj.links) { |
| 411 | $.each(obj.links, function (_, link) { |
| 412 | var from = {line: line, ch: link.offset}; |
| 413 | var to = {line: line, ch: link.offset + link.length}; |
| 414 | var address = link.to.toString(16); |
| 415 | var thing = $("<a href='#' class='cm-number'>" + address + "</a>"); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 416 | slot.asmCodeMirror.markText( |
Matt Godbolt | b8dac7e | 2015-11-23 22:26:00 -0600 | [diff] [blame] | 417 | from, to, {replacedWith: thing[0], handleMouseEvents: false}); |
| 418 | var dest = addrToAddrDiv[address]; |
| 419 | if (dest) { |
| 420 | thing.on('hover', function (e) { |
| 421 | var entered = e.type == "mouseenter"; |
| 422 | dest.div.toggleClass("highlighted", entered); |
| 423 | thing.toggleClass("highlighted", entered); |
| 424 | }); |
| 425 | thing.on('click', function (e) { |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 426 | slot.asmCodeMirror.scrollIntoView({line: dest.line, ch: 0}, 30); |
Matt Godbolt | 7eb2f5e | 2015-11-23 23:14:39 -0600 | [diff] [blame] | 427 | dest.div.toggleClass("highlighted", false); |
| 428 | thing.toggleClass("highlighted", false); |
Gabriel Devillers | 68379dd | 2016-08-04 15:43:12 +0200 | [diff] [blame] | 429 | |
| 430 | // do not bring user to the top of index.html |
| 431 | e.preventDefault(); |
Matt Godbolt | b8dac7e | 2015-11-23 22:26:00 -0600 | [diff] [blame] | 432 | }); |
| 433 | } |
| 434 | }); |
| 435 | } |
Matt Godbolt | 0f95490 | 2015-11-23 18:16:34 -0600 | [diff] [blame] | 436 | }); |
Matt Godbolt | 7eb2f5e | 2015-11-23 23:14:39 -0600 | [diff] [blame] | 437 | if (filters.binary) { |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 438 | slot.asmCodeMirror.setOption('lineNumbers', false); |
| 439 | slot.asmCodeMirror.setOption('gutters', ['address', 'opcodes']); |
Matt Godbolt | 7eb2f5e | 2015-11-23 23:14:39 -0600 | [diff] [blame] | 440 | } else { |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 441 | slot.asmCodeMirror.setOption('lineNumbers', true); |
| 442 | slot.asmCodeMirror.setOption('gutters', ['CodeMirror-linenumbers']); |
Matt Godbolt | 7eb2f5e | 2015-11-23 23:14:39 -0600 | [diff] [blame] | 443 | } |
Matt Godbolt | cc62e71 | 2013-02-05 17:30:49 -0600 | [diff] [blame] | 444 | }); |
Gabriel Devillers | ac2338f | 2016-07-04 16:54:38 +0200 | [diff] [blame] | 445 | |
Matt Godbolt | 480b130 | 2016-08-07 20:47:28 -0500 | [diff] [blame] | 446 | // colourise the editor |
| 447 | if (filters.colouriseAsm) { |
| 448 | colourise(); |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
Matt Godbolt | 4b47e42 | 2015-11-11 17:41:19 -0600 | [diff] [blame] | 452 | function fakeAsm(text) { |
Matt Godbolt | 480b130 | 2016-08-07 20:47:28 -0500 | [diff] [blame] | 453 | return [{text: text, source: null, fake: true}]; |
Matt Godbolt | 498930d | 2012-09-19 18:16:10 -0500 | [diff] [blame] | 454 | } |
| 455 | |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 456 | // slot's parameters callback |
| 457 | // TODO : refactor with onEditorChange : those functions could call an updateSlot(slot) |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 458 | function onParamChange(slot, force) { |
| 459 | console.log("[CALLBACK] onParamChange() on slot " + slot.id); |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 460 | |
| 461 | // update the UI of the diff that depends of this slot |
| 462 | for (var i = 0; i < slot.pendingDiffs.length; i++) { |
| 463 | updateDiffUI(slot.pendingDiffs[i]); |
| 464 | } |
| 465 | |
Gabriel Devillers | 3f62aa6 | 2016-06-23 13:38:03 +0200 | [diff] [blame] | 466 | if (ignoreChanges) return; // Ugly hack during startup. |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 467 | if (slot.pendingTimeoutInSlot) { |
| 468 | console.log("[TIME] Clearing time out in slot " + slot.id); |
| 469 | clearTimeout(slot.pendingTimeoutInSlot); |
Gabriel Devillers | 3f62aa6 | 2016-06-23 13:38:03 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 472 | console.log("[TIME] Setting time out in slot " + slot.id); |
| 473 | slot.pendingTimeoutInSlot = setTimeout(function () { |
| 474 | console.log("[TIME] Timed out : compiling in slot " + slot.id + " triggered by modification of params..."); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 475 | (function (slot) { |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 476 | var data = { |
Gabriel Devillers | 3205e56 | 2016-07-25 15:57:15 +0200 | [diff] [blame] | 477 | fromEditor: false, |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 478 | source: cppEditor.getValue(), |
| 479 | compiler: currentCompilerId(slot), |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 480 | options: slot.node.find('.compiler-options').val(), |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 481 | filters: currentFilters() |
| 482 | }; |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 483 | setSetting('compiler' + slot.id, data.compiler); |
| 484 | setSetting('compilerOptions' + slot.id, data.options); |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 485 | var stringifiedReq = JSON.stringify(data); |
Gabriel Devillers | 6d94cab | 2016-07-29 15:44:31 +0200 | [diff] [blame] | 486 | if (!force && stringifiedReq == slot.lastRequest) return; |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 487 | slot.lastRequest = stringifiedReq; |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 488 | data.timestamp = new Date(); |
| 489 | $.ajax({ |
| 490 | type: 'POST', |
| 491 | url: '/compile', |
| 492 | dataType: 'json', |
| 493 | contentType: 'application/json', |
| 494 | data: JSON.stringify(data), |
| 495 | success: function (result) { |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 496 | onCompileResponse(slot, data, result); |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 497 | }, |
| 498 | error: function (xhr, e_status, error) { |
| 499 | console.log("AJAX request failed, reason : " + error); |
| 500 | }, |
| 501 | cache: false |
| 502 | }); |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 503 | setSetting('code', cppEditor.getValue()); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 504 | slot.currentAssembly = fakeAsm("[Processing...]"); |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 505 | updateAsm(slot); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 506 | })(slot); |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 507 | }, 750); // Time in ms after which action is taken (if inactivity) |
| 508 | |
| 509 | // (maybe redundant) execute the callback passed to Compiler() |
| 510 | onEditorChangeCallback(); |
| 511 | } |
| 512 | |
Gabriel Devillers | e157d9f | 2016-06-28 14:51:27 +0200 | [diff] [blame] | 513 | function onEditorChange(force) { |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 514 | console.log("[CALLBACK] onEditorChange()"); |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 515 | if (ignoreChanges) return; // Ugly hack during startup. |
| 516 | if (pendingTimeoutInEditor) { |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 517 | console.log("[TIME] Clearing time out in editor"); |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 518 | clearTimeout(pendingTimeoutInEditor); |
| 519 | } |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 520 | |
| 521 | console.log("[TIME] Setting time out in editor"); |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 522 | pendingTimeoutInEditor = setTimeout(function () { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 523 | console.log("[TIME] Timed out in editor : compiling in all " + slots.length + " slots..."); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 524 | for (var i = 0; i < slots.length; i++) { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 525 | (function (slot) { |
Gabriel Devillers | 3f62aa6 | 2016-06-23 13:38:03 +0200 | [diff] [blame] | 526 | var data = { |
Gabriel Devillers | 3205e56 | 2016-07-25 15:57:15 +0200 | [diff] [blame] | 527 | fromEditor: true, |
Gabriel Devillers | 3f62aa6 | 2016-06-23 13:38:03 +0200 | [diff] [blame] | 528 | source: cppEditor.getValue(), |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 529 | compiler: currentCompilerId(slot), |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 530 | options: slot.node.find('.compiler-options').val(), |
Gabriel Devillers | 3f62aa6 | 2016-06-23 13:38:03 +0200 | [diff] [blame] | 531 | filters: currentFilters() |
| 532 | }; |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 533 | setSetting('compiler' + slot.id, data.compiler); |
| 534 | setSetting('compilerOptions' + slot.id, data.options); |
Gabriel Devillers | 3f62aa6 | 2016-06-23 13:38:03 +0200 | [diff] [blame] | 535 | var stringifiedReq = JSON.stringify(data); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 536 | if (!force && stringifiedReq == slot.lastRequest) return; |
| 537 | slot.lastRequest = stringifiedReq; |
Gabriel Devillers | 3f62aa6 | 2016-06-23 13:38:03 +0200 | [diff] [blame] | 538 | data.timestamp = new Date(); |
| 539 | $.ajax({ |
| 540 | type: 'POST', |
| 541 | url: '/compile', |
| 542 | dataType: 'json', |
| 543 | contentType: 'application/json', |
| 544 | data: JSON.stringify(data), |
| 545 | success: function (result) { |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 546 | onCompileResponse(slot, data, result); |
Gabriel Devillers | 3f62aa6 | 2016-06-23 13:38:03 +0200 | [diff] [blame] | 547 | }, |
| 548 | error: function (xhr, e_status, error) { |
Gabriel Devillers | e157d9f | 2016-06-28 14:51:27 +0200 | [diff] [blame] | 549 | console.log("AJAX request failed, reason : " + error); |
Gabriel Devillers | 3f62aa6 | 2016-06-23 13:38:03 +0200 | [diff] [blame] | 550 | }, |
| 551 | cache: false |
| 552 | }); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 553 | slot.currentAssembly = fakeAsm("[Processing...]"); |
Gabriel Devillers | 3f62aa6 | 2016-06-23 13:38:03 +0200 | [diff] [blame] | 554 | updateAsm(slot); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 555 | })(slots[i]); |
Gabriel Devillers | ac2338f | 2016-07-04 16:54:38 +0200 | [diff] [blame] | 556 | } |
| 557 | }, 750); // Time in ms after which action is taken (if inactivity) |
Matt Godbolt | 35156ff | 2012-07-09 06:56:11 -0500 | [diff] [blame] | 558 | setSetting('code', cppEditor.getValue()); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 559 | for (var i = 0; i < slots.length; i++) { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 560 | (function (slot) { |
Gabriel Devillers | ac2338f | 2016-07-04 16:54:38 +0200 | [diff] [blame] | 561 | updateAsm(slot); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 562 | })(slots[i]); |
Gabriel Devillers | ac2338f | 2016-07-04 16:54:38 +0200 | [diff] [blame] | 563 | } |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 564 | |
Gabriel Devillers | ac2338f | 2016-07-04 16:54:38 +0200 | [diff] [blame] | 565 | // execute the callback passed to Compiler() |
Gabriel Devillers | 3f62aa6 | 2016-06-23 13:38:03 +0200 | [diff] [blame] | 566 | onEditorChangeCallback(); |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | function setSource(code) { |
| 570 | cppEditor.setValue(code); |
| 571 | } |
| 572 | |
| 573 | function getSource() { |
| 574 | return cppEditor.getValue(); |
| 575 | } |
| 576 | |
Matt Godbolt | 1e810d3 | 2016-03-23 22:14:21 -0500 | [diff] [blame] | 577 | function serialiseState(compress) { |
Gabriel Devillers | 2718921 | 2016-07-04 17:17:39 +0200 | [diff] [blame] | 578 | console.log("[WINDOW] Serialising state..."); |
Gabriel Devillers | 3247579 | 2016-08-04 09:52:50 +0200 | [diff] [blame] | 579 | // Beware: we do not serialise the whole objects Slots / Diffs ! |
| 580 | // (they are to big to be passed by a URL) |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 581 | |
| 582 | // Memorize informations on slots |
Gabriel Devillers | 60c96cd | 2016-07-25 14:30:34 +0200 | [diff] [blame] | 583 | var slotIds = []; // necessary only to link with iiffs |
Gabriel Devillers | 3247579 | 2016-08-04 09:52:50 +0200 | [diff] [blame] | 584 | var leaderSlotId = leaderSlot.id; |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 585 | var compilersInSlots = []; |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 586 | var optionsInSlots = []; |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 587 | slots.forEach(function (slot) { |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 588 | slotIds.push(slot.id); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 589 | compilersInSlots.push(currentCompilerId(slot)); |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 590 | optionsInSlots.push(slot.node.find('.compiler-options').val()); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 591 | }); |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 592 | |
| 593 | // Memorize informations on diffs |
| 594 | var diffIds = []; |
| 595 | var slotsInDiffs = []; |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 596 | diffs.forEach(function (diff) { |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 597 | diffIds.push(diff.id); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 598 | slotsInDiffs.push({ |
| 599 | before: diff.beforeSlot.id, |
| 600 | after: diff.afterSlot.id |
| 601 | }); |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 602 | }); |
| 603 | |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 604 | var state = { |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 605 | slotCount: slots.length, |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 606 | slotIds: slotIds, |
Gabriel Devillers | 3247579 | 2016-08-04 09:52:50 +0200 | [diff] [blame] | 607 | leaderSlotId: leaderSlotId, |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 608 | compilersInSlots: compilersInSlots, |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 609 | optionsInSlots: optionsInSlots, |
| 610 | |
| 611 | diffCount: diffs.length, |
| 612 | diffIds: diffIds, |
| 613 | slotsInDiffs: slotsInDiffs |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 614 | }; |
Matt Godbolt | 1e810d3 | 2016-03-23 22:14:21 -0500 | [diff] [blame] | 615 | if (compress) { |
| 616 | state.sourcez = LZString.compressToBase64(cppEditor.getValue()); |
| 617 | } else { |
| 618 | state.source = cppEditor.getValue(); |
| 619 | } |
Matt Godbolt | 35156ff | 2012-07-09 06:56:11 -0500 | [diff] [blame] | 620 | return state; |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 621 | } |
| 622 | |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 623 | function deserialiseState(state, compilers, defaultCompiler) { |
| 624 | console.log("[WINDOW] Deserialising state ..."); |
| 625 | if (state.hasOwnProperty('sourcez')) { |
| 626 | cppEditor.setValue(LZString.decompressFromBase64(state.sourcez)); |
| 627 | } else { |
| 628 | cppEditor.setValue(state.source); |
| 629 | } |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 630 | |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 631 | if (slots.length != 0) { |
| 632 | console.log("[WINDOW] Deserialisation : deleting existing slots ..."); |
| 633 | while (slots.length != 0) { |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 634 | deleteAndUnplaceSlot(slots[slots.length - 1]); |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 635 | } |
| 636 | } |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 637 | |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 638 | if (diffs.length != 0) { |
| 639 | console.log("[WINDOW] Deserialisation : deleting existing diffs ..."); |
| 640 | while (diffs.length != 0) { |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 641 | deleteAndUnplaceDiff(diffs[diffs.length - 1]); |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
Gabriel Devillers | 3247579 | 2016-08-04 09:52:50 +0200 | [diff] [blame] | 645 | // Deserialise slots |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 646 | console.log("[WINDOW] Deserialisation : deserializing slots..."); |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 647 | var i; |
| 648 | for (i = 0; i < state.slotCount; i++) { |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 649 | var newSlot = createAndPlaceSlot(compilers, |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 650 | defaultCompiler, |
| 651 | state.slotIds[i]); |
| 652 | setCompilerById(state.compilersInSlots[i], newSlot); |
Gabriel Devillers | 3247579 | 2016-08-04 09:52:50 +0200 | [diff] [blame] | 653 | newSlot.node.find('.compiler-options').val(state.optionsInSlots[i]); |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 654 | // Somewhat hackily persist compiler into local storage else when the ajax response comes in |
| 655 | // with the list of compilers it can splat over the deserialized version. |
| 656 | // The whole serialize/hash/localStorage code is a mess! TODO(mg): fix |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 657 | |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 658 | setSetting('compiler' + newSlot.id, state.compilersInSlots[i]); |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 659 | } |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 660 | |
Gabriel Devillers | 3247579 | 2016-08-04 09:52:50 +0200 | [diff] [blame] | 661 | // Deserialise leaderSlot |
| 662 | setLeaderSlotIcon(getSlotById(state.leaderSlotId)); |
| 663 | leaderSlot = getSlotById(state.leaderSlotId); |
| 664 | setSetting('leaderSlot', leaderSlot.id); |
| 665 | |
| 666 | // Deserialise diffs |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 667 | console.log("[WINDOW] Deserialisation : deserializing diffs..."); |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 668 | for (i = 0; i < state.diffCount; i++) { |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 669 | var newDiff = createAndPlaceDiff(state.diffIds[i]); |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 670 | setSlotInDiff(newDiff, |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 671 | "before", |
| 672 | getSlotById(state.slotsInDiffs[i]["before"])); |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 673 | setSlotInDiff(newDiff, |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 674 | "after", |
| 675 | getSlotById(state.slotsInDiffs[i]["after"])); |
Gabriel Devillers | 7a8fec3 | 2016-07-28 10:32:29 +0200 | [diff] [blame] | 676 | onDiffChange(newDiff, false); |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | resizeEditors(); |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 680 | return true; |
| 681 | } |
| 682 | |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 683 | // TODO : split in two functions : one is slot dependant, the other set parameters common to all slots |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 684 | function updateCompilerAndButtons(slot) { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 685 | var compiler = currentCompiler(slot); // TODO handle compiler being null (if e.g. a compiler is removed from the site) |
Gabriel Devillers | f6a5dd1 | 2016-08-02 14:08:46 +0200 | [diff] [blame] | 686 | slot.node.find('.compilerVersion').text(compiler.name + " (" + compiler.version + ")"); |
Matt Godbolt | b8fca03 | 2015-11-28 13:46:39 -0600 | [diff] [blame] | 687 | var filters = currentFilters(); |
Matt Godbolt | e6dbd2b | 2015-11-28 11:04:38 -0600 | [diff] [blame] | 688 | var supportsIntel = compiler.intelAsm || filters.binary; |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 689 | domRoot.find('#commonParams .filter button.btn[value="intel"]').toggleClass("disabled", !supportsIntel); |
| 690 | domRoot.find('#commonParams .filter button.btn[value="binary"]').toggleClass("disabled", !compiler.supportsBinary).toggle(OPTIONS.supportsBinary); |
| 691 | domRoot.find('#commonParams .filter .nonbinary').toggleClass("disabled", !!filters.binary); |
Matt Godbolt | e6dbd2b | 2015-11-28 11:04:38 -0600 | [diff] [blame] | 692 | } |
| 693 | |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 694 | function onCompilerChange(slot) { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 695 | console.log("[DEBUG] onCompilerChange called with slot.id = " + slot.id); |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 696 | onParamChange(slot); |
| 697 | updateCompilerAndButtons(slot); |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 698 | setAllDiffSlotsMenus(); |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 699 | } |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 700 | |
Gabriel Devillers | 3205e56 | 2016-07-25 15:57:15 +0200 | [diff] [blame] | 701 | function onDiffChange(diff, fromEditor) { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 702 | console.log("[DEBUG] inside onDiffChange with diff id = " + diff.id + |
| 703 | ", seen fromEditor = " + fromEditor); |
Gabriel Devillers | 3205e56 | 2016-07-25 15:57:15 +0200 | [diff] [blame] | 704 | if (fromEditor == false) { |
| 705 | diff.remainingTriggers = 2; |
| 706 | } else { |
| 707 | diff.remainingTriggers = diff.remainingTriggers - 1; |
| 708 | if (diff.remainingTriggers == 0) { |
| 709 | diff.remainingTriggers = 2; |
| 710 | } else { |
| 711 | return null; |
| 712 | } |
| 713 | } |
| 714 | |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 715 | // If one slot is not mentioned, stop before making the ajax request |
| 716 | if (diff.beforeSlot == null || diff.afterSlot == null) { |
| 717 | return null; |
| 718 | } |
| 719 | var data = { |
| 720 | // it should also be possible to use .currentAsembly |
| 721 | before: diff.beforeSlot.asmCodeMirror.getValue(), |
| 722 | after: diff.afterSlot.asmCodeMirror.getValue() |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 723 | }; |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 724 | |
| 725 | $.ajax({ |
| 726 | type: 'POST', |
| 727 | url: '/diff', |
| 728 | dataType: 'json', |
| 729 | contentType: 'application/json', |
| 730 | data: JSON.stringify(data), |
| 731 | success: function (result) { |
| 732 | //console.log("Success : "+JSON.stringify(result)); |
| 733 | onDiffResponse(diff, data, result); |
| 734 | }, |
| 735 | error: function (xhr, e_status, error) { |
| 736 | console.log("AJAX request for diff failed, reason : " + error); |
| 737 | }, |
| 738 | cache: false |
| 739 | }); |
| 740 | } |
| 741 | |
| 742 | function onDiffResponse(diff, data, result) { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 743 | console.log("[CALLBACK] onDiffResponse() with diff = " + diff.id); |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 744 | // console.log("[DEBUG] result: "+result); |
| 745 | diff.currentDiff = result.computedDiff; |
Gabriel Devillers | 269615a | 2016-07-20 18:18:55 +0200 | [diff] [blame] | 746 | diff.zones = result.zones; |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 747 | updateDiff(diff); |
| 748 | } |
| 749 | |
| 750 | function updateDiff(diff) { |
| 751 | console.log("[CALLBACK] updateDiff() with diff = " + diff.id); |
| 752 | // console.log("[DEBUG] currentDiff: " + JSON.stringify(diff.currentDiff)); |
| 753 | if (!diff.currentDiff) { |
| 754 | return; |
| 755 | } |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 756 | |
| 757 | diff.asmCodeMirror.operation(function () { |
| 758 | diff.asmCodeMirror.setValue(diff.currentDiff); |
| 759 | clearBackground(diff.asmCodeMirror); |
| 760 | }); |
Matt Godbolt | dc57aba | 2016-08-06 15:50:06 -0500 | [diff] [blame] | 761 | if (!diff.zones) return; |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 762 | var doc = diff.asmCodeMirror.getDoc(); |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 763 | var computeLineChCoord = buildComputeLineChCoord(diff.currentDiff); |
Gabriel Devillers | 269615a | 2016-07-20 18:18:55 +0200 | [diff] [blame] | 764 | // Same colors as in phabricator's diffs |
| 765 | var cssStyles = ["background-color: rgba(151,234,151,.6);", |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 766 | "background-color: rgba(251,175,175,.7);"]; |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 767 | var colorMarkedZones = []; |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 768 | for (var i = 0; i < diff.zones.length; i++) { |
| 769 | for (var j = 0; j < diff.zones[i].length; j++) { |
Gabriel Devillers | 269615a | 2016-07-20 18:18:55 +0200 | [diff] [blame] | 770 | colorMarkedZones.push( |
| 771 | doc.markText(computeLineChCoord(diff.zones[i][j].begin), |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 772 | computeLineChCoord(diff.zones[i][j].end + 1), |
| 773 | {css: cssStyles[i]})); |
Gabriel Devillers | 269615a | 2016-07-20 18:18:55 +0200 | [diff] [blame] | 774 | } |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | |
Gabriel Devillers | 269615a | 2016-07-20 18:18:55 +0200 | [diff] [blame] | 778 | // This function is required to place multiline marks in a CodeMirror |
| 779 | // windows: markText accepts only coordinates in the form (line, column) |
| 780 | // with line and column starting at 1. |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 781 | function buildComputeLineChCoord(text) { |
| 782 | // assume text is 1 line containing '\n' to break lines |
| 783 | // below calculations are placed outside the function to speed up |
| 784 | var splitedStr = text.split("\n"); |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 785 | var i; |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 786 | for (i = 0; i < splitedStr.length; i++) { |
| 787 | splitedStr[i] = splitedStr[i] + "\n"; |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 788 | } |
| 789 | var lastPosInLine = []; |
| 790 | var currentSum = splitedStr[0].length - 1; |
| 791 | // console.log("Last pos in line "+0+": "+currentSum); |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 792 | lastPosInLine.push(currentSum); |
| 793 | for (i = 1; i < splitedStr.length; i++) { |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 794 | currentSum = currentSum + splitedStr[i].length; |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 795 | lastPosInLine.push(currentSum); |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 796 | // console.log("Last pos in line "+i+": "+currentSum); |
| 797 | } |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 798 | return function (pos) { |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 799 | var line = 0; |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 800 | while (lastPosInLine[line] < pos) { |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 801 | line = line + 1; |
| 802 | } |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 803 | var ch = (line === 0) ? pos : pos - lastPosInLine[line - 1] - 1; |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 804 | return {line: line, ch: ch}; |
| 805 | } |
| 806 | } |
Matt Godbolt | c60cad2 | 2014-11-26 08:24:40 -0600 | [diff] [blame] | 807 | |
Matt Godbolt | 7f39772 | 2015-01-22 22:30:35 -0600 | [diff] [blame] | 808 | function mapCompiler(compiler) { |
| 809 | if (!compilersById[compiler]) { |
| 810 | // Handle old settings and try the alias table. |
Matt Godbolt | d5bef9e | 2015-01-27 12:52:38 -0600 | [diff] [blame] | 811 | compiler = compilersByAlias[compiler]; |
| 812 | if (compiler) compiler = compiler.id; |
Matt Godbolt | 7f39772 | 2015-01-22 22:30:35 -0600 | [diff] [blame] | 813 | } |
| 814 | return compiler; |
| 815 | } |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 816 | |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 817 | // added has auxiliary to setCompilers, in order not to break interface |
| 818 | // TODO : consider refactoring as some tasks are repeated |
| 819 | function setCompilersInSlot(compilers, defaultCompiler, slot) { |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 820 | console.log("[INIT] inside setCompilersInSlot()"); |
Gabriel Devillers | f6a5dd1 | 2016-08-02 14:08:46 +0200 | [diff] [blame] | 821 | slot.node.find('.compilers li').remove(); |
Matt Godbolt | f4e76d2 | 2015-01-22 07:22:25 -0600 | [diff] [blame] | 822 | compilersById = {}; |
| 823 | compilersByAlias = {}; |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 824 | // fills the compiler list |
Matt Godbolt | c60cad2 | 2014-11-26 08:24:40 -0600 | [diff] [blame] | 825 | $.each(compilers, function (index, arg) { |
Matt Godbolt | f4e76d2 | 2015-01-22 07:22:25 -0600 | [diff] [blame] | 826 | compilersById[arg.id] = arg; |
| 827 | if (arg.alias) compilersByAlias[arg.alias] = arg; |
Matt Godbolt | 3b39141 | 2016-03-27 12:04:52 -0500 | [diff] [blame] | 828 | var elem = $('<li><a href="#">' + arg.name + '</a></li>'); |
Gabriel Devillers | f6a5dd1 | 2016-08-02 14:08:46 +0200 | [diff] [blame] | 829 | slot.node.find('.compilers').append(elem); |
Matt Godbolt | 3b39141 | 2016-03-27 12:04:52 -0500 | [diff] [blame] | 830 | (function () { |
Gabriel Devillers | 68379dd | 2016-08-04 15:43:12 +0200 | [diff] [blame] | 831 | elem.click(function (e) { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 832 | setCompilerById(arg.id, slot); |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 833 | onCompilerChange(slot); |
Gabriel Devillers | 68379dd | 2016-08-04 15:43:12 +0200 | [diff] [blame] | 834 | |
| 835 | // do not bring user to the top of index.html |
| 836 | e.preventDefault(); |
Matt Godbolt | 3b39141 | 2016-03-27 12:04:52 -0500 | [diff] [blame] | 837 | }); |
| 838 | })(elem.find("a"), arg.id); |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 839 | }); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 840 | var compiler = getSetting('compiler' + slot.id); |
Gabriel Devillers | b70d82e | 2016-07-04 17:06:29 +0200 | [diff] [blame] | 841 | if (!compiler) { |
| 842 | compiler = defaultCompiler; |
| 843 | compiler = mapCompiler(compiler); |
| 844 | if (!compiler) |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 845 | console.log("Could not map the default compiler id. " + |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 846 | "Please double check your configuration file."); |
Gabriel Devillers | b70d82e | 2016-07-04 17:06:29 +0200 | [diff] [blame] | 847 | } else { |
| 848 | compiler = mapCompiler(compiler); |
| 849 | if (!compiler) |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 850 | console.log("Could not map the compiler found in settings. " + |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 851 | "Please clear your browser cache."); |
Gabriel Devillers | b70d82e | 2016-07-04 17:06:29 +0200 | [diff] [blame] | 852 | } |
Matt Godbolt | f4e76d2 | 2015-01-22 07:22:25 -0600 | [diff] [blame] | 853 | if (compiler) { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 854 | setCompilerById(compiler, slot); |
Matt Godbolt | 42945b4 | 2012-07-06 06:22:11 -0500 | [diff] [blame] | 855 | } |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 856 | onCompilerChange(slot); |
| 857 | } |
| 858 | |
| 859 | function setCompilers(compilers, defaultCompiler) { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 860 | console.log("[INIT] setCompilers() was called with compilers = " + |
| 861 | JSON.stringify(compilers) + ", defaultCompiler = " + defaultCompiler); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 862 | for (var i = 0; i < slots.length; i++) { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 863 | (function (slot) { |
| 864 | setCompilersInSlot(compilers, defaultCompiler, slot); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 865 | })(slots[i]); |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 866 | } |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 867 | } |
| 868 | |
Matt Godbolt | b8fca03 | 2015-11-28 13:46:39 -0600 | [diff] [blame] | 869 | function currentFilters() { |
| 870 | return patchUpFilters(filters_); |
| 871 | } |
Matt Godbolt | 3b39141 | 2016-03-27 12:04:52 -0500 | [diff] [blame] | 872 | |
Matt Godbolt | 7700691 | 2012-06-28 06:12:41 -0500 | [diff] [blame] | 873 | function setFilters(f) { |
Matt Godbolt | b8fca03 | 2015-11-28 13:46:39 -0600 | [diff] [blame] | 874 | filters_ = $.extend({}, f); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 875 | slots.forEach(function (slot) { |
Gabriel Devillers | fdbf39f | 2016-08-01 15:40:07 +0200 | [diff] [blame] | 876 | onParamChange(slot); |
Gabriel Devillers | 28ef768 | 2016-06-27 11:29:35 +0200 | [diff] [blame] | 877 | updateCompilerAndButtons(slot); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 878 | }); |
Matt Godbolt | 7700691 | 2012-06-28 06:12:41 -0500 | [diff] [blame] | 879 | } |
| 880 | |
Gabriel Devillers | 2718921 | 2016-07-04 17:17:39 +0200 | [diff] [blame] | 881 | // External wrapper used by gcc.js only |
| 882 | function refreshSlot(slot) { |
| 883 | onCompilerChange(slot); |
| 884 | } |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 885 | |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 886 | // Auxiliary function to slotUseDom |
| 887 | function generateChangeCallback(slot) { |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 888 | return function callback() { |
| 889 | onParamChange(slot); |
| 890 | } |
| 891 | } |
Gabriel Devillers | 2718921 | 2016-07-04 17:17:39 +0200 | [diff] [blame] | 892 | |
Gabriel Devillers | 0586ec4 | 2016-08-03 10:32:50 +0200 | [diff] [blame] | 893 | // must be called *before* doing slot = leaderSlot; |
Gabriel Devillers | 6d94cab | 2016-07-29 15:44:31 +0200 | [diff] [blame] | 894 | function setLeaderSlotIcon(slot) { |
| 895 | console.log("[UI] Toggling icon(s)..."); |
| 896 | |
| 897 | // source for javascript unicode escape codes: |
| 898 | // http://www.fileformat.info/info/unicode/char/search.htm |
| 899 | // to convert a character to JS/HTML/CSS/URIs... escape code: |
| 900 | // https://r12a.github.io/apps/conversion/ |
Gabriel Devillers | 0586ec4 | 2016-08-03 10:32:50 +0200 | [diff] [blame] | 901 | |
| 902 | // accessKey property : www.w3schools.com/jsref/prop_html_accesskey.asp |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 903 | |
Gabriel Devillers | 6d94cab | 2016-07-29 15:44:31 +0200 | [diff] [blame] | 904 | var selectedIcon = "\u2605"; // Unicode character: full star |
| 905 | var unselectedIcon = "\u2606"; // Unicode character: empty star |
| 906 | // you must keep those characters in sync with the template |
| 907 | // slotTemplate in index.html |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 908 | |
Gabriel Devillers | 0586ec4 | 2016-08-03 10:32:50 +0200 | [diff] [blame] | 909 | // if there was a leaderSlot, |
Gabriel Devillers | 6d94cab | 2016-07-29 15:44:31 +0200 | [diff] [blame] | 910 | if (leaderSlot != null) { |
Gabriel Devillers | 0586ec4 | 2016-08-03 10:32:50 +0200 | [diff] [blame] | 911 | // toggle icon in previous leaderSlot: |
Gabriel Devillers | f6a5dd1 | 2016-08-02 14:08:46 +0200 | [diff] [blame] | 912 | var prevIcon = leaderSlot.node.find('.leaderSlotIcon'); |
Gabriel Devillers | 6d94cab | 2016-07-29 15:44:31 +0200 | [diff] [blame] | 913 | prevIcon.text(unselectedIcon); |
| 914 | prevIcon.addClass("unselectedCharacterIcon"); |
| 915 | prevIcon.removeClass("selectedCharacterIcon"); |
Gabriel Devillers | 0586ec4 | 2016-08-03 10:32:50 +0200 | [diff] [blame] | 916 | |
| 917 | // removes access key in previous leaderSlot |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 918 | leaderSlot.node.find('.compiler-selection').prop("accessKey", ""); |
| 919 | leaderSlot.node.find('.compiler-options').prop("accessKey", ""); |
Gabriel Devillers | 6d94cab | 2016-07-29 15:44:31 +0200 | [diff] [blame] | 920 | } |
| 921 | |
Gabriel Devillers | 0586ec4 | 2016-08-03 10:32:50 +0200 | [diff] [blame] | 922 | // toggles the new icon |
Gabriel Devillers | f6a5dd1 | 2016-08-02 14:08:46 +0200 | [diff] [blame] | 923 | var newIcon = slot.node.find('.leaderSlotIcon'); |
Gabriel Devillers | 6d94cab | 2016-07-29 15:44:31 +0200 | [diff] [blame] | 924 | newIcon.text(selectedIcon); |
| 925 | newIcon.addClass("selectedCharacterIcon"); |
| 926 | newIcon.removeClass("unselectedCharacterIcon"); |
Gabriel Devillers | 0586ec4 | 2016-08-03 10:32:50 +0200 | [diff] [blame] | 927 | |
| 928 | // enables accesskeys in the leaderSlot |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 929 | slot.node.find('.compiler-selection').prop("accessKey", "c"); |
| 930 | slot.node.find('.compiler-options').prop("accessKey", "o"); |
Gabriel Devillers | 6d94cab | 2016-07-29 15:44:31 +0200 | [diff] [blame] | 931 | } |
| 932 | |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 933 | // Function to call each time a slot is added to the page. |
| 934 | // This function requires that the slot's DOM object already exists. |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 935 | function slotUseDom(slot) { |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 936 | slot.asmCodeMirror = CodeMirror.fromTextArea( |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 937 | slot.node.find(".asm textarea")[0], { |
| 938 | lineNumbers: true, |
| 939 | mode: "text/x-asm", |
| 940 | readOnly: true, |
| 941 | gutters: ['CodeMirror-linenumbers'], |
| 942 | lineWrapping: true |
| 943 | }); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 944 | // handle compiler option (slot specific) such as '-O1' |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 945 | slot.node.find('.compiler-options').change( |
| 946 | generateChangeCallback(slot)).keyup( |
| 947 | generateChangeCallback(slot)); |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 948 | |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 949 | slot.node.find('.leaderSlotIcon').on('click', function (e) { |
| 950 | console.log("[UI] Clicked on leaderSlotIcon in slot " + slot.id); |
Gabriel Devillers | 6d94cab | 2016-07-29 15:44:31 +0200 | [diff] [blame] | 951 | if (slot != leaderSlot) { |
| 952 | clearBackground(leaderSlot.asmCodeMirror); |
| 953 | |
| 954 | setLeaderSlotIcon(slot); |
| 955 | leaderSlot = slot; |
Gabriel Devillers | a5857b9 | 2016-08-02 13:24:37 +0200 | [diff] [blame] | 956 | setSetting('leaderSlot', slot.id); |
Gabriel Devillers | 6d94cab | 2016-07-29 15:44:31 +0200 | [diff] [blame] | 957 | onParamChange(slot, true); |
| 958 | } |
Gabriel Devillers | 68379dd | 2016-08-04 15:43:12 +0200 | [diff] [blame] | 959 | |
| 960 | // do not bring user to the top of index.html |
| 961 | e.preventDefault(); |
Gabriel Devillers | 6d94cab | 2016-07-29 15:44:31 +0200 | [diff] [blame] | 962 | }); |
| 963 | |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 964 | if (slots.length == 1) { |
| 965 | // "force" menu update if this is the first slot added |
| 966 | var leaderSlotMenuNode = domRoot.find('#commonParams .leaderSlot'); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 967 | leaderSlotMenuNode.text('leader slot : ' + slots[0].id); |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 968 | } |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 969 | } |
| 970 | |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 971 | function diffUseDom(diff) { |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 972 | diff.asmCodeMirror = CodeMirror.fromTextArea( |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 973 | diff.node.find('.diffText textarea')[0], { |
| 974 | lineNumbers: true, |
| 975 | mode: "text/x-asm", |
| 976 | readOnly: true, |
| 977 | gutters: ['CodeMirror-linenumbers'], |
| 978 | lineWrapping: true |
| 979 | }); |
Gabriel Devillers | f6a5dd1 | 2016-08-02 14:08:46 +0200 | [diff] [blame] | 980 | var reverseDiffButton = diff.node.find('.reverse-diff'); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 981 | reverseDiffButton.on('click', function (e) { |
| 982 | console.log("[UI] User clicked on reverse-diff button in diff " + diff.id); |
Gabriel Devillers | 43d3d99 | 2016-07-29 17:29:56 +0200 | [diff] [blame] | 983 | var tmp = diff.beforeSlot; |
| 984 | diff.beforeSlot = diff.afterSlot; |
| 985 | diff.afterSlot = tmp; |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 986 | setSlotInDiff(diff, "before", diff.beforeSlot); |
| 987 | setSlotInDiff(diff, "after", diff.afterSlot); |
Gabriel Devillers | 43d3d99 | 2016-07-29 17:29:56 +0200 | [diff] [blame] | 988 | setDiffSlotsMenus(diff); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 989 | onDiffChange(diff, false); |
Gabriel Devillers | 68379dd | 2016-08-04 15:43:12 +0200 | [diff] [blame] | 990 | |
| 991 | // do not bring user to the top of index.html |
| 992 | e.preventDefault(); |
Gabriel Devillers | 43d3d99 | 2016-07-29 17:29:56 +0200 | [diff] [blame] | 993 | }); |
Gabriel Devillers | 2718921 | 2016-07-04 17:17:39 +0200 | [diff] [blame] | 994 | } |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 995 | |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 996 | function getSlotsIds() { |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 997 | var ids = []; |
| 998 | for (var i = 0; i < slots.length; i++) { |
| 999 | ids.push(slots[i].id); |
| 1000 | } |
| 1001 | return ids; |
| 1002 | } |
| 1003 | |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1004 | // TODO : refactor ! |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 1005 | function getDiffsIds() { |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1006 | var ids = []; |
| 1007 | for (var i = 0; i < diffs.length; i++) { |
| 1008 | ids.push(diffs[i].id); |
| 1009 | } |
| 1010 | return ids; |
| 1011 | } |
| 1012 | |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1013 | function slotCtor(optionalId) { |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 1014 | var newSlot = new Slot(); |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1015 | if (optionalId) { |
| 1016 | newSlot.id = optionalId; |
| 1017 | } else { |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1018 | newSlot.id = getAvailableId(slots); |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1019 | } |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 1020 | slots.push(newSlot); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1021 | setSetting('slotIds', JSON.stringify(getSlotsIds())); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 1022 | return newSlot; |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1023 | } |
| 1024 | |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1025 | function diffCtor(optionalId) { |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1026 | var newDiff = new Diff(); |
| 1027 | if (optionalId) { |
| 1028 | newDiff.id = optionalId; |
| 1029 | } else { |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1030 | newDiff.id = getAvailableId(diffs); |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1031 | } |
| 1032 | diffs.push(newDiff); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1033 | setSetting('diffIds', JSON.stringify(getDiffsIds())); |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1034 | return newDiff; |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 1035 | } |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1036 | |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 1037 | // Array Remove - By John Resig (MIT Licensed) |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1038 | Array.prototype.remove = function (from, to) { |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 1039 | var rest = this.slice((to || from) + 1 || this.length); |
| 1040 | this.length = from < 0 ? this.length + from : from; |
| 1041 | return this.push.apply(this, rest); |
| 1042 | }; |
| 1043 | |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1044 | function removeIdInArray(id, array) { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1045 | for (var i = 0; i < array.length; i++) { |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 1046 | if (array[i].id == id) { |
| 1047 | array.remove(i); |
| 1048 | break; |
| 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1053 | function anotherSlot(slot) { |
Gabriel Devillers | 6d94cab | 2016-07-29 15:44:31 +0200 | [diff] [blame] | 1054 | for (var i = 0; i < slots.length; i++) { |
| 1055 | if (slots[i] != slot) { |
| 1056 | return slots[i]; |
| 1057 | } |
| 1058 | } |
| 1059 | return null; // should not happen (at least 1 slot is open) |
| 1060 | } |
| 1061 | |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1062 | function slotDtor(slot) { |
Gabriel Devillers | 6d94cab | 2016-07-29 15:44:31 +0200 | [diff] [blame] | 1063 | // if slot is the leader, find a new leader and change the icon |
Gabriel Devillers | 3247579 | 2016-08-04 09:52:50 +0200 | [diff] [blame] | 1064 | if (slots.length > 1) { |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 1065 | leaderSlot = anotherSlot(slot); |
Gabriel Devillers | 3247579 | 2016-08-04 09:52:50 +0200 | [diff] [blame] | 1066 | setLeaderSlotIcon(leaderSlot); |
| 1067 | setSetting('leaderSlot', leaderSlot.id); |
| 1068 | } |
Gabriel Devillers | 6d94cab | 2016-07-29 15:44:31 +0200 | [diff] [blame] | 1069 | |
Gabriel Devillers | 37076eb | 2016-08-04 14:13:05 +0200 | [diff] [blame] | 1070 | // hide the close-slot icon if there will remain 1 slot |
| 1071 | if (slots.length <= 2) { |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1072 | domRoot.find('.slot .closeButton').fadeOut(550); |
Gabriel Devillers | 37076eb | 2016-08-04 14:13:05 +0200 | [diff] [blame] | 1073 | } |
| 1074 | |
Gabriel Devillers | 6d94cab | 2016-07-29 15:44:31 +0200 | [diff] [blame] | 1075 | // now safely delete: |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1076 | removeSetting('compiler' + slot.id); |
| 1077 | removeSetting('compilerOptions' + slot.id); |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1078 | removeIdInArray(slot.id, slots); |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1079 | // after the deletion, update the browser's settings : |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1080 | setSetting('slotIds', JSON.stringify(getSlotsIds())); |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1081 | } |
| 1082 | |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1083 | function diffDtor(diff) { |
| 1084 | removeIdInArray(diff.id, diffs); |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1085 | // after the deletion, update the browser's settings : |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 1086 | setSetting('diffIds', JSON.stringify(getDiffsIds())); |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 1087 | } |
| 1088 | |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1089 | function getSlotById(slotId) { |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1090 | for (var i = 0; i < slots.length; i++) { |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 1091 | if (slots[i].id == slotId) return slots[i]; |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1092 | } |
| 1093 | return null; |
| 1094 | } |
| 1095 | |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1096 | function getDiffById(diffId) { |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1097 | for (var i = 0; i < diffs.length; i++) { |
| 1098 | if (diffs[i].id == diffId) return diffs[i]; |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 1099 | } |
| 1100 | return null; |
| 1101 | } |
| 1102 | |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1103 | function setPanelListSortable() { |
| 1104 | // source : JQuery UI examples |
| 1105 | var panelList = $('#draggablePanelList'); |
| 1106 | panelList.sortable({ |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1107 | handle: '.panel-heading', |
| 1108 | update: function () { |
| 1109 | $('.panel', panelList).each(function (index, elem) { |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1110 | var $listItem = $(elem), |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1111 | newIndex = $listItem.index(); |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1112 | |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1113 | // TODO Persist the new indices. |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1114 | }); |
| 1115 | } |
| 1116 | }); |
Matt Godbolt | 889cf27 | 2016-08-16 06:59:46 -0500 | [diff] [blame] | 1117 | $('.resizable:visible').resizable( |
| 1118 | { |
| 1119 | resize: resizeOutputs, |
| 1120 | minHeight: 300, |
| 1121 | minWidth: 300, |
| 1122 | alsoResize: ".resizable:visible" |
| 1123 | } |
| 1124 | ); |
| 1125 | $('.resizable-e:visible').resizable({handles: 'e'}); |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1126 | } |
| 1127 | |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1128 | function slotDomCtor(slot, onUserAction) { |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1129 | // source : http://stackoverflow.com/questions/10126395/how-to-jquery-clone-and-change-id |
Gabriel Devillers | f6a5dd1 | 2016-08-02 14:08:46 +0200 | [diff] [blame] | 1130 | var slotTemplate = domRoot.find('#slotTemplate'); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1131 | var clone = slotTemplate.clone().prop('id', 'slot' + slot.id); |
Gabriel Devillers | 1dfd409 | 2016-08-04 11:45:35 +0200 | [diff] [blame] | 1132 | |
| 1133 | // Insert the slot in the list of panels |
| 1134 | // domRoot.find('#slotTemplate').after(clone); |
| 1135 | domRoot.find('#draggablePanelList').append(clone); |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1136 | |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1137 | slot.node = domRoot.find('#slot' + slot.id); |
Gabriel Devillers | f6a5dd1 | 2016-08-02 14:08:46 +0200 | [diff] [blame] | 1138 | |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1139 | slot.node.find('.title').text("Slot " + slot.id + " (drag me) "); |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1140 | |
| 1141 | if (onUserAction) { |
| 1142 | slot.node.fadeIn(550); |
| 1143 | } else { |
| 1144 | // auto spawn of the panel: no need to fade in |
| 1145 | slot.node.show(); |
| 1146 | } |
| 1147 | |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1148 | slot.node.find('.closeButton').on('click', function (e) { |
| 1149 | console.log("[UI] User clicked on closeButton in slot " + slot.id); |
Gabriel Devillers | 37076eb | 2016-08-04 14:13:05 +0200 | [diff] [blame] | 1150 | var slotToDelete = getSlotById(slot.id); |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1151 | deleteAndUnplaceSlot(slotToDelete, true); |
Gabriel Devillers | 68379dd | 2016-08-04 15:43:12 +0200 | [diff] [blame] | 1152 | |
| 1153 | // do not bring user to the top of index.html |
| 1154 | e.preventDefault(); |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1155 | }); |
Gabriel Devillers | 37076eb | 2016-08-04 14:13:05 +0200 | [diff] [blame] | 1156 | |
| 1157 | // show the close-slot icons if there will be more than 1 slot |
| 1158 | if (slots.length == 1) { |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1159 | domRoot.find('.slot .closeButton').fadeOut(550); |
Gabriel Devillers | 37076eb | 2016-08-04 14:13:05 +0200 | [diff] [blame] | 1160 | } |
| 1161 | if (slots.length == 2) { |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1162 | domRoot.find('.slot .closeButton').fadeIn(550); |
Gabriel Devillers | 37076eb | 2016-08-04 14:13:05 +0200 | [diff] [blame] | 1163 | } |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1164 | |
| 1165 | setPanelListSortable(); |
| 1166 | } |
| 1167 | |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1168 | function diffDomCtor(diff, onUserAction) { |
Gabriel Devillers | f6a5dd1 | 2016-08-02 14:08:46 +0200 | [diff] [blame] | 1169 | var diffTemplate = domRoot.find('#diffTemplate'); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1170 | var clone = diffTemplate.clone().prop('id', 'diff' + diff.id); |
Gabriel Devillers | 1dfd409 | 2016-08-04 11:45:35 +0200 | [diff] [blame] | 1171 | |
| 1172 | // Insert the diff in the list of panels |
| 1173 | // domRoot.find('#diffTemplate').after(clone); |
| 1174 | domRoot.find('#draggablePanelList').append(clone); |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1175 | |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1176 | diff.node = domRoot.find('#diff' + diff.id); |
Gabriel Devillers | f6a5dd1 | 2016-08-02 14:08:46 +0200 | [diff] [blame] | 1177 | |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1178 | diff.node.find('.title').text("Diff " + diff.id + " (drag me) "); |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1179 | |
| 1180 | if (onUserAction) { |
| 1181 | diff.node.fadeIn(550); |
| 1182 | } else { |
| 1183 | // auto spawn of the panel: no need to fade in |
| 1184 | diff.node.show(); |
| 1185 | } |
| 1186 | |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1187 | diff.node.find('.closeButton').on('click', function (e) { |
| 1188 | console.log("[UI] User clicked on closeButton in diff " + diff.id); |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1189 | var diffToDelete = getDiffById(diff.id); |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1190 | deleteAndUnplaceDiff(diffToDelete, true); |
Gabriel Devillers | 68379dd | 2016-08-04 15:43:12 +0200 | [diff] [blame] | 1191 | // do not bring user to the top of index.html |
| 1192 | e.preventDefault(); |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1193 | }); |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1194 | |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1195 | setPanelListSortable(); |
| 1196 | setDiffSlotsMenus(diff); |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1197 | } |
| 1198 | |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1199 | function slotDomDtor(slot, onUserAction) { |
| 1200 | if (onUserAction) { |
| 1201 | slot.node.fadeOut(550, function () { |
| 1202 | slot.node.remove(); |
| 1203 | }); |
| 1204 | } else { |
| 1205 | slot.node.remove(); |
| 1206 | } |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1207 | } |
| 1208 | |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1209 | function diffDomDtor(diff, onUserAction) { |
| 1210 | if (onUserAction) { |
| 1211 | diff.node.fadeOut(550, function () { |
| 1212 | diff.node.remove(); |
| 1213 | }); |
| 1214 | } else { |
| 1215 | diff.node.remove(); |
| 1216 | } |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1217 | } |
| 1218 | |
Matt Godbolt | 889cf27 | 2016-08-16 06:59:46 -0500 | [diff] [blame] | 1219 | function ensureAllSlotsSameSize() { |
| 1220 | var allResizable = $('.resizable:visible'); |
| 1221 | var w = allResizable.width(); |
| 1222 | var h = allResizable.height(); |
| 1223 | allResizable.width(w); |
| 1224 | allResizable.height(h); |
| 1225 | } |
| 1226 | |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1227 | function createAndPlaceSlot(compilers, defaultCompiler, optionalId, onUserAction) { |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1228 | var newSlot = slotCtor(optionalId); |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1229 | slotDomCtor(newSlot, onUserAction); |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1230 | slotUseDom(newSlot); |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1231 | setCompilersInSlot(compilers, defaultCompiler, newSlot); |
Matt Godbolt | 889cf27 | 2016-08-16 06:59:46 -0500 | [diff] [blame] | 1232 | ensureAllSlotsSameSize(); |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1233 | return newSlot; |
| 1234 | } |
| 1235 | |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1236 | function createAndPlaceDiff(optionalId, onUserAction) { |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1237 | var newDiff = diffCtor(optionalId); |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1238 | diffDomCtor(newDiff, onUserAction); |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1239 | diffUseDom(newDiff); |
Matt Godbolt | 889cf27 | 2016-08-16 06:59:46 -0500 | [diff] [blame] | 1240 | ensureAllSlotsSameSize(); |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1241 | return newDiff; |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1242 | } |
| 1243 | |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1244 | function createAndPlaceDiffUI(optionalId, onUserAction) { |
| 1245 | var newDiff = createAndPlaceDiff(optionalId, onUserAction); |
Gabriel Devillers | 48c6e8d | 2016-07-25 12:29:13 +0200 | [diff] [blame] | 1246 | if (slots.length > 0) { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1247 | setSlotInDiff(newDiff, "before", slots[0]); |
Gabriel Devillers | 48c6e8d | 2016-07-25 12:29:13 +0200 | [diff] [blame] | 1248 | if (slots.length > 1) { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1249 | setSlotInDiff(newDiff, "after", slots[1]); |
Gabriel Devillers | 48c6e8d | 2016-07-25 12:29:13 +0200 | [diff] [blame] | 1250 | } else { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1251 | setSlotInDiff(newDiff, "after", slots[0]); |
Gabriel Devillers | 48c6e8d | 2016-07-25 12:29:13 +0200 | [diff] [blame] | 1252 | } |
Gabriel Devillers | 7a8fec3 | 2016-07-28 10:32:29 +0200 | [diff] [blame] | 1253 | onDiffChange(newDiff, false); |
Gabriel Devillers | 48c6e8d | 2016-07-25 12:29:13 +0200 | [diff] [blame] | 1254 | } |
| 1255 | return newDiff; |
| 1256 | } |
| 1257 | |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1258 | function deleteAndUnplaceSlot(slot, onUserAction) { |
| 1259 | slotDomDtor(slot, onUserAction); |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1260 | slotDtor(slot); |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1261 | } |
| 1262 | |
Gabriel Devillers | 17e3609 | 2016-08-04 17:06:44 +0200 | [diff] [blame] | 1263 | function deleteAndUnplaceDiff(diff, onUserAction) { |
| 1264 | diffDomDtor(diff, onUserAction); |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1265 | diffDtor(diff); |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1266 | } |
| 1267 | |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1268 | // refresh (or place) the two drop-down lists of the diff panel containging |
| 1269 | // descriptions of the slots that can be used to make a diff |
| 1270 | |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 1271 | function updateDiffButton(diff, className) { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1272 | var slot = diff[className + 'Slot']; |
| 1273 | var diffSlotMenuNode = diff.node.find('.' + className + ' .slotName'); |
Gabriel Devillers | 68379dd | 2016-08-04 15:43:12 +0200 | [diff] [blame] | 1274 | diffSlotMenuNode.text(slot.shortDescription()); |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | // this function is called if the name or option of a compiler is changed. |
| 1278 | function updateDiffUI(diff) { |
| 1279 | updateDiffButton(diff, "before"); |
| 1280 | updateDiffButton(diff, "after"); |
| 1281 | setDiffSlotsMenus(diff); |
| 1282 | } |
| 1283 | |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1284 | // Auxilary to setDiffSlotsMenus and deserialisation |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 1285 | function setSlotInDiff(diff, className, slot) { |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1286 | // className can be "before" or "after" |
Gabriel Devillers | 48c6e8d | 2016-07-25 12:29:13 +0200 | [diff] [blame] | 1287 | if (slot == null) { |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 1288 | console.log("[DEBUG] setSlotInDiff: diff.id = " + |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1289 | diff.id + ", " + className + " -> " + "null.id"); |
Gabriel Devillers | 48c6e8d | 2016-07-25 12:29:13 +0200 | [diff] [blame] | 1290 | } else { |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 1291 | console.log("[DEBUG] setSlotInDiff: diff.id = " + |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1292 | diff.id + ", " + className + " -> " + slot.id); |
Gabriel Devillers | 48c6e8d | 2016-07-25 12:29:13 +0200 | [diff] [blame] | 1293 | } |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1294 | diff[className + 'Slot'] = slot; |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1295 | if (slot != null) { |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 1296 | updateDiffButton(diff, className); |
| 1297 | |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1298 | setSetting('diff' + diff.id + className, slot.id); |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 1299 | |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1300 | addToPendings(slot, diff); |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1301 | } |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1302 | } |
| 1303 | |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1304 | function setDiffSlotsMenus(diff) { |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1305 | // className can be "before" or "after" |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1306 | function setSlotMenu(className) { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1307 | console.log("[DEBUG] : setSlotMenu with " + className + |
| 1308 | " and diff.id = " + diff.id); |
| 1309 | diff.node.find('.' + className + ' li').remove(); |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1310 | for (var i = 0; i < slots.length; i++) { |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 1311 | var elem = $('<li><a href="#">' + slots[i].description() + '</a></li>'); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1312 | diff.node.find('.' + className + ' .slotNameList').append(elem); |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1313 | (function (i) { |
Gabriel Devillers | 68379dd | 2016-08-04 15:43:12 +0200 | [diff] [blame] | 1314 | elem.click(function (e) { |
Gabriel Devillers | f6a5dd1 | 2016-08-02 14:08:46 +0200 | [diff] [blame] | 1315 | // TODO: check if modifying diff with [] will survive to |
Gabriel Devillers | 711af7a | 2016-08-01 14:47:12 +0200 | [diff] [blame] | 1316 | // minifying http://stackoverflow.com/questions/4244896/ |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1317 | console.log("[UI] user set " + slots[i].id + " as " + className + |
| 1318 | " slot in diff with id " + diff.id); |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 1319 | setSlotInDiff(diff, className, slots[i]); |
Gabriel Devillers | 7a8fec3 | 2016-07-28 10:32:29 +0200 | [diff] [blame] | 1320 | onDiffChange(diff, false); |
Gabriel Devillers | 68379dd | 2016-08-04 15:43:12 +0200 | [diff] [blame] | 1321 | |
| 1322 | // do not bring user to the top of index.html |
| 1323 | e.preventDefault(); |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1324 | }); |
| 1325 | })(i); |
| 1326 | } |
| 1327 | } |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1328 | |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1329 | setSlotMenu("before"); |
| 1330 | setSlotMenu("after"); |
| 1331 | } |
| 1332 | |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1333 | // refresh (or place) all of the drop-down lists containging descriptions of |
| 1334 | // the slots that can be used to make a diff |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1335 | function setAllDiffSlotsMenus() { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1336 | console.log("[DEBUG] : inside setAllDiffSlotsMenus, diffs.length = " + diffs.length); |
| 1337 | for (var i = 0; i < diffs.length; i++) { |
Gabriel Devillers | a49e0e9 | 2016-07-18 14:36:57 +0200 | [diff] [blame] | 1338 | setDiffSlotsMenus(diffs[i]); |
| 1339 | } |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1340 | } |
| 1341 | |
Gabriel Devillers | 39966c7 | 2016-08-01 17:29:11 +0200 | [diff] [blame] | 1342 | // on startup, for each slot, |
| 1343 | // if a setting is defined, set it on static/index.html page |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1344 | var slotIds = getSetting('slotIds'); |
Gabriel Devillers | 7a8fec3 | 2016-07-28 10:32:29 +0200 | [diff] [blame] | 1345 | if (slotIds != null) { |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1346 | slotIds = JSON.parse(slotIds); |
Gabriel Devillers | 7a8fec3 | 2016-07-28 10:32:29 +0200 | [diff] [blame] | 1347 | } else { |
| 1348 | slotIds = []; |
| 1349 | } |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 1350 | var i, newSlot; |
Gabriel Devillers | 7a8fec3 | 2016-07-28 10:32:29 +0200 | [diff] [blame] | 1351 | if (slotIds.length > 0) { |
| 1352 | console.log("[STARTUP] found slot data : restoring from previous session"); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1353 | console.log("[DEBUG] slotIds: " + JSON.stringify(slotIds)); |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 1354 | for (i = 0; i < slotIds.length; i++) { |
| 1355 | newSlot = slotCtor(slotIds[i]); |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1356 | slotDomCtor(newSlot); |
| 1357 | slotUseDom(newSlot); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1358 | if (getSetting('compilerOptions' + slotIds[i]) === undefined) { |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 1359 | console.log("[STARTUP] There was a problem while restoring slots from previous session."); |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1360 | } else { |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1361 | newSlot.node.find('.compiler-options').val(getSetting('compilerOptions' + newSlot.id)); |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1362 | } |
| 1363 | } |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1364 | var leaderSlotSetting = getSetting('leaderSlot'); |
| 1365 | if (!leaderSlotSetting) { |
| 1366 | console.log("Leader slot was missing; picking first"); |
| 1367 | leaderSlot = getSlotById(0); |
| 1368 | } else { |
| 1369 | leaderSlot = getSlotById(leaderSlotSetting); |
| 1370 | } |
| 1371 | if (leaderSlot) setLeaderSlotIcon(leaderSlot); |
Gabriel Devillers | 2e057e5 | 2016-07-29 11:39:05 +0200 | [diff] [blame] | 1372 | } else { |
Gabriel Devillers | 2e057e5 | 2016-07-29 11:39:05 +0200 | [diff] [blame] | 1373 | // not slot data found. It is probably the first time the user come to |
| 1374 | // visit (or to debug a wipeSetting(); was done in the browser console) |
| 1375 | // therefore it seems logical to open at least 1 slot |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1376 | leaderSlot = createAndPlaceSlot(compilers, defaultCompiler); |
Gabriel Devillers | a5857b9 | 2016-08-02 13:24:37 +0200 | [diff] [blame] | 1377 | setSetting('leaderSlot', leaderSlot.id); |
Gabriel Devillers | 6d94cab | 2016-07-29 15:44:31 +0200 | [diff] [blame] | 1378 | setLeaderSlotIcon(leaderSlot); |
Gabriel Devillers | 27463ac | 2016-07-01 17:09:18 +0200 | [diff] [blame] | 1379 | } |
Gabriel Devillers | 2e057e5 | 2016-07-29 11:39:05 +0200 | [diff] [blame] | 1380 | |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 1381 | // This part of the initialisation must (currently) occur |
| 1382 | // after the creation of slots, and before the creation of diffs. |
| 1383 | // Previously it was located in gcc.js, in Initialize() |
| 1384 | setCompilers(compilers, defaultCompiler); |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1385 | |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1386 | // on startup, for each diff, |
| 1387 | // if a setting is defined, set it on static/index.html page |
| 1388 | var diffIds = getSetting('diffIds'); |
Gabriel Devillers | 7a8fec3 | 2016-07-28 10:32:29 +0200 | [diff] [blame] | 1389 | if (diffIds != null) { |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1390 | diffIds = JSON.parse(diffIds); |
Gabriel Devillers | 7a8fec3 | 2016-07-28 10:32:29 +0200 | [diff] [blame] | 1391 | } else { |
| 1392 | diffIds = []; |
| 1393 | } |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 1394 | console.log("[DEBUG] diffIds: " + JSON.stringify(diffIds)); |
Gabriel Devillers | 7a8fec3 | 2016-07-28 10:32:29 +0200 | [diff] [blame] | 1395 | if (diffIds.length > 0) { |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 1396 | console.log("[STARTUP] found diff data : restoring diffs from previous session"); |
Matt Godbolt | 51ae548 | 2016-08-06 14:46:47 -0500 | [diff] [blame] | 1397 | for (i = 0; i < diffIds.length; i++) { |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1398 | var newDiff = createAndPlaceDiff(diffIds[i]); |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1399 | |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1400 | newDiff.beforeSlot = getSlotById(getSetting('diff' + newDiff.id + "before")); |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 1401 | setSlotInDiff(newDiff, "before", newDiff.beforeSlot); |
| 1402 | addToPendings(newDiff.beforeSlot, newDiff); |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1403 | |
Matt Godbolt | 32e742e | 2016-08-06 15:20:03 -0500 | [diff] [blame] | 1404 | newDiff.afterSlot = getSlotById(getSetting('diff' + newDiff.id + "after")); |
Gabriel Devillers | ece6789 | 2016-08-03 14:49:14 +0200 | [diff] [blame] | 1405 | setSlotInDiff(newDiff, "after", newDiff.afterSlot); |
| 1406 | addToPendings(newDiff.afterSlot, newDiff); |
Gabriel Devillers | 48c6e8d | 2016-07-25 12:29:13 +0200 | [diff] [blame] | 1407 | //onDiffChange(newDiff); |
Gabriel Devillers | 60c04b1 | 2016-07-25 11:25:06 +0200 | [diff] [blame] | 1408 | } |
| 1409 | } |
Gabriel Devillers | 2718921 | 2016-07-04 17:17:39 +0200 | [diff] [blame] | 1410 | |
Matt Godbolt | 889cf27 | 2016-08-16 06:59:46 -0500 | [diff] [blame] | 1411 | function resizeOutputs() { |
| 1412 | function doResize(cm) { |
| 1413 | var parent = $(cm.getTextArea()).parent().find(".CodeMirror"); |
| 1414 | var container = parent.closest(".panel"); |
| 1415 | var containerSize = container.height(); |
| 1416 | var top = parent.position().top; |
Matt Godbolt | d93da76 | 2016-08-17 20:46:56 -0500 | [diff] [blame] | 1417 | var footerHeight = container.find('.panel-footer').height() || 0; |
| 1418 | // TODO - padding! |
| 1419 | cm.setSize(null, containerSize - top - footerHeight); |
Matt Godbolt | 889cf27 | 2016-08-16 06:59:46 -0500 | [diff] [blame] | 1420 | } |
| 1421 | _.each(slots, function(slot) { doResize(slot.asmCodeMirror); }); |
| 1422 | _.each(diffs, function(slot) { doResize(slot.asmCodeMirror); }); |
| 1423 | } |
| 1424 | |
| 1425 | function resize(windowHeight) { |
| 1426 | var editor = $(cppEditor.getTextArea()).parent(); |
| 1427 | var top = editor.offset().top; |
Matt Godbolt | d93da76 | 2016-08-17 20:46:56 -0500 | [diff] [blame] | 1428 | // TODO: a total rethink here. kill output/result etc |
| 1429 | // make errors a pop-up-able thing? |
| 1430 | // var compOutputSize = Math.max(100, windowHeight * 0.05); |
| 1431 | // $('.output').height(compOutputSize); |
| 1432 | // var resultHeight = $('.result').height(); |
| 1433 | var height = windowHeight - top;// - resultHeight - 10; |
Matt Godbolt | 889cf27 | 2016-08-16 06:59:46 -0500 | [diff] [blame] | 1434 | cppEditor.setSize(null, height); |
| 1435 | resizeOutputs(); |
| 1436 | } |
| 1437 | |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 1438 | return { |
Matt Godbolt | 35156ff | 2012-07-09 06:56:11 -0500 | [diff] [blame] | 1439 | serialiseState: serialiseState, |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 1440 | deserialiseState: deserialiseState, |
Matt Godbolt | 186cabb | 2012-06-20 15:05:59 -0500 | [diff] [blame] | 1441 | getSource: getSource, |
Matt Godbolt | 7700691 | 2012-06-28 06:12:41 -0500 | [diff] [blame] | 1442 | setSource: setSource, |
Matt Godbolt | 9b87317 | 2013-02-07 07:30:10 -0600 | [diff] [blame] | 1443 | setFilters: setFilters, |
Gabriel Devillers | 6d7e68c | 2016-08-02 17:34:39 +0200 | [diff] [blame] | 1444 | createAndPlaceSlot: createAndPlaceSlot, |
| 1445 | createAndPlaceDiffUI: createAndPlaceDiffUI, |
Matt Godbolt | 889cf27 | 2016-08-16 06:59:46 -0500 | [diff] [blame] | 1446 | refreshSlot: refreshSlot, |
| 1447 | resize: resize |
Matt Godbolt | 5be6268 | 2012-06-20 14:46:45 -0500 | [diff] [blame] | 1448 | }; |
| 1449 | } |