Home Reference Source

cables_dev/cables_ui/src/ui/components/usersettings.js

  1. import { Events } from "cables-shared-client";
  2.  
  3. /**
  4. * storing/loading user settings/ sending to the user and in localstorage etc.
  5. *
  6. * @class UserSettings
  7. * @extends {Events}
  8. */
  9. class UserSettings extends Events
  10. {
  11. constructor()
  12. {
  13. super();
  14.  
  15. this._settings = {};
  16. this._LOCALSTORAGE_KEY = "cables.usersettings";
  17. this._wasLoaded = false;
  18. this._serverDelay = null;
  19. this.init();
  20.  
  21. this._lsSettings = JSON.parse(localStorage.getItem(this._LOCALSTORAGE_KEY)) || {};
  22. }
  23.  
  24. reset()
  25. {
  26. this._settings = {};
  27. this.init();
  28. this.save();
  29. }
  30.  
  31. init()
  32. {
  33. if (!this.get("patch_wheelmode")) this.set("patch_wheelmode", "zoom");
  34. if (this.get("glflowmode") === null) this.set("glflowmode", 2);
  35. if (this.get("snapToGrid2") === null) this.set("snapToGrid2", false);
  36. if (this.get("bgpreview") === null) this.set("bgpreview", true);
  37. if (this.get("idlemode") === null) this.set("idlemode", false);
  38. if (this.get("showTipps") === null) this.set("showTipps", true);
  39. if (this.get("overlaysShow") === null) this.set("overlaysShow", true);
  40. if (this.get("quickLinkMiddleMouse") === null) this.set("quickLinkMiddleMouse", true);
  41. if (this.get("doubleClickAction") === null) this.set("doubleClickAction", "parentSub");
  42. }
  43.  
  44. load(settings)
  45. {
  46. for (const i in settings)
  47. {
  48. this.set(i, settings[i]);
  49. }
  50.  
  51. if (!this._wasLoaded) this.emitEvent("loaded");
  52. this._wasLoaded = true;
  53. }
  54.  
  55. setLS(key, value)
  56. {
  57. this._lsSettings[key] = value || false;
  58. localStorage.setItem(CABLES.UI.LOCALSTORAGE_KEY, JSON.stringify(this._lsSettings));
  59. }
  60.  
  61. getLS(key)
  62. {
  63. if (!this._lsSettings || !this._lsSettings.hasOwnProperty(key)) return null;
  64. return this._lsSettings[key];
  65. }
  66.  
  67.  
  68. save()
  69. {
  70. CABLESUILOADER.talkerAPI.send("saveUserSettings", { "settings": this._settings });
  71. }
  72.  
  73. set(key, value)
  74. {
  75. if (value === "true") value = true;
  76. else if (value === "false") value = false;
  77.  
  78. if (CABLES.UTILS.isNumeric(value)) value = parseFloat(value);
  79.  
  80. const wasChanged = this._settings[key] != value;
  81.  
  82. this._settings[key] = value || false;
  83.  
  84. if (this._wasLoaded)
  85. {
  86. let delay = 250;
  87. if (!CABLES.UI.loaded)delay = 2000;
  88. if (wasChanged)
  89. {
  90. clearTimeout(this._serverDelay);
  91. this._serverDelay = setTimeout(() =>
  92. {
  93. this.save();
  94. }, delay);
  95. }
  96. if (wasChanged) this.emitEvent("change", key, value);
  97. }
  98. }
  99.  
  100. get(key, defaultValue = null)
  101. {
  102. if (!this._settings || !this._settings.hasOwnProperty(key)) return defaultValue;
  103. return this._settings[key];
  104. }
  105.  
  106. getAll()
  107. {
  108. return this._settings;
  109. }
  110. }
  111.  
  112.  
  113. const userSettings = new UserSettings();
  114.  
  115. export default userSettings;