annotation_layer.js 68 KB

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