UNPKG

@syncfusion/ej2-pdfviewer

Version:
1,036 lines 130 kB
import { createElement, Browser, isNullOrUndefined, isBlazor, SanitizeHtmlHelper } from '@syncfusion/ej2-base'; import { CheckBox } from '@syncfusion/ej2-buttons'; import { AjaxHandler } from '../index'; import { createSpinner, showSpinner, hideSpinner } from '../index'; import { Rect } from '@syncfusion/ej2-drawings'; import { AutoComplete } from '@syncfusion/ej2-dropdowns'; import { TaskPriorityLevel } from '../base/pdfviewer-utlis'; var searchTextCollection = []; /** * TextSearch module * * @param {Event} event - event * @returns {void} */ var TextSearch = /** @class */ (function () { /** * @param {PdfViewer} pdfViewer - It describes about the pdf viewer * @param {PdfViewerBase} pdfViewerBase - It describes about the pdfviewer base * @private * @returns {void} */ function TextSearch(pdfViewer, pdfViewerBase) { var _this = this; /** * @private */ this.isTextSearch = false; /** * @private */ this.searchCount = 0; this.currentOccurrence = 0; this.searchIndex = 0; this.currentSearchIndex = 0; this.startIndex = null; this.searchPageIndex = null; this.startSearchPageIndex = null; this.searchString = null; this.isMatchCase = false; this.isMultiSearch = false; this.isSingleSearch = false; this.searchRequestHandler = null; this.textContents = []; this.searchMatches = []; this.multiSearchCounts = {}; this.searchedPages = []; this.isPrevSearch = false; this.isExactMatch = false; this.autompleteDataSource = []; this.searchedOccurrences = []; this.isSelectedFromPopup = false; /** * @private */ this.isDocumentTextCollectionReady = false; this.intervalId = null; /** * @private */ this.searchTextDivzIndex = '-1'; this.tempElementStorage = []; /** * @private */ this.isMessagePopupOpened = false; /** * @private */ this.isTextRetrieved = false; this.isTextSearched = false; this.isTextSearchEventTriggered = false; this.isSearchText = false; this.isLastOccurrenceCompleted = false; this.checkBoxOnChange = function (event) { if (event.checked) { _this.isMatchCase = true; _this.searchString = ''; if (_this.searchInput.value && _this.searchInput.value !== '') { _this.isTextSearch = true; } } else { _this.isMatchCase = false; _this.searchString = ''; if (_this.searchInput.value && _this.searchInput.value !== '') { _this.isTextSearch = true; } } if (_this.isTextSearch) { _this.resetVariables(); _this.clearAllOccurrences(); var inputString = _this.searchInput.value; _this.searchIndex = 0; _this.initiateTextSearch(inputString); if (_this.searchCount === 0 && !_this.isMessagePopupOpened) { _this.onMessageBoxOpen(); } } }; this.searchKeypressHandler = function (event) { var char = String.fromCharCode(event.which || event.keyCode); var isAlphanumeric = /[a-zA-Z0-9]/.test(char); var isSpecialCharacter = function (char) { return /[!@#$%^&*(),.?":{}|<>]/.test(char); }; if ((isAlphanumeric || isSpecialCharacter) && !(event.ctrlKey || event.altKey)) { _this.initiateTextSearch(_this.searchInput.value); _this.updateSearchInputIcon(false); } else { _this.resetVariables(); } }; this.searchClickHandler = function (event) { _this.searchButtonClick(_this.searchBtn, _this.searchAutocompleteObj); }; this.nextButtonOnClick = function (event) { _this.nextSearch(); }; this.prevButtonOnClick = function (event) { _this.prevSearch(); }; this.pdfViewer = pdfViewer; this.pdfViewerBase = pdfViewerBase; } /** * @private * @returns {void} */ TextSearch.prototype.createTextSearchBox = function () { var _this = this; this.searchBox = createElement('div', { id: this.pdfViewer.element.id + '_search_box', className: 'e-pv-text-search-bar' }); var toolbarElement; if (isBlazor()) { toolbarElement = document.getElementById('toolbarContainer'); } else { toolbarElement = this.pdfViewerBase.getElement('_toolbarContainer'); } if (toolbarElement) { if (isBlazor()) { this.searchBox.style.top = toolbarElement.clientHeight + 'px'; } else { this.searchBox.style.top = toolbarElement.clientHeight + 'px'; } } var searchElementsContainer = createElement('div', { id: this.pdfViewer.element.id + '_search_box_elements', className: 'e-pv-text-search-bar-elements' }); this.searchInputContainer = createElement('div', { id: this.pdfViewer.element.id + '_search_input_container', className: 'e-input-group e-pv-text-search-input' }); this.searchInput = createElement('input', { id: this.pdfViewer.element.id + '_search_input', className: 'e-input e-pv-search-input-ele' }); var tempData = { matches: [ { 'SearchString': '', 'Count': '' } ] }; var isFirst = false; this.searchAutocompleteObj = new AutoComplete({ dataSource: tempData.matches, fields: { value: 'SearchString' }, headerTemplate: "<div class=\"e-pv-automplete-header\"><span class=\"e-pv-search-exact-matches\">" + this.pdfViewer.localeObj.getConstant('Exact Matches') + "</span><span class=\"e-pv-total-exact-matches\"></span></div>", itemTemplate: '<div class="e-pv-autocomplete-item">' + '<span class="e-pv-autocomplete-word"> ${SearchString} </span><span class="e-pv-autocomplete-count">${Count}</span></div>', placeholder: this.pdfViewer.localeObj.getConstant('Find in document'), popupHeight: '200px', beforeOpen: function (event) { if (Array.isArray(_this.autompleteDataSource) && (_this.autompleteDataSource.length === 0)) { event.cancel = true; } }, select: function (event) { _this.isSelectedFromPopup = true; _this.isSingleSearch = true; _this.isMultiSearch = false; _this.isExactMatch = true; _this.initiateSearch(event.itemData.SearchString); }, filtering: function (event) { _this.isSelectedFromPopup = false; if (_this.searchCountEle) { _this.searchCountEle.innerHTML = ''; _this.adjustInputContainerWidth(); } _this.isSingleSearch = false; _this.isExactMatch = false; _this.isMultiSearch = _this.matchAnyWordCheckBox.checked; _this.initiateTextSearch(event.text); if (event.text === '') { clearInterval(_this.intervalId); _this.showLoadingIndicator(false); } _this.searchString = ''; var updateInterval = setInterval(function () { if (_this.documentTextCollection.length === _this.pdfViewerBase.pageCount) { event.updateData(_this.autompleteDataSource, null); if (Array.isArray(_this.autompleteDataSource) && (_this.autompleteDataSource.length !== 0)) { var dataSourceInfo = _this.autompleteDataSource; var totalCount = dataSourceInfo.reduce(function (acc, obj) { return acc + parseInt(obj.Count, 10); }, 0); document.querySelector('.e-pv-total-exact-matches').innerHTML = totalCount; if (_this.isMultiSearch) { document.querySelector('.e-pv-search-exact-matches').innerHTML = _this.pdfViewer.localeObj.getConstant('Total Matches'); } else { document.querySelector('.e-pv-search-exact-matches').innerHTML = _this.pdfViewer.localeObj.getConstant('Exact Matches'); } } else if (_this.autompleteDataSource.length === 0) { var element = document.querySelector('.e-pv-total-exact-matches'); if (!isNullOrUndefined(element)) { element.innerHTML = '0'; } _this.searchAutocompleteObj.hidePopup(); } clearInterval(updateInterval); _this.isDocumentTextCollectionReady = true; } }, 1000); }, created: function (event) { _this.searchAutocompleteObj.element.addEventListener('keydown', function (args) { if (args.key === 'Enter') { _this.isSingleSearch = true; _this.isExactMatch = _this.isSelectedFromPopup; _this.isMultiSearch = false; _this.initiateTextSearch(_this.searchInput.value); if (_this.searchCount === 0 && !_this.isMessagePopupOpened && _this.documentTextCollection.length === _this.pdfViewerBase.pageCount) { _this.onMessageBoxOpen(); } } }); _this.searchAutocompleteObj.element.parentElement.querySelector('.e-clear-icon').addEventListener('mousedown', function (args) { _this.showLoadingIndicator(false); clearInterval(_this.intervalId); _this.searchInput.value = ''; _this.resetTextSearch(); if (_this.searchCountEle) { _this.searchCountEle.innerHTML = ''; _this.adjustInputContainerWidth(); } _this.searchInput.focus(); _this.searchString = ''; }); } }); this.searchBtn = createElement('span', { id: this.pdfViewer.element.id + '_search_box-icon', className: 'e-input-group-icon e-input-search-group-icon e-pv-search-icon' }); this.searchBtn.setAttribute('tabindex', '0'); this.searchInputContainer.appendChild(this.searchInput); this.searchAutocompleteObj.appendTo(this.searchInput); searchElementsContainer.appendChild(this.searchInputContainer); this.searchCountEle = createElement('span', { id: this.pdfViewer.element.id + '_search_count', className: 'e-pv-search-count', innerHTML: '' }); searchElementsContainer.appendChild(this.searchCountEle); if (this.pdfViewer.enableRtl) { this.prevSearchBtn = this.createSearchBoxButtons('prev_occurrence', 'e-pv-next-search'); } else { this.prevSearchBtn = this.createSearchBoxButtons('prev_occurrence', 'e-pv-prev-search'); } this.prevSearchBtn.setAttribute('aria-label', 'Previous Search text'); searchElementsContainer.appendChild(this.prevSearchBtn); if (this.pdfViewer.enableRtl) { this.nextSearchBtn = this.createSearchBoxButtons('next_occurrence', 'e-pv-prev-search'); } else { this.nextSearchBtn = this.createSearchBoxButtons('next_occurrence', 'e-pv-next-search'); } this.nextSearchBtn.setAttribute('aria-label', 'Next Search text'); searchElementsContainer.appendChild(this.nextSearchBtn); var matchCaseContainer = createElement('div', { id: this.pdfViewer.element.id + '_match_case_container', className: 'e-pv-textsearch-match-case-container' }); var matchCaseInput = createElement('input', { id: this.pdfViewer.element.id + '_match_case' }); matchCaseInput.type = 'checkbox'; if (isBlazor()) { matchCaseInput.style.height = '17px'; matchCaseInput.style.width = '17px'; matchCaseInput.addEventListener('change', this.checkBoxOnChange.bind(this)); } matchCaseContainer.appendChild(matchCaseInput); var matchAnyWordInput = createElement('input', { id: this.pdfViewer.element.id + '_match_any_word' }); matchAnyWordInput.type = 'checkbox'; matchCaseContainer.appendChild(matchAnyWordInput); this.searchBox.appendChild(searchElementsContainer); this.searchBox.appendChild(matchCaseContainer); this.pdfViewerBase.mainContainer.appendChild(this.searchBox); if (isBlazor()) { var matchCaseText_1 = createElement('span', { id: this.pdfViewer.element.id + '_search_box_text', styles: 'position: absolute; padding-top: 3px; padding-left: 8px; padding-right: 8px; font-size: 13px' }); var promise = this.pdfViewer._dotnetInstance.invokeMethodAsync('GetLocaleText', 'PdfViewer_Matchcase'); promise.then(function (value) { matchCaseText_1.textContent = value; }); matchCaseContainer.appendChild(matchCaseText_1); } else { var checkBox = new CheckBox({ cssClass: 'e-pv-match-case', label: this.pdfViewer.localeObj.getConstant('Match case'), htmlAttributes: { 'tabindex': '0' }, change: this.checkBoxOnChange.bind(this) }); checkBox.appendTo(matchCaseInput); this.matchAnyWordCheckBox = new CheckBox({ cssClass: 'e-pv-match-any-word', label: this.pdfViewer.localeObj.getConstant('Match any word'), htmlAttributes: { 'tabindex': '0' }, change: function () { _this.isMultiSearch = _this.matchAnyWordCheckBox.checked; } }); this.matchAnyWordCheckBox.appendTo(matchAnyWordInput); } matchCaseContainer.firstElementChild.addEventListener('keydown', function (event) { if (event.key === 'Enter' || event.key === ' ') { event.target.click(); event.preventDefault(); event.stopPropagation(); } }); matchAnyWordInput.addEventListener('keydown', function (event) { if (event.key === 'Enter' || event.key === ' ') { event.target.click(); event.preventDefault(); event.stopPropagation(); } }); var waitingPopup = createElement('div', { id: this.pdfViewer.element.id + '_textSearchLoadingIndicator' }); this.searchInputContainer.appendChild(waitingPopup); waitingPopup.style.position = 'absolute'; waitingPopup.style.top = '15px'; waitingPopup.style.left = this.searchInputContainer.clientWidth - 46 + 'px'; createSpinner({ target: waitingPopup, cssClass: 'e-spin-center' }); this.setLoaderProperties(waitingPopup); this.showSearchBox(false); if (this.pdfViewer.enableRtl) { this.searchBox.classList.add('e-rtl'); this.searchBox.style.left = '88.3px'; } else { this.searchBox.classList.remove('e-rtl'); this.searchBox.style.right = '88.3px'; } this.searchBtn.addEventListener('click', this.searchClickHandler.bind(this)); this.searchBtn.addEventListener('keydown', function (event) { if (event.key === 'Enter' || event.key === ' ') { _this.searchClickHandler(event); event.preventDefault(); event.stopPropagation(); } }); this.nextSearchBtn.addEventListener('click', this.nextButtonOnClick.bind(this)); this.prevSearchBtn.addEventListener('click', this.prevButtonOnClick.bind(this)); }; TextSearch.prototype.setLoaderProperties = function (element) { var spinnerElement = element.firstChild.firstChild.firstChild; if (spinnerElement) { spinnerElement.style.height = '18px'; spinnerElement.style.width = '18px'; spinnerElement.style.transformOrigin = '9px 9px 9px'; } }; TextSearch.prototype.showLoadingIndicator = function (isShow) { var waitingPopup = document.getElementById(this.pdfViewer.element.id + '_textSearchLoadingIndicator'); if (waitingPopup) { if (isShow) { showSpinner(waitingPopup); } else { hideSpinner(waitingPopup); } } }; /** * @private * @returns {void} */ TextSearch.prototype.textSearchBoxOnResize = function () { if (this.pdfViewer.toolbarModule && this.pdfViewer.enableToolbar) { var secondaryToolbar = this.pdfViewerBase.getElement('_toolbarContainer_popup'); if (secondaryToolbar) { if (secondaryToolbar.contains(this.pdfViewerBase.getElement('_search').parentElement)) { this.searchBox.style.right = '0px'; } } } else { if (this.pdfViewerBase.viewerContainer.clientWidth + this.pdfViewerBase.viewerContainer.offsetLeft < this.searchBox.offsetLeft + this.searchBox.clientWidth) { this.searchBox.style.right = '0px'; this.searchBox.style.width = parseInt(this.searchBox.style.width, 10) - ((this.searchBox.offsetLeft + this.searchBox.clientWidth) - (this.pdfViewerBase.viewerContainer.clientWidth)) + 'px'; this.searchInput.style.width = parseInt(this.searchInput.style.width, 10) - ((this.searchBox.offsetLeft + this.searchBox.clientWidth) - (this.pdfViewerBase.viewerContainer.clientWidth)) + 'px'; } else { this.searchBox.style.right = '88.3px'; this.searchBox.style.width = ''; this.searchInput.style.width = ''; } } }; /** * @param {boolean} isShow - It describes about the isShow * @private * @returns {void} */ TextSearch.prototype.showSearchBox = function (isShow) { if (!isNullOrUndefined(this.searchBox)) { if (isShow) { this.searchBox.style.display = 'block'; } else { this.searchBox.style.display = 'none'; this.searchInput.value = ''; this.searchCountEle.style.display = 'none'; if (this.pdfViewer.toolbarModule) { this.pdfViewer.toolbarModule.isTextSearchBoxDisplayed = false; } } this.onTextSearchClose(); } }; /** * @private * @returns {void} */ TextSearch.prototype.searchAfterSelection = function () { if (this.currentOccurrence === 0) { this.searchPageIndex = this.pdfViewerBase.currentPageNumber - 1; this.startSearchPageIndex = this.searchPageIndex; } if (this.isTextSearch) { this.initSearch(this.searchPageIndex, true); this.highlightOthers(); } }; TextSearch.prototype.calculateSearchCount = function (inputString) { var _this = this; this.searchCount = 0; this.currentOccurrence = 0; this.resetVariables(); this.searchIndex = 0; if (!inputString || inputString.trim() === '') { if (this.searchCountEle) { this.searchCountEle.innerHTML = ''; this.adjustInputContainerWidth(); } return; } if (this.isMultiSearch) { if (this.searchCountEle) { this.searchCountEle.innerHTML = ''; this.adjustInputContainerWidth(); } var wordsToSearch = inputString.split(' '); this.multiSearchCounts = {}; for (var _i = 0, wordsToSearch_1 = wordsToSearch; _i < wordsToSearch_1.length; _i++) { var word = wordsToSearch_1[_i]; word = word.trim(); if (word === '') { continue; } var wordCount = 0; for (var i = 0; i < this.documentTextCollection.length; i++) { var documentIndex = this.documentTextCollection[parseInt(i.toString(), 10)][parseInt(i.toString(), 10)]; var pageTextData = documentIndex.pageText ? documentIndex.pageText : documentIndex.PageText; if (!this.isMatchCase) { pageTextData = pageTextData.toLowerCase(); word = word.toLowerCase(); } var matchIndex = pageTextData.indexOf(word); while (matchIndex !== -1) { wordCount++; matchIndex = pageTextData.indexOf(word, matchIndex + 1); } } this.multiSearchCounts["" + word] = wordCount; } var transformedData_1 = Object.keys(this.multiSearchCounts).map(function (key) { return ({ SearchString: key, Count: _this.multiSearchCounts["" + key].toString() }); }); this.autompleteDataSource = transformedData_1; return; } var wordCounts = {}; for (var i = 0; i < this.documentTextCollection.length; i++) { var documentIndex = this.documentTextCollection[parseInt(i.toString(), 10)][parseInt(i.toString(), 10)]; var pageTextData = documentIndex.pageText ? documentIndex.pageText : documentIndex.PageText; var pageIndex = i; var multiSearch = (pageTextData.replace((/(\s\r\n)/gm), ' ')).replace((/(\r\n)/gm), ' '); var Multiline = (pageTextData.replace((/(\s\r\n)/gm), ' ')).replace((/(\r\n)/gm), ' '); var specialCharcterSearch = multiSearch.replace(/[^a-zA-Z0-9]+/g, ' '); var arrayReturns = void 0; var queryLength = inputString.length; var matches = []; var matchedArray = []; var matchIndex = -queryLength; var newIndex = -queryLength; var multiSearchIndex = -queryLength; var MultilineIndex = -queryLength; var specialcharcterIndex = -queryLength; if (!this.isMatchCase) { inputString = inputString.toLowerCase(); pageTextData = pageTextData.toLowerCase(); multiSearch = multiSearch.toLowerCase(); Multiline = Multiline.toLowerCase(); specialCharcterSearch = specialCharcterSearch.toLowerCase(); } while (matchIndex !== 0 || (matchIndex === 0 && matches.length > 0 && matches[0] === 0)) { if (!inputString || inputString === ' ') { break; } if (this.isExactMatch) { var match = void 0; // eslint-disable-next-line var escapedInputString = inputString.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); // eslint-disable-next-line var regex_1 = new RegExp("(?<!\\w)" + escapedInputString + "(?!\\w)", 'g'); match = regex_1.exec(pageTextData); while (match !== null) { matches.push(match.index); match = regex_1.exec(pageTextData); } break; } matchIndex = pageTextData.indexOf(inputString, matchIndex + queryLength); if (inputString.indexOf(' ') !== -1) { var newString = inputString.replace(' ', '\r\n'); newIndex = pageTextData.indexOf(newString, newIndex + queryLength); if (!(newIndex <= -1)) { if (newIndex < matchIndex) { matches.push(newIndex); } } } if (matchIndex <= -1 && newIndex <= -1) { break; } if (!(matchIndex <= -1)) { matches.push(matchIndex); } if (newIndex > matchIndex && !(newIndex <= -1)) { matches.push(newIndex); } } if (matches.length === 0) { multiSearchIndex = multiSearch.indexOf(inputString, multiSearchIndex + queryLength); MultilineIndex = Multiline.indexOf(inputString, MultilineIndex + queryLength); specialcharcterIndex = specialCharcterSearch.indexOf(inputString, specialcharcterIndex + queryLength); if (multiSearchIndex !== -1) { arrayReturns = this.correctLinetext(inputString, matchIndex, pageTextData); matchIndex = -arrayReturns[0].length; for (var i_1 = 0; i_1 < arrayReturns.length; i_1++) { matchIndex = pageTextData.indexOf(arrayReturns[parseInt(i_1.toString(), 10)].trim(), matchIndex + (arrayReturns[i_1 - 1] === undefined || null ? arrayReturns[0].length : arrayReturns[i_1 - 1].length)); matchedArray.push(matchIndex); if (matchedArray.length > 1) { if ((matchedArray[1] - (matchedArray[0] + arrayReturns[0].length)) <= 3) { matches.push(matchedArray); this.searchMatches[parseInt(pageIndex.toString(), 10)] = matches; } else { i_1 = -1; matchIndex = matchedArray[0] + arrayReturns[0].length; matchedArray.splice(0, matchedArray.length); } } } } else if (specialcharcterIndex !== -1) { arrayReturns = this.correctLinetext(inputString, matchIndex, pageTextData); matchIndex = -arrayReturns[0].length; for (var i_2 = 0; i_2 < arrayReturns.length; i_2++) { matchIndex = pageTextData.indexOf(arrayReturns[parseInt(i_2.toString(), 10)].trim(), matchIndex + (arrayReturns[i_2 - 1] === undefined || null ? arrayReturns[0].length : arrayReturns[i_2 - 1].length)); matchedArray.push(matchIndex); if (matchedArray.length > 1) { if ((matchedArray[1] - (matchedArray[0] + arrayReturns[0].length)) <= 3) { matches.push(matchedArray); this.searchMatches[parseInt(pageIndex.toString(), 10)] = matches; } else { i_2 = -1; matchIndex = matchedArray[0] + arrayReturns[0].length; matchedArray.splice(0, matchedArray.length); } } } } else if (MultilineIndex !== -1) { arrayReturns = this.correctLinetext(inputString, matchIndex, pageTextData); matchIndex = -arrayReturns[0].length; for (var i_3 = 0; i_3 < arrayReturns.length; i_3++) { matchIndex = pageTextData.indexOf(arrayReturns[parseInt(i_3.toString(), 10)].trim(), matchIndex + (arrayReturns[i_3 - 1] === undefined || null ? arrayReturns[0].length : arrayReturns[i_3 - 1].length)); matchedArray.push(matchIndex); if (matchedArray.length > 1) { if ((matchedArray[1] - (matchedArray[0] + arrayReturns[0].length)) <= 3) { matches.push(matchedArray); this.searchMatches[parseInt(pageIndex.toString(), 10)] = matches; } else { i_3 = -1; matchIndex = matchedArray[0] + arrayReturns[0].length; matchedArray.splice(0, matchedArray.length); } } } } if (matches.length > 1) { matches.splice(1, matches.length); } } var words = pageTextData.match(/[a-zA-Z]+|\d+/g); if (!isNullOrUndefined(words)) { words.forEach(function (word) { if (word.startsWith(inputString)) { if (wordCounts["" + word]) { wordCounts["" + word]++; } else { wordCounts["" + word] = 1; } } }); } if (matches.length > 0) { this.searchCount = this.searchCount + matches.length; } if (this.searchMatches && matches.length > 0) { this.searchMatches[parseInt(pageIndex.toString(), 10)] = matches; } } var transformedData = Object.keys(wordCounts).map(function (key) { return ({ SearchString: key, Count: wordCounts["" + key].toString() }); }); if (this.isSingleSearch) { if (this.searchCountEle) { if (this.searchedOccurrences.indexOf(this.currentOccurrence + 1) === -1) { this.searchedOccurrences.push(this.currentOccurrence + 1); } this.adjustInputContainerWidth(); } if (this.searchedOccurrences.length === 1) { this.pdfViewer.fireTextSearchStart(this.searchString, this.isMatchCase); } this.currentOccurrence += 1; } else { if (this.searchAutocompleteObj) { this.autompleteDataSource = transformedData; } } }; TextSearch.prototype.getSearchCountText = function () { if (this.pdfViewer.enableRtl) { this.searchCountEle.innerHTML = this.searchCount + " " + this.pdfViewer.localeObj.getConstant('of') + " " + this.currentOccurrence; } else { this.searchCountEle.innerHTML = this.currentOccurrence + " " + this.pdfViewer.localeObj.getConstant('of') + " " + this.searchCount; } }; TextSearch.prototype.adjustInputContainerWidth = function () { var parentContainer = this.searchCountEle.parentElement; if (this.searchCount > 0) { if (Browser.isDevice && !this.pdfViewer.enableDesktopMode) { if (parentContainer) { parentContainer.style.display = 'block'; } } this.searchCountEle.style.display = 'inline-block'; } else { this.searchCountEle.style.display = 'none'; if (Browser.isDevice && !this.pdfViewer.enableDesktopMode) { if (parentContainer) { parentContainer.style.display = 'none'; } } } }; TextSearch.prototype.initiateTextSearch = function (inputString, isMobileSearch) { this.enableNextButton(true); this.enablePrevButton(true); this.autompleteDataSource = []; if (this.pdfViewer.enableHtmlSanitizer && typeof inputString === 'string') { inputString = SanitizeHtmlHelper.sanitize(inputString); } if (inputString && inputString.length > 0 && inputString[inputString.length - 1] === ' ') { inputString = inputString.slice(0, inputString.length - 1); } this.initiateSearch(inputString, isMobileSearch); }; TextSearch.prototype.handleSearchAfterTextCollectionReady = function (inputString, isMobileSearch) { if (isMobileSearch) { var searchCountElement = document.getElementById(this.pdfViewer.element.id + '_search_count'); this.searchCountEle = searchCountElement; } this.isSingleSearch = isMobileSearch ? isMobileSearch : this.isSingleSearch; if (inputString !== this.searchString || this.isLastOccurrenceCompleted) { this.calculateSearchCount(inputString); this.isInitialSearch = true; this.isLastOccurrenceCompleted = false; if (this.currentOccurrence === 0) { this.startSearchPageIndex = this.searchPageIndex; } } if (inputString !== this.searchString || this.searchPageIndex === null || this.startSearchPageIndex === null) { this.isTextSearch = false; this.searchPageIndex = this.pdfViewerBase.currentPageNumber - 1; this.startSearchPageIndex = this.searchPageIndex; } this.clearAllOccurrences(); if (inputString !== '' && !this.isMultiSearch && this.isSingleSearch && this.searchCount > 0) { if (this.searchMatches[this.searchPageIndex] && inputString === this.searchString) { if (this.searchMatches[this.searchPageIndex].length === 0) { this.initSearch(this.searchPageIndex, false); } else { this.nextSearch(); } } else if (isNullOrUndefined(this.searchMatches[this.searchPageIndex]) && inputString === this.searchString) { this.initSearch(this.searchPageIndex, false); } else { this.textSearch(inputString); } } if (inputString === '') { this.searchString = ''; } }; /** * @param {string} inputString - It describes about the input string * @param {boolean} isMobileSearch - Indicates is mobile search or not * @private * @returns {void} */ TextSearch.prototype.initiateSearch = function (inputString, isMobileSearch) { var _this = this; var pageCount = this.pdfViewerBase.pageCount; if (!isNullOrUndefined(this.intervalId)) { clearInterval(this.intervalId); } var checkDocumentTextCollection = function () { _this.showLoadingIndicator(true); if (_this.documentTextCollection.length === pageCount) { clearInterval(_this.intervalId); _this.isDocumentTextCollectionReady = true; _this.showLoadingIndicator(false); _this.intervalId = null; _this.handleSearchAfterTextCollectionReady(inputString, isMobileSearch); } if (isMobileSearch) { if (_this.pdfViewerBase.navigationPane) { _this.pdfViewerBase.navigationPane.setSearchInputWidth(); } } }; if (!this.isDocumentTextCollectionReady) { this.intervalId = setInterval(checkDocumentTextCollection, 1000); } else { this.handleSearchAfterTextCollectionReady(inputString, isMobileSearch); } }; TextSearch.prototype.textSearch = function (inputString) { if (inputString !== '' || inputString) { this.searchString = inputString; this.isTextSearch = true; this.isSearchText = true; this.searchPageIndex = this.pdfViewerBase.currentPageNumber - 1; this.isTextSearchEventTriggered = false; this.showLoadingIndicator(true); if (this.pdfViewer.isExtractText) { if (this.isTextRetrieved) { for (var i = 0; i < this.pdfViewerBase.pageCount; i++) { this.initSearch(i, false, true); } } else { this.isTextSearched = true; for (var i = 0; i < this.documentTextCollection.length; i++) { this.initSearch(i, false, true); } } } this.searchPageIndex = this.findNextPageWithText(this.searchPageIndex, true); this.initSearch(this.searchPageIndex, false); this.highlightOthers(); } }; TextSearch.prototype.nextSearch = function () { this.isPrevSearch = false; this.isTextSearch = true; this.isSearchText = false; var isCountIncreased = false; if (this.searchString) { this.clearAllOccurrences(); if (this.currentOccurrence !== 0) { this.searchIndex = this.searchIndex + 1; } else { this.searchIndex = 0; this.currentOccurrence += 1; isCountIncreased = true; this.getSearchCountText(); if (this.searchedOccurrences.indexOf(this.currentOccurrence) === -1) { this.searchedOccurrences.push(this.currentOccurrence); } if (this.searchedOccurrences.length === 1) { this.pdfViewer.fireTextSearchStart(this.searchString, this.isMatchCase); } } if (this.searchMatches[this.searchPageIndex]) { if (this.searchIndex >= this.searchMatches[this.searchPageIndex].length) { this.searchIndex = 0; this.searchPageIndex = this.findNextPageWithText(this.searchPageIndex); if (this.pdfViewerBase.pageCount > 1) { this.initSearch(this.searchPageIndex, false); } else { this.initSearch(this.searchPageIndex, true); if (!this.isMessagePopupOpened) { this.onMessageBoxOpen(); } this.pdfViewerBase.updateScrollTop(this.searchPageIndex); } this.showLoadingIndicator(true); } else { this.highlightSearchedTexts(this.searchPageIndex, false, undefined); var element_1 = this.pdfViewerBase.getElement('_searchtext_' + this.searchPageIndex + '_' + this.searchIndex); if (!isNullOrUndefined(element_1)) { var isScroll = this.isScrollPages(element_1); if (isScroll) { this.pdfViewerBase.updateScrollTop(this.searchPageIndex); } } this.showLoadingIndicator(false); } this.highlightOthers(true); if (this.areAllOccurencesSearched() && !this.isMessagePopupOpened) { this.onMessageBoxOpen(); } if (this.currentOccurrence <= this.searchCount && !this.isMessagePopupOpened) { if (this.currentOccurrence === this.searchCount) { this.currentOccurrence = 1; } else { if (!isCountIncreased) { this.currentOccurrence = (this.currentOccurrence + 1); } } if (this.searchCountEle && this.isSingleSearch) { this.getSearchCountText(); if (this.searchedOccurrences.indexOf(this.currentOccurrence) === -1) { this.searchedOccurrences.push(this.currentOccurrence); } this.adjustInputContainerWidth(); } } } else if (!this.searchMatches[this.searchPageIndex]) { if (this.pdfViewerBase.pageCount > 1) { this.initSearch(this.searchPageIndex, false); } } else { this.initiateTextSearch(this.searchInput.value); } } else { this.initiateTextSearch(this.searchInput.value); } var element = this.pdfViewerBase.getElement('_searchtext_' + this.searchPageIndex + '_' + this.searchIndex); if (!isNullOrUndefined(element)) { this.scrollToSearch(element); } }; TextSearch.prototype.findNextPageWithText = function (currentPageIndex, isInitialSearch) { if (isInitialSearch) { this.isInitialSearch = false; if (this.searchMatches[parseInt(currentPageIndex.toString(), 10)] && this.searchMatches[parseInt(currentPageIndex.toString(), 10)].length > 0) { return currentPageIndex; } } for (var i = 1; i < this.pdfViewerBase.pageCount; i++) { var nextPageIndex = (currentPageIndex + i) % this.pdfViewerBase.pageCount; if (this.searchMatches[parseInt(nextPageIndex.toString(), 10)] && this.searchMatches[parseInt(nextPageIndex.toString(), 10)].length > 0) { if (this.searchedOccurrences.length === this.searchCount && !isInitialSearch) { nextPageIndex = this.startSearchPageIndex; return nextPageIndex; } return nextPageIndex; } } return currentPageIndex; }; TextSearch.prototype.findFirstNonEmptyPage = function () { for (var j = 0; j < this.pdfViewerBase.pageCount; j++) { if (this.searchMatches[parseInt(j.toString(), 10)] && this.searchMatches[parseInt(j.toString(), 10)].length > 0) { return j; } } return null; }; TextSearch.prototype.prevSearch = function () { searchTextCollection.push(this.searchPageIndex); this.isPrevSearch = true; this.isTextSearch = true; this.isSearchText = false; if (this.searchString) { this.clearAllOccurrences(); this.searchIndex = this.searchIndex - 1; if (this.currentOccurrence === 0) { this.currentOccurrence = this.searchCount + 1; } if (this.searchIndex < 0) { this.searchPageIndex = this.findPreviousPageWithText(); this.initSearch(this.searchPageIndex, false); this.showLoadingIndicator(true); } else { this.highlightSearchedTexts(this.searchPageIndex, false, undefined); this.showLoadingIndicator(false); } this.highlightOthers(true); if (this.areAllOccurencesSearched() && !this.isMessagePopupOpened) { this.onMessageBoxOpen(); } if (this.currentOccurrence - 1 >= 0 && !this.isMessagePopupOpened) { if (this.currentOccurrence - 1 === 0) { this.currentOccurrence = this.searchCount; } else { this.currentOccurrence = this.currentOccurrence - 1; } if (this.searchCountEle && this.isSingleSearch) { this.getSearchCountText(); if (this.searchedOccurrences.indexOf(this.currentOccurrence) === -1) { this.searchedOccurrences.push(this.currentOccurrence); } if (this.searchedOccurrences.length === 1) { this.pdfViewer.fireTextSearchStart(this.searchString, this.isMatchCase); } this.adjustInputContainerWidth(); } } } else { this.searchIndex = this.searchIndex - 1; this.searchPageIndex = ((this.searchPageIndex - 1) < 0) ? (this.pdfViewerBase.pageCount - 1) : this.searchPageIndex - 1; var inputString = this.searchInput.value; this.textSearch(inputString); } var element = this.pdfViewerBase.getElement('_searchtext_' + this.searchPageIndex + '_' + this.searchIndex); if (!isNullOrUndefined(element)) { this.scrollToSearch(element); } }; TextSearch.prototype.scrollToSearch = function (element) { var scrollPoint = { y: -100, x: -100 }; var isScroll = this.isScrollPages(element); if (isScroll) { this.scrollToSearchStr(element, scrollPoint); } }; TextSearch.prototype.isScrollPages = function (childEle) { var parentRect = this.pdfViewer.element.getBoundingClientRect(); var childRect = childEle.getBoundingClientRect(); var toolbarHeight = (this.pdfViewer.enableToolbar && !isNullOrUndefined(this.pdfViewer.toolbarModule)) ? this.pdfViewer.toolbar.toolbarElement.getBoundingClientRect().height : 0; var isScroll = childRect.top >= (parentRect.top + toolbarHeight) && childRect.left >= parentRect.left && childRect.bottom <= parentRect.bottom && (childRect.bottom <= (window.innerHeight || document.documentElement.clientHeight)) && childRect.right <= parentRect.right; return !isScroll; }; TextSearch.prototype.findPreviousPageWithText = function () { var currentPageIndex = this.searchPageIndex; for (var i = 1; i < this.pdfViewerBase.pageCount; i++) { var prevPageIndex = (currentPageIndex - i + this.pdfViewerBase.pageCount) % this.pdfViewerBase.pageCount; if (this.searchMatches[parseInt(prevPageIndex.toString(), 10)] && this.searchMatches[parseInt(prevPageIndex.toString(), 10)].length > 0) { if (this.searchedOccurrences.length === this.searchCount) { prevPageIndex = 0; return prevPageIndex; } return prevPageIndex; } } return currentPageIndex; }; TextSearch.prototype.initSearch = function (pageIndex, isSinglePageSearch, isCount) { var storedData = this.pdfViewerBase.getStoredData(pageIndex, true); var pageText = null; var textContents = null; var characterBounds = null; if (isCount) { if (this.documentTextCollection.length !== 0) { var documentIndex = this. documentTextCollection[parseInt(pageIndex.toString(), 10)][parseInt(pageIndex.toString(), 10)]; var pageTextData = documentIndex.pageText ? documentIndex.pageText : documentIndex.PageText; if (this.documentTextCollection[parseInt(pageIndex.toString(), 10)] && documentIndex) { this.getSearchTextContent(pageIndex, this.searchString, pageTextData, textContents, isSinglePageSearch, this.documentTextCollection[parseInt(pageIndex.toString(), 10)]); } } } else { if (storedData) { pageText = storedData['pageText']; textContents = storedData['textContent']; characterBounds = this.pdfViewerBase.textLayer.characterBound[parseInt(pageIndex.toString(), 10)]; this.textContents[parseInt(pageIndex.toString(), 10)] = textContents; this.getPossibleMatches(pageIndex, this.searchString, pageText, textContents, isSinglePageSearch, characterBounds); this.getSearchCountText(); } else { if (!isSinglePageSearch && !isNullOrUndefined(pageIndex)) { this.createRequestForSearch(pageIndex); } } } }; TextSearch.prototype.getPossibleMatches = function (pageIndex, searchString, pageString, textContents, isSinglePageSearch, characterBounds) { var arrayReturns; var pageText = pageString; var searchText = searchString; var multiSearch = (pageText.replace((/(\s\r\n)/gm), ' ')).replace((/(\r\n)/gm), ' '); var Multiline = (pageString.replace((/(\s\r\n)/gm), ' ')).replace((/(\r\n)/gm), ' '); var specialCharcterSearch = multiSearch.replace(/[^a-zA-Z0-9]+/g, ' '); var queryLength = searchString.length; if (!this.isMatchCase) { searchText = searchString.toLowerCase(); pageText = pageString.toLowerCase(); multiSearch = multiSearch.toLowerCase(); Multiline = Multiline.toLowerCase(); specialCharcterSearch = specialCharcterSearch.toLowerCase(); } var matches = []; var matchedArray = []; var matchIndex = -queryLength; var newIndex = -queryLength; var mul