view_history.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 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. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  27. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  28. var DEFAULT_VIEW_HISTORY_CACHE_SIZE = 20;
  29. var ViewHistory = function () {
  30. function ViewHistory(fingerprint) {
  31. var _this = this;
  32. var cacheSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_VIEW_HISTORY_CACHE_SIZE;
  33. _classCallCheck(this, ViewHistory);
  34. this.fingerprint = fingerprint;
  35. this.cacheSize = cacheSize;
  36. this._initializedPromise = this._readFromStorage().then(function (databaseStr) {
  37. var database = JSON.parse(databaseStr || '{}');
  38. if (!('files' in database)) {
  39. database.files = [];
  40. }
  41. if (database.files.length >= _this.cacheSize) {
  42. database.files.shift();
  43. }
  44. var index = void 0;
  45. for (var i = 0, length = database.files.length; i < length; i++) {
  46. var branch = database.files[i];
  47. if (branch.fingerprint === _this.fingerprint) {
  48. index = i;
  49. break;
  50. }
  51. }
  52. if (typeof index !== 'number') {
  53. index = database.files.push({ fingerprint: _this.fingerprint }) - 1;
  54. }
  55. _this.file = database.files[index];
  56. _this.database = database;
  57. });
  58. }
  59. _createClass(ViewHistory, [{
  60. key: '_writeToStorage',
  61. value: function _writeToStorage() {
  62. var _this2 = this;
  63. return new Promise(function (resolve) {
  64. var databaseStr = JSON.stringify(_this2.database);
  65. localStorage.setItem('pdfjs.history', databaseStr);
  66. resolve();
  67. });
  68. }
  69. }, {
  70. key: '_readFromStorage',
  71. value: function _readFromStorage() {
  72. return new Promise(function (resolve) {
  73. resolve(localStorage.getItem('pdfjs.history'));
  74. });
  75. }
  76. }, {
  77. key: 'set',
  78. value: function set(name, val) {
  79. var _this3 = this;
  80. return this._initializedPromise.then(function () {
  81. _this3.file[name] = val;
  82. return _this3._writeToStorage();
  83. });
  84. }
  85. }, {
  86. key: 'setMultiple',
  87. value: function setMultiple(properties) {
  88. var _this4 = this;
  89. return this._initializedPromise.then(function () {
  90. for (var name in properties) {
  91. _this4.file[name] = properties[name];
  92. }
  93. return _this4._writeToStorage();
  94. });
  95. }
  96. }, {
  97. key: 'get',
  98. value: function get(name, defaultValue) {
  99. var _this5 = this;
  100. return this._initializedPromise.then(function () {
  101. var val = _this5.file[name];
  102. return val !== undefined ? val : defaultValue;
  103. });
  104. }
  105. }, {
  106. key: 'getMultiple',
  107. value: function getMultiple(properties) {
  108. var _this6 = this;
  109. return this._initializedPromise.then(function () {
  110. var values = Object.create(null);
  111. for (var name in properties) {
  112. var val = _this6.file[name];
  113. values[name] = val !== undefined ? val : properties[name];
  114. }
  115. return values;
  116. });
  117. }
  118. }]);
  119. return ViewHistory;
  120. }();
  121. exports.ViewHistory = ViewHistory;