1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | 'use strict';
|
23 |
|
24 | Object.defineProperty(exports, "__esModule", {
|
25 | value: true
|
26 | });
|
27 | exports.PDFOutlineViewer = undefined;
|
28 |
|
29 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
30 |
|
31 | var _pdf = require('../pdf');
|
32 |
|
33 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
34 |
|
35 | var DEFAULT_TITLE = '\u2013';
|
36 |
|
37 | var PDFOutlineViewer = function () {
|
38 | function PDFOutlineViewer(_ref) {
|
39 | var container = _ref.container,
|
40 | linkService = _ref.linkService,
|
41 | eventBus = _ref.eventBus;
|
42 |
|
43 | _classCallCheck(this, PDFOutlineViewer);
|
44 |
|
45 | this.container = container;
|
46 | this.linkService = linkService;
|
47 | this.eventBus = eventBus;
|
48 | this.reset();
|
49 | }
|
50 |
|
51 | _createClass(PDFOutlineViewer, [{
|
52 | key: 'reset',
|
53 | value: function reset() {
|
54 | this.outline = null;
|
55 | this.lastToggleIsShow = true;
|
56 | this.container.textContent = '';
|
57 | this.container.classList.remove('outlineWithDeepNesting');
|
58 | }
|
59 | }, {
|
60 | key: '_dispatchEvent',
|
61 | value: function _dispatchEvent(outlineCount) {
|
62 | this.eventBus.dispatch('outlineloaded', {
|
63 | source: this,
|
64 | outlineCount: outlineCount
|
65 | });
|
66 | }
|
67 | }, {
|
68 | key: '_bindLink',
|
69 | value: function _bindLink(element, _ref2) {
|
70 | var url = _ref2.url,
|
71 | newWindow = _ref2.newWindow,
|
72 | dest = _ref2.dest;
|
73 | var linkService = this.linkService;
|
74 |
|
75 | if (url) {
|
76 | (0, _pdf.addLinkAttributes)(element, {
|
77 | url: url,
|
78 | target: newWindow ? _pdf.LinkTarget.BLANK : linkService.externalLinkTarget,
|
79 | rel: linkService.externalLinkRel
|
80 | });
|
81 | return;
|
82 | }
|
83 | element.href = linkService.getDestinationHash(dest);
|
84 | element.onclick = function () {
|
85 | if (dest) {
|
86 | linkService.navigateTo(dest);
|
87 | }
|
88 | return false;
|
89 | };
|
90 | }
|
91 | }, {
|
92 | key: '_setStyles',
|
93 | value: function _setStyles(element, _ref3) {
|
94 | var bold = _ref3.bold,
|
95 | italic = _ref3.italic;
|
96 |
|
97 | var styleStr = '';
|
98 | if (bold) {
|
99 | styleStr += 'font-weight: bold;';
|
100 | }
|
101 | if (italic) {
|
102 | styleStr += 'font-style: italic;';
|
103 | }
|
104 | if (styleStr) {
|
105 | element.setAttribute('style', styleStr);
|
106 | }
|
107 | }
|
108 | }, {
|
109 | key: '_addToggleButton',
|
110 | value: function _addToggleButton(div) {
|
111 | var _this = this;
|
112 |
|
113 | var toggler = document.createElement('div');
|
114 | toggler.className = 'outlineItemToggler';
|
115 | toggler.onclick = function (evt) {
|
116 | evt.stopPropagation();
|
117 | toggler.classList.toggle('outlineItemsHidden');
|
118 | if (evt.shiftKey) {
|
119 | var shouldShowAll = !toggler.classList.contains('outlineItemsHidden');
|
120 | _this._toggleOutlineItem(div, shouldShowAll);
|
121 | }
|
122 | };
|
123 | div.insertBefore(toggler, div.firstChild);
|
124 | }
|
125 | }, {
|
126 | key: '_toggleOutlineItem',
|
127 | value: function _toggleOutlineItem(root, show) {
|
128 | this.lastToggleIsShow = show;
|
129 | var togglers = root.querySelectorAll('.outlineItemToggler');
|
130 | for (var i = 0, ii = togglers.length; i < ii; ++i) {
|
131 | togglers[i].classList[show ? 'remove' : 'add']('outlineItemsHidden');
|
132 | }
|
133 | }
|
134 | }, {
|
135 | key: 'toggleOutlineTree',
|
136 | value: function toggleOutlineTree() {
|
137 | if (!this.outline) {
|
138 | return;
|
139 | }
|
140 | this._toggleOutlineItem(this.container, !this.lastToggleIsShow);
|
141 | }
|
142 | }, {
|
143 | key: 'render',
|
144 | value: function render(_ref4) {
|
145 | var outline = _ref4.outline;
|
146 |
|
147 | var outlineCount = 0;
|
148 | if (this.outline) {
|
149 | this.reset();
|
150 | }
|
151 | this.outline = outline || null;
|
152 | if (!outline) {
|
153 | this._dispatchEvent(outlineCount);
|
154 | return;
|
155 | }
|
156 | var fragment = document.createDocumentFragment();
|
157 | var queue = [{
|
158 | parent: fragment,
|
159 | items: this.outline
|
160 | }];
|
161 | var hasAnyNesting = false;
|
162 | while (queue.length > 0) {
|
163 | var levelData = queue.shift();
|
164 | for (var i = 0, len = levelData.items.length; i < len; i++) {
|
165 | var item = levelData.items[i];
|
166 | var div = document.createElement('div');
|
167 | div.className = 'outlineItem';
|
168 | var element = document.createElement('a');
|
169 | this._bindLink(element, item);
|
170 | this._setStyles(element, item);
|
171 | element.textContent = (0, _pdf.removeNullCharacters)(item.title) || DEFAULT_TITLE;
|
172 | div.appendChild(element);
|
173 | if (item.items.length > 0) {
|
174 | hasAnyNesting = true;
|
175 | this._addToggleButton(div);
|
176 | var itemsDiv = document.createElement('div');
|
177 | itemsDiv.className = 'outlineItems';
|
178 | div.appendChild(itemsDiv);
|
179 | queue.push({
|
180 | parent: itemsDiv,
|
181 | items: item.items
|
182 | });
|
183 | }
|
184 | levelData.parent.appendChild(div);
|
185 | outlineCount++;
|
186 | }
|
187 | }
|
188 | if (hasAnyNesting) {
|
189 | this.container.classList.add('outlineWithDeepNesting');
|
190 | }
|
191 | this.container.appendChild(fragment);
|
192 | this._dispatchEvent(outlineCount);
|
193 | }
|
194 | }]);
|
195 |
|
196 | return PDFOutlineViewer;
|
197 | }();
|
198 |
|
199 | exports.PDFOutlineViewer = PDFOutlineViewer; |
\ | No newline at end of file |