annotation_layer.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.AnnotationLayer = undefined;
  20. 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; }; }();
  21. var _dom_utils = require('./dom_utils');
  22. var _util = require('../shared/util');
  23. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  24. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  25. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  26. var AnnotationElementFactory = function () {
  27. function AnnotationElementFactory() {
  28. _classCallCheck(this, AnnotationElementFactory);
  29. }
  30. _createClass(AnnotationElementFactory, null, [{
  31. key: 'create',
  32. value: function create(parameters) {
  33. var subtype = parameters.data.annotationType;
  34. switch (subtype) {
  35. case _util.AnnotationType.LINK:
  36. return new LinkAnnotationElement(parameters);
  37. case _util.AnnotationType.TEXT:
  38. return new TextAnnotationElement(parameters);
  39. case _util.AnnotationType.WIDGET:
  40. var fieldType = parameters.data.fieldType;
  41. switch (fieldType) {
  42. case 'Tx':
  43. return new TextWidgetAnnotationElement(parameters);
  44. case 'Btn':
  45. if (parameters.data.radioButton) {
  46. return new RadioButtonWidgetAnnotationElement(parameters);
  47. } else if (parameters.data.checkBox) {
  48. return new CheckboxWidgetAnnotationElement(parameters);
  49. }
  50. (0, _util.warn)('Unimplemented button widget annotation: pushbutton');
  51. break;
  52. case 'Ch':
  53. return new ChoiceWidgetAnnotationElement(parameters);
  54. }
  55. return new WidgetAnnotationElement(parameters);
  56. case _util.AnnotationType.POPUP:
  57. return new PopupAnnotationElement(parameters);
  58. case _util.AnnotationType.LINE:
  59. return new LineAnnotationElement(parameters);
  60. case _util.AnnotationType.HIGHLIGHT:
  61. return new HighlightAnnotationElement(parameters);
  62. case _util.AnnotationType.UNDERLINE:
  63. return new UnderlineAnnotationElement(parameters);
  64. case _util.AnnotationType.SQUIGGLY:
  65. return new SquigglyAnnotationElement(parameters);
  66. case _util.AnnotationType.STRIKEOUT:
  67. return new StrikeOutAnnotationElement(parameters);
  68. case _util.AnnotationType.FILEATTACHMENT:
  69. return new FileAttachmentAnnotationElement(parameters);
  70. default:
  71. return new AnnotationElement(parameters);
  72. }
  73. }
  74. }]);
  75. return AnnotationElementFactory;
  76. }();
  77. var AnnotationElement = function () {
  78. function AnnotationElement(parameters) {
  79. var isRenderable = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  80. var ignoreBorder = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  81. _classCallCheck(this, AnnotationElement);
  82. this.isRenderable = isRenderable;
  83. this.data = parameters.data;
  84. this.layer = parameters.layer;
  85. this.page = parameters.page;
  86. this.viewport = parameters.viewport;
  87. this.linkService = parameters.linkService;
  88. this.downloadManager = parameters.downloadManager;
  89. this.imageResourcesPath = parameters.imageResourcesPath;
  90. this.renderInteractiveForms = parameters.renderInteractiveForms;
  91. if (isRenderable) {
  92. this.container = this._createContainer(ignoreBorder);
  93. }
  94. }
  95. _createClass(AnnotationElement, [{
  96. key: '_createContainer',
  97. value: function _createContainer() {
  98. var ignoreBorder = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  99. var data = this.data,
  100. page = this.page,
  101. viewport = this.viewport;
  102. var container = document.createElement('section');
  103. var width = data.rect[2] - data.rect[0];
  104. var height = data.rect[3] - data.rect[1];
  105. container.setAttribute('data-annotation-id', data.id);
  106. var rect = _util.Util.normalizeRect([data.rect[0], page.view[3] - data.rect[1] + page.view[1], data.rect[2], page.view[3] - data.rect[3] + page.view[1]]);
  107. _dom_utils.CustomStyle.setProp('transform', container, 'matrix(' + viewport.transform.join(',') + ')');
  108. _dom_utils.CustomStyle.setProp('transformOrigin', container, -rect[0] + 'px ' + -rect[1] + 'px');
  109. if (!ignoreBorder && data.borderStyle.width > 0) {
  110. container.style.borderWidth = data.borderStyle.width + 'px';
  111. if (data.borderStyle.style !== _util.AnnotationBorderStyleType.UNDERLINE) {
  112. width = width - 2 * data.borderStyle.width;
  113. height = height - 2 * data.borderStyle.width;
  114. }
  115. var horizontalRadius = data.borderStyle.horizontalCornerRadius;
  116. var verticalRadius = data.borderStyle.verticalCornerRadius;
  117. if (horizontalRadius > 0 || verticalRadius > 0) {
  118. var radius = horizontalRadius + 'px / ' + verticalRadius + 'px';
  119. _dom_utils.CustomStyle.setProp('borderRadius', container, radius);
  120. }
  121. switch (data.borderStyle.style) {
  122. case _util.AnnotationBorderStyleType.SOLID:
  123. container.style.borderStyle = 'solid';
  124. break;
  125. case _util.AnnotationBorderStyleType.DASHED:
  126. container.style.borderStyle = 'dashed';
  127. break;
  128. case _util.AnnotationBorderStyleType.BEVELED:
  129. (0, _util.warn)('Unimplemented border style: beveled');
  130. break;
  131. case _util.AnnotationBorderStyleType.INSET:
  132. (0, _util.warn)('Unimplemented border style: inset');
  133. break;
  134. case _util.AnnotationBorderStyleType.UNDERLINE:
  135. container.style.borderBottomStyle = 'solid';
  136. break;
  137. default:
  138. break;
  139. }
  140. if (data.color) {
  141. container.style.borderColor = _util.Util.makeCssRgb(data.color[0] | 0, data.color[1] | 0, data.color[2] | 0);
  142. } else {
  143. container.style.borderWidth = 0;
  144. }
  145. }
  146. container.style.left = rect[0] + 'px';
  147. container.style.top = rect[1] + 'px';
  148. container.style.width = width + 'px';
  149. container.style.height = height + 'px';
  150. return container;
  151. }
  152. }, {
  153. key: '_createPopup',
  154. value: function _createPopup(container, trigger, data) {
  155. if (!trigger) {
  156. trigger = document.createElement('div');
  157. trigger.style.height = container.style.height;
  158. trigger.style.width = container.style.width;
  159. container.appendChild(trigger);
  160. }
  161. var popupElement = new PopupElement({
  162. container: container,
  163. trigger: trigger,
  164. color: data.color,
  165. title: data.title,
  166. contents: data.contents,
  167. hideWrapper: true
  168. });
  169. var popup = popupElement.render();
  170. popup.style.left = container.style.width;
  171. container.appendChild(popup);
  172. }
  173. }, {
  174. key: 'render',
  175. value: function render() {
  176. throw new Error('Abstract method `AnnotationElement.render` called');
  177. }
  178. }]);
  179. return AnnotationElement;
  180. }();
  181. var LinkAnnotationElement = function (_AnnotationElement) {
  182. _inherits(LinkAnnotationElement, _AnnotationElement);
  183. function LinkAnnotationElement(parameters) {
  184. _classCallCheck(this, LinkAnnotationElement);
  185. return _possibleConstructorReturn(this, (LinkAnnotationElement.__proto__ || Object.getPrototypeOf(LinkAnnotationElement)).call(this, parameters, true));
  186. }
  187. _createClass(LinkAnnotationElement, [{
  188. key: 'render',
  189. value: function render() {
  190. this.container.className = 'linkAnnotation';
  191. var link = document.createElement('a');
  192. (0, _dom_utils.addLinkAttributes)(link, {
  193. url: this.data.url,
  194. target: this.data.newWindow ? _dom_utils.LinkTarget.BLANK : undefined
  195. });
  196. if (!this.data.url) {
  197. if (this.data.action) {
  198. this._bindNamedAction(link, this.data.action);
  199. } else {
  200. this._bindLink(link, this.data.dest);
  201. }
  202. }
  203. this.container.appendChild(link);
  204. return this.container;
  205. }
  206. }, {
  207. key: '_bindLink',
  208. value: function _bindLink(link, destination) {
  209. var _this2 = this;
  210. link.href = this.linkService.getDestinationHash(destination);
  211. link.onclick = function () {
  212. if (destination) {
  213. _this2.linkService.navigateTo(destination);
  214. }
  215. return false;
  216. };
  217. if (destination) {
  218. link.className = 'internalLink';
  219. }
  220. }
  221. }, {
  222. key: '_bindNamedAction',
  223. value: function _bindNamedAction(link, action) {
  224. var _this3 = this;
  225. link.href = this.linkService.getAnchorUrl('');
  226. link.onclick = function () {
  227. _this3.linkService.executeNamedAction(action);
  228. return false;
  229. };
  230. link.className = 'internalLink';
  231. }
  232. }]);
  233. return LinkAnnotationElement;
  234. }(AnnotationElement);
  235. var TextAnnotationElement = function (_AnnotationElement2) {
  236. _inherits(TextAnnotationElement, _AnnotationElement2);
  237. function TextAnnotationElement(parameters) {
  238. _classCallCheck(this, TextAnnotationElement);
  239. var isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  240. return _possibleConstructorReturn(this, (TextAnnotationElement.__proto__ || Object.getPrototypeOf(TextAnnotationElement)).call(this, parameters, isRenderable));
  241. }
  242. _createClass(TextAnnotationElement, [{
  243. key: 'render',
  244. value: function render() {
  245. this.container.className = 'textAnnotation';
  246. var image = document.createElement('img');
  247. image.style.height = this.container.style.height;
  248. image.style.width = this.container.style.width;
  249. image.src = this.imageResourcesPath + 'annotation-' + this.data.name.toLowerCase() + '.svg';
  250. image.alt = '[{{type}} Annotation]';
  251. image.dataset.l10nId = 'text_annotation_type';
  252. image.dataset.l10nArgs = JSON.stringify({ type: this.data.name });
  253. if (!this.data.hasPopup) {
  254. this._createPopup(this.container, image, this.data);
  255. }
  256. this.container.appendChild(image);
  257. return this.container;
  258. }
  259. }]);
  260. return TextAnnotationElement;
  261. }(AnnotationElement);
  262. var WidgetAnnotationElement = function (_AnnotationElement3) {
  263. _inherits(WidgetAnnotationElement, _AnnotationElement3);
  264. function WidgetAnnotationElement() {
  265. _classCallCheck(this, WidgetAnnotationElement);
  266. return _possibleConstructorReturn(this, (WidgetAnnotationElement.__proto__ || Object.getPrototypeOf(WidgetAnnotationElement)).apply(this, arguments));
  267. }
  268. _createClass(WidgetAnnotationElement, [{
  269. key: 'render',
  270. value: function render() {
  271. return this.container;
  272. }
  273. }]);
  274. return WidgetAnnotationElement;
  275. }(AnnotationElement);
  276. var TextWidgetAnnotationElement = function (_WidgetAnnotationElem) {
  277. _inherits(TextWidgetAnnotationElement, _WidgetAnnotationElem);
  278. function TextWidgetAnnotationElement(parameters) {
  279. _classCallCheck(this, TextWidgetAnnotationElement);
  280. var isRenderable = parameters.renderInteractiveForms || !parameters.data.hasAppearance && !!parameters.data.fieldValue;
  281. return _possibleConstructorReturn(this, (TextWidgetAnnotationElement.__proto__ || Object.getPrototypeOf(TextWidgetAnnotationElement)).call(this, parameters, isRenderable));
  282. }
  283. _createClass(TextWidgetAnnotationElement, [{
  284. key: 'render',
  285. value: function render() {
  286. var TEXT_ALIGNMENT = ['left', 'center', 'right'];
  287. this.container.className = 'textWidgetAnnotation';
  288. var element = null;
  289. if (this.renderInteractiveForms) {
  290. if (this.data.multiLine) {
  291. element = document.createElement('textarea');
  292. element.textContent = this.data.fieldValue;
  293. } else {
  294. element = document.createElement('input');
  295. element.type = 'text';
  296. element.setAttribute('value', this.data.fieldValue);
  297. }
  298. element.disabled = this.data.readOnly;
  299. if (this.data.maxLen !== null) {
  300. element.maxLength = this.data.maxLen;
  301. }
  302. if (this.data.comb) {
  303. var fieldWidth = this.data.rect[2] - this.data.rect[0];
  304. var combWidth = fieldWidth / this.data.maxLen;
  305. element.classList.add('comb');
  306. element.style.letterSpacing = 'calc(' + combWidth + 'px - 1ch)';
  307. }
  308. } else {
  309. element = document.createElement('div');
  310. element.textContent = this.data.fieldValue;
  311. element.style.verticalAlign = 'middle';
  312. element.style.display = 'table-cell';
  313. var font = null;
  314. if (this.data.fontRefName) {
  315. font = this.page.commonObjs.getData(this.data.fontRefName);
  316. }
  317. this._setTextStyle(element, font);
  318. }
  319. if (this.data.textAlignment !== null) {
  320. element.style.textAlign = TEXT_ALIGNMENT[this.data.textAlignment];
  321. }
  322. this.container.appendChild(element);
  323. return this.container;
  324. }
  325. }, {
  326. key: '_setTextStyle',
  327. value: function _setTextStyle(element, font) {
  328. var style = element.style;
  329. style.fontSize = this.data.fontSize + 'px';
  330. style.direction = this.data.fontDirection < 0 ? 'rtl' : 'ltr';
  331. if (!font) {
  332. return;
  333. }
  334. style.fontWeight = font.black ? font.bold ? '900' : 'bold' : font.bold ? 'bold' : 'normal';
  335. style.fontStyle = font.italic ? 'italic' : 'normal';
  336. var fontFamily = font.loadedName ? '"' + font.loadedName + '", ' : '';
  337. var fallbackName = font.fallbackName || 'Helvetica, sans-serif';
  338. style.fontFamily = fontFamily + fallbackName;
  339. }
  340. }]);
  341. return TextWidgetAnnotationElement;
  342. }(WidgetAnnotationElement);
  343. var CheckboxWidgetAnnotationElement = function (_WidgetAnnotationElem2) {
  344. _inherits(CheckboxWidgetAnnotationElement, _WidgetAnnotationElem2);
  345. function CheckboxWidgetAnnotationElement(parameters) {
  346. _classCallCheck(this, CheckboxWidgetAnnotationElement);
  347. return _possibleConstructorReturn(this, (CheckboxWidgetAnnotationElement.__proto__ || Object.getPrototypeOf(CheckboxWidgetAnnotationElement)).call(this, parameters, parameters.renderInteractiveForms));
  348. }
  349. _createClass(CheckboxWidgetAnnotationElement, [{
  350. key: 'render',
  351. value: function render() {
  352. this.container.className = 'buttonWidgetAnnotation checkBox';
  353. var element = document.createElement('input');
  354. element.disabled = this.data.readOnly;
  355. element.type = 'checkbox';
  356. if (this.data.fieldValue && this.data.fieldValue !== 'Off') {
  357. element.setAttribute('checked', true);
  358. }
  359. this.container.appendChild(element);
  360. return this.container;
  361. }
  362. }]);
  363. return CheckboxWidgetAnnotationElement;
  364. }(WidgetAnnotationElement);
  365. var RadioButtonWidgetAnnotationElement = function (_WidgetAnnotationElem3) {
  366. _inherits(RadioButtonWidgetAnnotationElement, _WidgetAnnotationElem3);
  367. function RadioButtonWidgetAnnotationElement(parameters) {
  368. _classCallCheck(this, RadioButtonWidgetAnnotationElement);
  369. return _possibleConstructorReturn(this, (RadioButtonWidgetAnnotationElement.__proto__ || Object.getPrototypeOf(RadioButtonWidgetAnnotationElement)).call(this, parameters, parameters.renderInteractiveForms));
  370. }
  371. _createClass(RadioButtonWidgetAnnotationElement, [{
  372. key: 'render',
  373. value: function render() {
  374. this.container.className = 'buttonWidgetAnnotation radioButton';
  375. var element = document.createElement('input');
  376. element.disabled = this.data.readOnly;
  377. element.type = 'radio';
  378. element.name = this.data.fieldName;
  379. if (this.data.fieldValue === this.data.buttonValue) {
  380. element.setAttribute('checked', true);
  381. }
  382. this.container.appendChild(element);
  383. return this.container;
  384. }
  385. }]);
  386. return RadioButtonWidgetAnnotationElement;
  387. }(WidgetAnnotationElement);
  388. var ChoiceWidgetAnnotationElement = function (_WidgetAnnotationElem4) {
  389. _inherits(ChoiceWidgetAnnotationElement, _WidgetAnnotationElem4);
  390. function ChoiceWidgetAnnotationElement(parameters) {
  391. _classCallCheck(this, ChoiceWidgetAnnotationElement);
  392. return _possibleConstructorReturn(this, (ChoiceWidgetAnnotationElement.__proto__ || Object.getPrototypeOf(ChoiceWidgetAnnotationElement)).call(this, parameters, parameters.renderInteractiveForms));
  393. }
  394. _createClass(ChoiceWidgetAnnotationElement, [{
  395. key: 'render',
  396. value: function render() {
  397. this.container.className = 'choiceWidgetAnnotation';
  398. var selectElement = document.createElement('select');
  399. selectElement.disabled = this.data.readOnly;
  400. if (!this.data.combo) {
  401. selectElement.size = this.data.options.length;
  402. if (this.data.multiSelect) {
  403. selectElement.multiple = true;
  404. }
  405. }
  406. for (var i = 0, ii = this.data.options.length; i < ii; i++) {
  407. var option = this.data.options[i];
  408. var optionElement = document.createElement('option');
  409. optionElement.textContent = option.displayValue;
  410. optionElement.value = option.exportValue;
  411. if (this.data.fieldValue.indexOf(option.displayValue) >= 0) {
  412. optionElement.setAttribute('selected', true);
  413. }
  414. selectElement.appendChild(optionElement);
  415. }
  416. this.container.appendChild(selectElement);
  417. return this.container;
  418. }
  419. }]);
  420. return ChoiceWidgetAnnotationElement;
  421. }(WidgetAnnotationElement);
  422. var PopupAnnotationElement = function (_AnnotationElement4) {
  423. _inherits(PopupAnnotationElement, _AnnotationElement4);
  424. function PopupAnnotationElement(parameters) {
  425. _classCallCheck(this, PopupAnnotationElement);
  426. var isRenderable = !!(parameters.data.title || parameters.data.contents);
  427. return _possibleConstructorReturn(this, (PopupAnnotationElement.__proto__ || Object.getPrototypeOf(PopupAnnotationElement)).call(this, parameters, isRenderable));
  428. }
  429. _createClass(PopupAnnotationElement, [{
  430. key: 'render',
  431. value: function render() {
  432. var IGNORE_TYPES = ['Line'];
  433. this.container.className = 'popupAnnotation';
  434. if (IGNORE_TYPES.indexOf(this.data.parentType) >= 0) {
  435. return this.container;
  436. }
  437. var selector = '[data-annotation-id="' + this.data.parentId + '"]';
  438. var parentElement = this.layer.querySelector(selector);
  439. if (!parentElement) {
  440. return this.container;
  441. }
  442. var popup = new PopupElement({
  443. container: this.container,
  444. trigger: parentElement,
  445. color: this.data.color,
  446. title: this.data.title,
  447. contents: this.data.contents
  448. });
  449. var parentLeft = parseFloat(parentElement.style.left);
  450. var parentWidth = parseFloat(parentElement.style.width);
  451. _dom_utils.CustomStyle.setProp('transformOrigin', this.container, -(parentLeft + parentWidth) + 'px -' + parentElement.style.top);
  452. this.container.style.left = parentLeft + parentWidth + 'px';
  453. this.container.appendChild(popup.render());
  454. return this.container;
  455. }
  456. }]);
  457. return PopupAnnotationElement;
  458. }(AnnotationElement);
  459. var PopupElement = function () {
  460. function PopupElement(parameters) {
  461. _classCallCheck(this, PopupElement);
  462. this.container = parameters.container;
  463. this.trigger = parameters.trigger;
  464. this.color = parameters.color;
  465. this.title = parameters.title;
  466. this.contents = parameters.contents;
  467. this.hideWrapper = parameters.hideWrapper || false;
  468. this.pinned = false;
  469. }
  470. _createClass(PopupElement, [{
  471. key: 'render',
  472. value: function render() {
  473. var BACKGROUND_ENLIGHT = 0.7;
  474. var wrapper = document.createElement('div');
  475. wrapper.className = 'popupWrapper';
  476. this.hideElement = this.hideWrapper ? wrapper : this.container;
  477. this.hideElement.setAttribute('hidden', true);
  478. var popup = document.createElement('div');
  479. popup.className = 'popup';
  480. var color = this.color;
  481. if (color) {
  482. var r = BACKGROUND_ENLIGHT * (255 - color[0]) + color[0];
  483. var g = BACKGROUND_ENLIGHT * (255 - color[1]) + color[1];
  484. var b = BACKGROUND_ENLIGHT * (255 - color[2]) + color[2];
  485. popup.style.backgroundColor = _util.Util.makeCssRgb(r | 0, g | 0, b | 0);
  486. }
  487. var contents = this._formatContents(this.contents);
  488. var title = document.createElement('h1');
  489. title.textContent = this.title;
  490. this.trigger.addEventListener('click', this._toggle.bind(this));
  491. this.trigger.addEventListener('mouseover', this._show.bind(this, false));
  492. this.trigger.addEventListener('mouseout', this._hide.bind(this, false));
  493. popup.addEventListener('click', this._hide.bind(this, true));
  494. popup.appendChild(title);
  495. popup.appendChild(contents);
  496. wrapper.appendChild(popup);
  497. return wrapper;
  498. }
  499. }, {
  500. key: '_formatContents',
  501. value: function _formatContents(contents) {
  502. var p = document.createElement('p');
  503. var lines = contents.split(/(?:\r\n?|\n)/);
  504. for (var i = 0, ii = lines.length; i < ii; ++i) {
  505. var line = lines[i];
  506. p.appendChild(document.createTextNode(line));
  507. if (i < ii - 1) {
  508. p.appendChild(document.createElement('br'));
  509. }
  510. }
  511. return p;
  512. }
  513. }, {
  514. key: '_toggle',
  515. value: function _toggle() {
  516. if (this.pinned) {
  517. this._hide(true);
  518. } else {
  519. this._show(true);
  520. }
  521. }
  522. }, {
  523. key: '_show',
  524. value: function _show() {
  525. var pin = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  526. if (pin) {
  527. this.pinned = true;
  528. }
  529. if (this.hideElement.hasAttribute('hidden')) {
  530. this.hideElement.removeAttribute('hidden');
  531. this.container.style.zIndex += 1;
  532. }
  533. }
  534. }, {
  535. key: '_hide',
  536. value: function _hide() {
  537. var unpin = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  538. if (unpin) {
  539. this.pinned = false;
  540. }
  541. if (!this.hideElement.hasAttribute('hidden') && !this.pinned) {
  542. this.hideElement.setAttribute('hidden', true);
  543. this.container.style.zIndex -= 1;
  544. }
  545. }
  546. }]);
  547. return PopupElement;
  548. }();
  549. var LineAnnotationElement = function (_AnnotationElement5) {
  550. _inherits(LineAnnotationElement, _AnnotationElement5);
  551. function LineAnnotationElement(parameters) {
  552. _classCallCheck(this, LineAnnotationElement);
  553. var isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  554. return _possibleConstructorReturn(this, (LineAnnotationElement.__proto__ || Object.getPrototypeOf(LineAnnotationElement)).call(this, parameters, isRenderable, true));
  555. }
  556. _createClass(LineAnnotationElement, [{
  557. key: 'render',
  558. value: function render() {
  559. var SVG_NS = 'http://www.w3.org/2000/svg';
  560. this.container.className = 'lineAnnotation';
  561. var data = this.data;
  562. var width = data.rect[2] - data.rect[0];
  563. var height = data.rect[3] - data.rect[1];
  564. var svg = document.createElementNS(SVG_NS, 'svg:svg');
  565. svg.setAttributeNS(null, 'version', '1.1');
  566. svg.setAttributeNS(null, 'width', width + 'px');
  567. svg.setAttributeNS(null, 'height', height + 'px');
  568. svg.setAttributeNS(null, 'preserveAspectRatio', 'none');
  569. svg.setAttributeNS(null, 'viewBox', '0 0 ' + width + ' ' + height);
  570. var line = document.createElementNS(SVG_NS, 'svg:line');
  571. line.setAttributeNS(null, 'x1', data.rect[2] - data.lineCoordinates[0]);
  572. line.setAttributeNS(null, 'y1', data.rect[3] - data.lineCoordinates[1]);
  573. line.setAttributeNS(null, 'x2', data.rect[2] - data.lineCoordinates[2]);
  574. line.setAttributeNS(null, 'y2', data.rect[3] - data.lineCoordinates[3]);
  575. line.setAttributeNS(null, 'stroke-width', data.borderStyle.width);
  576. line.setAttributeNS(null, 'stroke', 'transparent');
  577. svg.appendChild(line);
  578. this.container.append(svg);
  579. this._createPopup(this.container, line, this.data);
  580. return this.container;
  581. }
  582. }]);
  583. return LineAnnotationElement;
  584. }(AnnotationElement);
  585. var HighlightAnnotationElement = function (_AnnotationElement6) {
  586. _inherits(HighlightAnnotationElement, _AnnotationElement6);
  587. function HighlightAnnotationElement(parameters) {
  588. _classCallCheck(this, HighlightAnnotationElement);
  589. var isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  590. return _possibleConstructorReturn(this, (HighlightAnnotationElement.__proto__ || Object.getPrototypeOf(HighlightAnnotationElement)).call(this, parameters, isRenderable, true));
  591. }
  592. _createClass(HighlightAnnotationElement, [{
  593. key: 'render',
  594. value: function render() {
  595. this.container.className = 'highlightAnnotation';
  596. if (!this.data.hasPopup) {
  597. this._createPopup(this.container, null, this.data);
  598. }
  599. return this.container;
  600. }
  601. }]);
  602. return HighlightAnnotationElement;
  603. }(AnnotationElement);
  604. var UnderlineAnnotationElement = function (_AnnotationElement7) {
  605. _inherits(UnderlineAnnotationElement, _AnnotationElement7);
  606. function UnderlineAnnotationElement(parameters) {
  607. _classCallCheck(this, UnderlineAnnotationElement);
  608. var isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  609. return _possibleConstructorReturn(this, (UnderlineAnnotationElement.__proto__ || Object.getPrototypeOf(UnderlineAnnotationElement)).call(this, parameters, isRenderable, true));
  610. }
  611. _createClass(UnderlineAnnotationElement, [{
  612. key: 'render',
  613. value: function render() {
  614. this.container.className = 'underlineAnnotation';
  615. if (!this.data.hasPopup) {
  616. this._createPopup(this.container, null, this.data);
  617. }
  618. return this.container;
  619. }
  620. }]);
  621. return UnderlineAnnotationElement;
  622. }(AnnotationElement);
  623. var SquigglyAnnotationElement = function (_AnnotationElement8) {
  624. _inherits(SquigglyAnnotationElement, _AnnotationElement8);
  625. function SquigglyAnnotationElement(parameters) {
  626. _classCallCheck(this, SquigglyAnnotationElement);
  627. var isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  628. return _possibleConstructorReturn(this, (SquigglyAnnotationElement.__proto__ || Object.getPrototypeOf(SquigglyAnnotationElement)).call(this, parameters, isRenderable, true));
  629. }
  630. _createClass(SquigglyAnnotationElement, [{
  631. key: 'render',
  632. value: function render() {
  633. this.container.className = 'squigglyAnnotation';
  634. if (!this.data.hasPopup) {
  635. this._createPopup(this.container, null, this.data);
  636. }
  637. return this.container;
  638. }
  639. }]);
  640. return SquigglyAnnotationElement;
  641. }(AnnotationElement);
  642. var StrikeOutAnnotationElement = function (_AnnotationElement9) {
  643. _inherits(StrikeOutAnnotationElement, _AnnotationElement9);
  644. function StrikeOutAnnotationElement(parameters) {
  645. _classCallCheck(this, StrikeOutAnnotationElement);
  646. var isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  647. return _possibleConstructorReturn(this, (StrikeOutAnnotationElement.__proto__ || Object.getPrototypeOf(StrikeOutAnnotationElement)).call(this, parameters, isRenderable, true));
  648. }
  649. _createClass(StrikeOutAnnotationElement, [{
  650. key: 'render',
  651. value: function render() {
  652. this.container.className = 'strikeoutAnnotation';
  653. if (!this.data.hasPopup) {
  654. this._createPopup(this.container, null, this.data);
  655. }
  656. return this.container;
  657. }
  658. }]);
  659. return StrikeOutAnnotationElement;
  660. }(AnnotationElement);
  661. var FileAttachmentAnnotationElement = function (_AnnotationElement10) {
  662. _inherits(FileAttachmentAnnotationElement, _AnnotationElement10);
  663. function FileAttachmentAnnotationElement(parameters) {
  664. _classCallCheck(this, FileAttachmentAnnotationElement);
  665. var _this16 = _possibleConstructorReturn(this, (FileAttachmentAnnotationElement.__proto__ || Object.getPrototypeOf(FileAttachmentAnnotationElement)).call(this, parameters, true));
  666. var file = _this16.data.file;
  667. _this16.filename = (0, _dom_utils.getFilenameFromUrl)(file.filename);
  668. _this16.content = file.content;
  669. _this16.linkService.onFileAttachmentAnnotation({
  670. id: (0, _util.stringToPDFString)(file.filename),
  671. filename: file.filename,
  672. content: file.content
  673. });
  674. return _this16;
  675. }
  676. _createClass(FileAttachmentAnnotationElement, [{
  677. key: 'render',
  678. value: function render() {
  679. this.container.className = 'fileAttachmentAnnotation';
  680. var trigger = document.createElement('div');
  681. trigger.style.height = this.container.style.height;
  682. trigger.style.width = this.container.style.width;
  683. trigger.addEventListener('dblclick', this._download.bind(this));
  684. if (!this.data.hasPopup && (this.data.title || this.data.contents)) {
  685. this._createPopup(this.container, trigger, this.data);
  686. }
  687. this.container.appendChild(trigger);
  688. return this.container;
  689. }
  690. }, {
  691. key: '_download',
  692. value: function _download() {
  693. if (!this.downloadManager) {
  694. (0, _util.warn)('Download cannot be started due to unavailable download manager');
  695. return;
  696. }
  697. this.downloadManager.downloadData(this.content, this.filename, '');
  698. }
  699. }]);
  700. return FileAttachmentAnnotationElement;
  701. }(AnnotationElement);
  702. var AnnotationLayer = function () {
  703. function AnnotationLayer() {
  704. _classCallCheck(this, AnnotationLayer);
  705. }
  706. _createClass(AnnotationLayer, null, [{
  707. key: 'render',
  708. value: function render(parameters) {
  709. for (var i = 0, ii = parameters.annotations.length; i < ii; i++) {
  710. var data = parameters.annotations[i];
  711. if (!data) {
  712. continue;
  713. }
  714. var element = AnnotationElementFactory.create({
  715. data: data,
  716. layer: parameters.div,
  717. page: parameters.page,
  718. viewport: parameters.viewport,
  719. linkService: parameters.linkService,
  720. downloadManager: parameters.downloadManager,
  721. imageResourcesPath: parameters.imageResourcesPath || (0, _dom_utils.getDefaultSetting)('imageResourcesPath'),
  722. renderInteractiveForms: parameters.renderInteractiveForms || false
  723. });
  724. if (element.isRenderable) {
  725. parameters.div.appendChild(element.render());
  726. }
  727. }
  728. }
  729. }, {
  730. key: 'update',
  731. value: function update(parameters) {
  732. for (var i = 0, ii = parameters.annotations.length; i < ii; i++) {
  733. var data = parameters.annotations[i];
  734. var element = parameters.div.querySelector('[data-annotation-id="' + data.id + '"]');
  735. if (element) {
  736. _dom_utils.CustomStyle.setProp('transform', element, 'matrix(' + parameters.viewport.transform.join(',') + ')');
  737. }
  738. }
  739. parameters.div.removeAttribute('hidden');
  740. }
  741. }]);
  742. return AnnotationLayer;
  743. }();
  744. exports.AnnotationLayer = AnnotationLayer;