2016-01-15 21:21:22 +01:00
|
|
|
function toggleHelp() {
|
|
|
|
$('#help').toggleClass('hidden');
|
|
|
|
}
|
|
|
|
|
2015-10-20 20:16:00 +02:00
|
|
|
function searchHandle(tmid) {
|
2015-08-26 20:13:57 +02:00
|
|
|
var concordiaRequest = {
|
|
|
|
operation: 'concordiaSearch',
|
2015-10-20 20:16:00 +02:00
|
|
|
tmId: tmid,
|
2016-01-21 13:12:46 +01:00
|
|
|
pattern:$("#search-input").val()
|
2015-08-26 20:13:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$.ajax({
|
2015-12-13 20:53:46 +01:00
|
|
|
url: '/concordia_gate.php',
|
2015-08-26 20:13:57 +02:00
|
|
|
type: 'post',
|
|
|
|
dataType: 'json',
|
|
|
|
success: function (data) {
|
|
|
|
$('#result').html(renderResult(data));
|
|
|
|
},
|
2015-12-13 20:53:46 +01:00
|
|
|
data: concordiaRequest
|
2015-08-26 20:13:57 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderResult(data) {
|
|
|
|
var res = '';
|
|
|
|
|
|
|
|
var score = data['result']['bestOverlayScore']*100;
|
|
|
|
|
|
|
|
res += '<div id="result-score">Concordia score: <b>'+score.toFixed(0)+'%</b></div>';
|
2016-01-21 13:12:46 +01:00
|
|
|
res += '<div id="phrase-selection"><img id="phrase-icon" src="../images/phrase.png" alt="phrase search" onclick="togglePhraseSearchMode()" title="search for phrases"/><span id="phrase-prompt" class="hidden">Select continuous phrase: <img id="cancel-button" src="../images/cancel-button.png" alt="cancel phrase search" onclick="togglePhraseSearchMode()" title="cancel searching for phrases"/></span></div>';
|
2015-08-26 20:13:57 +02:00
|
|
|
|
2016-01-21 13:12:46 +01:00
|
|
|
var inputSentence = $('#search-input').val();
|
2015-08-26 20:13:57 +02:00
|
|
|
var markedSentence = '';
|
|
|
|
var fragments = '';
|
|
|
|
lastInsertedEnd = 0;
|
|
|
|
for(var i = 0; i < data['result']['bestOverlay'].length; i++) {
|
|
|
|
var fragment = data['result']['bestOverlay'][i];
|
|
|
|
//previous unmarked fragment
|
|
|
|
markedSentence += inputSentence.slice(lastInsertedEnd, fragment['matchedPatternStart']);
|
|
|
|
|
|
|
|
//the marked fragment
|
2015-08-27 07:29:29 +02:00
|
|
|
markedSentence += '<span onclick="displayDetails(this, '+i+')" class="matchedFragment">'+inputSentence.slice(fragment['matchedPatternStart'], fragment['matchedPatternEnd'])+'</span>';
|
2015-08-26 20:13:57 +02:00
|
|
|
|
|
|
|
lastInsertedEnd = fragment['matchedPatternEnd'];
|
|
|
|
|
|
|
|
fragments += renderFragment(fragment, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
//remaining unmarked fragment
|
|
|
|
markedSentence += inputSentence.slice(lastInsertedEnd);
|
|
|
|
|
2016-01-21 13:12:46 +01:00
|
|
|
res += '<div id="result-sentence" onMouseUp="phraseSearch(this)">'+markedSentence+'</div>';
|
2015-08-26 20:13:57 +02:00
|
|
|
|
|
|
|
res += '<br/><br/><br/>'+fragments;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderFragment(fragment, number) {
|
|
|
|
var result = '<div style="display:none" id="fragment'+number+'" class="fragmentDetails"><table><tr><td>';
|
|
|
|
|
2016-01-02 15:58:28 +01:00
|
|
|
// source segment
|
2015-08-26 20:13:57 +02:00
|
|
|
var sourceSegment = fragment['sourceSegment'];
|
|
|
|
result += sourceSegment.slice(0, fragment['matchedExampleStart']);
|
|
|
|
result += '<span class="matchedFragment">';
|
|
|
|
result += sourceSegment.slice(fragment['matchedExampleStart'], fragment['matchedExampleEnd']);
|
|
|
|
result += '</span>';
|
|
|
|
result += sourceSegment.slice(fragment['matchedExampleEnd']);
|
|
|
|
|
2016-01-02 15:58:28 +01:00
|
|
|
// target segment
|
|
|
|
result += '</td></tr><tr><td>';
|
|
|
|
var targetSegment = fragment['targetSegment'];
|
|
|
|
var currStart = 0;
|
|
|
|
for (i=0;i<fragment['targetFragments'].length;i++) {
|
|
|
|
result += targetSegment.slice(currStart, fragment['targetFragments'][i][0]);
|
|
|
|
result += '<span class="matchedFragment">';
|
|
|
|
result += targetSegment.slice(fragment['targetFragments'][i][0], fragment['targetFragments'][i][1]);
|
|
|
|
result += '</span>';
|
|
|
|
currStart = fragment['targetFragments'][i][1];
|
|
|
|
}
|
|
|
|
result += targetSegment.slice(currStart);
|
|
|
|
result += '</td></tr></table></div>';
|
2015-08-26 20:13:57 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-01-21 13:12:46 +01:00
|
|
|
function togglePhraseSearchMode() {
|
|
|
|
$('#result-sentence').toggleClass('phrase-mode');
|
|
|
|
$('#phrase-icon').toggleClass('hidden');
|
|
|
|
$('#phrase-prompt').toggleClass('hidden');
|
|
|
|
clearTextSelections();
|
|
|
|
}
|
|
|
|
|
2015-08-27 07:29:29 +02:00
|
|
|
function displayDetails(caller, number) {
|
2016-01-21 13:12:46 +01:00
|
|
|
if (!$('#result-sentence').hasClass('phrase-mode')) {
|
|
|
|
$('#result-sentence .matchedFragmentSelected').attr("class","matchedFragment");
|
|
|
|
caller.className='matchedFragmentSelected';
|
|
|
|
$('.fragmentDetails').css('display', 'none');
|
|
|
|
$('#fragment'+number).css('display', 'block');
|
|
|
|
}
|
2015-08-26 20:13:57 +02:00
|
|
|
}
|
2015-09-01 09:34:27 +02:00
|
|
|
|
2015-10-21 09:24:23 +02:00
|
|
|
function searchText(text, tmid) {
|
2016-01-21 13:12:46 +01:00
|
|
|
$("#search-input").val(text);
|
2015-10-21 09:24:23 +02:00
|
|
|
searchHandle(tmid);
|
2015-09-01 09:34:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function showHideSuggestions() {
|
|
|
|
$('#suggestions').toggleClass('suggestionsInvisible');
|
|
|
|
}
|
2016-01-21 13:12:46 +01:00
|
|
|
|
|
|
|
function phraseSearch(caller) {
|
|
|
|
if ($('#result-sentence').hasClass('phrase-mode')) {
|
|
|
|
var phrase = getSelectedTextWithin(caller);
|
|
|
|
console.log('phrase search for: '+phrase);
|
|
|
|
console.log(getIndicesOf(phrase, $("#search-input").val(), true));
|
|
|
|
var phrases = $('phrase-prompt').data();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSelectedTextWithin(el) {
|
|
|
|
var selectedText = "";
|
|
|
|
if (typeof window.getSelection != "undefined") {
|
|
|
|
var sel = window.getSelection(), rangeCount;
|
|
|
|
if ( (rangeCount = sel.rangeCount) > 0 ) {
|
|
|
|
var range = document.createRange();
|
|
|
|
for (var i = 0, selRange; i < rangeCount; ++i) {
|
|
|
|
range.selectNodeContents(el);
|
|
|
|
selRange = sel.getRangeAt(i);
|
|
|
|
if (selRange.compareBoundaryPoints(range.START_TO_END, range) == 1 && selRange.compareBoundaryPoints(range.END_TO_START, range) == -1) {
|
|
|
|
if (selRange.compareBoundaryPoints(range.START_TO_START, range) == 1) {
|
|
|
|
range.setStart(selRange.startContainer, selRange.startOffset);
|
|
|
|
}
|
|
|
|
if (selRange.compareBoundaryPoints(range.END_TO_END, range) == -1) {
|
|
|
|
range.setEnd(selRange.endContainer, selRange.endOffset);
|
|
|
|
}
|
|
|
|
selectedText += range.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (typeof document.selection != "undefined" && document.selection.type == "Text") {
|
|
|
|
var selTextRange = document.selection.createRange();
|
|
|
|
var textRange = selTextRange.duplicate();
|
|
|
|
textRange.moveToElementText(el);
|
|
|
|
if (selTextRange.compareEndPoints("EndToStart", textRange) == 1 && selTextRange.compareEndPoints("StartToEnd", textRange) == -1) {
|
|
|
|
if (selTextRange.compareEndPoints("StartToStart", textRange) == 1) {
|
|
|
|
textRange.setEndPoint("StartToStart", selTextRange);
|
|
|
|
}
|
|
|
|
if (selTextRange.compareEndPoints("EndToEnd", textRange) == -1) {
|
|
|
|
textRange.setEndPoint("EndToEnd", selTextRange);
|
|
|
|
}
|
|
|
|
selectedText = textRange.text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return selectedText;
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearTextSelections() {
|
|
|
|
if (window.getSelection) {
|
|
|
|
if (window.getSelection().empty) { // Chrome
|
|
|
|
window.getSelection().empty();
|
|
|
|
} else if (window.getSelection().removeAllRanges) { // Firefox
|
|
|
|
window.getSelection().removeAllRanges();
|
|
|
|
}
|
|
|
|
} else if (document.selection) { // IE?
|
|
|
|
document.selection.empty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getIndicesOf(searchStr, str, caseSensitive) {
|
|
|
|
var startIndex = 0, searchStrLen = searchStr.length;
|
|
|
|
var index, indices = [];
|
|
|
|
if (!caseSensitive) {
|
|
|
|
str = str.toLowerCase();
|
|
|
|
searchStr = searchStr.toLowerCase();
|
|
|
|
}
|
|
|
|
while ((index = str.indexOf(searchStr, startIndex)) > -1) {
|
|
|
|
indices.push(index);
|
|
|
|
startIndex = index + searchStrLen;
|
|
|
|
}
|
|
|
|
return indices;
|
|
|
|
}
|