xfa_layer.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.XfaLayer = void 0;
  27. var _xfa_text = require("./xfa_text.js");
  28. class XfaLayer {
  29. static setupStorage(html, id, element, storage, intent) {
  30. const storedData = storage.getValue(id, {
  31. value: null
  32. });
  33. switch (element.name) {
  34. case "textarea":
  35. if (storedData.value !== null) {
  36. html.textContent = storedData.value;
  37. }
  38. if (intent === "print") {
  39. break;
  40. }
  41. html.addEventListener("input", event => {
  42. storage.setValue(id, {
  43. value: event.target.value
  44. });
  45. });
  46. break;
  47. case "input":
  48. if (element.attributes.type === "radio" || element.attributes.type === "checkbox") {
  49. if (storedData.value === element.attributes.xfaOn) {
  50. html.setAttribute("checked", true);
  51. } else if (storedData.value === element.attributes.xfaOff) {
  52. html.removeAttribute("checked");
  53. }
  54. if (intent === "print") {
  55. break;
  56. }
  57. html.addEventListener("change", event => {
  58. storage.setValue(id, {
  59. value: event.target.checked ? event.target.getAttribute("xfaOn") : event.target.getAttribute("xfaOff")
  60. });
  61. });
  62. } else {
  63. if (storedData.value !== null) {
  64. html.setAttribute("value", storedData.value);
  65. }
  66. if (intent === "print") {
  67. break;
  68. }
  69. html.addEventListener("input", event => {
  70. storage.setValue(id, {
  71. value: event.target.value
  72. });
  73. });
  74. }
  75. break;
  76. case "select":
  77. if (storedData.value !== null) {
  78. for (const option of element.children) {
  79. if (option.attributes.value === storedData.value) {
  80. option.attributes.selected = true;
  81. }
  82. }
  83. }
  84. html.addEventListener("input", event => {
  85. const options = event.target.options;
  86. const value = options.selectedIndex === -1 ? "" : options[options.selectedIndex].value;
  87. storage.setValue(id, {
  88. value
  89. });
  90. });
  91. break;
  92. }
  93. }
  94. static setAttributes({
  95. html,
  96. element,
  97. storage = null,
  98. intent,
  99. linkService
  100. }) {
  101. const {
  102. attributes
  103. } = element;
  104. const isHTMLAnchorElement = html instanceof HTMLAnchorElement;
  105. if (attributes.type === "radio") {
  106. attributes.name = `${attributes.name}-${intent}`;
  107. }
  108. for (const [key, value] of Object.entries(attributes)) {
  109. if (value === null || value === undefined) {
  110. continue;
  111. }
  112. switch (key) {
  113. case "class":
  114. if (value.length) {
  115. html.setAttribute(key, value.join(" "));
  116. }
  117. break;
  118. case "dataId":
  119. break;
  120. case "id":
  121. html.setAttribute("data-element-id", value);
  122. break;
  123. case "style":
  124. Object.assign(html.style, value);
  125. break;
  126. case "textContent":
  127. html.textContent = value;
  128. break;
  129. default:
  130. if (!isHTMLAnchorElement || key !== "href" && key !== "newWindow") {
  131. html.setAttribute(key, value);
  132. }
  133. }
  134. }
  135. if (isHTMLAnchorElement) {
  136. linkService.addLinkAttributes(html, attributes.href, attributes.newWindow);
  137. }
  138. if (storage && attributes.dataId) {
  139. this.setupStorage(html, attributes.dataId, element, storage);
  140. }
  141. }
  142. static render(parameters) {
  143. const storage = parameters.annotationStorage;
  144. const linkService = parameters.linkService;
  145. const root = parameters.xfaHtml;
  146. const intent = parameters.intent || "display";
  147. const rootHtml = document.createElement(root.name);
  148. if (root.attributes) {
  149. this.setAttributes({
  150. html: rootHtml,
  151. element: root,
  152. intent,
  153. linkService
  154. });
  155. }
  156. const stack = [[root, -1, rootHtml]];
  157. const rootDiv = parameters.div;
  158. rootDiv.append(rootHtml);
  159. if (parameters.viewport) {
  160. const transform = `matrix(${parameters.viewport.transform.join(",")})`;
  161. rootDiv.style.transform = transform;
  162. }
  163. if (intent !== "richText") {
  164. rootDiv.setAttribute("class", "xfaLayer xfaFont");
  165. }
  166. const textDivs = [];
  167. while (stack.length > 0) {
  168. const [parent, i, html] = stack.at(-1);
  169. if (i + 1 === parent.children.length) {
  170. stack.pop();
  171. continue;
  172. }
  173. const child = parent.children[++stack.at(-1)[1]];
  174. if (child === null) {
  175. continue;
  176. }
  177. const {
  178. name
  179. } = child;
  180. if (name === "#text") {
  181. const node = document.createTextNode(child.value);
  182. textDivs.push(node);
  183. html.append(node);
  184. continue;
  185. }
  186. let childHtml;
  187. if (child?.attributes?.xmlns) {
  188. childHtml = document.createElementNS(child.attributes.xmlns, name);
  189. } else {
  190. childHtml = document.createElement(name);
  191. }
  192. html.append(childHtml);
  193. if (child.attributes) {
  194. this.setAttributes({
  195. html: childHtml,
  196. element: child,
  197. storage,
  198. intent,
  199. linkService
  200. });
  201. }
  202. if (child.children && child.children.length > 0) {
  203. stack.push([child, -1, childHtml]);
  204. } else if (child.value) {
  205. const node = document.createTextNode(child.value);
  206. if (_xfa_text.XfaText.shouldBuildText(name)) {
  207. textDivs.push(node);
  208. }
  209. childHtml.append(node);
  210. }
  211. }
  212. for (const el of rootDiv.querySelectorAll(".xfaNonInteractive input, .xfaNonInteractive textarea")) {
  213. el.setAttribute("readOnly", true);
  214. }
  215. return {
  216. textDivs
  217. };
  218. }
  219. static update(parameters) {
  220. const transform = `matrix(${parameters.viewport.transform.join(",")})`;
  221. parameters.div.style.transform = transform;
  222. parameters.div.hidden = false;
  223. }
  224. }
  225. exports.XfaLayer = XfaLayer;