xfa_layer.js 5.0 KB

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