UNPKG

@syncfusion/ej2-pdfviewer

Version:
1,000 lines 159 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import { createElement, Browser, isNullOrUndefined, isBlazor, SanitizeHtmlHelper } from '@syncfusion/ej2-base'; import { CheckBox } from '@syncfusion/ej2-buttons'; import { AjaxHandler, ExtractTextOption } from '../index'; import { createSpinner, showSpinner, hideSpinner } from '../index'; import { Rect } from '@syncfusion/ej2-drawings'; import { AutoComplete } from '@syncfusion/ej2-dropdowns'; import { PdfViewerUtils, 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; /** * @private */ this.currentOccurrence = 0; this.searchIndex = 0; this.currentSearchIndex = 0; this.startIndex = null; /** * @private */ this.searchPageIndex = null; this.startSearchPageIndex = null; this.searchString = null; this.isMatchCase = false; this.isMultiSearch = false; this.isSingleSearch = false; this.findTextDocumentCollection = []; this.searchRequestHandler = null; this.textSearchHandleRequest = null; /** * @private */ this.isTextSearchHandled = false; this.textSearchOpen = false; /** * @private */ this.programaticalSearch = false; /** * @private */ this.isFiltering = false; this.textContents = []; /** * @private */ this.searchMatches = []; this.multiSearchCounts = {}; this.getSearchTextDetails = {}; 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.isDocumentTextCollectionReady) { _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.searchString = _this.searchInput.value; _this.nextSearch(); }; this.prevButtonOnClick = function (event) { _this.searchString = _this.searchInput.value; _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) { if (!_this.isDocumentTextCollectionReady) { _this.resetVariablesTextSearch(); _this.clearAllOccurrences(); } _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 = ''; if (_this.documentTextCollection.length === _this.pdfViewerBase.pageCount) { _this.isDocumentTextCollectionReady = true; } 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; if (!_this.isDocumentTextCollectionReady && _this.pdfViewerBase.clientSideRendering) { if (_this.searchInput.value !== '' && _this.searchInput.value !== _this.searchString) { _this.isTextSearchHandled = false; _this.searchCount = 0; _this.searchIndex = 0; _this.searchPageIndex = 0; _this.textSearchWhileLoading(_this.searchInput.value, _this.isMatchCase); _this.searchString = _this.searchInput.value; } if (_this.isTextSearchHandled) { _this.nextSearch(); } } else { _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); } } }; TextSearch.prototype.textSearchWhileLoading = function (searchWord, isMatchCase, startIndex, endIndex, isFirstResult, isCompleted) { var endPage = endIndex ? endIndex : 100; var isPagesCompleted = isCompleted ? isCompleted : false; if (endPage >= this.pdfViewerBase.pageCount) { endPage = this.pdfViewerBase.pageCount; isPagesCompleted = true; } if (!this.pdfViewerBase.clientSideRendering) { // eslint-disable-next-line var proxy_1 = this; var jsonObject = { text: searchWord, matchCase: isMatchCase, documentId: this.pdfViewerBase.getDocumentId(), hashId: this.pdfViewerBase.hashId, action: 'SearchTextPdf', elementId: this.pdfViewer.element.id, uniqueId: this.pdfViewerBase.documentId, startIndex: startIndex ? startIndex : 0, endIndex: endPage, isCompleted: isPagesCompleted, isRequestsend: !isNullOrUndefined(isFirstResult) ? isFirstResult : true }; this.textSearchHandleRequest = new AjaxHandler(this.pdfViewer); this.textSearchHandleRequest.url = this.pdfViewer.serviceUrl + '/' + 'SearchTextPdf'; this.textSearchHandleRequest.responseType = 'json'; this.textSearchHandleRequest.send(jsonObject); this.textSearchHandleRequest.onSuccess = function (result) { var data = result.data; if (data) { try { proxy_1.searchTextAfteresult(data.resultPages, data.totalSearchCount, data.searchWord, data.matchCase, (data.isRequestsend.toLowerCase() === 'true'), (data.isCompleted.toLowerCase() === 'true'), data.endIndex); } catch (error) { data = null; } } }; } if (this.pdfViewerBase.clientSideRendering) { this.pdfViewerBase.pdfViewerRunner.addTask({ message: 'searchText', zoomFactor: this.pdfViewerBase.getZoomFactor(), searchWord: searchWord, matchCase: isMatchCase, startIndex: startIndex ? startIndex : 0, endIndex: endPage, isCompleted: isPagesCompleted, isRequestsend: !isNullOrUndefined(isFirstResult) ? isFirstResult : false }, TaskPriorityLevel.Medium); } }; /** * @param {any} resultPages - Result of the text search word * @param {number} totalSearchCount - Search count of the word * @param {string} searchWord - Word that given for the text search * @param {boolean} matchCase - It gives about the match case * @param {boolean} isFirstResult - It gives first result from the request * @param {boolean} isCompleted - It gives the search complete indication * @param {number} endIndex - It describes end pageindex of the result * @private * @returns {void} */ TextSearch.prototype.searchTextAfteresult = function (resultPages, totalSearchCount, searchWord, matchCase, isFirstResult, isCompleted, endIndex) { if (totalSearchCount === 0 && this.searchCount === 0) { if (isCompleted) { this.isTextSearchHandled = true; this.pdfViewerBase.createNotificationPopup(this.pdfViewer.localeObj.getConstant('No Matches')); this.showLoadingIndicator(false); } } else { if (this.isSingleSearch && this.searchString === searchWord && this.isMatchCase === matchCase && (this.textSearchOpen || this.programaticalSearch)) { this.isTextSearchHandled = true; var details = this.getSearchTextDetails; this.getSearchTextDetails = __assign({}, details, resultPages); this.hightlightSearchedTexts(this.searchPageIndex, isFirstResult); if (isCompleted) { this.showLoadingIndicator(false); } else { this.showLoadingIndicator(true); } this.searchCount = this.searchCount + totalSearchCount; if (!isFirstResult) { this.currentOccurrence = 1; this.searchedOccurrences.push(this.currentOccurrence); if (this.searchedOccurrences.length === 1) { this.pdfViewer.fireTextSearchStart(this.searchString, this.isMatchCase); } } this.searchCountEle.style.display = 'inline-block'; this.getSearchCountText(); this.updateLoadingIndicator(); } } if (!isCompleted && this.searchString === searchWord && (this.textSearchOpen || this.programaticalSearch)) { this.textSearchWhileLoading(searchWord, matchCase, endIndex, endIndex + 100, (this.searchCount !== 0) ? true : false, isCompleted); } }; TextSearch.prototype.updateLoadingIndicator = function () { var loadingIndicator = document.getElementById(this.pdfViewer.element.id + '_textSearchLoadingIndicator'); loadingIndicator.style.left = this.searchInputContainer.clientWidth - 46 + 'px'; }; /** * @param {number} pageNumber - It decribes the search pageIndex value * @param {boolean} isPageChange - It describes the first result highlight * @param {boolean} isSearchCompleted - It describes the text search has been completed or not. * @private * @returns {void} */ TextSearch.prototype.hightlightSearchedTexts = function (pageNumber, isPageChange, isSearchCompleted) { this.clearAllOccurrences(); var elementIdCount; var keys = []; for (var key in this.getSearchTextDetails) { if (Object.prototype.hasOwnProperty.call(this.getSearchTextDetails, key)) { keys.push(parseInt(key, 10)); } } keys.sort(function (a, b) { return a - b; }); if (!isNullOrUndefined(pageNumber)) { var previous = keys[keys.length - 1]; var next = keys[0]; for (var i = 0; i < keys.length; i++) { if (keys[parseInt(i.toString(), 10)] < pageNumber) { previous = keys[parseInt(i.toString(), 10)]; } else if (keys[parseInt(i.toString(), 10)] > pageNumber) { next = keys[parseInt(i.toString(), 10)]; break; } } if (!this.getSearchTextDetails[parseInt(pageNumber.toString(), 10)]) { pageNumber = next; } if ((this.searchIndex + 1) > this.getSearchTextDetails[parseInt(pageNumber.toString(), 10)].pageOccurrence || this.searchIndex < 0) { if ((this.searchIndex + 1) > this.getSearchTextDetails[parseInt(pageNumber.toString(), 10)].pageOccurrence) { pageNumber = ((pageNumber + 1) === keys[keys.length - 1]) ? 0 : next; this.searchIndex = 0; } if (this.searchIndex < 0) { pageNumber = ((pageNumber - 1) === -1) ? keys[keys.length - 1] : previous; this.searchIndex = (this.getSearchTextDetails[parseInt(pageNumber.toString(), 10)].pageOccurrence - 1); } } } for (var key in this.getSearchTextDetails) { if (Object.prototype.hasOwnProperty.call(this.getSearchTextDetails, key)) { var value = this.getSearchTextDetails[parseInt(key.toString(), 10)]; var idSearchIndexCount = 0; // eslint-disable-next-line guard-for-in for (var boundsKey in value.Bounds) { if (Object.prototype.hasOwnProperty.call(value.Bounds, boundsKey)) { var bounds = value.Bounds[parseInt(boundsKey.toString(), 10)]; for (var i = 0; i < bounds.length; i++) { var leftValue = (this.pdfViewerBase.clientSideRendering === true) ? bounds[parseInt(i.toString(), 10)].Left : this.pdfViewerBase.ConvertPointToPixel(bounds[parseInt(i.toString(), 10)].Left); var topValue = (this.pdfViewerBase.clientSideRendering === true) ? bounds[parseInt(i.toString(), 10)].Top : this.pdfViewerBase.ConvertPointToPixel(bounds[parseInt(i.toString(), 10)].Top); var heightValue = (this.pdfViewerBase.clientSideRendering === true) ? bounds[parseInt(i.toString(), 10)].Height : this.pdfViewerBase.ConvertPointToPixel(bounds[parseInt(i.toString(), 10)].Height); var widthValue = (this.pdfViewerBase.clientSideRendering === true) ? bounds[parseInt(i.toString(), 10)].Width : this.pdfViewerBase.ConvertPointToPixel(bounds[parseInt(i.toString(), 10)].Width); var pageIndex = null; if (isNullOrUndefined(isSearchCompleted)) { pageIndex = pageNumber ? pageNumber : keys[0]; } else { if (isSearchCompleted) { pageIndex = null; } } if ((this.searchIndex === idSearchIndexCount) && pageIndex === Number(key)) { elementIdCount = idSearchIndexCount; if ((!isNullOrUndefined(isPageChange) && !isPageChange) || isNullOrUndefined(isPageChange)) { this.pdfViewerBase.updateScrollTop(Number(key)); } var idString = '_searchtext_' + Number(key) + '_' + idSearchIndexCount; var element = void 0; if (bounds.length <= 1 || i === 0) { element = document.getElementById(this.pdfViewer.element.id + idString); } else { element = document.getElementById(this.pdfViewer.element.id + idString + '_' + i); } if (element) { element.parentElement.removeChild(element); } this.searchPageIndex = Number(key); this.createSearchTextDiv(idSearchIndexCount, Number(key), heightValue, widthValue, topValue, leftValue, 'e-pv-search-text-highlight', false, 0, i); } else { this.createSearchTextDiv(idSearchIndexCount, Number(key), heightValue, widthValue, topValue, leftValue, 'e-pv-search-text-highlightother', false, 0, i); } } } idSearchIndexCount++; } } } if ((!isNullOrUndefined(isPageChange) && !isPageChange) || isNullOrUndefined(isPageChange)) { var element = this.pdfViewerBase.getElement('_searchtext_' + this.searchPageIndex + '_' + elementIdCount); var scrollPoint = { y: -100, x: -100 }; this.scrollToSearchStr(element, scrollPoint); } }; /** * @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'; this.textSearchOpen = true; } else { this.searchBox.style.display = 'none'; this.searchInput.value = ''; this.searchCountEle.style.display = 'none'; if (this.pdfViewer.toolbarModule) { this.pdfViewer.toolbarModule.isTextSearchBoxDisplayed = false; } this.textSearchOpen = 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) { if (this.currentOccurrence !== 0) { this.initSearch(this.searchPageIndex, true); this.highlightOthers(); } } }; TextSearch.prototype.calculateSearchCount = function (inputString, documentTextCollection) { var _this = this; this.searchCount = 0; if (!this.isTextSearchHandled) { 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 < documentTextCollection.length; i++) { var pageIndex = parseInt(Object.keys(documentTextCollection[parseInt(i.toString(), 10)])[0], 10); var documentIndex = documentTextCollection[parseInt(i.toString(), 10)][parseInt(pageIndex.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 < documentTextCollection.length; i++) { var pageIndex = parseInt(Object.keys(documentTextCollection[parseInt(i.toString(), 10)])[0], 10); var documentIndex = documentTextCollection[parseInt(i.toString(), 10)][parseInt(pageIndex.toString(), 10)]; var pageTextData = documentIndex.pageText ? documentIndex.pageText : documentIndex.PageText; 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) { if (!this.isTextSearchHandled) { this.searchedOccurrences.push(this.currentOccurrence + 1); } } this.adjustInputContainerWidth(); } if (this.searchedOccurrences.length === 1) { this.pdfViewer.fireTextSearchStart(this.searchString, this.isMatchCase); } if (!this.isTextSearchHandled) { 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.isTextSearchHandled = false; this.calculateSearchCount(inputString, this.documentTextCollection); 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; if (!this.isTextSearchHandled) { this.searchPageIndex = this.pdfViewerBase.currentPageNumber - 1; this.startSearchPageIndex = this.searchPageIndex; } } if (!this.isTextSearchHandled) { 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