var pageLimit = 20; function secondStepSearch(fragment, resetOffset=false) { if (resetOffset) { $('#current-offset').val(0); } var searchQuery = $('body').data('current_fragment'); if (typeof(fragment) !== "undefined") { searchQuery = fragment; $('body').data('current_fragment', fragment); } var fullSearchRequest = { operation: 'fullSearch', tmId: currentTmId, limit: pageLimit, offset: $('#current-offset').val(), pattern:searchQuery } $.ajax({ url: '/cat/concordia_gate.php', type: 'post', dataType: 'json', success: function (data) { presentFullSearchResults(data); }, data: fullSearchRequest }); } function prevPage() { var currentOffset = parseInt($('#current-offset').val()); if (currentOffset >= pageLimit) { $('#current-offset').val(currentOffset-pageLimit) secondStepSearch(); } } function nextPage() { var currentOffset = parseInt($('#current-offset').val()); var currentTotalCount = $('body').data('currentTotalCount'); if (currentOffset < currentTotalCount - pageLimit) { $('#current-offset').val(currentOffset+pageLimit) secondStepSearch(); } } 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']); $('body').data('currentTotalCount', totalCount); if (end > totalCount) { end = totalCount; } var result = 'Showing results '+begin+' - '+end+' of '+totalCount+' '; result += ' '; result += '
'; for (j=0;j'+occurence['sourceName']+''; } $('#occurrences').html(result); } 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'); } function toggleHelp() { $('#help').toggleClass('hidden'); } function searchHandle(tmid) { var concordiaRequest = { operation: 'concordiaSearch', tmId: tmid, pattern:$("#search-input").val() } $.ajax({ url: '/cat/concordia_gate.php', type: 'post', dataType: 'json', success: function (data) { $('#result').html(renderResult(data)); }, data: concordiaRequest }); } function phraseSearchHandle(tmid, intervals) { var concordiaRequest = { operation: 'concordiaPhraseSearch', tmId: tmid, pattern:$("#search-input").val(), intervals: intervals } $.ajax({ url: '/cat/concordia_gate.php', type: 'post', dataType: 'json', success: function (data) { if (data['found']) { $('#result').html(renderResult(data)); } else { $('#phrase-prompt').html('Your phrase was not found. Try selecting another phrase: ').fadeOut(200).fadeIn(200); clearTextSelections(); } }, data: concordiaRequest }); } function renderResult(data) { $('#occurrences').html(''); var res = ''; var disablePhraseSearch = true; if (typeof(data['result']['bestOverlayScore']) === 'undefined') { // ignore } else { var score = data['result']['bestOverlayScore']*100; res += '
Concordia score: '+score.toFixed(0)+'%
'; } if (typeof disablePhraseSearch !== 'undefined') { // ignore } else { res += '
Phrase search mode: enter phrase search mode
'; } var inputSentence = $('#search-input').val(); 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 += htmlEncode(inputSentence.slice(lastInsertedEnd, fragment['matchedPatternStart'])); //the marked fragment var textFragment = htmlEncode(inputSentence.slice(fragment['matchedPatternStart'], fragment['matchedPatternEnd'])); markedSentence += ''+textFragment+''; lastInsertedEnd = fragment['matchedPatternEnd']; fragments += renderFragment(fragment, i); } //remaining unmarked fragment markedSentence += htmlEncode(inputSentence.slice(lastInsertedEnd)); res += '
'+markedSentence+'
'; res += '


'+fragments; return res; } 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 $('
').text(value).html(); } function renderFragment(fragment, number) { var result = ''; return result; } function togglePhraseSearchMode() { $('#result-sentence').toggleClass('phrase-mode'); $('#phrase-on-icon').toggleClass('hidden'); $('#phrase-off-icon').toggleClass('hidden'); $('#phrase-prompt').toggleClass('hidden'); clearTextSelections(); } function displayDetails(caller, number) { if (!$('#result-sentence').hasClass('phrase-mode')) { $('#result-sentence .matchedFragmentSelected').attr("class","matchedFragment"); caller.className='matchedFragmentSelected'; $('.fragmentDetails').css('display', 'none'); $('#fragment'+number).css('display', 'block'); } } function searchText(text, tmid) { $("#search-input").val(text); searchHandle(tmid); } function showHideSuggestions() { $('#suggestions').toggleClass('suggestionsInvisible'); } function phraseSearch(caller) { if ($('#result-sentence').hasClass('phrase-mode')) { var phrase = getSelectedTextWithin(caller); if (phrase.length > 0) { var intervalStarts = getIndicesOf(phrase, $("#search-input").val(), true); var intervals = []; for (var i=0;i 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; }