UserStyle.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. class UserStyle{
  2. static #localStorageIdentifier = 'userStyle';
  3. static #domId = '#userConfiguredStyle';
  4. static fromLocalStorage(){
  5. const dataFromLocalStorage = localStorage.getItem(UserStyle.#localStorageIdentifier);
  6. if(!dataFromLocalStorage)
  7. return new UserStyle();
  8. return JSON.parse(dataFromLocalStorage);
  9. }
  10. constructor(){
  11. }
  12. saveToLocalStorage(){
  13. localStorage.setItem(UserStyle.#localStorageIdentifier,JSON.stringify(this));
  14. }
  15. update(){
  16. this.saveToLocalStorage();
  17. if($(UserStyle.#domId).length)
  18. $(UserStyle.#domId).text(this.css);
  19. if(this._grid && !this._gridIsCreated){
  20. setup.uiGridCreate();
  21. this._gridIsCreated = true;
  22. }
  23. else if(!this._grid && this._gridIsCreated){
  24. setup.uiGridDestroy();
  25. this._gridIsCreated = false;
  26. }
  27. }
  28. get css(){
  29. return `body{
  30. font-family:${this.font};
  31. font-size:${this.fontSize}px;
  32. }
  33. `+ this.toggleErrorsCSS + this.toggleNoticesCSS + this.gridCSS + this.customCSS;
  34. }
  35. _customCSS = '';
  36. get customCSS(){return this._customCSS;}
  37. set customCSS(v){
  38. this._customCSS = v;
  39. this.update();
  40. }
  41. _font = 'Helmet, Freesans, sans-serif';
  42. get font(){return this._font;}
  43. set font(v){
  44. this._font = v;
  45. this.update();
  46. }
  47. _fontSize = 16;
  48. get fontSize(){return this._fontSize;}
  49. set fontSize(v){
  50. this._fontSize = v;
  51. this.update();
  52. }
  53. _gridIsCreated = false;
  54. _grid = false;
  55. get grid(){return this._grid;}
  56. set grid(v){
  57. this._grid = v;
  58. this.update();
  59. }
  60. get gridCSS(){
  61. if(!this._grid)
  62. return ".grid-stack-item{display: contents;} #interface>div>*{overflow:auto}"
  63. return "";
  64. }
  65. _toggleErrors = false;
  66. get toggleErrors(){return this._toggleErrors;}
  67. set toggleErrors(v){
  68. this._toggleErrors = v;
  69. this.update();
  70. }
  71. get toggleErrorsCSS(){
  72. if(this._toggleErrors)
  73. return '';
  74. return '.error-view{display:none;}';
  75. }
  76. _toggleNotices = false;
  77. get toggleNotices(){return this._toggleNotices;}
  78. set toggleNotices(v){
  79. this._toggleNotices = v;
  80. this.update();
  81. }
  82. get toggleNoticesCSS(){
  83. if(this._toggleNotices)
  84. return '';
  85. return '.debugNotice{display:none;}';
  86. }
  87. //#region System
  88. _init(data){
  89. Object.keys(data).forEach(function (pn) {
  90. this[pn] = clone(data[pn]);
  91. }, this);
  92. return this;
  93. }
  94. clone = function () {
  95. return (new setup.UserStyle())._init(this);
  96. };
  97. toJSON = function () {
  98. var ownData = {};
  99. Object.keys(this).forEach(function (pn) {
  100. if(typeof this[pn] !== "function")
  101. ownData[pn] = clone(this[pn]);
  102. }, this);
  103. return JSON.reviveWrapper('(new setup.UserStyle())._init($ReviveData$)', ownData);
  104. };
  105. //#endregion
  106. }
  107. setup.UserStyle = UserStyle;
  108. setup.userStyle = UserStyle.fromLocalStorage();
  109. $(document).one(':storyready', function (ev) {
  110. //let userStyle = UserStyle.fromLocalStorage();
  111. setup.userStyle.update();
  112. });