annotation_layer.js 68 KB

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