/** * Copyright (c) 2012 The Chromium Authors. All rights reserved. * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. **/ var dmp = new diff_match_patch(); dmp.Diff_Timeout = 1; var clicked; var selectedChunk = 0; var totalChunks; var totalCollapsible = 0; var numCollapsed; $(document).ready(function() { var buttons = [ '#new-diff', '#save', '#edit', '#expand-all', '#collapse-all', '#next-chunk', '#prev-chunk' ]; for (var i = 0; i < buttons.length; i++) { $(buttons[i]).hover( function() { var button = $(this).attr('id'); $('.tooltip.' + button).removeClass('hidden'); }, function() { var button = $(this).attr('id'); $('.tooltip.' + button).addClass('hidden'); } ); } // Check for connection every 5 seconds setInterval(function() { if (navigator.onLine) { $('.offline').addClass('hidden'); $('.url').removeClass('hidden'); } else { $('.offline').removeClass('hidden'); $('.url').addClass('hidden'); } }, 500); $('.file-diff > div').click(function() { console.log(this); selectChunk(this); }); for (i = 0; i < 2; i++) { $('.file-diff.' + i).scroll(function() { var i = $(this).attr('class').split(' ')[1]; $('.file-diff.' + ((parseInt(i) + 1) % 2)).scrollTop( $('.file-diff.' + i).scrollTop()); $('#arrow-container').scrollTop($('.file-diff.' + i).scrollTop()); $('#check-container').scrollTop($('.file-diff.' + i).scrollTop()); }); } $('#arrow-container').scroll(function() { $('.file-diff').scrollTop($('#arrow-container').scrollTop()); $('#check-container').scrollTop($('#arrow-container').scrollTop()); }); $('#check-container').scroll(function() { $('.file-diff').scrollTop($('#check-container').scrollTop()); $('#arrow-container').scrollTop($('#check-container').scrollTop()); }); $('#collapse-all').click(function () { collapseAllMatches(); }); $('#expand-all').click(function () { expandAllMatches(); }); $('#prev-chunk').click(function() { selectPrevChunk(); }); $('#next-chunk').click(function() { selectNextChunk(); }); $(document).keyup(function(event) { var target = event.target || event.srcElement; var targetName = target.tagName.toLowerCase(); if (!disableShortcuts()) { keyboardShortcut(event); } }); }); function keyboardShortcut(event) { if (event.which == 74) selectNextChunk(); else if (event.which == 75) selectPrevChunk(); else if ((event.which == 76) && (selectedChunk > 0) && (selectedChunk <= totalChunks)) moveChunk('chunk-' + selectedChunk); else if ((event.which == 82) && (selectedChunk > 0) && (selectedChunk <= totalChunks)) checkRight('chunk-' + selectedChunk); else if (event.which == 69) expandAllMatches(); else if (event.which == 67) collapseAllMatches(); } function disableShortcuts () { return ( !$('#modal-shield').hasClass('hidden') || $('.button.edit').hasClass('hidden') ) } function getText(fileNum) { var lines = $('.file-diff.' + fileNum + ' div').children('.text'); var text = ''; for (var i = 0; i < lines.length; i++) { if (!isBlankLine($(lines[i]).parent(), null) && !$(lines[i]).hasClass('hidden')) { var line = $(lines[i]).children(); for (var j = 0; j < line.length; j++) { if ($(lines[i]).hasClass('merged')) { if (fileNum == 1 && !$(line[j]).hasClass('ins')) text += $(line[j]).html(); if (fileNum == 0 && !$(line[j]).hasClass('del')) text += $(line[j]).html(); } else { if (fileNum == 0 && !$(line[j]).hasClass('ins')) text += $(line[j]).html(); if (fileNum == 1 && !$(line[j]).hasClass('del')) text += $(line[j]).html(); } } text += '\n'; } } text = escapeHtml(text); return text; } function escapeHtml(text) { var text = text.replace(/&/g, '&') .replace(/</g, '<') .replace(/>/g, '>') .replace(/ /g, ' '); return text; } function ajaxErrorMessage(xhr) { message = 'Please enter a valid URL'; switch (xhr.status) { case 401: message = 'You do not have permission to view that URL'; break; case 403: message = 'You do not have permission to view that URL'; break; case 404: message = 'The URL you entered could not be found'; break; case 500: message = 'There was a server error'; break; } return message; } function getChunk(line) { return $(line).attr('class').replace('del ', '') .replace('ins ', '') .replace('blank ', '') .replace('first ', '') .replace('chunk ', '') .replace('fix ', '') .replace('correct ', '') .split(' ')[1]; } function getRealLine(line) { var classes = $(line).attr('class'); classes = classes.replace('del ', '') .replace('ins ', '') .replace('blank ', '') .replace('first ', '') .replace('fix ', '') .replace('correct ', '') .replace('chunk ', ''); var realLine = classes.split(' ')[0]; return realLine; } function computeDiff(file1, file2) { var d = dmp.diff_main(file1, file2); dmp.diff_cleanupSemantic(d); var ds = createHtmlLines(d); if (texts[0] != '') $('.file-diff.0').html(ds[0]); if (texts[1] != '') $('.file-diff.1').html(ds[1]); setLineTypes(); setLineNums(); if (texts[0] != '' && texts[1] != '') { setNumDiffs(true); setArrowsAndChecks(); createCollapsibleMatches(); $('.button.save').removeClass('hidden'); $('.button.edit').removeClass('hidden'); $('#collapse-all').removeClass('hidden'); $('#expand-all').removeClass('hidden'); } } function patchToFile2(file1, patchText) { var patches = dmp.patch_fromText(patchText); var patchData = dmp.patch_apply(patches, file1); return patchData[0]; } function createHtmlLines(diffs) { var pattern_amp = /&/g; var pattern_lt = //g; var pattern_para = /\n/g; var pattern_cr = /\r/g; var pattern_space = / /g; var pattern_para_sign = /