all files / pdfviewer/bookmark-view/ bookmark-view.js

8.53% Statements 22/258
0% Branches 0/190
7.14% Functions 2/28
8.53% Lines 22/258
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 4101×   1× 1× 1×                                                                                                                                                                             1×                   1×         1×                                                                       1×                                                                                         1×                                                                                                     1×                                           1×                                                               1×                               1×                   1×                               1×               1×         1×                                           1×                                                           1×     1×     1×   1×    
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-navigations", "@syncfusion/ej2-lists", "../index"], function (require, exports, ej2_base_1, ej2_navigations_1, ej2_lists_1, index_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var BookmarkView = (function () {
        function BookmarkView(pdfViewer, pdfViewerBase) {
            var _this = this;
            this.isKeyboardNavigation = false;
            this.childNavigateCount = 0;
            this.bookmarkClick = function (args) {
                if (!args.event.target.classList.contains('e-icons')) {
                    var bookid = args.data.Id;
                    _this.childNavigateCount = 0;
                    _this.pdfViewerBase.navigationPane.goBackToToolbar();
                    var selectedItem = _this.bookmarkList.getSelectedItems();
                    _this.navigateToBookmark(bookid, args.text, selectedItem.data.FileName);
                }
                else {
                    _this.childNavigateCount++;
                }
                return false;
            };
            this.nodeClick = function (args) {
                _this.setHeight(args.node);
                var data = _this.treeObj.getTreeData(args.node);
                var bookid = Number(data[0].Id);
                _this.navigateToBookmark(bookid, args.node.textContent, data[0].FileName);
                if (_this.pdfViewer.annotationModule && _this.pdfViewer.annotationModule.inkAnnotationModule) {
                    var currentPageNumber = parseInt(_this.pdfViewer.annotationModule.inkAnnotationModule.currentPageNumber, 10);
                    _this.pdfViewer.annotationModule.inkAnnotationModule.drawInkAnnotation(currentPageNumber);
                }
                return false;
            };
            this.bookmarkKeypress = function (args) {
                if (args.event && args.event.pointerType !== 'mouse' && args.event.pointerType !== 'touch' && (args.event.key === 'Enter' || args.event.key === ' ')) {
                    _this.isKeyboardNavigation = true;
                    _this.nodeClick(args);
                    _this.isKeyboardNavigation = false;
                }
            };
            this.bookmarkPanelBeforeOpen = function (args) {
                if (_this.pdfViewer.enableBookmarkStyles) {
                    for (var k = 0; k < _this.bookmarkStyles.length; k++) {
                        if ((args.text.trim()) === (_this.bookmarkStyles[parseInt(k.toString(), 10)].Text.trim())) {
                            var element = args.node.querySelector(':scope > div.e-text-content');
                            if (element) {
                                var fontStyle = _this.bookmarkStyles[parseInt(k.toString(), 10)].FontStyle.split(',');
                                for (var n = 0; n < fontStyle.length; n++) {
                                    switch (fontStyle[parseInt(n.toString(), 10)].trim()) {
                                        case 'Italic':
                                            element.style.fontStyle = 'italic';
                                            break;
                                        case 'Bold':
                                            element.style.fontWeight = 'Bold';
                                    }
                                }
                                var currentElement = element.getElementsByClassName('e-pv-bookmark-title')[0];
                                if (currentElement) {
                                    currentElement.style.color = _this.bookmarkStyles[parseInt(k.toString(), 10)].Color;
                                }
                                else {
                                    element.children[0].style.color = _this.bookmarkStyles[parseInt(k.toString(), 10)].Color;
                                }
                            }
                            break;
                        }
                    }
                }
                else {
                    var element = args.node.querySelector(':scope > div.e-text-content');
                    if (element) {
                        if (element.style.fontStyle) {
                            element.style.removeProperty('font-style');
                        }
                        if (element.style.fontWeight) {
                            element.style.removeProperty('font-weight');
                        }
                        var currentElement = element.getElementsByClassName('e-pv-bookmark-title')[0];
                        if (currentElement && currentElement.style.color) {
                            currentElement.style.removeProperty('color');
                        }
                        else {
                            if (element.children.length > 0 && element.children[0].style.color) {
                                element.children[0].style.removeProperty('color');
                            }
                        }
                    }
                }
            };
            this.pdfViewer = pdfViewer;
            this.pdfViewerBase = pdfViewerBase;
        }
        BookmarkView.prototype.openBookmarkPane = function () {
            if (this.pdfViewerBase.navigationPane && this.bookmarks) {
                if (ej2_base_1.Browser.isDevice && !this.pdfViewer.enableDesktopMode) {
                    this.pdfViewerBase.navigationPane.showBookmarksPaneMobile();
                }
                else {
                    this.pdfViewerBase.navigationPane.openBookmarkcontentInitially();
                }
            }
        };
        BookmarkView.prototype.closeBookmarkPane = function () {
            if (this.pdfViewerBase.navigationPane) {
                this.pdfViewerBase.navigationPane.closeBookmarkPane(true);
            }
        };
        BookmarkView.prototype.createRequestForBookmarks = function () {
            var proxy = this;
            var jsonObject = { hashId: this.pdfViewerBase.hashId, action: 'Bookmarks', elementId: this.pdfViewer.element.id, uniqueId: this.pdfViewerBase.documentId };
            if (this.pdfViewerBase.jsonDocumentId) {
                jsonObject.documentId = this.pdfViewerBase.jsonDocumentId;
            }
            jsonObject.bookmarkStyles = true;
            this.bookmarkRequestHandler = new index_1.AjaxHandler(this.pdfViewer);
            this.bookmarkRequestHandler.url = proxy.pdfViewer.serviceUrl + '/Bookmarks';
            this.bookmarkRequestHandler.responseType = 'json';
            if (this.pdfViewerBase.clientSideRendering) {
                var data = this.pdfViewer.pdfRendererModule.getBookmarks(jsonObject);
                this.renderBookmarksOnSuccess(data, proxy);
            }
            else {
                this.pdfViewerBase.requestCollection.push(this.bookmarkRequestHandler);
                this.bookmarkRequestHandler.send(jsonObject);
                this.bookmarkRequestHandler.onSuccess = function (result) {
                    if (proxy.pdfViewerBase.navigationPane) {
                        proxy.pdfViewerBase.navigationPane.disableBookmarkButton();
                    }
                    var data = result.data;
                    var redirect = proxy.pdfViewerBase.checkRedirection(data);
                    if (!redirect) {
                        proxy.renderBookmarksOnSuccess(data, proxy);
                    }
                };
                this.bookmarkRequestHandler.onFailure = function (result) {
                    proxy.pdfViewer.fireAjaxRequestFailed(result.status, result.statusText, 'Bookmarks');
                };
                this.bookmarkRequestHandler.onError = function (result) {
                    proxy.pdfViewerBase.openNotificationPopup();
                    proxy.pdfViewer.fireAjaxRequestFailed(result.status, result.statusText, 'Bookmarks');
                };
            }
        };
        BookmarkView.prototype.renderBookmarksOnSuccess = function (data, proxy) {
            if (data) {
                if (typeof data !== 'object') {
                    try {
                        data = JSON.parse(data);
                    }
                    catch (error) {
                        proxy.pdfViewerBase.onControlError(500, data, 'Bookmarks');
                        data = null;
                    }
                }
                if (data && data.uniqueId === proxy.pdfViewerBase.documentId) {
                    proxy.pdfViewer.fireAjaxRequestSuccess('Bookmarks', data);
                    proxy.bookmarks = { bookMark: data.Bookmarks };
                    proxy.bookmarkStyles = data.Bookmarkstyles;
                    proxy.bookmarksDestination = { bookMarkDestination: data.BookmarksDestination };
                    if (ej2_base_1.isBlazor()) {
                        var bookmarkCollection = { bookmarks: proxy.bookmarks, bookmarksDestination: proxy.bookmarksDestination };
                        if (proxy.pdfViewer && proxy.pdfViewer._dotnetInstance) {
                            proxy.pdfViewer._dotnetInstance.invokeMethodAsync('UpdateBookmarkCollection', bookmarkCollection);
                        }
                    }
                }
            }
            if (proxy.pdfViewerBase.navigationPane) {
                if (proxy.bookmarks == null) {
                    proxy.pdfViewerBase.navigationPane.disableBookmarkButton();
                    if (ej2_base_1.isBlazor() && proxy.pdfViewer._dotnetInstance) {
                        proxy.pdfViewer._dotnetInstance.invokeMethodAsync('UpdateBookmarkCollection', null);
                    }
                }
                else {
                    proxy.pdfViewerBase.navigationPane.enableBookmarkButton();
                    proxy.isBookmarkViewDiv = false;
                    if (proxy.pdfViewer.isBookmarkPanelOpen) {
                        if (ej2_base_1.Browser.isDevice && !proxy.pdfViewer.enableDesktopMode) {
                            proxy.pdfViewerBase.navigationPane.showBookmarksPaneMobile();
                        }
                        else {
                            proxy.pdfViewerBase.navigationPane.openBookmarkcontentInitially();
                        }
                    }
                }
            }
        };
        BookmarkView.prototype.renderBookmarkcontent = function () {
            var _this = this;
            if (!this.isBookmarkViewDiv) {
                var isblazor = ej2_base_1.isBlazor();
                this.bookmarkView = isblazor ? this.pdfViewer.element.querySelector('.e-pv-bookmark-view') : ej2_base_1.createElement('div', { id: this.pdfViewer.element.id + '_bookmark_view', className: 'e-pv-bookmark-view' });
                this.pdfViewerBase.navigationPane.sideBarContent.appendChild(this.bookmarkView);
                var bookmarkIconView_1 = ej2_base_1.createElement('div', { id: this.pdfViewer.element.id + '_bookmark_iconview', className: 'e-pv-bookmark-icon-view' });
                if (!this.pdfViewer.enableRtl) {
                    var bookmarkIcon = ej2_base_1.createElement('span', { id: this.pdfViewer.element.id + '_bookmark_icon', className: 'e-pv-bookmark-icon e-pv-icon' });
                    bookmarkIconView_1.appendChild(bookmarkIcon);
                }
                else {
                    var bookmarkIcon = ej2_base_1.createElement('span', { id: this.pdfViewer.element.id + '_bookmark_icon', className: 'e-pv-bookmark-icon e-pv-icon e-right' });
                    bookmarkIconView_1.appendChild(bookmarkIcon);
                }
                var bookmarkTitle = ej2_base_1.createElement('div', { id: this.pdfViewer.element.id + '_bookmark_Title', className: 'e-pv-bookmark-title' });
                if (this.pdfViewer.enableRtl) {
                    bookmarkTitle.style.paddingRight = 26 + 'px';
                }
                bookmarkTitle.innerText = '${Title}';
                bookmarkIconView_1.appendChild(bookmarkTitle);
                if (!isblazor) {
                    this.treeObj = new ej2_navigations_1.TreeView({
                        fields: {
                            dataSource: this.bookmarks.bookMark,
                            id: 'Id',
                            text: 'Title',
                            child: 'Child',
                            hasChildren: 'HasChild'
                        },
                        nodeTemplate: ej2_base_1.initializeCSPTemplate(function (data) { return bookmarkIconView_1.outerHTML.replace('${Title}', data.Title); }),
                        nodeClicked: this.nodeClick.bind(this),
                        keyPress: this.bookmarkKeypress.bind(this),
                        drawNode: this.bookmarkPanelBeforeOpen.bind(this)
                    });
                    this.treeObj.isStringTemplate = true;
                    if (this.pdfViewer.enableRtl) {
                        this.treeObj.enableRtl = true;
                    }
                    this.treeObj.appendTo(this.bookmarkView);
                }
                var event_1 = ['mouseover', 'keydown'];
                for (var m = 0; m < event_1.length; m++) {
                    this.bookmarkView.addEventListener(event_1[parseInt(m.toString(), 10)], function (event) {
                        _this.setHeight(event.target);
                    });
                }
                this.isBookmarkViewDiv = true;
            }
            this.bookmarkView.style.display = 'block';
        };
        BookmarkView.prototype.renderBookmarkContentMobile = function () {
            if (this.bookmarkView != null) {
                this.bookmarkView.remove();
            }
            this.bookmarkView = ej2_base_1.createElement('div', { id: this.pdfViewer.element.id + '_bookmark_view', className: 'e-pv-bookmark-view' });
            this.pdfViewerBase.getElement('_bookmarks_container').appendChild(this.bookmarkView);
            this.bookmarkList = new ej2_lists_1.ListView({
                dataSource: this.bookmarks.bookMark,
                fields: {
                    id: 'Id',
                    text: 'Title',
                    child: 'Child'
                },
                showHeader: false,
                select: this.bookmarkClick.bind(this)
            });
            this.bookmarkList.isStringTemplate = true;
            if (this.pdfViewer.enableRtl) {
                this.bookmarkList.enableRtl = true;
            }
            this.bookmarkList.appendTo(this.bookmarkView);
        };
        BookmarkView.prototype.setHeight = function (element) {
            if (this.treeObj) {
                if (this.treeObj.fullRowSelect && element.classList) {
                    if (element.classList.contains('e-treeview') && element.classList.contains('.e-active')) {
                        element = element.querySelector('.e-active').querySelector('.e-fullrow');
                    }
                    else if (element.classList.contains('e-treeview')) {
                        element = element.querySelector('.e-fullrow');
                    }
                    else if (element.classList.contains('e-list-parent')) {
                        element = element.querySelector('.e-fullrow');
                    }
                    else if (element.classList.value !== ('e-fullrow')) {
                        if (element.closest && element.closest('.e-list-item')) {
                            element = element.closest('.e-list-item').querySelector('.e-fullrow');
                        }
                        else {
                            if (element.classList.contains('e-list-item')) {
                                element = element.querySelector('.e-fullrow');
                            }
                            else if (element.classList.contains('e-icons') && element.classList.contains('interaction')
                                && element.parentElement.parentElement.classList.contains('e-list-item')) {
                                element = element.parentElement.parentElement.querySelector('.e-fullrow');
                            }
                        }
                    }
                    if (element.nextElementSibling) {
                        element.style.height = element.nextElementSibling.offsetHeight + 'px';
                    }
                }
            }
        };
        BookmarkView.prototype.setBookmarkContentHeight = function () {
            if (this.treeObj) {
                var element = this.treeObj.element;
                if (this.treeObj.fullRowSelect) {
                    if (element && element.classList.contains('e-treeview') && element.classList.contains('.e-active')) {
                        element = element.querySelector('.e-active').querySelector('.e-fullrow');
                    }
                    else if (element && element.classList.contains('e-treeview')) {
                        element = element.querySelector('.e-fullrow');
                    }
                    if (element && element.nextElementSibling) {
                        element.style.height = element.nextElementSibling.offsetHeight + 'px';
                    }
                }
            }
        };
        BookmarkView.prototype.handleBookmarkStyles = function () {
            var _a;
            var bookmarkNodes = Array.from(this.treeObj.getRootElement().querySelectorAll('li.e-list-item'));
            for (var _i = 0, bookmarkNodes_1 = bookmarkNodes; _i < bookmarkNodes_1.length; _i++) {
                var node = bookmarkNodes_1[_i];
                var nodeId = node.dataset['uid'];
                var nodeData = this.findBookmarkNodeData(this.bookmarks.bookMark, nodeId);
                this.bookmarkPanelBeforeOpen({ node: node, nodeData: (_a = {}, _a[nodeId] = nodeData, _a), text: nodeData.Title });
            }
        };
        BookmarkView.prototype.findBookmarkNodeData = function (nodes, targetId) {
            if (ej2_base_1.isNullOrUndefined(nodes)) {
                return null;
            }
            for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
                var node = nodes_1[_i];
                if (String(node.Id) === targetId) {
                    return node;
                }
                var foundInChild = this.findBookmarkNodeData(node.Child, targetId);
                if (foundInChild) {
                    return foundInChild;
                }
            }
            return null;
        };
        BookmarkView.prototype.navigateToBookmark = function (bookid, text, fileName) {
            var pageIndex = this.bookmarksDestination.bookMarkDestination[parseInt(bookid.toString(), 10)].PageIndex;
            var Y = this.bookmarksDestination.bookMarkDestination[parseInt(bookid.toString(), 10)].Y;
            if (pageIndex !== -1) {
                this.goToBookmark(pageIndex, Y);
            }
            this.pdfViewer.fireBookmarkClick(pageIndex !== -1 ? pageIndex + 1 : pageIndex, Y, text, fileName);
        };
        BookmarkView.prototype.getBookmarks = function () {
            if (this.bookmarks && this.bookmarksDestination) {
                return { bookmarks: this.bookmarks, bookmarksDestination: this.bookmarksDestination };
            }
        };
        BookmarkView.prototype.goToBookmark = function (pageIndex, y) {
            var proxy = this;
            var destPage = (this.pdfViewerBase.pageSize[parseInt(pageIndex.toString(), 10)].height);
            var scrollValue;
            if (y === 0) {
                scrollValue = this.pdfViewerBase.pageSize[parseInt(pageIndex.toString(), 10)].top * this.pdfViewerBase.getZoomFactor();
            }
            else {
                scrollValue = this.pdfViewerBase.pageSize[parseInt(pageIndex.toString(), 10)].top *
                    this.pdfViewerBase.getZoomFactor() + ((destPage - y) * this.pdfViewerBase.getZoomFactor());
            }
            var scroll = scrollValue.toString();
            proxy.pdfViewerBase.viewerContainer.scrollTop = parseInt(scroll, 10);
            if (ej2_base_1.Browser.isDevice && !this.pdfViewer.enableDesktopMode) {
                this.pdfViewerBase.mobileScrollerContainer.style.display = '';
                this.pdfViewerBase.updateMobileScrollerPosition();
            }
            if (pageIndex > 0 && pageIndex <= this.pdfViewerBase.pageCount && this.pdfViewerBase.currentPageNumber !== pageIndex + 1) {
                this.pdfViewerBase.updateScrollTop(pageIndex, false);
            }
            return false;
        };
        BookmarkView.prototype.clear = function () {
            this.closeBookmarkPane();
            if (!ej2_base_1.Browser.isDevice || this.pdfViewer.enableDesktopMode) {
                if (this.pdfViewer.thumbnailView) {
                    this.pdfViewer.thumbnailView.closeThumbnailPane();
                }
            }
            if (this.pdfViewerBase.navigationPane) {
                this.pdfViewerBase.navigationPane.disableBookmarkButton();
                this.pdfViewerBase.navigationPane.updateViewerContainerOnClose();
            }
            if (this.bookmarks) {
                this.bookmarks.bookMark = [];
                this.bookmarks = null;
            }
            if (this.bookmarksDestination) {
                this.bookmarksDestination.bookMarkDestination = [];
            }
            if (this.bookmarkView != null && !ej2_base_1.isBlazor()) {
                if (this.bookmarkView.parentElement !== null) {
                    this.bookmarkView.parentElement.removeChild(this.bookmarkView);
                }
                while (this.bookmarkView.hasChildNodes()) {
                    this.bookmarkView.removeChild(this.bookmarkView.lastChild);
                }
            }
            if (this.bookmarkRequestHandler) {
                this.bookmarkRequestHandler.clear();
            }
        };
        BookmarkView.prototype.destroy = function () {
            this.clear();
        };
        BookmarkView.prototype.getModuleName = function () {
            return 'BookmarkView';
        };
        return BookmarkView;
    }());
    exports.BookmarkView = BookmarkView;
});