Move duplicated JavaScript and CSS for diff to common files

This commit is contained in:
Naoki Takezoe
2014-04-30 10:55:42 +09:00
parent c136823170
commit 3ee4143235
4 changed files with 52 additions and 99 deletions

View File

@@ -66,4 +66,40 @@ function displayErrors(data){
window.clearInterval(timer);
});
};
})(jQuery);
})(jQuery);
function diffUsingJS(oldTextId, newTextId, outputId) {
// get the baseText and newText values from the two textboxes, and split them into lines
var oldText = document.getElementById(oldTextId).value;
if(oldText == ''){
var oldLines = [];
} else {
var oldLines = difflib.stringAsLines(oldText);
}
var newText = document.getElementById(newTextId).value
if(newText == ''){
var newLines = [];
} else {
var newLines = difflib.stringAsLines(newText);
}
// create a SequenceMatcher instance that diffs the two sets of lines
var sm = new difflib.SequenceMatcher(oldLines, newLines);
// get the opcodes from the SequenceMatcher instance
// opcodes is a list of 3-tuples describing what changes should be made to the base text
// in order to yield the new text
var opcodes = sm.get_opcodes();
var diffoutputdiv = document.getElementById(outputId);
while (diffoutputdiv.firstChild) diffoutputdiv.removeChild(diffoutputdiv.firstChild);
// build the diff view and add it to the current DOM
diffoutputdiv.appendChild(diffview.buildView({
baseTextLines: oldLines,
newTextLines: newLines,
opcodes: opcodes,
contextSize: 4,
viewType: 1
}));
}