DynamicTagActionProxy.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { __awaiter } from "tslib";
  2. import { PromiseQueue } from "./PromiseQueue";
  3. export class DynamicTagActionProxy {
  4. constructor(krpanoRenderer) {
  5. this.queue = new PromiseQueue();
  6. /**
  7. * 同步标签是否加载完成
  8. */
  9. this.syncTagsLoaded = false;
  10. this.syncTagStack = [];
  11. this.krpanoRenderer = krpanoRenderer;
  12. }
  13. /**
  14. * 等待 include 标签加载完成
  15. */
  16. waitIncludeLoaded(push) {
  17. return this.syncTagsLoaded
  18. ? Promise.resolve()
  19. : // 先进后出
  20. this.queue[push ? "push" : "unshift"]();
  21. }
  22. /**
  23. * 将异步标签推入堆中
  24. */
  25. pushSyncTag(tagName, attribute) {
  26. this.syncTagStack.unshift({
  27. tagName,
  28. attribute,
  29. });
  30. }
  31. /**
  32. * 创建一个插入同步标签后的 XMLDOM
  33. */
  34. createSyncTags() {
  35. return __awaiter(this, void 0, void 0, function* () {
  36. const xmlDoc = yield this.getXMLContent();
  37. const krpanoElement = xmlDoc.querySelector("krpano");
  38. while (this.syncTagStack.length) {
  39. const tag = this.syncTagStack.pop();
  40. const element = xmlDoc.createElement(tag.tagName);
  41. for (const key in tag.attribute) {
  42. element.setAttribute(key, tag.attribute[key]);
  43. }
  44. krpanoElement === null || krpanoElement === void 0 ? void 0 : krpanoElement.insertBefore(element, null);
  45. }
  46. return xmlDoc;
  47. });
  48. }
  49. getXMLContent() {
  50. var _a;
  51. return __awaiter(this, void 0, void 0, function* () {
  52. let contentText = "";
  53. const xml = (_a = this.krpanoRenderer) === null || _a === void 0 ? void 0 : _a.get("xml");
  54. const parser = new DOMParser();
  55. if (xml.content) {
  56. contentText = xml.content;
  57. }
  58. else if (xml.url) {
  59. contentText = yield fetch(xml.url).then((res) => res.text());
  60. }
  61. return parser.parseFromString(contentText, "text/xml");
  62. });
  63. }
  64. }