2
0

xfa_layer.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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. class XfaLayer {
  28. static setupStorage(html, id, element, storage, intent) {
  29. const storedData = storage.getValue(id, {
  30. value: null
  31. });
  32. switch (element.name) {
  33. case "textarea":
  34. if (storedData.value !== null) {
  35. html.textContent = storedData.value;
  36. }
  37. if (intent === "print") {
  38. break;
  39. }
  40. html.addEventListener("input", event => {
  41. storage.setValue(id, {
  42. value: event.target.value
  43. });
  44. });
  45. break;
  46. case "input":
  47. if (element.attributes.type === "radio" || element.attributes.type === "checkbox") {
  48. if (storedData.value === element.attributes.xfaOn) {
  49. html.setAttribute("checked", true);
  50. }
  51. if (intent === "print") {
  52. break;
  53. }
  54. html.addEventListener("change", event => {
  55. storage.setValue(id, {
  56. value: event.target.getAttribute("xfaOn")
  57. });
  58. });
  59. } else {
  60. if (storedData.value !== null) {
  61. html.setAttribute("value", storedData.value);
  62. }
  63. if (intent === "print") {
  64. break;
  65. }
  66. html.addEventListener("input", event => {
  67. storage.setValue(id, {
  68. value: event.target.value
  69. });
  70. });
  71. }
  72. break;
  73. case "select":
  74. if (storedData.value !== null) {
  75. for (const option of element.children) {
  76. if (option.attributes.value === storedData.value) {
  77. option.attributes.selected = true;
  78. }
  79. }
  80. }
  81. html.addEventListener("input", event => {
  82. const options = event.target.options;
  83. const value = options.selectedIndex === -1 ? "" : options[options.selectedIndex].value;
  84. storage.setValue(id, {
  85. value
  86. });
  87. });
  88. break;
  89. }
  90. }
  91. static setAttributes(html, element, storage, intent) {
  92. const {
  93. attributes
  94. } = element;
  95. if (attributes.type === "radio") {
  96. attributes.name = `${attributes.name}-${intent}`;
  97. }
  98. for (const [key, value] of Object.entries(attributes)) {
  99. if (value === null || value === undefined || key === "dataId") {
  100. continue;
  101. }
  102. if (key !== "style") {
  103. if (key === "textContent") {
  104. html.textContent = value;
  105. } else if (key === "class") {
  106. html.setAttribute(key, value.join(" "));
  107. } else {
  108. html.setAttribute(key, value);
  109. }
  110. } else {
  111. Object.assign(html.style, value);
  112. }
  113. }
  114. if (storage && attributes.dataId) {
  115. this.setupStorage(html, attributes.dataId, element, storage);
  116. }
  117. }
  118. static render(parameters) {
  119. const storage = parameters.annotationStorage;
  120. const root = parameters.xfa;
  121. const intent = parameters.intent || "display";
  122. const rootHtml = document.createElement(root.name);
  123. if (root.attributes) {
  124. this.setAttributes(rootHtml, root);
  125. }
  126. const stack = [[root, -1, rootHtml]];
  127. const rootDiv = parameters.div;
  128. rootDiv.appendChild(rootHtml);
  129. const transform = `matrix(${parameters.viewport.transform.join(",")})`;
  130. rootDiv.style.transform = transform;
  131. rootDiv.setAttribute("class", "xfaLayer xfaFont");
  132. while (stack.length > 0) {
  133. const [parent, i, html] = stack[stack.length - 1];
  134. if (i + 1 === parent.children.length) {
  135. stack.pop();
  136. continue;
  137. }
  138. const child = parent.children[++stack[stack.length - 1][1]];
  139. if (child === null) {
  140. continue;
  141. }
  142. const {
  143. name
  144. } = child;
  145. if (name === "#text") {
  146. html.appendChild(document.createTextNode(child.value));
  147. continue;
  148. }
  149. let childHtml;
  150. if (child?.attributes?.xmlns) {
  151. childHtml = document.createElementNS(child.attributes.xmlns, name);
  152. } else {
  153. childHtml = document.createElement(name);
  154. }
  155. html.appendChild(childHtml);
  156. if (child.attributes) {
  157. this.setAttributes(childHtml, child, storage, intent);
  158. }
  159. if (child.children && child.children.length > 0) {
  160. stack.push([child, -1, childHtml]);
  161. } else if (child.value) {
  162. childHtml.appendChild(document.createTextNode(child.value));
  163. }
  164. }
  165. for (const el of rootDiv.querySelectorAll(".xfaNonInteractive input, .xfaNonInteractive textarea")) {
  166. el.setAttribute("readOnly", true);
  167. }
  168. }
  169. static update(parameters) {
  170. const transform = `matrix(${parameters.viewport.transform.join(",")})`;
  171. parameters.div.style.transform = transform;
  172. parameters.div.hidden = false;
  173. }
  174. }
  175. exports.XfaLayer = XfaLayer;