Source: service/local-storage.js

  1. import Storage from './storage';
  2. /**
  3. * OLE LocalStorage.
  4. * Saves control properties to the browser's localStorage.
  5. * @alias ole.service.LocalStorage
  6. */
  7. export default class LocalStorage extends Storage {
  8. /**
  9. * @inheritdoc
  10. */
  11. storeProperties(controlName, properties) {
  12. const props = super.storeProperties(controlName, properties);
  13. window.localStorage.setItem(controlName, JSON.stringify(props));
  14. }
  15. /**
  16. * @inheritdoc
  17. */
  18. restoreProperties() {
  19. for (let i = 0; i < this.controls.length; i += 1) {
  20. const controlName = this.controls[i].getProperties().title;
  21. const props = window.localStorage.getItem(controlName);
  22. if (props) {
  23. this.controls[i].setProperties(JSON.parse(props), true);
  24. }
  25. }
  26. }
  27. /**
  28. * @inheritdoc
  29. */
  30. storeActiveControls() {
  31. const activeControlNames = super.storeActiveControls();
  32. window.localStorage.setItem('active', JSON.stringify(activeControlNames));
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. restoreActiveControls() {
  38. let activeControlNames = window.localStorage.getItem('active');
  39. activeControlNames = activeControlNames
  40. ? JSON.parse(activeControlNames)
  41. : [];
  42. if (!activeControlNames.length) {
  43. return;
  44. }
  45. for (let i = 0; i < this.controls.length; i += 1) {
  46. const controlName = this.controls[i].getProperties().title;
  47. if (activeControlNames.indexOf(controlName) > -1) {
  48. this.controls[i].activate();
  49. } else {
  50. this.controls[i].deactivate();
  51. }
  52. }
  53. }
  54. }