annotation_layer.js 68 KB

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