annotation_layer.js 36 KB

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