annotation_layer.js 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.AnnotationLayer = void 0;
  27. var _util = require("../shared/util.js");
  28. var _display_utils = require("./display_utils.js");
  29. var _annotation_storage = require("./annotation_storage.js");
  30. var _scripting_utils = require("../shared/scripting_utils.js");
  31. const DEFAULT_TAB_INDEX = 1000;
  32. const GetElementsByNameSet = new WeakSet();
  33. class AnnotationElementFactory {
  34. static create(parameters) {
  35. const subtype = parameters.data.annotationType;
  36. switch (subtype) {
  37. case _util.AnnotationType.LINK:
  38. return new LinkAnnotationElement(parameters);
  39. case _util.AnnotationType.TEXT:
  40. return new TextAnnotationElement(parameters);
  41. case _util.AnnotationType.WIDGET:
  42. const fieldType = parameters.data.fieldType;
  43. switch (fieldType) {
  44. case "Tx":
  45. return new TextWidgetAnnotationElement(parameters);
  46. case "Btn":
  47. if (parameters.data.radioButton) {
  48. return new RadioButtonWidgetAnnotationElement(parameters);
  49. } else if (parameters.data.checkBox) {
  50. return new CheckboxWidgetAnnotationElement(parameters);
  51. }
  52. return new PushButtonWidgetAnnotationElement(parameters);
  53. case "Ch":
  54. return new ChoiceWidgetAnnotationElement(parameters);
  55. }
  56. return new WidgetAnnotationElement(parameters);
  57. case _util.AnnotationType.POPUP:
  58. return new PopupAnnotationElement(parameters);
  59. case _util.AnnotationType.FREETEXT:
  60. return new FreeTextAnnotationElement(parameters);
  61. case _util.AnnotationType.LINE:
  62. return new LineAnnotationElement(parameters);
  63. case _util.AnnotationType.SQUARE:
  64. return new SquareAnnotationElement(parameters);
  65. case _util.AnnotationType.CIRCLE:
  66. return new CircleAnnotationElement(parameters);
  67. case _util.AnnotationType.POLYLINE:
  68. return new PolylineAnnotationElement(parameters);
  69. case _util.AnnotationType.CARET:
  70. return new CaretAnnotationElement(parameters);
  71. case _util.AnnotationType.INK:
  72. return new InkAnnotationElement(parameters);
  73. case _util.AnnotationType.POLYGON:
  74. return new PolygonAnnotationElement(parameters);
  75. case _util.AnnotationType.HIGHLIGHT:
  76. return new HighlightAnnotationElement(parameters);
  77. case _util.AnnotationType.UNDERLINE:
  78. return new UnderlineAnnotationElement(parameters);
  79. case _util.AnnotationType.SQUIGGLY:
  80. return new SquigglyAnnotationElement(parameters);
  81. case _util.AnnotationType.STRIKEOUT:
  82. return new StrikeOutAnnotationElement(parameters);
  83. case _util.AnnotationType.STAMP:
  84. return new StampAnnotationElement(parameters);
  85. case _util.AnnotationType.FILEATTACHMENT:
  86. return new FileAttachmentAnnotationElement(parameters);
  87. default:
  88. return new AnnotationElement(parameters);
  89. }
  90. }
  91. }
  92. class AnnotationElement {
  93. constructor(parameters, {
  94. isRenderable = false,
  95. ignoreBorder = false,
  96. createQuadrilaterals = false
  97. } = {}) {
  98. this.isRenderable = isRenderable;
  99. this.data = parameters.data;
  100. this.layer = parameters.layer;
  101. this.page = parameters.page;
  102. this.viewport = parameters.viewport;
  103. this.linkService = parameters.linkService;
  104. this.downloadManager = parameters.downloadManager;
  105. this.imageResourcesPath = parameters.imageResourcesPath;
  106. this.renderForms = parameters.renderForms;
  107. this.svgFactory = parameters.svgFactory;
  108. this.annotationStorage = parameters.annotationStorage;
  109. this.enableScripting = parameters.enableScripting;
  110. this.hasJSActions = parameters.hasJSActions;
  111. this._fieldObjects = parameters.fieldObjects;
  112. this._mouseState = parameters.mouseState;
  113. if (isRenderable) {
  114. this.container = this._createContainer(ignoreBorder);
  115. }
  116. if (createQuadrilaterals) {
  117. this.quadrilaterals = this._createQuadrilaterals(ignoreBorder);
  118. }
  119. }
  120. _createContainer(ignoreBorder = false) {
  121. const data = this.data,
  122. page = this.page,
  123. viewport = this.viewport;
  124. const container = document.createElement("section");
  125. let width = data.rect[2] - data.rect[0];
  126. let height = data.rect[3] - data.rect[1];
  127. container.setAttribute("data-annotation-id", data.id);
  128. const 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]]);
  129. container.style.transform = `matrix(${viewport.transform.join(",")})`;
  130. container.style.transformOrigin = `${-rect[0]}px ${-rect[1]}px`;
  131. if (!ignoreBorder && data.borderStyle.width > 0) {
  132. container.style.borderWidth = `${data.borderStyle.width}px`;
  133. if (data.borderStyle.style !== _util.AnnotationBorderStyleType.UNDERLINE) {
  134. width -= 2 * data.borderStyle.width;
  135. height -= 2 * data.borderStyle.width;
  136. }
  137. const horizontalRadius = data.borderStyle.horizontalCornerRadius;
  138. const verticalRadius = data.borderStyle.verticalCornerRadius;
  139. if (horizontalRadius > 0 || verticalRadius > 0) {
  140. const radius = `${horizontalRadius}px / ${verticalRadius}px`;
  141. container.style.borderRadius = radius;
  142. }
  143. switch (data.borderStyle.style) {
  144. case _util.AnnotationBorderStyleType.SOLID:
  145. container.style.borderStyle = "solid";
  146. break;
  147. case _util.AnnotationBorderStyleType.DASHED:
  148. container.style.borderStyle = "dashed";
  149. break;
  150. case _util.AnnotationBorderStyleType.BEVELED:
  151. (0, _util.warn)("Unimplemented border style: beveled");
  152. break;
  153. case _util.AnnotationBorderStyleType.INSET:
  154. (0, _util.warn)("Unimplemented border style: inset");
  155. break;
  156. case _util.AnnotationBorderStyleType.UNDERLINE:
  157. container.style.borderBottomStyle = "solid";
  158. break;
  159. default:
  160. break;
  161. }
  162. const borderColor = data.borderColor || data.color || null;
  163. if (borderColor) {
  164. container.style.borderColor = _util.Util.makeHexColor(data.color[0] | 0, data.color[1] | 0, data.color[2] | 0);
  165. } else {
  166. container.style.borderWidth = 0;
  167. }
  168. }
  169. container.style.left = `${rect[0]}px`;
  170. container.style.top = `${rect[1]}px`;
  171. container.style.width = `${width}px`;
  172. container.style.height = `${height}px`;
  173. return container;
  174. }
  175. _createQuadrilaterals(ignoreBorder = false) {
  176. if (!this.data.quadPoints) {
  177. return null;
  178. }
  179. const quadrilaterals = [];
  180. const savedRect = this.data.rect;
  181. for (const quadPoint of this.data.quadPoints) {
  182. this.data.rect = [quadPoint[2].x, quadPoint[2].y, quadPoint[1].x, quadPoint[1].y];
  183. quadrilaterals.push(this._createContainer(ignoreBorder));
  184. }
  185. this.data.rect = savedRect;
  186. return quadrilaterals;
  187. }
  188. _createPopup(trigger, data) {
  189. let container = this.container;
  190. if (this.quadrilaterals) {
  191. trigger = trigger || this.quadrilaterals;
  192. container = this.quadrilaterals[0];
  193. }
  194. if (!trigger) {
  195. trigger = document.createElement("div");
  196. trigger.style.height = container.style.height;
  197. trigger.style.width = container.style.width;
  198. container.appendChild(trigger);
  199. }
  200. const popupElement = new PopupElement({
  201. container,
  202. trigger,
  203. color: data.color,
  204. titleObj: data.titleObj,
  205. modificationDate: data.modificationDate,
  206. contentsObj: data.contentsObj,
  207. hideWrapper: true
  208. });
  209. const popup = popupElement.render();
  210. popup.style.left = container.style.width;
  211. container.appendChild(popup);
  212. }
  213. _renderQuadrilaterals(className) {
  214. for (const quadrilateral of this.quadrilaterals) {
  215. quadrilateral.className = className;
  216. }
  217. return this.quadrilaterals;
  218. }
  219. render() {
  220. (0, _util.unreachable)("Abstract method `AnnotationElement.render` called");
  221. }
  222. _getElementsByName(name, skipId = null) {
  223. const fields = [];
  224. if (this._fieldObjects) {
  225. const fieldObj = this._fieldObjects[name];
  226. if (fieldObj) {
  227. for (const {
  228. page,
  229. id,
  230. exportValues
  231. } of fieldObj) {
  232. if (page === -1) {
  233. continue;
  234. }
  235. if (id === skipId) {
  236. continue;
  237. }
  238. const exportValue = typeof exportValues === "string" ? exportValues : null;
  239. const domElement = document.getElementById(id);
  240. if (domElement && !GetElementsByNameSet.has(domElement)) {
  241. (0, _util.warn)(`_getElementsByName - element not allowed: ${id}`);
  242. continue;
  243. }
  244. fields.push({
  245. id,
  246. exportValue,
  247. domElement
  248. });
  249. }
  250. }
  251. return fields;
  252. }
  253. for (const domElement of document.getElementsByName(name)) {
  254. const {
  255. id,
  256. exportValue
  257. } = domElement;
  258. if (id === skipId) {
  259. continue;
  260. }
  261. if (!GetElementsByNameSet.has(domElement)) {
  262. continue;
  263. }
  264. fields.push({
  265. id,
  266. exportValue,
  267. domElement
  268. });
  269. }
  270. return fields;
  271. }
  272. static get platform() {
  273. const platform = typeof navigator !== "undefined" ? navigator.platform : "";
  274. return (0, _util.shadow)(this, "platform", {
  275. isWin: platform.includes("Win"),
  276. isMac: platform.includes("Mac")
  277. });
  278. }
  279. }
  280. class LinkAnnotationElement extends AnnotationElement {
  281. constructor(parameters) {
  282. const isRenderable = !!(parameters.data.url || parameters.data.dest || parameters.data.action || parameters.data.isTooltipOnly || parameters.data.resetForm || parameters.data.actions && (parameters.data.actions.Action || parameters.data.actions["Mouse Up"] || parameters.data.actions["Mouse Down"]));
  283. super(parameters, {
  284. isRenderable,
  285. createQuadrilaterals: true
  286. });
  287. }
  288. render() {
  289. const {
  290. data,
  291. linkService
  292. } = this;
  293. const link = document.createElement("a");
  294. if (data.url) {
  295. if (!linkService.addLinkAttributes) {
  296. (0, _util.warn)("LinkAnnotationElement.render - missing `addLinkAttributes`-method on the `linkService`-instance.");
  297. }
  298. linkService.addLinkAttributes?.(link, data.url, data.newWindow);
  299. } else if (data.action) {
  300. this._bindNamedAction(link, data.action);
  301. } else if (data.dest) {
  302. this._bindLink(link, data.dest);
  303. } else {
  304. let hasClickAction = false;
  305. if (data.actions && (data.actions.Action || data.actions["Mouse Up"] || data.actions["Mouse Down"]) && this.enableScripting && this.hasJSActions) {
  306. hasClickAction = true;
  307. this._bindJSAction(link, data);
  308. }
  309. if (data.resetForm) {
  310. this._bindResetFormAction(link, data.resetForm);
  311. } else if (!hasClickAction) {
  312. this._bindLink(link, "");
  313. }
  314. }
  315. if (this.quadrilaterals) {
  316. return this._renderQuadrilaterals("linkAnnotation").map((quadrilateral, index) => {
  317. const linkElement = index === 0 ? link : link.cloneNode();
  318. quadrilateral.appendChild(linkElement);
  319. return quadrilateral;
  320. });
  321. }
  322. this.container.className = "linkAnnotation";
  323. this.container.appendChild(link);
  324. return this.container;
  325. }
  326. _bindLink(link, destination) {
  327. link.href = this.linkService.getDestinationHash(destination);
  328. link.onclick = () => {
  329. if (destination) {
  330. this.linkService.goToDestination(destination);
  331. }
  332. return false;
  333. };
  334. if (destination || destination === "") {
  335. link.className = "internalLink";
  336. }
  337. }
  338. _bindNamedAction(link, action) {
  339. link.href = this.linkService.getAnchorUrl("");
  340. link.onclick = () => {
  341. this.linkService.executeNamedAction(action);
  342. return false;
  343. };
  344. link.className = "internalLink";
  345. }
  346. _bindJSAction(link, data) {
  347. link.href = this.linkService.getAnchorUrl("");
  348. const map = new Map([["Action", "onclick"], ["Mouse Up", "onmouseup"], ["Mouse Down", "onmousedown"]]);
  349. for (const name of Object.keys(data.actions)) {
  350. const jsName = map.get(name);
  351. if (!jsName) {
  352. continue;
  353. }
  354. link[jsName] = () => {
  355. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  356. source: this,
  357. detail: {
  358. id: data.id,
  359. name
  360. }
  361. });
  362. return false;
  363. };
  364. }
  365. if (!link.onclick) {
  366. link.onclick = () => false;
  367. }
  368. link.className = "internalLink";
  369. }
  370. _bindResetFormAction(link, resetForm) {
  371. const otherClickAction = link.onclick;
  372. if (!otherClickAction) {
  373. link.href = this.linkService.getAnchorUrl("");
  374. }
  375. link.className = "internalLink";
  376. if (!this._fieldObjects) {
  377. (0, _util.warn)(`_bindResetFormAction - "resetForm" action not supported, ` + "ensure that the `fieldObjects` parameter is provided.");
  378. if (!otherClickAction) {
  379. link.onclick = () => false;
  380. }
  381. return;
  382. }
  383. link.onclick = () => {
  384. if (otherClickAction) {
  385. otherClickAction();
  386. }
  387. const {
  388. fields: resetFormFields,
  389. refs: resetFormRefs,
  390. include
  391. } = resetForm;
  392. const allFields = [];
  393. if (resetFormFields.length !== 0 || resetFormRefs.length !== 0) {
  394. const fieldIds = new Set(resetFormRefs);
  395. for (const fieldName of resetFormFields) {
  396. const fields = this._fieldObjects[fieldName] || [];
  397. for (const {
  398. id
  399. } of fields) {
  400. fieldIds.add(id);
  401. }
  402. }
  403. for (const fields of Object.values(this._fieldObjects)) {
  404. for (const field of fields) {
  405. if (fieldIds.has(field.id) === include) {
  406. allFields.push(field);
  407. }
  408. }
  409. }
  410. } else {
  411. for (const fields of Object.values(this._fieldObjects)) {
  412. allFields.push(...fields);
  413. }
  414. }
  415. const storage = this.annotationStorage;
  416. const allIds = [];
  417. for (const field of allFields) {
  418. const {
  419. id
  420. } = field;
  421. allIds.push(id);
  422. switch (field.type) {
  423. case "text":
  424. {
  425. const value = field.defaultValue || "";
  426. storage.setValue(id, {
  427. value,
  428. valueAsString: value
  429. });
  430. break;
  431. }
  432. case "checkbox":
  433. case "radiobutton":
  434. {
  435. const value = field.defaultValue === field.exportValues;
  436. storage.setValue(id, {
  437. value
  438. });
  439. break;
  440. }
  441. case "combobox":
  442. case "listbox":
  443. {
  444. const value = field.defaultValue || "";
  445. storage.setValue(id, {
  446. value
  447. });
  448. break;
  449. }
  450. default:
  451. continue;
  452. }
  453. const domElement = document.getElementById(id);
  454. if (!domElement || !GetElementsByNameSet.has(domElement)) {
  455. continue;
  456. }
  457. domElement.dispatchEvent(new Event("resetform"));
  458. }
  459. if (this.enableScripting) {
  460. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  461. source: this,
  462. detail: {
  463. id: "app",
  464. ids: allIds,
  465. name: "ResetForm"
  466. }
  467. });
  468. }
  469. return false;
  470. };
  471. }
  472. }
  473. class TextAnnotationElement extends AnnotationElement {
  474. constructor(parameters) {
  475. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str);
  476. super(parameters, {
  477. isRenderable
  478. });
  479. }
  480. render() {
  481. this.container.className = "textAnnotation";
  482. const image = document.createElement("img");
  483. image.style.height = this.container.style.height;
  484. image.style.width = this.container.style.width;
  485. image.src = this.imageResourcesPath + "annotation-" + this.data.name.toLowerCase() + ".svg";
  486. image.alt = "[{{type}} Annotation]";
  487. image.dataset.l10nId = "text_annotation_type";
  488. image.dataset.l10nArgs = JSON.stringify({
  489. type: this.data.name
  490. });
  491. if (!this.data.hasPopup) {
  492. this._createPopup(image, this.data);
  493. }
  494. this.container.appendChild(image);
  495. return this.container;
  496. }
  497. }
  498. class WidgetAnnotationElement extends AnnotationElement {
  499. render() {
  500. if (this.data.alternativeText) {
  501. this.container.title = this.data.alternativeText;
  502. }
  503. return this.container;
  504. }
  505. _getKeyModifier(event) {
  506. const {
  507. isWin,
  508. isMac
  509. } = AnnotationElement.platform;
  510. return isWin && event.ctrlKey || isMac && event.metaKey;
  511. }
  512. _setEventListener(element, baseName, eventName, valueGetter) {
  513. if (baseName.includes("mouse")) {
  514. element.addEventListener(baseName, event => {
  515. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  516. source: this,
  517. detail: {
  518. id: this.data.id,
  519. name: eventName,
  520. value: valueGetter(event),
  521. shift: event.shiftKey,
  522. modifier: this._getKeyModifier(event)
  523. }
  524. });
  525. });
  526. } else {
  527. element.addEventListener(baseName, event => {
  528. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  529. source: this,
  530. detail: {
  531. id: this.data.id,
  532. name: eventName,
  533. value: event.target.checked
  534. }
  535. });
  536. });
  537. }
  538. }
  539. _setEventListeners(element, names, getter) {
  540. for (const [baseName, eventName] of names) {
  541. if (eventName === "Action" || this.data.actions?.[eventName]) {
  542. this._setEventListener(element, baseName, eventName, getter);
  543. }
  544. }
  545. }
  546. _setBackgroundColor(element) {
  547. const color = this.data.backgroundColor || null;
  548. element.style.backgroundColor = color === null ? "transparent" : _util.Util.makeHexColor(color[0], color[1], color[2]);
  549. }
  550. _dispatchEventFromSandbox(actions, jsEvent) {
  551. const setColor = (jsName, styleName, event) => {
  552. const color = event.detail[jsName];
  553. event.target.style[styleName] = _scripting_utils.ColorConverters[`${color[0]}_HTML`](color.slice(1));
  554. };
  555. const commonActions = {
  556. display: event => {
  557. const hidden = event.detail.display % 2 === 1;
  558. event.target.style.visibility = hidden ? "hidden" : "visible";
  559. this.annotationStorage.setValue(this.data.id, {
  560. hidden,
  561. print: event.detail.display === 0 || event.detail.display === 3
  562. });
  563. },
  564. print: event => {
  565. this.annotationStorage.setValue(this.data.id, {
  566. print: event.detail.print
  567. });
  568. },
  569. hidden: event => {
  570. event.target.style.visibility = event.detail.hidden ? "hidden" : "visible";
  571. this.annotationStorage.setValue(this.data.id, {
  572. hidden: event.detail.hidden
  573. });
  574. },
  575. focus: event => {
  576. setTimeout(() => event.target.focus({
  577. preventScroll: false
  578. }), 0);
  579. },
  580. userName: event => {
  581. event.target.title = event.detail.userName;
  582. },
  583. readonly: event => {
  584. if (event.detail.readonly) {
  585. event.target.setAttribute("readonly", "");
  586. } else {
  587. event.target.removeAttribute("readonly");
  588. }
  589. },
  590. required: event => {
  591. if (event.detail.required) {
  592. event.target.setAttribute("required", "");
  593. } else {
  594. event.target.removeAttribute("required");
  595. }
  596. },
  597. bgColor: event => {
  598. setColor("bgColor", "backgroundColor", event);
  599. },
  600. fillColor: event => {
  601. setColor("fillColor", "backgroundColor", event);
  602. },
  603. fgColor: event => {
  604. setColor("fgColor", "color", event);
  605. },
  606. textColor: event => {
  607. setColor("textColor", "color", event);
  608. },
  609. borderColor: event => {
  610. setColor("borderColor", "borderColor", event);
  611. },
  612. strokeColor: event => {
  613. setColor("strokeColor", "borderColor", event);
  614. }
  615. };
  616. for (const name of Object.keys(jsEvent.detail)) {
  617. const action = actions[name] || commonActions[name];
  618. if (action) {
  619. action(jsEvent);
  620. }
  621. }
  622. }
  623. }
  624. class TextWidgetAnnotationElement extends WidgetAnnotationElement {
  625. constructor(parameters) {
  626. const isRenderable = parameters.renderForms || !parameters.data.hasAppearance && !!parameters.data.fieldValue;
  627. super(parameters, {
  628. isRenderable
  629. });
  630. }
  631. setPropertyOnSiblings(base, key, value, keyInStorage) {
  632. const storage = this.annotationStorage;
  633. for (const element of this._getElementsByName(base.name, base.id)) {
  634. if (element.domElement) {
  635. element.domElement[key] = value;
  636. }
  637. storage.setValue(element.id, {
  638. [keyInStorage]: value
  639. });
  640. }
  641. }
  642. render() {
  643. const storage = this.annotationStorage;
  644. const id = this.data.id;
  645. this.container.className = "textWidgetAnnotation";
  646. let element = null;
  647. if (this.renderForms) {
  648. const storedData = storage.getValue(id, {
  649. value: this.data.fieldValue,
  650. valueAsString: this.data.fieldValue
  651. });
  652. const textContent = storedData.valueAsString || storedData.value || "";
  653. const elementData = {
  654. userValue: null,
  655. formattedValue: null,
  656. beforeInputSelectionRange: null,
  657. beforeInputValue: null
  658. };
  659. if (this.data.multiLine) {
  660. element = document.createElement("textarea");
  661. element.textContent = textContent;
  662. } else {
  663. element = document.createElement("input");
  664. element.type = "text";
  665. element.setAttribute("value", textContent);
  666. }
  667. GetElementsByNameSet.add(element);
  668. element.disabled = this.data.readOnly;
  669. element.name = this.data.fieldName;
  670. element.tabIndex = DEFAULT_TAB_INDEX;
  671. elementData.userValue = textContent;
  672. element.setAttribute("id", id);
  673. element.addEventListener("input", event => {
  674. storage.setValue(id, {
  675. value: event.target.value
  676. });
  677. this.setPropertyOnSiblings(element, "value", event.target.value, "value");
  678. });
  679. element.addEventListener("resetform", event => {
  680. const defaultValue = this.data.defaultFieldValue || "";
  681. element.value = elementData.userValue = defaultValue;
  682. delete elementData.formattedValue;
  683. });
  684. let blurListener = event => {
  685. if (elementData.formattedValue) {
  686. event.target.value = elementData.formattedValue;
  687. }
  688. event.target.scrollLeft = 0;
  689. elementData.beforeInputSelectionRange = null;
  690. };
  691. if (this.enableScripting && this.hasJSActions) {
  692. element.addEventListener("focus", event => {
  693. if (elementData.userValue) {
  694. event.target.value = elementData.userValue;
  695. }
  696. });
  697. element.addEventListener("updatefromsandbox", jsEvent => {
  698. const actions = {
  699. value(event) {
  700. elementData.userValue = event.detail.value || "";
  701. storage.setValue(id, {
  702. value: elementData.userValue.toString()
  703. });
  704. if (!elementData.formattedValue) {
  705. event.target.value = elementData.userValue;
  706. }
  707. },
  708. valueAsString(event) {
  709. elementData.formattedValue = event.detail.valueAsString || "";
  710. if (event.target !== document.activeElement) {
  711. event.target.value = elementData.formattedValue;
  712. }
  713. storage.setValue(id, {
  714. formattedValue: elementData.formattedValue
  715. });
  716. },
  717. selRange(event) {
  718. const [selStart, selEnd] = event.detail.selRange;
  719. if (selStart >= 0 && selEnd < event.target.value.length) {
  720. event.target.setSelectionRange(selStart, selEnd);
  721. }
  722. }
  723. };
  724. this._dispatchEventFromSandbox(actions, jsEvent);
  725. });
  726. element.addEventListener("keydown", event => {
  727. elementData.beforeInputValue = event.target.value;
  728. let commitKey = -1;
  729. if (event.key === "Escape") {
  730. commitKey = 0;
  731. } else if (event.key === "Enter") {
  732. commitKey = 2;
  733. } else if (event.key === "Tab") {
  734. commitKey = 3;
  735. }
  736. if (commitKey === -1) {
  737. return;
  738. }
  739. elementData.userValue = event.target.value;
  740. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  741. source: this,
  742. detail: {
  743. id,
  744. name: "Keystroke",
  745. value: event.target.value,
  746. willCommit: true,
  747. commitKey,
  748. selStart: event.target.selectionStart,
  749. selEnd: event.target.selectionEnd
  750. }
  751. });
  752. });
  753. const _blurListener = blurListener;
  754. blurListener = null;
  755. element.addEventListener("blur", event => {
  756. if (this._mouseState.isDown) {
  757. elementData.userValue = event.target.value;
  758. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  759. source: this,
  760. detail: {
  761. id,
  762. name: "Keystroke",
  763. value: event.target.value,
  764. willCommit: true,
  765. commitKey: 1,
  766. selStart: event.target.selectionStart,
  767. selEnd: event.target.selectionEnd
  768. }
  769. });
  770. }
  771. _blurListener(event);
  772. });
  773. element.addEventListener("mousedown", event => {
  774. elementData.beforeInputValue = event.target.value;
  775. elementData.beforeInputSelectionRange = null;
  776. });
  777. element.addEventListener("keyup", event => {
  778. if (event.target.selectionStart === event.target.selectionEnd) {
  779. elementData.beforeInputSelectionRange = null;
  780. }
  781. });
  782. element.addEventListener("select", event => {
  783. elementData.beforeInputSelectionRange = [event.target.selectionStart, event.target.selectionEnd];
  784. });
  785. if (this.data.actions?.Keystroke) {
  786. element.addEventListener("input", event => {
  787. let selStart = -1;
  788. let selEnd = -1;
  789. if (elementData.beforeInputSelectionRange) {
  790. [selStart, selEnd] = elementData.beforeInputSelectionRange;
  791. }
  792. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  793. source: this,
  794. detail: {
  795. id,
  796. name: "Keystroke",
  797. value: elementData.beforeInputValue,
  798. change: event.data,
  799. willCommit: false,
  800. selStart,
  801. selEnd
  802. }
  803. });
  804. });
  805. }
  806. this._setEventListeners(element, [["focus", "Focus"], ["blur", "Blur"], ["mousedown", "Mouse Down"], ["mouseenter", "Mouse Enter"], ["mouseleave", "Mouse Exit"], ["mouseup", "Mouse Up"]], event => event.target.value);
  807. }
  808. if (blurListener) {
  809. element.addEventListener("blur", blurListener);
  810. }
  811. if (this.data.maxLen !== null) {
  812. element.maxLength = this.data.maxLen;
  813. }
  814. if (this.data.comb) {
  815. const fieldWidth = this.data.rect[2] - this.data.rect[0];
  816. const combWidth = fieldWidth / this.data.maxLen;
  817. element.classList.add("comb");
  818. element.style.letterSpacing = `calc(${combWidth}px - 1ch)`;
  819. }
  820. } else {
  821. element = document.createElement("div");
  822. element.textContent = this.data.fieldValue;
  823. element.style.verticalAlign = "middle";
  824. element.style.display = "table-cell";
  825. }
  826. this._setTextStyle(element);
  827. this._setBackgroundColor(element);
  828. this.container.appendChild(element);
  829. return this.container;
  830. }
  831. _setTextStyle(element) {
  832. const TEXT_ALIGNMENT = ["left", "center", "right"];
  833. const {
  834. fontSize,
  835. fontColor
  836. } = this.data.defaultAppearanceData;
  837. const style = element.style;
  838. if (fontSize) {
  839. style.fontSize = `${fontSize}px`;
  840. }
  841. style.color = _util.Util.makeHexColor(fontColor[0], fontColor[1], fontColor[2]);
  842. if (this.data.textAlignment !== null) {
  843. style.textAlign = TEXT_ALIGNMENT[this.data.textAlignment];
  844. }
  845. }
  846. }
  847. class CheckboxWidgetAnnotationElement extends WidgetAnnotationElement {
  848. constructor(parameters) {
  849. super(parameters, {
  850. isRenderable: parameters.renderForms
  851. });
  852. }
  853. render() {
  854. const storage = this.annotationStorage;
  855. const data = this.data;
  856. const id = data.id;
  857. let value = storage.getValue(id, {
  858. value: data.exportValue === data.fieldValue
  859. }).value;
  860. if (typeof value === "string") {
  861. value = value !== "Off";
  862. storage.setValue(id, {
  863. value
  864. });
  865. }
  866. this.container.className = "buttonWidgetAnnotation checkBox";
  867. const element = document.createElement("input");
  868. GetElementsByNameSet.add(element);
  869. element.disabled = data.readOnly;
  870. element.type = "checkbox";
  871. element.name = data.fieldName;
  872. if (value) {
  873. element.setAttribute("checked", true);
  874. }
  875. element.setAttribute("id", id);
  876. element.setAttribute("exportValue", data.exportValue);
  877. element.tabIndex = DEFAULT_TAB_INDEX;
  878. element.addEventListener("change", event => {
  879. const {
  880. name,
  881. checked
  882. } = event.target;
  883. for (const checkbox of this._getElementsByName(name, id)) {
  884. const curChecked = checked && checkbox.exportValue === data.exportValue;
  885. if (checkbox.domElement) {
  886. checkbox.domElement.checked = curChecked;
  887. }
  888. storage.setValue(checkbox.id, {
  889. value: curChecked
  890. });
  891. }
  892. storage.setValue(id, {
  893. value: checked
  894. });
  895. });
  896. element.addEventListener("resetform", event => {
  897. const defaultValue = data.defaultFieldValue || "Off";
  898. event.target.checked = defaultValue === data.exportValue;
  899. });
  900. if (this.enableScripting && this.hasJSActions) {
  901. element.addEventListener("updatefromsandbox", jsEvent => {
  902. const actions = {
  903. value(event) {
  904. event.target.checked = event.detail.value !== "Off";
  905. storage.setValue(id, {
  906. value: event.target.checked
  907. });
  908. }
  909. };
  910. this._dispatchEventFromSandbox(actions, jsEvent);
  911. });
  912. this._setEventListeners(element, [["change", "Validate"], ["change", "Action"], ["focus", "Focus"], ["blur", "Blur"], ["mousedown", "Mouse Down"], ["mouseenter", "Mouse Enter"], ["mouseleave", "Mouse Exit"], ["mouseup", "Mouse Up"]], event => event.target.checked);
  913. }
  914. this._setBackgroundColor(element);
  915. this.container.appendChild(element);
  916. return this.container;
  917. }
  918. }
  919. class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement {
  920. constructor(parameters) {
  921. super(parameters, {
  922. isRenderable: parameters.renderForms
  923. });
  924. }
  925. render() {
  926. this.container.className = "buttonWidgetAnnotation radioButton";
  927. const storage = this.annotationStorage;
  928. const data = this.data;
  929. const id = data.id;
  930. let value = storage.getValue(id, {
  931. value: data.fieldValue === data.buttonValue
  932. }).value;
  933. if (typeof value === "string") {
  934. value = value !== data.buttonValue;
  935. storage.setValue(id, {
  936. value
  937. });
  938. }
  939. const element = document.createElement("input");
  940. GetElementsByNameSet.add(element);
  941. element.disabled = data.readOnly;
  942. element.type = "radio";
  943. element.name = data.fieldName;
  944. if (value) {
  945. element.setAttribute("checked", true);
  946. }
  947. element.setAttribute("id", id);
  948. element.tabIndex = DEFAULT_TAB_INDEX;
  949. element.addEventListener("change", event => {
  950. const {
  951. name,
  952. checked
  953. } = event.target;
  954. for (const radio of this._getElementsByName(name, id)) {
  955. storage.setValue(radio.id, {
  956. value: false
  957. });
  958. }
  959. storage.setValue(id, {
  960. value: checked
  961. });
  962. });
  963. element.addEventListener("resetform", event => {
  964. const defaultValue = data.defaultFieldValue;
  965. event.target.checked = defaultValue !== null && defaultValue !== undefined && defaultValue === data.buttonValue;
  966. });
  967. if (this.enableScripting && this.hasJSActions) {
  968. const pdfButtonValue = data.buttonValue;
  969. element.addEventListener("updatefromsandbox", jsEvent => {
  970. const actions = {
  971. value: event => {
  972. const checked = pdfButtonValue === event.detail.value;
  973. for (const radio of this._getElementsByName(event.target.name)) {
  974. const curChecked = checked && radio.id === id;
  975. if (radio.domElement) {
  976. radio.domElement.checked = curChecked;
  977. }
  978. storage.setValue(radio.id, {
  979. value: curChecked
  980. });
  981. }
  982. }
  983. };
  984. this._dispatchEventFromSandbox(actions, jsEvent);
  985. });
  986. this._setEventListeners(element, [["change", "Validate"], ["change", "Action"], ["focus", "Focus"], ["blur", "Blur"], ["mousedown", "Mouse Down"], ["mouseenter", "Mouse Enter"], ["mouseleave", "Mouse Exit"], ["mouseup", "Mouse Up"]], event => event.target.checked);
  987. }
  988. this._setBackgroundColor(element);
  989. this.container.appendChild(element);
  990. return this.container;
  991. }
  992. }
  993. class PushButtonWidgetAnnotationElement extends LinkAnnotationElement {
  994. render() {
  995. const container = super.render();
  996. container.className = "buttonWidgetAnnotation pushButton";
  997. if (this.data.alternativeText) {
  998. container.title = this.data.alternativeText;
  999. }
  1000. return container;
  1001. }
  1002. }
  1003. class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
  1004. constructor(parameters) {
  1005. super(parameters, {
  1006. isRenderable: parameters.renderForms
  1007. });
  1008. }
  1009. render() {
  1010. this.container.className = "choiceWidgetAnnotation";
  1011. const storage = this.annotationStorage;
  1012. const id = this.data.id;
  1013. storage.getValue(id, {
  1014. value: this.data.fieldValue.length > 0 ? this.data.fieldValue[0] : undefined
  1015. });
  1016. let {
  1017. fontSize
  1018. } = this.data.defaultAppearanceData;
  1019. if (!fontSize) {
  1020. fontSize = 9;
  1021. }
  1022. const fontSizeStyle = `calc(${fontSize}px * var(--zoom-factor))`;
  1023. const selectElement = document.createElement("select");
  1024. GetElementsByNameSet.add(selectElement);
  1025. selectElement.disabled = this.data.readOnly;
  1026. selectElement.name = this.data.fieldName;
  1027. selectElement.setAttribute("id", id);
  1028. selectElement.tabIndex = DEFAULT_TAB_INDEX;
  1029. selectElement.style.fontSize = `${fontSize}px`;
  1030. if (!this.data.combo) {
  1031. selectElement.size = this.data.options.length;
  1032. if (this.data.multiSelect) {
  1033. selectElement.multiple = true;
  1034. }
  1035. }
  1036. selectElement.addEventListener("resetform", event => {
  1037. const defaultValue = this.data.defaultFieldValue;
  1038. for (const option of selectElement.options) {
  1039. option.selected = option.value === defaultValue;
  1040. }
  1041. });
  1042. for (const option of this.data.options) {
  1043. const optionElement = document.createElement("option");
  1044. optionElement.textContent = option.displayValue;
  1045. optionElement.value = option.exportValue;
  1046. if (this.data.combo) {
  1047. optionElement.style.fontSize = fontSizeStyle;
  1048. }
  1049. if (this.data.fieldValue.includes(option.exportValue)) {
  1050. optionElement.setAttribute("selected", true);
  1051. }
  1052. selectElement.appendChild(optionElement);
  1053. }
  1054. const getValue = (event, isExport) => {
  1055. const name = isExport ? "value" : "textContent";
  1056. const options = event.target.options;
  1057. if (!event.target.multiple) {
  1058. return options.selectedIndex === -1 ? null : options[options.selectedIndex][name];
  1059. }
  1060. return Array.prototype.filter.call(options, option => option.selected).map(option => option[name]);
  1061. };
  1062. const getItems = event => {
  1063. const options = event.target.options;
  1064. return Array.prototype.map.call(options, option => {
  1065. return {
  1066. displayValue: option.textContent,
  1067. exportValue: option.value
  1068. };
  1069. });
  1070. };
  1071. if (this.enableScripting && this.hasJSActions) {
  1072. selectElement.addEventListener("updatefromsandbox", jsEvent => {
  1073. const actions = {
  1074. value(event) {
  1075. const value = event.detail.value;
  1076. const values = new Set(Array.isArray(value) ? value : [value]);
  1077. for (const option of selectElement.options) {
  1078. option.selected = values.has(option.value);
  1079. }
  1080. storage.setValue(id, {
  1081. value: getValue(event, true)
  1082. });
  1083. },
  1084. multipleSelection(event) {
  1085. selectElement.multiple = true;
  1086. },
  1087. remove(event) {
  1088. const options = selectElement.options;
  1089. const index = event.detail.remove;
  1090. options[index].selected = false;
  1091. selectElement.remove(index);
  1092. if (options.length > 0) {
  1093. const i = Array.prototype.findIndex.call(options, option => option.selected);
  1094. if (i === -1) {
  1095. options[0].selected = true;
  1096. }
  1097. }
  1098. storage.setValue(id, {
  1099. value: getValue(event, true),
  1100. items: getItems(event)
  1101. });
  1102. },
  1103. clear(event) {
  1104. while (selectElement.length !== 0) {
  1105. selectElement.remove(0);
  1106. }
  1107. storage.setValue(id, {
  1108. value: null,
  1109. items: []
  1110. });
  1111. },
  1112. insert(event) {
  1113. const {
  1114. index,
  1115. displayValue,
  1116. exportValue
  1117. } = event.detail.insert;
  1118. const optionElement = document.createElement("option");
  1119. optionElement.textContent = displayValue;
  1120. optionElement.value = exportValue;
  1121. selectElement.insertBefore(optionElement, selectElement.children[index]);
  1122. storage.setValue(id, {
  1123. value: getValue(event, true),
  1124. items: getItems(event)
  1125. });
  1126. },
  1127. items(event) {
  1128. const {
  1129. items
  1130. } = event.detail;
  1131. while (selectElement.length !== 0) {
  1132. selectElement.remove(0);
  1133. }
  1134. for (const item of items) {
  1135. const {
  1136. displayValue,
  1137. exportValue
  1138. } = item;
  1139. const optionElement = document.createElement("option");
  1140. optionElement.textContent = displayValue;
  1141. optionElement.value = exportValue;
  1142. selectElement.appendChild(optionElement);
  1143. }
  1144. if (selectElement.options.length > 0) {
  1145. selectElement.options[0].selected = true;
  1146. }
  1147. storage.setValue(id, {
  1148. value: getValue(event, true),
  1149. items: getItems(event)
  1150. });
  1151. },
  1152. indices(event) {
  1153. const indices = new Set(event.detail.indices);
  1154. for (const option of event.target.options) {
  1155. option.selected = indices.has(option.index);
  1156. }
  1157. storage.setValue(id, {
  1158. value: getValue(event, true)
  1159. });
  1160. },
  1161. editable(event) {
  1162. event.target.disabled = !event.detail.editable;
  1163. }
  1164. };
  1165. this._dispatchEventFromSandbox(actions, jsEvent);
  1166. });
  1167. selectElement.addEventListener("input", event => {
  1168. const exportValue = getValue(event, true);
  1169. const value = getValue(event, false);
  1170. storage.setValue(id, {
  1171. value: exportValue
  1172. });
  1173. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  1174. source: this,
  1175. detail: {
  1176. id,
  1177. name: "Keystroke",
  1178. value,
  1179. changeEx: exportValue,
  1180. willCommit: true,
  1181. commitKey: 1,
  1182. keyDown: false
  1183. }
  1184. });
  1185. });
  1186. this._setEventListeners(selectElement, [["focus", "Focus"], ["blur", "Blur"], ["mousedown", "Mouse Down"], ["mouseenter", "Mouse Enter"], ["mouseleave", "Mouse Exit"], ["mouseup", "Mouse Up"], ["input", "Action"]], event => event.target.checked);
  1187. } else {
  1188. selectElement.addEventListener("input", function (event) {
  1189. storage.setValue(id, {
  1190. value: getValue(event)
  1191. });
  1192. });
  1193. }
  1194. this._setBackgroundColor(selectElement);
  1195. this.container.appendChild(selectElement);
  1196. return this.container;
  1197. }
  1198. }
  1199. class PopupAnnotationElement extends AnnotationElement {
  1200. constructor(parameters) {
  1201. const isRenderable = !!(parameters.data.titleObj?.str || parameters.data.contentsObj?.str);
  1202. super(parameters, {
  1203. isRenderable
  1204. });
  1205. }
  1206. render() {
  1207. const IGNORE_TYPES = ["Line", "Square", "Circle", "PolyLine", "Polygon", "Ink"];
  1208. this.container.className = "popupAnnotation";
  1209. if (IGNORE_TYPES.includes(this.data.parentType)) {
  1210. return this.container;
  1211. }
  1212. const selector = `[data-annotation-id="${this.data.parentId}"]`;
  1213. const parentElements = this.layer.querySelectorAll(selector);
  1214. if (parentElements.length === 0) {
  1215. return this.container;
  1216. }
  1217. const popup = new PopupElement({
  1218. container: this.container,
  1219. trigger: Array.from(parentElements),
  1220. color: this.data.color,
  1221. titleObj: this.data.titleObj,
  1222. modificationDate: this.data.modificationDate,
  1223. contentsObj: this.data.contentsObj
  1224. });
  1225. const page = this.page;
  1226. const rect = _util.Util.normalizeRect([this.data.parentRect[0], page.view[3] - this.data.parentRect[1] + page.view[1], this.data.parentRect[2], page.view[3] - this.data.parentRect[3] + page.view[1]]);
  1227. const popupLeft = rect[0] + this.data.parentRect[2] - this.data.parentRect[0];
  1228. const popupTop = rect[1];
  1229. this.container.style.transformOrigin = `${-popupLeft}px ${-popupTop}px`;
  1230. this.container.style.left = `${popupLeft}px`;
  1231. this.container.style.top = `${popupTop}px`;
  1232. this.container.appendChild(popup.render());
  1233. return this.container;
  1234. }
  1235. }
  1236. class PopupElement {
  1237. constructor(parameters) {
  1238. this.container = parameters.container;
  1239. this.trigger = parameters.trigger;
  1240. this.color = parameters.color;
  1241. this.titleObj = parameters.titleObj;
  1242. this.modificationDate = parameters.modificationDate;
  1243. this.contentsObj = parameters.contentsObj;
  1244. this.hideWrapper = parameters.hideWrapper || false;
  1245. this.pinned = false;
  1246. }
  1247. render() {
  1248. const BACKGROUND_ENLIGHT = 0.7;
  1249. const wrapper = document.createElement("div");
  1250. wrapper.className = "popupWrapper";
  1251. this.hideElement = this.hideWrapper ? wrapper : this.container;
  1252. this.hideElement.hidden = true;
  1253. const popup = document.createElement("div");
  1254. popup.className = "popup";
  1255. const color = this.color;
  1256. if (color) {
  1257. const r = BACKGROUND_ENLIGHT * (255 - color[0]) + color[0];
  1258. const g = BACKGROUND_ENLIGHT * (255 - color[1]) + color[1];
  1259. const b = BACKGROUND_ENLIGHT * (255 - color[2]) + color[2];
  1260. popup.style.backgroundColor = _util.Util.makeHexColor(r | 0, g | 0, b | 0);
  1261. }
  1262. const title = document.createElement("h1");
  1263. title.dir = this.titleObj.dir;
  1264. title.textContent = this.titleObj.str;
  1265. popup.appendChild(title);
  1266. const dateObject = _display_utils.PDFDateString.toDateObject(this.modificationDate);
  1267. if (dateObject) {
  1268. const modificationDate = document.createElement("span");
  1269. modificationDate.textContent = "{{date}}, {{time}}";
  1270. modificationDate.dataset.l10nId = "annotation_date_string";
  1271. modificationDate.dataset.l10nArgs = JSON.stringify({
  1272. date: dateObject.toLocaleDateString(),
  1273. time: dateObject.toLocaleTimeString()
  1274. });
  1275. popup.appendChild(modificationDate);
  1276. }
  1277. const contents = this._formatContents(this.contentsObj);
  1278. popup.appendChild(contents);
  1279. if (!Array.isArray(this.trigger)) {
  1280. this.trigger = [this.trigger];
  1281. }
  1282. for (const element of this.trigger) {
  1283. element.addEventListener("click", this._toggle.bind(this));
  1284. element.addEventListener("mouseover", this._show.bind(this, false));
  1285. element.addEventListener("mouseout", this._hide.bind(this, false));
  1286. }
  1287. popup.addEventListener("click", this._hide.bind(this, true));
  1288. wrapper.appendChild(popup);
  1289. return wrapper;
  1290. }
  1291. _formatContents({
  1292. str,
  1293. dir
  1294. }) {
  1295. const p = document.createElement("p");
  1296. p.dir = dir;
  1297. const lines = str.split(/(?:\r\n?|\n)/);
  1298. for (let i = 0, ii = lines.length; i < ii; ++i) {
  1299. const line = lines[i];
  1300. p.appendChild(document.createTextNode(line));
  1301. if (i < ii - 1) {
  1302. p.appendChild(document.createElement("br"));
  1303. }
  1304. }
  1305. return p;
  1306. }
  1307. _toggle() {
  1308. if (this.pinned) {
  1309. this._hide(true);
  1310. } else {
  1311. this._show(true);
  1312. }
  1313. }
  1314. _show(pin = false) {
  1315. if (pin) {
  1316. this.pinned = true;
  1317. }
  1318. if (this.hideElement.hidden) {
  1319. this.hideElement.hidden = false;
  1320. this.container.style.zIndex += 1;
  1321. }
  1322. }
  1323. _hide(unpin = true) {
  1324. if (unpin) {
  1325. this.pinned = false;
  1326. }
  1327. if (!this.hideElement.hidden && !this.pinned) {
  1328. this.hideElement.hidden = true;
  1329. this.container.style.zIndex -= 1;
  1330. }
  1331. }
  1332. }
  1333. class FreeTextAnnotationElement extends AnnotationElement {
  1334. constructor(parameters) {
  1335. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str);
  1336. super(parameters, {
  1337. isRenderable,
  1338. ignoreBorder: true
  1339. });
  1340. }
  1341. render() {
  1342. this.container.className = "freeTextAnnotation";
  1343. if (!this.data.hasPopup) {
  1344. this._createPopup(null, this.data);
  1345. }
  1346. return this.container;
  1347. }
  1348. }
  1349. class LineAnnotationElement extends AnnotationElement {
  1350. constructor(parameters) {
  1351. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str);
  1352. super(parameters, {
  1353. isRenderable,
  1354. ignoreBorder: true
  1355. });
  1356. }
  1357. render() {
  1358. this.container.className = "lineAnnotation";
  1359. const data = this.data;
  1360. const width = data.rect[2] - data.rect[0];
  1361. const height = data.rect[3] - data.rect[1];
  1362. const svg = this.svgFactory.create(width, height);
  1363. const line = this.svgFactory.createElement("svg:line");
  1364. line.setAttribute("x1", data.rect[2] - data.lineCoordinates[0]);
  1365. line.setAttribute("y1", data.rect[3] - data.lineCoordinates[1]);
  1366. line.setAttribute("x2", data.rect[2] - data.lineCoordinates[2]);
  1367. line.setAttribute("y2", data.rect[3] - data.lineCoordinates[3]);
  1368. line.setAttribute("stroke-width", data.borderStyle.width || 1);
  1369. line.setAttribute("stroke", "transparent");
  1370. svg.appendChild(line);
  1371. this.container.append(svg);
  1372. this._createPopup(line, data);
  1373. return this.container;
  1374. }
  1375. }
  1376. class SquareAnnotationElement extends AnnotationElement {
  1377. constructor(parameters) {
  1378. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str);
  1379. super(parameters, {
  1380. isRenderable,
  1381. ignoreBorder: true
  1382. });
  1383. }
  1384. render() {
  1385. this.container.className = "squareAnnotation";
  1386. const data = this.data;
  1387. const width = data.rect[2] - data.rect[0];
  1388. const height = data.rect[3] - data.rect[1];
  1389. const svg = this.svgFactory.create(width, height);
  1390. const borderWidth = data.borderStyle.width;
  1391. const square = this.svgFactory.createElement("svg:rect");
  1392. square.setAttribute("x", borderWidth / 2);
  1393. square.setAttribute("y", borderWidth / 2);
  1394. square.setAttribute("width", width - borderWidth);
  1395. square.setAttribute("height", height - borderWidth);
  1396. square.setAttribute("stroke-width", borderWidth || 1);
  1397. square.setAttribute("stroke", "transparent");
  1398. square.setAttribute("fill", "none");
  1399. svg.appendChild(square);
  1400. this.container.append(svg);
  1401. this._createPopup(square, data);
  1402. return this.container;
  1403. }
  1404. }
  1405. class CircleAnnotationElement extends AnnotationElement {
  1406. constructor(parameters) {
  1407. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str);
  1408. super(parameters, {
  1409. isRenderable,
  1410. ignoreBorder: true
  1411. });
  1412. }
  1413. render() {
  1414. this.container.className = "circleAnnotation";
  1415. const data = this.data;
  1416. const width = data.rect[2] - data.rect[0];
  1417. const height = data.rect[3] - data.rect[1];
  1418. const svg = this.svgFactory.create(width, height);
  1419. const borderWidth = data.borderStyle.width;
  1420. const circle = this.svgFactory.createElement("svg:ellipse");
  1421. circle.setAttribute("cx", width / 2);
  1422. circle.setAttribute("cy", height / 2);
  1423. circle.setAttribute("rx", width / 2 - borderWidth / 2);
  1424. circle.setAttribute("ry", height / 2 - borderWidth / 2);
  1425. circle.setAttribute("stroke-width", borderWidth || 1);
  1426. circle.setAttribute("stroke", "transparent");
  1427. circle.setAttribute("fill", "none");
  1428. svg.appendChild(circle);
  1429. this.container.append(svg);
  1430. this._createPopup(circle, data);
  1431. return this.container;
  1432. }
  1433. }
  1434. class PolylineAnnotationElement extends AnnotationElement {
  1435. constructor(parameters) {
  1436. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str);
  1437. super(parameters, {
  1438. isRenderable,
  1439. ignoreBorder: true
  1440. });
  1441. this.containerClassName = "polylineAnnotation";
  1442. this.svgElementName = "svg:polyline";
  1443. }
  1444. render() {
  1445. this.container.className = this.containerClassName;
  1446. const data = this.data;
  1447. const width = data.rect[2] - data.rect[0];
  1448. const height = data.rect[3] - data.rect[1];
  1449. const svg = this.svgFactory.create(width, height);
  1450. let points = [];
  1451. for (const coordinate of data.vertices) {
  1452. const x = coordinate.x - data.rect[0];
  1453. const y = data.rect[3] - coordinate.y;
  1454. points.push(x + "," + y);
  1455. }
  1456. points = points.join(" ");
  1457. const polyline = this.svgFactory.createElement(this.svgElementName);
  1458. polyline.setAttribute("points", points);
  1459. polyline.setAttribute("stroke-width", data.borderStyle.width || 1);
  1460. polyline.setAttribute("stroke", "transparent");
  1461. polyline.setAttribute("fill", "none");
  1462. svg.appendChild(polyline);
  1463. this.container.append(svg);
  1464. this._createPopup(polyline, data);
  1465. return this.container;
  1466. }
  1467. }
  1468. class PolygonAnnotationElement extends PolylineAnnotationElement {
  1469. constructor(parameters) {
  1470. super(parameters);
  1471. this.containerClassName = "polygonAnnotation";
  1472. this.svgElementName = "svg:polygon";
  1473. }
  1474. }
  1475. class CaretAnnotationElement extends AnnotationElement {
  1476. constructor(parameters) {
  1477. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str);
  1478. super(parameters, {
  1479. isRenderable,
  1480. ignoreBorder: true
  1481. });
  1482. }
  1483. render() {
  1484. this.container.className = "caretAnnotation";
  1485. if (!this.data.hasPopup) {
  1486. this._createPopup(null, this.data);
  1487. }
  1488. return this.container;
  1489. }
  1490. }
  1491. class InkAnnotationElement extends AnnotationElement {
  1492. constructor(parameters) {
  1493. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str);
  1494. super(parameters, {
  1495. isRenderable,
  1496. ignoreBorder: true
  1497. });
  1498. this.containerClassName = "inkAnnotation";
  1499. this.svgElementName = "svg:polyline";
  1500. }
  1501. render() {
  1502. this.container.className = this.containerClassName;
  1503. const data = this.data;
  1504. const width = data.rect[2] - data.rect[0];
  1505. const height = data.rect[3] - data.rect[1];
  1506. const svg = this.svgFactory.create(width, height);
  1507. for (const inkList of data.inkLists) {
  1508. let points = [];
  1509. for (const coordinate of inkList) {
  1510. const x = coordinate.x - data.rect[0];
  1511. const y = data.rect[3] - coordinate.y;
  1512. points.push(`${x},${y}`);
  1513. }
  1514. points = points.join(" ");
  1515. const polyline = this.svgFactory.createElement(this.svgElementName);
  1516. polyline.setAttribute("points", points);
  1517. polyline.setAttribute("stroke-width", data.borderStyle.width || 1);
  1518. polyline.setAttribute("stroke", "transparent");
  1519. polyline.setAttribute("fill", "none");
  1520. this._createPopup(polyline, data);
  1521. svg.appendChild(polyline);
  1522. }
  1523. this.container.append(svg);
  1524. return this.container;
  1525. }
  1526. }
  1527. class HighlightAnnotationElement extends AnnotationElement {
  1528. constructor(parameters) {
  1529. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str);
  1530. super(parameters, {
  1531. isRenderable,
  1532. ignoreBorder: true,
  1533. createQuadrilaterals: true
  1534. });
  1535. }
  1536. render() {
  1537. if (!this.data.hasPopup) {
  1538. this._createPopup(null, this.data);
  1539. }
  1540. if (this.quadrilaterals) {
  1541. return this._renderQuadrilaterals("highlightAnnotation");
  1542. }
  1543. this.container.className = "highlightAnnotation";
  1544. return this.container;
  1545. }
  1546. }
  1547. class UnderlineAnnotationElement extends AnnotationElement {
  1548. constructor(parameters) {
  1549. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str);
  1550. super(parameters, {
  1551. isRenderable,
  1552. ignoreBorder: true,
  1553. createQuadrilaterals: true
  1554. });
  1555. }
  1556. render() {
  1557. if (!this.data.hasPopup) {
  1558. this._createPopup(null, this.data);
  1559. }
  1560. if (this.quadrilaterals) {
  1561. return this._renderQuadrilaterals("underlineAnnotation");
  1562. }
  1563. this.container.className = "underlineAnnotation";
  1564. return this.container;
  1565. }
  1566. }
  1567. class SquigglyAnnotationElement extends AnnotationElement {
  1568. constructor(parameters) {
  1569. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str);
  1570. super(parameters, {
  1571. isRenderable,
  1572. ignoreBorder: true,
  1573. createQuadrilaterals: true
  1574. });
  1575. }
  1576. render() {
  1577. if (!this.data.hasPopup) {
  1578. this._createPopup(null, this.data);
  1579. }
  1580. if (this.quadrilaterals) {
  1581. return this._renderQuadrilaterals("squigglyAnnotation");
  1582. }
  1583. this.container.className = "squigglyAnnotation";
  1584. return this.container;
  1585. }
  1586. }
  1587. class StrikeOutAnnotationElement extends AnnotationElement {
  1588. constructor(parameters) {
  1589. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str);
  1590. super(parameters, {
  1591. isRenderable,
  1592. ignoreBorder: true,
  1593. createQuadrilaterals: true
  1594. });
  1595. }
  1596. render() {
  1597. if (!this.data.hasPopup) {
  1598. this._createPopup(null, this.data);
  1599. }
  1600. if (this.quadrilaterals) {
  1601. return this._renderQuadrilaterals("strikeoutAnnotation");
  1602. }
  1603. this.container.className = "strikeoutAnnotation";
  1604. return this.container;
  1605. }
  1606. }
  1607. class StampAnnotationElement extends AnnotationElement {
  1608. constructor(parameters) {
  1609. const isRenderable = !!(parameters.data.hasPopup || parameters.data.titleObj?.str || parameters.data.contentsObj?.str);
  1610. super(parameters, {
  1611. isRenderable,
  1612. ignoreBorder: true
  1613. });
  1614. }
  1615. render() {
  1616. this.container.className = "stampAnnotation";
  1617. if (!this.data.hasPopup) {
  1618. this._createPopup(null, this.data);
  1619. }
  1620. return this.container;
  1621. }
  1622. }
  1623. class FileAttachmentAnnotationElement extends AnnotationElement {
  1624. constructor(parameters) {
  1625. super(parameters, {
  1626. isRenderable: true
  1627. });
  1628. const {
  1629. filename,
  1630. content
  1631. } = this.data.file;
  1632. this.filename = (0, _display_utils.getFilenameFromUrl)(filename);
  1633. this.content = content;
  1634. this.linkService.eventBus?.dispatch("fileattachmentannotation", {
  1635. source: this,
  1636. id: (0, _util.stringToPDFString)(filename),
  1637. filename,
  1638. content
  1639. });
  1640. }
  1641. render() {
  1642. this.container.className = "fileAttachmentAnnotation";
  1643. const trigger = document.createElement("div");
  1644. trigger.style.height = this.container.style.height;
  1645. trigger.style.width = this.container.style.width;
  1646. trigger.addEventListener("dblclick", this._download.bind(this));
  1647. if (!this.data.hasPopup && (this.data.titleObj?.str || this.data.contentsObj?.str)) {
  1648. this._createPopup(trigger, this.data);
  1649. }
  1650. this.container.appendChild(trigger);
  1651. return this.container;
  1652. }
  1653. _download() {
  1654. this.downloadManager?.openOrDownloadData(this.container, this.content, this.filename);
  1655. }
  1656. }
  1657. class AnnotationLayer {
  1658. static render(parameters) {
  1659. const sortedAnnotations = [],
  1660. popupAnnotations = [];
  1661. for (const data of parameters.annotations) {
  1662. if (!data) {
  1663. continue;
  1664. }
  1665. if (data.annotationType === _util.AnnotationType.POPUP) {
  1666. popupAnnotations.push(data);
  1667. continue;
  1668. }
  1669. sortedAnnotations.push(data);
  1670. }
  1671. if (popupAnnotations.length) {
  1672. sortedAnnotations.push(...popupAnnotations);
  1673. }
  1674. for (const data of sortedAnnotations) {
  1675. const element = AnnotationElementFactory.create({
  1676. data,
  1677. layer: parameters.div,
  1678. page: parameters.page,
  1679. viewport: parameters.viewport,
  1680. linkService: parameters.linkService,
  1681. downloadManager: parameters.downloadManager,
  1682. imageResourcesPath: parameters.imageResourcesPath || "",
  1683. renderForms: parameters.renderForms !== false,
  1684. svgFactory: new _display_utils.DOMSVGFactory(),
  1685. annotationStorage: parameters.annotationStorage || new _annotation_storage.AnnotationStorage(),
  1686. enableScripting: parameters.enableScripting,
  1687. hasJSActions: parameters.hasJSActions,
  1688. fieldObjects: parameters.fieldObjects,
  1689. mouseState: parameters.mouseState || {
  1690. isDown: false
  1691. }
  1692. });
  1693. if (element.isRenderable) {
  1694. const rendered = element.render();
  1695. if (data.hidden) {
  1696. rendered.style.visibility = "hidden";
  1697. }
  1698. if (Array.isArray(rendered)) {
  1699. for (const renderedElement of rendered) {
  1700. parameters.div.appendChild(renderedElement);
  1701. }
  1702. } else {
  1703. if (element instanceof PopupAnnotationElement) {
  1704. parameters.div.prepend(rendered);
  1705. } else {
  1706. parameters.div.appendChild(rendered);
  1707. }
  1708. }
  1709. }
  1710. }
  1711. }
  1712. static update(parameters) {
  1713. const transform = `matrix(${parameters.viewport.transform.join(",")})`;
  1714. for (const data of parameters.annotations) {
  1715. const elements = parameters.div.querySelectorAll(`[data-annotation-id="${data.id}"]`);
  1716. if (elements) {
  1717. for (const element of elements) {
  1718. element.style.transform = transform;
  1719. }
  1720. }
  1721. }
  1722. parameters.div.hidden = false;
  1723. }
  1724. }
  1725. exports.AnnotationLayer = AnnotationLayer;