annotation_layer.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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.SQUARE:
  61. return new SquareAnnotationElement(parameters);
  62. case _util.AnnotationType.CIRCLE:
  63. return new CircleAnnotationElement(parameters);
  64. case _util.AnnotationType.HIGHLIGHT:
  65. return new HighlightAnnotationElement(parameters);
  66. case _util.AnnotationType.UNDERLINE:
  67. return new UnderlineAnnotationElement(parameters);
  68. case _util.AnnotationType.SQUIGGLY:
  69. return new SquigglyAnnotationElement(parameters);
  70. case _util.AnnotationType.STRIKEOUT:
  71. return new StrikeOutAnnotationElement(parameters);
  72. case _util.AnnotationType.FILEATTACHMENT:
  73. return new FileAttachmentAnnotationElement(parameters);
  74. default:
  75. return new AnnotationElement(parameters);
  76. }
  77. }
  78. }]);
  79. return AnnotationElementFactory;
  80. }();
  81. var AnnotationElement = function () {
  82. function AnnotationElement(parameters) {
  83. var isRenderable = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  84. var ignoreBorder = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  85. _classCallCheck(this, AnnotationElement);
  86. this.isRenderable = isRenderable;
  87. this.data = parameters.data;
  88. this.layer = parameters.layer;
  89. this.page = parameters.page;
  90. this.viewport = parameters.viewport;
  91. this.linkService = parameters.linkService;
  92. this.downloadManager = parameters.downloadManager;
  93. this.imageResourcesPath = parameters.imageResourcesPath;
  94. this.renderInteractiveForms = parameters.renderInteractiveForms;
  95. this.svgFactory = parameters.svgFactory;
  96. if (isRenderable) {
  97. this.container = this._createContainer(ignoreBorder);
  98. }
  99. }
  100. _createClass(AnnotationElement, [{
  101. key: '_createContainer',
  102. value: function _createContainer() {
  103. var ignoreBorder = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  104. var data = this.data,
  105. page = this.page,
  106. viewport = this.viewport;
  107. var container = document.createElement('section');
  108. var width = data.rect[2] - data.rect[0];
  109. var height = data.rect[3] - data.rect[1];
  110. container.setAttribute('data-annotation-id', data.id);
  111. 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]]);
  112. _dom_utils.CustomStyle.setProp('transform', container, 'matrix(' + viewport.transform.join(',') + ')');
  113. _dom_utils.CustomStyle.setProp('transformOrigin', container, -rect[0] + 'px ' + -rect[1] + 'px');
  114. if (!ignoreBorder && data.borderStyle.width > 0) {
  115. container.style.borderWidth = data.borderStyle.width + 'px';
  116. if (data.borderStyle.style !== _util.AnnotationBorderStyleType.UNDERLINE) {
  117. width = width - 2 * data.borderStyle.width;
  118. height = height - 2 * data.borderStyle.width;
  119. }
  120. var horizontalRadius = data.borderStyle.horizontalCornerRadius;
  121. var verticalRadius = data.borderStyle.verticalCornerRadius;
  122. if (horizontalRadius > 0 || verticalRadius > 0) {
  123. var radius = horizontalRadius + 'px / ' + verticalRadius + 'px';
  124. _dom_utils.CustomStyle.setProp('borderRadius', container, radius);
  125. }
  126. switch (data.borderStyle.style) {
  127. case _util.AnnotationBorderStyleType.SOLID:
  128. container.style.borderStyle = 'solid';
  129. break;
  130. case _util.AnnotationBorderStyleType.DASHED:
  131. container.style.borderStyle = 'dashed';
  132. break;
  133. case _util.AnnotationBorderStyleType.BEVELED:
  134. (0, _util.warn)('Unimplemented border style: beveled');
  135. break;
  136. case _util.AnnotationBorderStyleType.INSET:
  137. (0, _util.warn)('Unimplemented border style: inset');
  138. break;
  139. case _util.AnnotationBorderStyleType.UNDERLINE:
  140. container.style.borderBottomStyle = 'solid';
  141. break;
  142. default:
  143. break;
  144. }
  145. if (data.color) {
  146. container.style.borderColor = _util.Util.makeCssRgb(data.color[0] | 0, data.color[1] | 0, data.color[2] | 0);
  147. } else {
  148. container.style.borderWidth = 0;
  149. }
  150. }
  151. container.style.left = rect[0] + 'px';
  152. container.style.top = rect[1] + 'px';
  153. container.style.width = width + 'px';
  154. container.style.height = height + 'px';
  155. return container;
  156. }
  157. }, {
  158. key: '_createPopup',
  159. value: function _createPopup(container, trigger, data) {
  160. if (!trigger) {
  161. trigger = document.createElement('div');
  162. trigger.style.height = container.style.height;
  163. trigger.style.width = container.style.width;
  164. container.appendChild(trigger);
  165. }
  166. var popupElement = new PopupElement({
  167. container: container,
  168. trigger: trigger,
  169. color: data.color,
  170. title: data.title,
  171. contents: data.contents,
  172. hideWrapper: true
  173. });
  174. var popup = popupElement.render();
  175. popup.style.left = container.style.width;
  176. container.appendChild(popup);
  177. }
  178. }, {
  179. key: 'render',
  180. value: function render() {
  181. throw new Error('Abstract method `AnnotationElement.render` called');
  182. }
  183. }]);
  184. return AnnotationElement;
  185. }();
  186. var LinkAnnotationElement = function (_AnnotationElement) {
  187. _inherits(LinkAnnotationElement, _AnnotationElement);
  188. function LinkAnnotationElement(parameters) {
  189. _classCallCheck(this, LinkAnnotationElement);
  190. var isRenderable = !!(parameters.data.url || parameters.data.dest || parameters.data.action);
  191. return _possibleConstructorReturn(this, (LinkAnnotationElement.__proto__ || Object.getPrototypeOf(LinkAnnotationElement)).call(this, parameters, isRenderable));
  192. }
  193. _createClass(LinkAnnotationElement, [{
  194. key: 'render',
  195. value: function render() {
  196. this.container.className = 'linkAnnotation';
  197. var link = document.createElement('a');
  198. (0, _dom_utils.addLinkAttributes)(link, {
  199. url: this.data.url,
  200. target: this.data.newWindow ? _dom_utils.LinkTarget.BLANK : undefined
  201. });
  202. if (!this.data.url) {
  203. if (this.data.action) {
  204. this._bindNamedAction(link, this.data.action);
  205. } else {
  206. this._bindLink(link, this.data.dest);
  207. }
  208. }
  209. this.container.appendChild(link);
  210. return this.container;
  211. }
  212. }, {
  213. key: '_bindLink',
  214. value: function _bindLink(link, destination) {
  215. var _this2 = this;
  216. link.href = this.linkService.getDestinationHash(destination);
  217. link.onclick = function () {
  218. if (destination) {
  219. _this2.linkService.navigateTo(destination);
  220. }
  221. return false;
  222. };
  223. if (destination) {
  224. link.className = 'internalLink';
  225. }
  226. }
  227. }, {
  228. key: '_bindNamedAction',
  229. value: function _bindNamedAction(link, action) {
  230. var _this3 = this;
  231. link.href = this.linkService.getAnchorUrl('');
  232. link.onclick = function () {
  233. _this3.linkService.executeNamedAction(action);
  234. return false;
  235. };
  236. link.className = 'internalLink';
  237. }
  238. }]);
  239. return LinkAnnotationElement;
  240. }(AnnotationElement);
  241. var TextAnnotationElement = function (_AnnotationElement2) {
  242. _inherits(TextAnnotationElement, _AnnotationElement2);
  243. function TextAnnotationElement(parameters) {
  244. _classCallCheck(this, TextAnnotationElement);
  245. var isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  246. return _possibleConstructorReturn(this, (TextAnnotationElement.__proto__ || Object.getPrototypeOf(TextAnnotationElement)).call(this, parameters, isRenderable));
  247. }
  248. _createClass(TextAnnotationElement, [{
  249. key: 'render',
  250. value: function render() {
  251. this.container.className = 'textAnnotation';
  252. var image = document.createElement('img');
  253. image.style.height = this.container.style.height;
  254. image.style.width = this.container.style.width;
  255. image.src = this.imageResourcesPath + 'annotation-' + this.data.name.toLowerCase() + '.svg';
  256. image.alt = '[{{type}} Annotation]';
  257. image.dataset.l10nId = 'text_annotation_type';
  258. image.dataset.l10nArgs = JSON.stringify({ type: this.data.name });
  259. if (!this.data.hasPopup) {
  260. this._createPopup(this.container, image, this.data);
  261. }
  262. this.container.appendChild(image);
  263. return this.container;
  264. }
  265. }]);
  266. return TextAnnotationElement;
  267. }(AnnotationElement);
  268. var WidgetAnnotationElement = function (_AnnotationElement3) {
  269. _inherits(WidgetAnnotationElement, _AnnotationElement3);
  270. function WidgetAnnotationElement() {
  271. _classCallCheck(this, WidgetAnnotationElement);
  272. return _possibleConstructorReturn(this, (WidgetAnnotationElement.__proto__ || Object.getPrototypeOf(WidgetAnnotationElement)).apply(this, arguments));
  273. }
  274. _createClass(WidgetAnnotationElement, [{
  275. key: 'render',
  276. value: function render() {
  277. return this.container;
  278. }
  279. }]);
  280. return WidgetAnnotationElement;
  281. }(AnnotationElement);
  282. var TextWidgetAnnotationElement = function (_WidgetAnnotationElem) {
  283. _inherits(TextWidgetAnnotationElement, _WidgetAnnotationElem);
  284. function TextWidgetAnnotationElement(parameters) {
  285. _classCallCheck(this, TextWidgetAnnotationElement);
  286. var isRenderable = parameters.renderInteractiveForms || !parameters.data.hasAppearance && !!parameters.data.fieldValue;
  287. return _possibleConstructorReturn(this, (TextWidgetAnnotationElement.__proto__ || Object.getPrototypeOf(TextWidgetAnnotationElement)).call(this, parameters, isRenderable));
  288. }
  289. _createClass(TextWidgetAnnotationElement, [{
  290. key: 'render',
  291. value: function render() {
  292. var TEXT_ALIGNMENT = ['left', 'center', 'right'];
  293. this.container.className = 'textWidgetAnnotation';
  294. var element = null;
  295. if (this.renderInteractiveForms) {
  296. if (this.data.multiLine) {
  297. element = document.createElement('textarea');
  298. element.textContent = this.data.fieldValue;
  299. } else {
  300. element = document.createElement('input');
  301. element.type = 'text';
  302. element.setAttribute('value', this.data.fieldValue);
  303. }
  304. element.disabled = this.data.readOnly;
  305. if (this.data.maxLen !== null) {
  306. element.maxLength = this.data.maxLen;
  307. }
  308. if (this.data.comb) {
  309. var fieldWidth = this.data.rect[2] - this.data.rect[0];
  310. var combWidth = fieldWidth / this.data.maxLen;
  311. element.classList.add('comb');
  312. element.style.letterSpacing = 'calc(' + combWidth + 'px - 1ch)';
  313. }
  314. } else {
  315. element = document.createElement('div');
  316. element.textContent = this.data.fieldValue;
  317. element.style.verticalAlign = 'middle';
  318. element.style.display = 'table-cell';
  319. var font = null;
  320. if (this.data.fontRefName) {
  321. font = this.page.commonObjs.getData(this.data.fontRefName);
  322. }
  323. this._setTextStyle(element, font);
  324. }
  325. if (this.data.textAlignment !== null) {
  326. element.style.textAlign = TEXT_ALIGNMENT[this.data.textAlignment];
  327. }
  328. this.container.appendChild(element);
  329. return this.container;
  330. }
  331. }, {
  332. key: '_setTextStyle',
  333. value: function _setTextStyle(element, font) {
  334. var style = element.style;
  335. style.fontSize = this.data.fontSize + 'px';
  336. style.direction = this.data.fontDirection < 0 ? 'rtl' : 'ltr';
  337. if (!font) {
  338. return;
  339. }
  340. style.fontWeight = font.black ? font.bold ? '900' : 'bold' : font.bold ? 'bold' : 'normal';
  341. style.fontStyle = font.italic ? 'italic' : 'normal';
  342. var fontFamily = font.loadedName ? '"' + font.loadedName + '", ' : '';
  343. var fallbackName = font.fallbackName || 'Helvetica, sans-serif';
  344. style.fontFamily = fontFamily + fallbackName;
  345. }
  346. }]);
  347. return TextWidgetAnnotationElement;
  348. }(WidgetAnnotationElement);
  349. var CheckboxWidgetAnnotationElement = function (_WidgetAnnotationElem2) {
  350. _inherits(CheckboxWidgetAnnotationElement, _WidgetAnnotationElem2);
  351. function CheckboxWidgetAnnotationElement(parameters) {
  352. _classCallCheck(this, CheckboxWidgetAnnotationElement);
  353. return _possibleConstructorReturn(this, (CheckboxWidgetAnnotationElement.__proto__ || Object.getPrototypeOf(CheckboxWidgetAnnotationElement)).call(this, parameters, parameters.renderInteractiveForms));
  354. }
  355. _createClass(CheckboxWidgetAnnotationElement, [{
  356. key: 'render',
  357. value: function render() {
  358. this.container.className = 'buttonWidgetAnnotation checkBox';
  359. var element = document.createElement('input');
  360. element.disabled = this.data.readOnly;
  361. element.type = 'checkbox';
  362. if (this.data.fieldValue && this.data.fieldValue !== 'Off') {
  363. element.setAttribute('checked', true);
  364. }
  365. this.container.appendChild(element);
  366. return this.container;
  367. }
  368. }]);
  369. return CheckboxWidgetAnnotationElement;
  370. }(WidgetAnnotationElement);
  371. var RadioButtonWidgetAnnotationElement = function (_WidgetAnnotationElem3) {
  372. _inherits(RadioButtonWidgetAnnotationElement, _WidgetAnnotationElem3);
  373. function RadioButtonWidgetAnnotationElement(parameters) {
  374. _classCallCheck(this, RadioButtonWidgetAnnotationElement);
  375. return _possibleConstructorReturn(this, (RadioButtonWidgetAnnotationElement.__proto__ || Object.getPrototypeOf(RadioButtonWidgetAnnotationElement)).call(this, parameters, parameters.renderInteractiveForms));
  376. }
  377. _createClass(RadioButtonWidgetAnnotationElement, [{
  378. key: 'render',
  379. value: function render() {
  380. this.container.className = 'buttonWidgetAnnotation radioButton';
  381. var element = document.createElement('input');
  382. element.disabled = this.data.readOnly;
  383. element.type = 'radio';
  384. element.name = this.data.fieldName;
  385. if (this.data.fieldValue === this.data.buttonValue) {
  386. element.setAttribute('checked', true);
  387. }
  388. this.container.appendChild(element);
  389. return this.container;
  390. }
  391. }]);
  392. return RadioButtonWidgetAnnotationElement;
  393. }(WidgetAnnotationElement);
  394. var ChoiceWidgetAnnotationElement = function (_WidgetAnnotationElem4) {
  395. _inherits(ChoiceWidgetAnnotationElement, _WidgetAnnotationElem4);
  396. function ChoiceWidgetAnnotationElement(parameters) {
  397. _classCallCheck(this, ChoiceWidgetAnnotationElement);
  398. return _possibleConstructorReturn(this, (ChoiceWidgetAnnotationElement.__proto__ || Object.getPrototypeOf(ChoiceWidgetAnnotationElement)).call(this, parameters, parameters.renderInteractiveForms));
  399. }
  400. _createClass(ChoiceWidgetAnnotationElement, [{
  401. key: 'render',
  402. value: function render() {
  403. this.container.className = 'choiceWidgetAnnotation';
  404. var selectElement = document.createElement('select');
  405. selectElement.disabled = this.data.readOnly;
  406. if (!this.data.combo) {
  407. selectElement.size = this.data.options.length;
  408. if (this.data.multiSelect) {
  409. selectElement.multiple = true;
  410. }
  411. }
  412. for (var i = 0, ii = this.data.options.length; i < ii; i++) {
  413. var option = this.data.options[i];
  414. var optionElement = document.createElement('option');
  415. optionElement.textContent = option.displayValue;
  416. optionElement.value = option.exportValue;
  417. if (this.data.fieldValue.indexOf(option.displayValue) >= 0) {
  418. optionElement.setAttribute('selected', true);
  419. }
  420. selectElement.appendChild(optionElement);
  421. }
  422. this.container.appendChild(selectElement);
  423. return this.container;
  424. }
  425. }]);
  426. return ChoiceWidgetAnnotationElement;
  427. }(WidgetAnnotationElement);
  428. var PopupAnnotationElement = function (_AnnotationElement4) {
  429. _inherits(PopupAnnotationElement, _AnnotationElement4);
  430. function PopupAnnotationElement(parameters) {
  431. _classCallCheck(this, PopupAnnotationElement);
  432. var isRenderable = !!(parameters.data.title || parameters.data.contents);
  433. return _possibleConstructorReturn(this, (PopupAnnotationElement.__proto__ || Object.getPrototypeOf(PopupAnnotationElement)).call(this, parameters, isRenderable));
  434. }
  435. _createClass(PopupAnnotationElement, [{
  436. key: 'render',
  437. value: function render() {
  438. var IGNORE_TYPES = ['Line', 'Square', 'Circle'];
  439. this.container.className = 'popupAnnotation';
  440. if (IGNORE_TYPES.indexOf(this.data.parentType) >= 0) {
  441. return this.container;
  442. }
  443. var selector = '[data-annotation-id="' + this.data.parentId + '"]';
  444. var parentElement = this.layer.querySelector(selector);
  445. if (!parentElement) {
  446. return this.container;
  447. }
  448. var popup = new PopupElement({
  449. container: this.container,
  450. trigger: parentElement,
  451. color: this.data.color,
  452. title: this.data.title,
  453. contents: this.data.contents
  454. });
  455. var parentLeft = parseFloat(parentElement.style.left);
  456. var parentWidth = parseFloat(parentElement.style.width);
  457. _dom_utils.CustomStyle.setProp('transformOrigin', this.container, -(parentLeft + parentWidth) + 'px -' + parentElement.style.top);
  458. this.container.style.left = parentLeft + parentWidth + 'px';
  459. this.container.appendChild(popup.render());
  460. return this.container;
  461. }
  462. }]);
  463. return PopupAnnotationElement;
  464. }(AnnotationElement);
  465. var PopupElement = function () {
  466. function PopupElement(parameters) {
  467. _classCallCheck(this, PopupElement);
  468. this.container = parameters.container;
  469. this.trigger = parameters.trigger;
  470. this.color = parameters.color;
  471. this.title = parameters.title;
  472. this.contents = parameters.contents;
  473. this.hideWrapper = parameters.hideWrapper || false;
  474. this.pinned = false;
  475. }
  476. _createClass(PopupElement, [{
  477. key: 'render',
  478. value: function render() {
  479. var BACKGROUND_ENLIGHT = 0.7;
  480. var wrapper = document.createElement('div');
  481. wrapper.className = 'popupWrapper';
  482. this.hideElement = this.hideWrapper ? wrapper : this.container;
  483. this.hideElement.setAttribute('hidden', true);
  484. var popup = document.createElement('div');
  485. popup.className = 'popup';
  486. var color = this.color;
  487. if (color) {
  488. var r = BACKGROUND_ENLIGHT * (255 - color[0]) + color[0];
  489. var g = BACKGROUND_ENLIGHT * (255 - color[1]) + color[1];
  490. var b = BACKGROUND_ENLIGHT * (255 - color[2]) + color[2];
  491. popup.style.backgroundColor = _util.Util.makeCssRgb(r | 0, g | 0, b | 0);
  492. }
  493. var contents = this._formatContents(this.contents);
  494. var title = document.createElement('h1');
  495. title.textContent = this.title;
  496. this.trigger.addEventListener('click', this._toggle.bind(this));
  497. this.trigger.addEventListener('mouseover', this._show.bind(this, false));
  498. this.trigger.addEventListener('mouseout', this._hide.bind(this, false));
  499. popup.addEventListener('click', this._hide.bind(this, true));
  500. popup.appendChild(title);
  501. popup.appendChild(contents);
  502. wrapper.appendChild(popup);
  503. return wrapper;
  504. }
  505. }, {
  506. key: '_formatContents',
  507. value: function _formatContents(contents) {
  508. var p = document.createElement('p');
  509. var lines = contents.split(/(?:\r\n?|\n)/);
  510. for (var i = 0, ii = lines.length; i < ii; ++i) {
  511. var line = lines[i];
  512. p.appendChild(document.createTextNode(line));
  513. if (i < ii - 1) {
  514. p.appendChild(document.createElement('br'));
  515. }
  516. }
  517. return p;
  518. }
  519. }, {
  520. key: '_toggle',
  521. value: function _toggle() {
  522. if (this.pinned) {
  523. this._hide(true);
  524. } else {
  525. this._show(true);
  526. }
  527. }
  528. }, {
  529. key: '_show',
  530. value: function _show() {
  531. var pin = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  532. if (pin) {
  533. this.pinned = true;
  534. }
  535. if (this.hideElement.hasAttribute('hidden')) {
  536. this.hideElement.removeAttribute('hidden');
  537. this.container.style.zIndex += 1;
  538. }
  539. }
  540. }, {
  541. key: '_hide',
  542. value: function _hide() {
  543. var unpin = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  544. if (unpin) {
  545. this.pinned = false;
  546. }
  547. if (!this.hideElement.hasAttribute('hidden') && !this.pinned) {
  548. this.hideElement.setAttribute('hidden', true);
  549. this.container.style.zIndex -= 1;
  550. }
  551. }
  552. }]);
  553. return PopupElement;
  554. }();
  555. var LineAnnotationElement = function (_AnnotationElement5) {
  556. _inherits(LineAnnotationElement, _AnnotationElement5);
  557. function LineAnnotationElement(parameters) {
  558. _classCallCheck(this, LineAnnotationElement);
  559. var isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  560. return _possibleConstructorReturn(this, (LineAnnotationElement.__proto__ || Object.getPrototypeOf(LineAnnotationElement)).call(this, parameters, isRenderable, true));
  561. }
  562. _createClass(LineAnnotationElement, [{
  563. key: 'render',
  564. value: function render() {
  565. this.container.className = 'lineAnnotation';
  566. var data = this.data;
  567. var width = data.rect[2] - data.rect[0];
  568. var height = data.rect[3] - data.rect[1];
  569. var svg = this.svgFactory.create(width, height);
  570. var line = this.svgFactory.createElement('svg:line');
  571. line.setAttribute('x1', data.rect[2] - data.lineCoordinates[0]);
  572. line.setAttribute('y1', data.rect[3] - data.lineCoordinates[1]);
  573. line.setAttribute('x2', data.rect[2] - data.lineCoordinates[2]);
  574. line.setAttribute('y2', data.rect[3] - data.lineCoordinates[3]);
  575. line.setAttribute('stroke-width', data.borderStyle.width);
  576. line.setAttribute('stroke', 'transparent');
  577. svg.appendChild(line);
  578. this.container.append(svg);
  579. this._createPopup(this.container, line, data);
  580. return this.container;
  581. }
  582. }]);
  583. return LineAnnotationElement;
  584. }(AnnotationElement);
  585. var SquareAnnotationElement = function (_AnnotationElement6) {
  586. _inherits(SquareAnnotationElement, _AnnotationElement6);
  587. function SquareAnnotationElement(parameters) {
  588. _classCallCheck(this, SquareAnnotationElement);
  589. var isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  590. return _possibleConstructorReturn(this, (SquareAnnotationElement.__proto__ || Object.getPrototypeOf(SquareAnnotationElement)).call(this, parameters, isRenderable, true));
  591. }
  592. _createClass(SquareAnnotationElement, [{
  593. key: 'render',
  594. value: function render() {
  595. this.container.className = 'squareAnnotation';
  596. var data = this.data;
  597. var width = data.rect[2] - data.rect[0];
  598. var height = data.rect[3] - data.rect[1];
  599. var svg = this.svgFactory.create(width, height);
  600. var borderWidth = data.borderStyle.width;
  601. var square = this.svgFactory.createElement('svg:rect');
  602. square.setAttribute('x', borderWidth / 2);
  603. square.setAttribute('y', borderWidth / 2);
  604. square.setAttribute('width', width - borderWidth);
  605. square.setAttribute('height', height - borderWidth);
  606. square.setAttribute('stroke-width', borderWidth);
  607. square.setAttribute('stroke', 'transparent');
  608. square.setAttribute('fill', 'none');
  609. svg.appendChild(square);
  610. this.container.append(svg);
  611. this._createPopup(this.container, square, data);
  612. return this.container;
  613. }
  614. }]);
  615. return SquareAnnotationElement;
  616. }(AnnotationElement);
  617. var CircleAnnotationElement = function (_AnnotationElement7) {
  618. _inherits(CircleAnnotationElement, _AnnotationElement7);
  619. function CircleAnnotationElement(parameters) {
  620. _classCallCheck(this, CircleAnnotationElement);
  621. var isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  622. return _possibleConstructorReturn(this, (CircleAnnotationElement.__proto__ || Object.getPrototypeOf(CircleAnnotationElement)).call(this, parameters, isRenderable, true));
  623. }
  624. _createClass(CircleAnnotationElement, [{
  625. key: 'render',
  626. value: function render() {
  627. this.container.className = 'circleAnnotation';
  628. var data = this.data;
  629. var width = data.rect[2] - data.rect[0];
  630. var height = data.rect[3] - data.rect[1];
  631. var svg = this.svgFactory.create(width, height);
  632. var borderWidth = data.borderStyle.width;
  633. var circle = this.svgFactory.createElement('svg:ellipse');
  634. circle.setAttribute('cx', width / 2);
  635. circle.setAttribute('cy', height / 2);
  636. circle.setAttribute('rx', width / 2 - borderWidth / 2);
  637. circle.setAttribute('ry', height / 2 - borderWidth / 2);
  638. circle.setAttribute('stroke-width', borderWidth);
  639. circle.setAttribute('stroke', 'transparent');
  640. circle.setAttribute('fill', 'none');
  641. svg.appendChild(circle);
  642. this.container.append(svg);
  643. this._createPopup(this.container, circle, data);
  644. return this.container;
  645. }
  646. }]);
  647. return CircleAnnotationElement;
  648. }(AnnotationElement);
  649. var HighlightAnnotationElement = function (_AnnotationElement8) {
  650. _inherits(HighlightAnnotationElement, _AnnotationElement8);
  651. function HighlightAnnotationElement(parameters) {
  652. _classCallCheck(this, HighlightAnnotationElement);
  653. var isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  654. return _possibleConstructorReturn(this, (HighlightAnnotationElement.__proto__ || Object.getPrototypeOf(HighlightAnnotationElement)).call(this, parameters, isRenderable, true));
  655. }
  656. _createClass(HighlightAnnotationElement, [{
  657. key: 'render',
  658. value: function render() {
  659. this.container.className = 'highlightAnnotation';
  660. if (!this.data.hasPopup) {
  661. this._createPopup(this.container, null, this.data);
  662. }
  663. return this.container;
  664. }
  665. }]);
  666. return HighlightAnnotationElement;
  667. }(AnnotationElement);
  668. var UnderlineAnnotationElement = function (_AnnotationElement9) {
  669. _inherits(UnderlineAnnotationElement, _AnnotationElement9);
  670. function UnderlineAnnotationElement(parameters) {
  671. _classCallCheck(this, UnderlineAnnotationElement);
  672. var isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  673. return _possibleConstructorReturn(this, (UnderlineAnnotationElement.__proto__ || Object.getPrototypeOf(UnderlineAnnotationElement)).call(this, parameters, isRenderable, true));
  674. }
  675. _createClass(UnderlineAnnotationElement, [{
  676. key: 'render',
  677. value: function render() {
  678. this.container.className = 'underlineAnnotation';
  679. if (!this.data.hasPopup) {
  680. this._createPopup(this.container, null, this.data);
  681. }
  682. return this.container;
  683. }
  684. }]);
  685. return UnderlineAnnotationElement;
  686. }(AnnotationElement);
  687. var SquigglyAnnotationElement = function (_AnnotationElement10) {
  688. _inherits(SquigglyAnnotationElement, _AnnotationElement10);
  689. function SquigglyAnnotationElement(parameters) {
  690. _classCallCheck(this, SquigglyAnnotationElement);
  691. var isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  692. return _possibleConstructorReturn(this, (SquigglyAnnotationElement.__proto__ || Object.getPrototypeOf(SquigglyAnnotationElement)).call(this, parameters, isRenderable, true));
  693. }
  694. _createClass(SquigglyAnnotationElement, [{
  695. key: 'render',
  696. value: function render() {
  697. this.container.className = 'squigglyAnnotation';
  698. if (!this.data.hasPopup) {
  699. this._createPopup(this.container, null, this.data);
  700. }
  701. return this.container;
  702. }
  703. }]);
  704. return SquigglyAnnotationElement;
  705. }(AnnotationElement);
  706. var StrikeOutAnnotationElement = function (_AnnotationElement11) {
  707. _inherits(StrikeOutAnnotationElement, _AnnotationElement11);
  708. function StrikeOutAnnotationElement(parameters) {
  709. _classCallCheck(this, StrikeOutAnnotationElement);
  710. var isRenderable = !!(parameters.data.hasPopup || parameters.data.title || parameters.data.contents);
  711. return _possibleConstructorReturn(this, (StrikeOutAnnotationElement.__proto__ || Object.getPrototypeOf(StrikeOutAnnotationElement)).call(this, parameters, isRenderable, true));
  712. }
  713. _createClass(StrikeOutAnnotationElement, [{
  714. key: 'render',
  715. value: function render() {
  716. this.container.className = 'strikeoutAnnotation';
  717. if (!this.data.hasPopup) {
  718. this._createPopup(this.container, null, this.data);
  719. }
  720. return this.container;
  721. }
  722. }]);
  723. return StrikeOutAnnotationElement;
  724. }(AnnotationElement);
  725. var FileAttachmentAnnotationElement = function (_AnnotationElement12) {
  726. _inherits(FileAttachmentAnnotationElement, _AnnotationElement12);
  727. function FileAttachmentAnnotationElement(parameters) {
  728. _classCallCheck(this, FileAttachmentAnnotationElement);
  729. var _this18 = _possibleConstructorReturn(this, (FileAttachmentAnnotationElement.__proto__ || Object.getPrototypeOf(FileAttachmentAnnotationElement)).call(this, parameters, true));
  730. var file = _this18.data.file;
  731. _this18.filename = (0, _dom_utils.getFilenameFromUrl)(file.filename);
  732. _this18.content = file.content;
  733. _this18.linkService.onFileAttachmentAnnotation({
  734. id: (0, _util.stringToPDFString)(file.filename),
  735. filename: file.filename,
  736. content: file.content
  737. });
  738. return _this18;
  739. }
  740. _createClass(FileAttachmentAnnotationElement, [{
  741. key: 'render',
  742. value: function render() {
  743. this.container.className = 'fileAttachmentAnnotation';
  744. var trigger = document.createElement('div');
  745. trigger.style.height = this.container.style.height;
  746. trigger.style.width = this.container.style.width;
  747. trigger.addEventListener('dblclick', this._download.bind(this));
  748. if (!this.data.hasPopup && (this.data.title || this.data.contents)) {
  749. this._createPopup(this.container, trigger, this.data);
  750. }
  751. this.container.appendChild(trigger);
  752. return this.container;
  753. }
  754. }, {
  755. key: '_download',
  756. value: function _download() {
  757. if (!this.downloadManager) {
  758. (0, _util.warn)('Download cannot be started due to unavailable download manager');
  759. return;
  760. }
  761. this.downloadManager.downloadData(this.content, this.filename, '');
  762. }
  763. }]);
  764. return FileAttachmentAnnotationElement;
  765. }(AnnotationElement);
  766. var AnnotationLayer = function () {
  767. function AnnotationLayer() {
  768. _classCallCheck(this, AnnotationLayer);
  769. }
  770. _createClass(AnnotationLayer, null, [{
  771. key: 'render',
  772. value: function render(parameters) {
  773. for (var i = 0, ii = parameters.annotations.length; i < ii; i++) {
  774. var data = parameters.annotations[i];
  775. if (!data) {
  776. continue;
  777. }
  778. var element = AnnotationElementFactory.create({
  779. data: data,
  780. layer: parameters.div,
  781. page: parameters.page,
  782. viewport: parameters.viewport,
  783. linkService: parameters.linkService,
  784. downloadManager: parameters.downloadManager,
  785. imageResourcesPath: parameters.imageResourcesPath || (0, _dom_utils.getDefaultSetting)('imageResourcesPath'),
  786. renderInteractiveForms: parameters.renderInteractiveForms || false,
  787. svgFactory: new _dom_utils.DOMSVGFactory()
  788. });
  789. if (element.isRenderable) {
  790. parameters.div.appendChild(element.render());
  791. }
  792. }
  793. }
  794. }, {
  795. key: 'update',
  796. value: function update(parameters) {
  797. for (var i = 0, ii = parameters.annotations.length; i < ii; i++) {
  798. var data = parameters.annotations[i];
  799. var element = parameters.div.querySelector('[data-annotation-id="' + data.id + '"]');
  800. if (element) {
  801. _dom_utils.CustomStyle.setProp('transform', element, 'matrix(' + parameters.viewport.transform.join(',') + ')');
  802. }
  803. }
  804. parameters.div.removeAttribute('hidden');
  805. }
  806. }]);
  807. return AnnotationLayer;
  808. }();
  809. exports.AnnotationLayer = AnnotationLayer;