2
0

annotation_storage.js 2.3 KB

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