annotation_layer.js 38 KB

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