optional_content_config.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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.OptionalContentConfig = void 0;
  27. var _util = require("../shared/util.js");
  28. class OptionalContentGroup {
  29. constructor(name, intent) {
  30. this.visible = true;
  31. this.name = name;
  32. this.intent = intent;
  33. }
  34. }
  35. class OptionalContentConfig {
  36. constructor(data) {
  37. this.name = null;
  38. this.creator = null;
  39. this._order = null;
  40. this._groups = new Map();
  41. if (data === null) {
  42. return;
  43. }
  44. this.name = data.name;
  45. this.creator = data.creator;
  46. this._order = data.order;
  47. for (const group of data.groups) {
  48. this._groups.set(group.id, new OptionalContentGroup(group.name, group.intent));
  49. }
  50. if (data.baseState === "OFF") {
  51. for (const group of this._groups) {
  52. group.visible = false;
  53. }
  54. }
  55. for (const on of data.on) {
  56. this._groups.get(on).visible = true;
  57. }
  58. for (const off of data.off) {
  59. this._groups.get(off).visible = false;
  60. }
  61. }
  62. isVisible(group) {
  63. if (group.type === "OCG") {
  64. if (!this._groups.has(group.id)) {
  65. (0, _util.warn)(`Optional content group not found: ${group.id}`);
  66. return true;
  67. }
  68. return this._groups.get(group.id).visible;
  69. } else if (group.type === "OCMD") {
  70. if (group.expression) {
  71. (0, _util.warn)("Visibility expression not supported yet.");
  72. }
  73. if (!group.policy || group.policy === "AnyOn") {
  74. for (const id of group.ids) {
  75. if (!this._groups.has(id)) {
  76. (0, _util.warn)(`Optional content group not found: ${id}`);
  77. return true;
  78. }
  79. if (this._groups.get(id).visible) {
  80. return true;
  81. }
  82. }
  83. return false;
  84. } else if (group.policy === "AllOn") {
  85. for (const id of group.ids) {
  86. if (!this._groups.has(id)) {
  87. (0, _util.warn)(`Optional content group not found: ${id}`);
  88. return true;
  89. }
  90. if (!this._groups.get(id).visible) {
  91. return false;
  92. }
  93. }
  94. return true;
  95. } else if (group.policy === "AnyOff") {
  96. for (const id of group.ids) {
  97. if (!this._groups.has(id)) {
  98. (0, _util.warn)(`Optional content group not found: ${id}`);
  99. return true;
  100. }
  101. if (!this._groups.get(id).visible) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. } else if (group.policy === "AllOff") {
  107. for (const id of group.ids) {
  108. if (!this._groups.has(id)) {
  109. (0, _util.warn)(`Optional content group not found: ${id}`);
  110. return true;
  111. }
  112. if (this._groups.get(id).visible) {
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. (0, _util.warn)(`Unknown optional content policy ${group.policy}.`);
  119. return true;
  120. }
  121. (0, _util.warn)(`Unknown group type ${group.type}.`);
  122. return true;
  123. }
  124. setVisibility(id, visible = true) {
  125. if (!this._groups.has(id)) {
  126. (0, _util.warn)(`Optional content group not found: ${id}`);
  127. return;
  128. }
  129. this._groups.get(id).visible = !!visible;
  130. }
  131. getOrder() {
  132. if (!this._groups.size) {
  133. return null;
  134. }
  135. if (this._order) {
  136. return this._order.slice();
  137. }
  138. return Array.from(this._groups.keys());
  139. }
  140. getGroups() {
  141. if (!this._groups.size) {
  142. return null;
  143. }
  144. return Object.fromEntries(this._groups);
  145. }
  146. getGroup(id) {
  147. return this._groups.get(id) || null;
  148. }
  149. }
  150. exports.OptionalContentConfig = OptionalContentConfig;