Sidebar.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * Register a new tab view
  47. *
  48. * @memberof Sidebar
  49. * @param {Object} tab a new unregistered tab
  50. * @returns {Boolean}
  51. */
  52. registerTab(tab) {
  53. const hasDuplicate = this.#state.tabs.findIndex(check => check.name === tab.name) > -1
  54. if (!hasDuplicate) {
  55. this.#state.tabs.push(tab)
  56. return true
  57. }
  58. console.error(`An tab with the same name ${tab.name} already exists`, tab)
  59. return false
  60. }
  61. registerSecondaryView(view) {
  62. const hasDuplicate = this.#state.views.findIndex(check => check.name === view.name) > -1
  63. if (!hasDuplicate) {
  64. this.#state.views.push(view)
  65. return true
  66. }
  67. console.error(`A similar view already exists`, view)
  68. return false
  69. }
  70. /**
  71. * Open the sidebar for the given file
  72. *
  73. * @memberof Sidebar
  74. * @param {string} path the file path to load
  75. */
  76. open(path) {
  77. this.#state.file = path
  78. }
  79. /**
  80. * Close the sidebar
  81. *
  82. * @memberof Sidebar
  83. */
  84. close() {
  85. this.#state.file = ''
  86. }
  87. /**
  88. * Return current opened file
  89. *
  90. * @memberof Sidebar
  91. * @returns {String} the current opened file
  92. */
  93. get file() {
  94. return this.#state.file
  95. }
  96. /**
  97. * Set the current visible sidebar tab
  98. *
  99. * @memberof Sidebar
  100. * @param {string} id the tab unique id
  101. */
  102. setActiveTab(id) {
  103. this.#state.activeTab = id
  104. }
  105. }