optional_content_config.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.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. _evaluateVisibilityExpression(array) {
  63. const length = array.length;
  64. if (length < 2) {
  65. return true;
  66. }
  67. const operator = array[0];
  68. for (let i = 1; i < length; i++) {
  69. const element = array[i];
  70. let state;
  71. if (Array.isArray(element)) {
  72. state = this._evaluateVisibilityExpression(element);
  73. } else if (this._groups.has(element)) {
  74. state = this._groups.get(element).visible;
  75. } else {
  76. (0, _util.warn)(`Optional content group not found: ${element}`);
  77. return true;
  78. }
  79. switch (operator) {
  80. case "And":
  81. if (!state) {
  82. return false;
  83. }
  84. break;
  85. case "Or":
  86. if (state) {
  87. return true;
  88. }
  89. break;
  90. case "Not":
  91. return !state;
  92. default:
  93. return true;
  94. }
  95. }
  96. return operator === "And";
  97. }
  98. isVisible(group) {
  99. if (group.type === "OCG") {
  100. if (!this._groups.has(group.id)) {
  101. (0, _util.warn)(`Optional content group not found: ${group.id}`);
  102. return true;
  103. }
  104. return this._groups.get(group.id).visible;
  105. } else if (group.type === "OCMD") {
  106. if (group.expression) {
  107. return this._evaluateVisibilityExpression(group.expression);
  108. }
  109. if (!group.policy || group.policy === "AnyOn") {
  110. for (const id of group.ids) {
  111. if (!this._groups.has(id)) {
  112. (0, _util.warn)(`Optional content group not found: ${id}`);
  113. return true;
  114. }
  115. if (this._groups.get(id).visible) {
  116. return true;
  117. }
  118. }
  119. return false;
  120. } else if (group.policy === "AllOn") {
  121. for (const id of group.ids) {
  122. if (!this._groups.has(id)) {
  123. (0, _util.warn)(`Optional content group not found: ${id}`);
  124. return true;
  125. }
  126. if (!this._groups.get(id).visible) {
  127. return false;
  128. }
  129. }
  130. return true;
  131. } else if (group.policy === "AnyOff") {
  132. for (const id of group.ids) {
  133. if (!this._groups.has(id)) {
  134. (0, _util.warn)(`Optional content group not found: ${id}`);
  135. return true;
  136. }
  137. if (!this._groups.get(id).visible) {
  138. return true;
  139. }
  140. }
  141. return false;
  142. } else if (group.policy === "AllOff") {
  143. for (const id of group.ids) {
  144. if (!this._groups.has(id)) {
  145. (0, _util.warn)(`Optional content group not found: ${id}`);
  146. return true;
  147. }
  148. if (this._groups.get(id).visible) {
  149. return false;
  150. }
  151. }
  152. return true;
  153. }
  154. (0, _util.warn)(`Unknown optional content policy ${group.policy}.`);
  155. return true;
  156. }
  157. (0, _util.warn)(`Unknown group type ${group.type}.`);
  158. return true;
  159. }
  160. setVisibility(id, visible = true) {
  161. if (!this._groups.has(id)) {
  162. (0, _util.warn)(`Optional content group not found: ${id}`);
  163. return;
  164. }
  165. this._groups.get(id).visible = !!visible;
  166. }
  167. getOrder() {
  168. if (!this._groups.size) {
  169. return null;
  170. }
  171. if (this._order) {
  172. return this._order.slice();
  173. }
  174. return Array.from(this._groups.keys());
  175. }
  176. getGroups() {
  177. return this._groups.size > 0 ? (0, _util.objectFromMap)(this._groups) : null;
  178. }
  179. getGroup(id) {
  180. return this._groups.get(id) || null;
  181. }
  182. }
  183. exports.OptionalContentConfig = OptionalContentConfig;