|
| 1 | +/* ***** BEGIN LICENSE BLOCK ***** |
| 2 | + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 3 | + * |
| 4 | + * The contents of this file are subject to the Mozilla Public License |
| 5 | + * Version 1.1 (the "License"); you may not use this file except in |
| 6 | + * compliance with the License. You may obtain a copy of the License at |
| 7 | + * http://www.mozilla.org/MPL/ |
| 8 | + * |
| 9 | + * Software distributed under the License is distributed on an "AS IS" |
| 10 | + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
| 11 | + * License for the specific language governing rights and limitations |
| 12 | + * under the License. |
| 13 | + * |
| 14 | + * The Original Code is "side by side diff" code. |
| 15 | + * |
| 16 | + * The Initial Developer of the Original Code is ActiveState Software Inc. |
| 17 | + * Portions created by ActiveState Software Inc are Copyright (C) 2008-2009 |
| 18 | + * ActiveState Software Inc. All Rights Reserved. |
| 19 | + * |
| 20 | + * Contributor(s): |
| 21 | + * Todd Whiteman @ ActiveState Software Inc |
| 22 | + * |
| 23 | + * Alternatively, the contents of this file may be used under the terms of |
| 24 | + * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 25 | + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 26 | + * in which case the provisions of the GPL or the LGPL are applicable instead |
| 27 | + * of those above. If you wish to allow use of your version of this file only |
| 28 | + * under the terms of either the GPL or the LGPL, and not to allow others to |
| 29 | + * use your version of this file under the terms of the MPL, indicate your |
| 30 | + * decision by deleting the provisions above and replace them with the notice |
| 31 | + * and other provisions required by the GPL or the LGPL. If you do not delete |
| 32 | + * the provisions above, a recipient may use your version of this file under |
| 33 | + * the terms of any one of the MPL, the GPL or the LGPL. |
| 34 | + * |
| 35 | + * ***** END LICENSE BLOCK ***** */ |
| 36 | + |
| 37 | +// Globals |
| 38 | + |
| 39 | +var g_diff_result = null; |
| 40 | +var g_diff_cwd = null; |
| 41 | +var g_sbsDiff = null; |
| 42 | + |
| 43 | + |
| 44 | +// Overriding functionality - overrides the diff.js loadDiffResult function. |
| 45 | + |
| 46 | +var _original_loadDiffResult = loadDiffResult; |
| 47 | +var loadDiffResult = function overwritten_loadDiffResult(result, cwd) { |
| 48 | + g_diff_result = result; |
| 49 | + g_diff_cwd = cwd; |
| 50 | + _original_loadDiffResult(result, cwd); |
| 51 | + loadSBSDiff(); |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +// Side-by-side diff implementation. |
| 56 | + |
| 57 | +function loadSBSDiff() { |
| 58 | + var koIDiff = Components.classes["@activestate.com/koDiff;1"]. |
| 59 | + createInstance(Components.interfaces.koIDiff); |
| 60 | + koIDiff.initWithDiffContent(g_diff_result); |
| 61 | + g_sbsDiff = Components.classes["@activestate.com/sbsDiff;1"]. |
| 62 | + createInstance(Components.interfaces.sbsIDiff) |
| 63 | + g_sbsDiff.enable_syntax_highlighting = document.getElementById('enable_highlighting_checkbox').checked; |
| 64 | + g_sbsDiff.cwd = g_diff_cwd; |
| 65 | + if (!g_diff_cwd) { |
| 66 | + // Cannot show full context diffs. |
| 67 | + document.getElementById('enable_highlighting_checkbox').setAttribute('disabled', 'true'); |
| 68 | + } |
| 69 | + var html = g_sbsDiff.generateSbsDiff(koIDiff); |
| 70 | + |
| 71 | + // Get the extension's on-disk location. |
| 72 | + |
| 73 | + var em = Components.classes["@mozilla.org/extensions/manager;1"]. |
| 74 | + getService(Components.interfaces.nsIExtensionManager); |
| 75 | + var aFile = em.getInstallLocation(MY_ID).getItemFile(MY_ID, "content"); |
| 76 | + aFile.append("diff.html"); |
| 77 | + if (aFile.exists()) |
| 78 | + aFile.remove(false); |
| 79 | + aFile.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0660); |
| 80 | + var stream = Components.classes["@mozilla.org/network/safe-file-output-stream;1"] |
| 81 | + .createInstance(Components.interfaces.nsIFileOutputStream); |
| 82 | + stream.init(aFile, 0x04 | 0x08 | 0x20, 0600, 0); // write, create, truncate |
| 83 | + stream.write(html, html.length); |
| 84 | + if (stream instanceof Components.interfaces.nsISafeOutputStream) { |
| 85 | + stream.finish(); |
| 86 | + } else { |
| 87 | + stream.close(); |
| 88 | + } |
| 89 | + |
| 90 | + // do whatever you need to the created file |
| 91 | + //dump("file.path: " + ko.uriparse.localPathToURI(aFile.path) + "\n"); |
| 92 | + var filepath = "chrome://sbsdiff/content/diff.html"; |
| 93 | + // Set src to "", in order to clear any existing path (to reload itself). |
| 94 | + document.getElementById("sbs_diff_browser").setAttribute("src", ""); |
| 95 | + document.getElementById("sbs_diff_browser").setAttribute("src", filepath); |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +function changeDiffStyle(style) { |
| 100 | + var deck = document.getElementById('deck'); |
| 101 | + if (style == 'contextual') { |
| 102 | + deck.selectedIndex = 0; |
| 103 | + } else if (style == 'side-by-side') { |
| 104 | + deck.selectedIndex = 1; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +function reloadDiffResult() { |
| 109 | + loadSBSDiff(); |
| 110 | +} |
| 111 | + |
| 112 | +function diffViewer_revealInEditor(menuitem, event) { |
| 113 | + //ko.logging.dumpEvent(event); |
| 114 | + //dump("\n\n"); |
| 115 | + //ko.logging.dumpObject(document.popupNode); |
| 116 | + var name; |
| 117 | + var chunk_id; |
| 118 | + var lineno = 0; |
| 119 | + var node = document.popupNode; |
| 120 | + while (node) { |
| 121 | + if (node.nodeName.toLowerCase() == "tbody") { |
| 122 | + chunk_id = node.getAttribute("id"); |
| 123 | + break; |
| 124 | + } else if ((lineno == 0) && (node.nodeName.toLowerCase() == "tr")) { |
| 125 | + for (var i=node.childNodes.length-1; i >=0; i--) { |
| 126 | + var child = node.childNodes[i]; |
| 127 | + if (child.nodeName.toLowerCase() == "th") { |
| 128 | + lineno = parseInt(child.textContent); |
| 129 | + if (lineno > 0) |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + node = node.parentNode; |
| 135 | + } |
| 136 | + if (!chunk_id || (chunk_id.substr(0, 6) != "chunk.")) { |
| 137 | + alert("Unable to reveal the position at this location"); |
| 138 | + return; |
| 139 | + } |
| 140 | + // We now know which file and which chunk. If we have a full-contextual |
| 141 | + // diff then we may know the line number as well. |
| 142 | + var filepath = g_sbsDiff.filepathFromChunkId(chunk_id); |
| 143 | + var uri = ko.uriparse.pathToURI(filepath); |
| 144 | + if (lineno <= 0 || !g_diff_cwd) { |
| 145 | + lineno = g_sbsDiff.diffLinenoFromChunkId(chunk_id); |
| 146 | + } |
| 147 | + var kowin = ko.windowManager.getMainWindow(); |
| 148 | + kowin.ko.views.manager.doFileOpenAtLineAsync(uri, lineno, null, |
| 149 | + null, -1, |
| 150 | + function(v) { |
| 151 | + if (v) { |
| 152 | + kowin.focus(); |
| 153 | + } |
| 154 | + }); |
| 155 | +} |
| 156 | + |
| 157 | +function sbsEnableUI() { |
| 158 | + var deck = document.getElementById('deck'); |
| 159 | + if (deck.selectedIndex == 0) { |
| 160 | + document.getElementById('enable_highlighting_checkbox').setAttribute('disabled', true); |
| 161 | + } else if (g_diff_cwd) { |
| 162 | + document.getElementById('enable_highlighting_checkbox').removeAttribute('disabled'); |
| 163 | + } |
| 164 | + |
| 165 | +} |
| 166 | + |
| 167 | +function sbsOnload() { |
| 168 | + var prefs = Components.classes["@mozilla.org/preferences-service;1"] |
| 169 | + .getService(Components.interfaces.nsIPrefService); |
| 170 | + prefs = prefs.getBranch("extensions.sbsdiff."); |
| 171 | + var deck = document.getElementById('deck'); |
| 172 | + deck.selectedIndex = prefs.getIntPref("deck.selectedIndex"); |
| 173 | + document.getElementById("diff_style_menulist").selectedIndex = deck.selectedIndex; |
| 174 | + sbsEnableUI(); |
| 175 | +} |
| 176 | + |
| 177 | +function sbsOnunload() { |
| 178 | + var prefs = Components.classes["@mozilla.org/preferences-service;1"] |
| 179 | + .getService(Components.interfaces.nsIPrefService); |
| 180 | + prefs = prefs.getBranch("extensions.sbsdiff."); |
| 181 | + var deck = document.getElementById('deck'); |
| 182 | + prefs.setIntPref("deck.selectedIndex", deck.selectedIndex); |
| 183 | +} |
| 184 | + |
| 185 | +window.addEventListener("load", sbsOnload, false); |
| 186 | +window.addEventListener("unload", sbsOnunload, false); |
0 commit comments