2
0

optional_content_config.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 (this._groups.size === 0) {
  100. return true;
  101. }
  102. if (!group) {
  103. (0, _util.warn)("Optional content group not defined.");
  104. return true;
  105. }
  106. if (group.type === "OCG") {
  107. if (!this._groups.has(group.id)) {
  108. (0, _util.warn)(`Optional content group not found: ${group.id}`);
  109. return true;
  110. }
  111. return this._groups.get(group.id).visible;
  112. } else if (group.type === "OCMD") {
  113. if (group.expression) {
  114. return this._evaluateVisibilityExpression(group.expression);
  115. }
  116. if (!group.policy || group.policy === "AnyOn") {
  117. for (const id of group.ids) {
  118. if (!this._groups.has(id)) {
  119. (0, _util.warn)(`Optional content group not found: ${id}`);
  120. return true;
  121. }
  122. if (this._groups.get(id).visible) {
  123. return true;
  124. }
  125. }
  126. return false;
  127. } else if (group.policy === "AllOn") {
  128. for (const id of group.ids) {
  129. if (!this._groups.has(id)) {
  130. (0, _util.warn)(`Optional content group not found: ${id}`);
  131. return true;
  132. }
  133. if (!this._groups.get(id).visible) {
  134. return false;
  135. }
  136. }
  137. return true;
  138. } else if (group.policy === "AnyOff") {
  139. for (const id of group.ids) {
  140. if (!this._groups.has(id)) {
  141. (0, _util.warn)(`Optional content group not found: ${id}`);
  142. return true;
  143. }
  144. if (!this._groups.get(id).visible) {
  145. return true;
  146. }
  147. }
  148. return false;
  149. } else if (group.policy === "AllOff") {
  150. for (const id of group.ids) {
  151. if (!this._groups.has(id)) {
  152. (0, _util.warn)(`Optional content group not found: ${id}`);
  153. return true;
  154. }
  155. if (this._groups.get(id).visible) {
  156. return false;
  157. }
  158. }
  159. return true;
  160. }
  161. (0, _util.warn)(`Unknown optional content policy ${group.policy}.`);
  162. return true;
  163. }
  164. (0, _util.warn)(`Unknown group type ${group.type}.`);
  165. return true;
  166. }
  167. setVisibility(id, visible = true) {
  168. if (!this._groups.has(id)) {
  169. (0, _util.warn)(`Optional content group not found: ${id}`);
  170. return;
  171. }
  172. this._groups.get(id).visible = !!visible;
  173. }
  174. getOrder() {
  175. if (!this._groups.size) {
  176. return null;
  177. }
  178. if (this._order) {
  179. return this._order.slice();
  180. }
  181. return Array.from(this._groups.keys());
  182. }
  183. getGroups() {
  184. return this._groups.size > 0 ? (0, _util.objectFromMap)(this._groups) : null;
  185. }
  186. getGroup(id) {
  187. return this._groups.get(id) || null;
  188. }
  189. }
  190. exports.OptionalContentConfig = OptionalContentConfig;