views.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Connectors
  2. const cwd = require('./cwd')
  3. // Subs
  4. const channels = require('../channels')
  5. // Utils
  6. const { log } = require('../util/logger')
  7. let currentView
  8. function createViewsSet () {
  9. // Builtin views
  10. return [
  11. {
  12. id: 'vue-project-dashboard',
  13. name: 'project-dashboard',
  14. icon: 'dashboard',
  15. tooltip: 'org.vue.components.project-nav.tooltips.dashboard'
  16. },
  17. {
  18. id: 'vue-project-plugins',
  19. name: 'project-plugins',
  20. icon: 'extension',
  21. tooltip: 'org.vue.components.project-nav.tooltips.plugins'
  22. },
  23. {
  24. id: 'vue-project-dependencies',
  25. name: 'project-dependencies',
  26. icon: 'collections_bookmark',
  27. tooltip: 'org.vue.components.project-nav.tooltips.dependencies',
  28. projectTypes: ['vue', 'unknown']
  29. },
  30. {
  31. id: 'vue-project-configurations',
  32. name: 'project-configurations',
  33. icon: 'settings_applications',
  34. tooltip: 'org.vue.components.project-nav.tooltips.configuration'
  35. },
  36. {
  37. id: 'vue-project-tasks',
  38. name: 'project-tasks',
  39. icon: 'assignment',
  40. tooltip: 'org.vue.components.project-nav.tooltips.tasks',
  41. projectTypes: ['vue', 'unknown']
  42. }
  43. ]
  44. }
  45. const viewsMap = new Map()
  46. function getViews () {
  47. const file = cwd.get()
  48. let list = viewsMap.get(file)
  49. if (!list) {
  50. list = createViewsSet()
  51. viewsMap.set(file, list)
  52. }
  53. return list
  54. }
  55. function list (context) {
  56. return getViews()
  57. }
  58. function findOne (id) {
  59. const views = getViews()
  60. return views.find(r => r.id === id)
  61. }
  62. async function add ({ view, project }, context) {
  63. remove(view.id, context)
  64. // Default icon
  65. if (!view.icon) {
  66. const plugins = require('./plugins')
  67. const plugin = plugins.findOne({ id: view.pluginId, file: cwd.get() }, context)
  68. const logo = await plugins.getLogo(plugin, context)
  69. view.icon = logo ? `${logo}?project=${project.id}` : 'radio_button_unchecked'
  70. }
  71. const views = getViews()
  72. views.push(view)
  73. context.pubsub.publish(channels.VIEW_ADDED, {
  74. viewAdded: view
  75. })
  76. log('View added', view.id)
  77. }
  78. function remove (id, context) {
  79. const views = getViews()
  80. const index = views.findIndex(r => r.id === id)
  81. if (index !== -1) {
  82. const view = views[index]
  83. views.splice(index, 1)
  84. context.pubsub.publish(channels.VIEW_REMOVED, {
  85. viewRemoved: view
  86. })
  87. }
  88. }
  89. function update (view, context) {
  90. const existingView = findOne(view.id)
  91. if (existingView) {
  92. Object.assign(existingView, view)
  93. context.pubsub.publish(channels.VIEW_CHANGED, {
  94. viewChanged: existingView
  95. })
  96. }
  97. }
  98. function addBadge ({ viewId, badge }, context) {
  99. const view = findOne(viewId)
  100. if (view) {
  101. if (!view.badges) view.badges = []
  102. const existingBadge = view.badges.find(b => b.id === badge.id)
  103. if (existingBadge) {
  104. Object.assign(existingBadge, badge, {
  105. count: existingBadge.count + 1
  106. })
  107. } else {
  108. view.badges.push({
  109. type: 'dim',
  110. count: 1,
  111. priority: 0,
  112. hidden: false,
  113. ...badge
  114. })
  115. }
  116. update(view, context)
  117. }
  118. }
  119. function removeBadge ({ viewId, badgeId }, context) {
  120. const view = findOne(viewId)
  121. if (view && view.badges) {
  122. const existingBadge = view.badges.find(b => b.id === badgeId)
  123. if (existingBadge) {
  124. existingBadge.count--
  125. if (existingBadge.count <= 0) {
  126. const index = view.badges.indexOf(existingBadge)
  127. index !== -1 && view.badges.splice(index, 1)
  128. }
  129. update(view, context)
  130. }
  131. }
  132. }
  133. function open (id, context) {
  134. const view = findOne(id)
  135. currentView = view
  136. const plugins = require('./plugins')
  137. plugins.callHook({
  138. id: 'viewOpen',
  139. args: [{
  140. view,
  141. cwd: cwd.get()
  142. }],
  143. file: cwd.get()
  144. }, context)
  145. return true
  146. }
  147. module.exports = {
  148. list,
  149. findOne,
  150. add,
  151. remove,
  152. update,
  153. addBadge,
  154. removeBadge,
  155. open,
  156. getCurrent: () => currentView
  157. }