Sidebar.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. export default class Sidebar {
  23. #state;
  24. #view;
  25. constructor() {
  26. // init empty state
  27. this.#state = {}
  28. // init default values
  29. this.#state.tabs = []
  30. this.#state.views = []
  31. this.#state.file = ''
  32. this.#state.activeTab = ''
  33. console.debug('OCA.Files.Sidebar initialized')
  34. }
  35. /**
  36. * Get the sidebar state
  37. *
  38. * @readonly
  39. * @memberof Sidebar
  40. * @returns {Object} the data state
  41. */
  42. get state() {
  43. return this.#state
  44. }
  45. /**
  46. * @memberof Sidebar
  47. * Register a new tab view
  48. *
  49. * @param {Object} tab a new unregistered tab
  50. * @memberof Sidebar
  51. * @returns {Boolean}
  52. */
  53. registerTab(tab) {
  54. const hasDuplicate = this.#state.tabs.findIndex(check => check.name === tab.name) > -1
  55. if (!hasDuplicate) {
  56. this.#state.tabs.push(tab)
  57. return true
  58. }
  59. console.error(`An tab with the same name ${tab.name} already exists`, tab)
  60. return false
  61. }
  62. registerSecondaryView(view) {
  63. const hasDuplicate = this.#state.views.findIndex(check => check.cid === view.cid) > -1
  64. if (!hasDuplicate) {
  65. this.#state.views.push(view)
  66. return true
  67. }
  68. console.error(`A similar view already exists`, view)
  69. return false
  70. }
  71. /**
  72. * Set the current sidebar file data
  73. *
  74. * @param {string} path the file path to load
  75. * @memberof Sidebar
  76. */
  77. set file(path) {
  78. this.#state.file = path
  79. }
  80. /**
  81. * Set the current sidebar file data
  82. *
  83. * @returns {String} the current opened file
  84. * @memberof Sidebar
  85. */
  86. get file() {
  87. return this.#state.file
  88. }
  89. /**
  90. * Set the current sidebar tab
  91. *
  92. * @param {string} id the tab unique id
  93. * @memberof Sidebar
  94. */
  95. set activeTab(id) {
  96. this.#state.activeTab = id
  97. }
  98. }