concordia-server/cat/js/cat.js

331 lines
11 KiB
JavaScript
Raw Normal View History

2019-01-17 12:20:36 +01:00
var pageLimit = 20;
function secondStepSearch(caller) {
var fullSearchRequest = {
operation: 'fullSearch',
tmId: currentTmId,
limit: pageLimit,
offset: $('#current-offset').val(),
pattern:caller.innerHTML
}
$.ajax({
url: '/cat/concordia_gate.php',
type: 'post',
dataType: 'json',
success: function (data) {
presentFullSearchResults(data);
},
data: fullSearchRequest
});
}
function presentFullSearchResults(data) {
var pattern = $('#current-pattern').val();
var offset = parseInt($('#current-offset').val());
var begin = offset+1;
var end = offset + pageLimit;
var totalCount = parseInt(data['result']['totalCount']);
if (end > totalCount) {
end = totalCount;
}
var result = 'Showing results '+begin+' - '+end+' of '+totalCount+'<br>';
for (j=0;j<data['result']['occurences'].length;j++) {
var occurence = data['result']['occurences'][j];
result += '<table class="example"><tr><td>';
// source segment
var sourceSegment = occurence['sourceSegment'];
result += sourceSegment.slice(0, occurence['matchedExampleStart']);
result += '<span class="matchedFragment">';
result += sourceSegment.slice(occurence['matchedExampleStart'], occurence['matchedExampleEnd']);
result += '</span>';
result += sourceSegment.slice(occurence['matchedExampleEnd']);
// target segment
result += '</td></tr><tr><td>';
var targetSegment = occurence['targetSegment'];
var currStart = 0;
for (i=0;i<occurence['targetFragments'].length;i++) {
result += targetSegment.slice(currStart, occurence['targetFragments'][i][0]);
result += '<span class="matchedFragment">';
result += targetSegment.slice(occurence['targetFragments'][i][0], occurence['targetFragments'][i][1]);
result += '</span>';
currStart = occurence['targetFragments'][i][1];
}
result += targetSegment.slice(currStart);
result += '</td></tr></table>';
}
$('#occurences').html(result);
}
2017-06-25 23:16:43 +02:00
function showNewTmOptions() {
showField('tm_name');
hideField('tm_id');
showField('src_lang_id');
showField('trg_lang_id');
}
function showExtendTmOptions() {
hideField('tm_name');
showField('tm_id');
hideField('src_lang_id');
hideField('trg_lang_id');
}
function hideField(fieldId) {
$('#'+fieldId).addClass('hidden');
$('label[for='+fieldId+']').addClass('hidden');
}
function showField(fieldId) {
$('#'+fieldId).removeClass('hidden');
$('label[for='+fieldId+']').removeClass('hidden');
}
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({
2016-10-28 11:49:52 +02:00
url: '/cat/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
});
}
2016-01-21 16:51:23 +01:00
function phraseSearchHandle(tmid, intervals) {
var concordiaRequest = {
operation: 'concordiaPhraseSearch',
tmId: tmid,
pattern:$("#search-input").val(),
intervals: intervals
}
$.ajax({
2016-10-28 11:49:52 +02:00
url: '/cat/concordia_gate.php',
2016-01-21 16:51:23 +01:00
type: 'post',
dataType: 'json',
success: function (data) {
2016-01-25 23:55:32 +01:00
if (data['found']) {
$('#result').html(renderResult(data));
} else {
$('#phrase-prompt').html('<b>Your phrase was not found. Try selecting another phrase: </b>').fadeOut(200).fadeIn(200);
clearTextSelections();
}
2016-01-21 16:51:23 +01:00
},
data: concordiaRequest
});
}
2015-08-26 20:13:57 +02:00
function renderResult(data) {
2019-01-17 12:20:36 +01:00
$('#occurences').html('');
2015-08-26 20:13:57 +02:00
var res = '';
2017-04-23 00:43:15 +02:00
var disablePhraseSearch = true;
2017-02-22 10:29:15 +01:00
2016-01-25 23:55:32 +01:00
if (typeof(data['result']['bestOverlayScore']) === 'undefined') {
2017-02-22 10:29:15 +01:00
// ignore
2016-01-25 23:55:32 +01:00
} else {
var score = data['result']['bestOverlayScore']*100;
res += '<div id="result-score">Concordia score: <b>'+score.toFixed(0)+'%</b></div>';
}
2017-02-22 10:29:15 +01:00
if (typeof disablePhraseSearch !== 'undefined') {
// ignore
} else {
res += '<div id="phrase-selection">Phrase search mode:&nbsp;<img id="phrase-off-icon" src="../images/switchOff.png" alt="enter phrase search mode" onclick="togglePhraseSearchMode()" title="search for phrases"/><img class="hidden" id="phrase-on-icon" src="../images/switchOn.png" alt="cancel phrase search" onclick="togglePhraseSearchMode()" title="cancel phrase search"/><span id="phrase-prompt" class="hidden">Select continuous phrase: </span></div>';
}
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
2017-04-28 13:49:30 +02:00
markedSentence += htmlEncode(inputSentence.slice(lastInsertedEnd, fragment['matchedPatternStart']));
2015-08-26 20:13:57 +02:00
//the marked fragment
2018-08-29 15:00:48 +02:00
var textFragment = htmlEncode(inputSentence.slice(fragment['matchedPatternStart'], fragment['matchedPatternEnd']));
markedSentence += '<span onclick="secondStepSearch(this)" class="matchedFragment">'+textFragment+'</span>';
2017-02-22 10:29:15 +01:00
2015-08-26 20:13:57 +02:00
lastInsertedEnd = fragment['matchedPatternEnd'];
2017-02-22 10:29:15 +01:00
2015-08-26 20:13:57 +02:00
fragments += renderFragment(fragment, i);
}
2017-02-22 10:29:15 +01:00
2015-08-26 20:13:57 +02:00
//remaining unmarked fragment
2017-04-28 13:49:30 +02:00
markedSentence += htmlEncode(inputSentence.slice(lastInsertedEnd));
2017-02-22 10:29:15 +01:00
2016-01-21 13:12:46 +01:00
res += '<div id="result-sentence" onMouseUp="phraseSearch(this)">'+markedSentence+'</div>';
2017-02-22 10:29:15 +01:00
2015-08-26 20:13:57 +02:00
res += '<br/><br/><br/>'+fragments;
2017-02-22 10:29:15 +01:00
2015-08-26 20:13:57 +02:00
return res;
}
2017-04-28 13:49:30 +02:00
function htmlEncode(value){
// Create a in-memory div, set its inner text (which jQuery automatically encodes)
// Then grab the encoded contents back out. The div never exists on the page.
return $('<div/>').text(value).html();
}
2015-08-26 20:13:57 +02:00
function renderFragment(fragment, number) {
2017-04-23 00:43:15 +02:00
var result = '<div style="display:none" id="fragment'+number+'" class="fragmentDetails">';
for (j=0;j<fragment['occurences'].length;j++) {
var occurence = fragment['occurences'][j];
result += '<table class="example"><tr><td>';
// source segment
var sourceSegment = occurence['sourceSegment'];
2017-04-26 13:53:51 +02:00
result += sourceSegment.slice(0, occurence['matchedExampleStart']);
2016-01-02 15:58:28 +01:00
result += '<span class="matchedFragment">';
2017-04-23 00:43:15 +02:00
result += sourceSegment.slice(occurence['matchedExampleStart'], occurence['matchedExampleEnd']);
2016-01-02 15:58:28 +01:00
result += '</span>';
2017-04-23 00:43:15 +02:00
result += sourceSegment.slice(occurence['matchedExampleEnd']);
// target segment
result += '</td></tr><tr><td>';
var targetSegment = occurence['targetSegment'];
var currStart = 0;
for (i=0;i<occurence['targetFragments'].length;i++) {
result += targetSegment.slice(currStart, occurence['targetFragments'][i][0]);
result += '<span class="matchedFragment">';
result += targetSegment.slice(occurence['targetFragments'][i][0], occurence['targetFragments'][i][1]);
result += '</span>';
currStart = occurence['targetFragments'][i][1];
}
result += targetSegment.slice(currStart);
result += '</td></tr></table>';
2016-01-02 15:58:28 +01:00
}
2017-04-23 00:43:15 +02:00
result += '</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');
2016-01-25 23:55:32 +01:00
$('#phrase-on-icon').toggleClass('hidden');
$('#phrase-off-icon').toggleClass('hidden');
2016-01-21 13:12:46 +01:00
$('#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
2018-08-29 15:00:48 +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);
2016-01-21 16:51:23 +01:00
if (phrase.length > 0) {
2017-02-22 10:29:15 +01:00
2016-01-21 16:51:23 +01:00
var intervalStarts = getIndicesOf(phrase, $("#search-input").val(), true);
var intervals = [];
2017-02-22 10:29:15 +01:00
2016-01-21 16:51:23 +01:00
for (var i=0;i<intervalStarts.length;i++) {
2017-02-22 10:29:15 +01:00
2016-01-21 16:51:23 +01:00
intervals.push([intervalStarts[i], intervalStarts[i]+phrase.length])
}
2017-02-22 10:29:15 +01:00
2016-01-21 16:51:23 +01:00
phraseSearchHandle(currentTmId, intervals);
}
2016-01-21 13:12:46 +01:00
}
}
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;
}