annotation_layer.js 65 KB

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