annotation_storage.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.AnnotationStorage = void 0;
  27. var _display_utils = require("./display_utils.js");
  28. var _util = require("../shared/util.js");
  29. class AnnotationStorage {
  30. constructor() {
  31. this._storage = new Map();
  32. this._modified = false;
  33. this.onSetModified = null;
  34. this.onResetModified = null;
  35. }
  36. getValue(key, defaultValue) {
  37. const obj = this._storage.get(key);
  38. return obj !== undefined ? obj : defaultValue;
  39. }
  40. getOrCreateValue(key, defaultValue) {
  41. (0, _display_utils.deprecated)("Use getValue instead.");
  42. if (this._storage.has(key)) {
  43. return this._storage.get(key);
  44. }
  45. this._storage.set(key, defaultValue);
  46. return defaultValue;
  47. }
  48. setValue(key, value) {
  49. const obj = this._storage.get(key);
  50. let modified = false;
  51. if (obj !== undefined) {
  52. for (const [entry, val] of Object.entries(value)) {
  53. if (obj[entry] !== val) {
  54. modified = true;
  55. obj[entry] = val;
  56. }
  57. }
  58. } else {
  59. this._storage.set(key, value);
  60. modified = true;
  61. }
  62. if (modified) {
  63. this._setModified();
  64. }
  65. }
  66. getAll() {
  67. return this._storage.size > 0 ? (0, _util.objectFromMap)(this._storage) : null;
  68. }
  69. get size() {
  70. return this._storage.size;
  71. }
  72. _setModified() {
  73. if (!this._modified) {
  74. this._modified = true;
  75. if (typeof this.onSetModified === "function") {
  76. this.onSetModified();
  77. }
  78. }
  79. }
  80. resetModified() {
  81. if (this._modified) {
  82. this._modified = false;
  83. if (typeof this.onResetModified === "function") {
  84. this.onResetModified();
  85. }
  86. }
  87. }
  88. get serializable() {
  89. return this._storage.size > 0 ? this._storage : null;
  90. }
  91. }
  92. exports.AnnotationStorage = AnnotationStorage;