AppList.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <!--
  2. - @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
  3. -
  4. - @author Julius Härtl <jus@bitgrid.net>
  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. <template>
  23. <div id="app-content-inner">
  24. <div id="apps-list" class="apps-list" :class="{installed: (useBundleView || useListView), store: useAppStoreView}">
  25. <template v-if="useListView">
  26. <div v-if="showUpdateAll" class="toolbar">
  27. {{ n('settings', '%n app has an update available', '%n apps have an update available', counter) }}
  28. <NcButton v-if="showUpdateAll"
  29. id="app-list-update-all"
  30. type="primary"
  31. @click="updateAll">
  32. {{ n('settings', 'Update', 'Update all', counter) }}
  33. </NcButton>
  34. </div>
  35. <div v-if="!showUpdateAll" class="toolbar">
  36. {{ t('settings', 'All apps are up-to-date.') }}
  37. </div>
  38. <transition-group name="app-list" tag="table" class="apps-list-container">
  39. <tr key="app-list-view-header" class="apps-header">
  40. <th class="app-image">
  41. <span class="hidden-visually">{{ t('settings', 'Icon') }}</span>
  42. </th>
  43. <th class="app-name">
  44. <span class="hidden-visually">{{ t('settings', 'Name') }}</span>
  45. </th>
  46. <th class="app-version">
  47. <span class="hidden-visually">{{ t('settings', 'Version') }}</span>
  48. </th>
  49. <th class="app-level">
  50. <span class="hidden-visually">{{ t('settings', 'Level') }}</span>
  51. </th>
  52. <th class="actions">
  53. <span class="hidden-visually">{{ t('settings', 'Actions') }}</span>
  54. </th>
  55. </tr>
  56. <AppItem v-for="app in apps"
  57. :key="app.id"
  58. :app="app"
  59. :category="category" />
  60. </transition-group>
  61. </template>
  62. <table v-if="useBundleView"
  63. class="apps-list-container">
  64. <tr key="app-list-view-header" class="apps-header">
  65. <th id="app-table-col-icon" class="app-image">
  66. <span class="hidden-visually">{{ t('settings', 'Icon') }}</span>
  67. </th>
  68. <th id="app-table-col-name" class="app-name">
  69. <span class="hidden-visually">{{ t('settings', 'Name') }}</span>
  70. </th>
  71. <th id="app-table-col-version" class="app-version">
  72. <span class="hidden-visually">{{ t('settings', 'Version') }}</span>
  73. </th>
  74. <th id="app-table-col-level" class="app-level">
  75. <span class="hidden-visually">{{ t('settings', 'Level') }}</span>
  76. </th>
  77. <th id="app-table-col-actions" class="actions">
  78. <span class="hidden-visually">{{ t('settings', 'Actions') }}</span>
  79. </th>
  80. </tr>
  81. <template v-for="bundle in bundles">
  82. <tr :key="bundle.id">
  83. <th :id="`app-table-rowgroup-${bundle.id}`" colspan="5" scope="rowgroup">
  84. <div class="app-bundle-heading">
  85. <span class="app-bundle-header">
  86. {{ bundle.name }}
  87. </span>
  88. <NcButton type="secondary" @click="toggleBundle(bundle.id)">
  89. {{ t('settings', bundleToggleText(bundle.id)) }}
  90. </NcButton>
  91. </div>
  92. </th>
  93. </tr>
  94. <AppItem v-for="app in bundleApps(bundle.id)"
  95. :key="bundle.id + app.id"
  96. :use-bundle-view="true"
  97. :headers="`app-table-rowgroup-${bundle.id}`"
  98. :app="app"
  99. :category="category" />
  100. </template>
  101. </table>
  102. <ul v-if="useAppStoreView" class="apps-store-view">
  103. <AppItem v-for="app in apps"
  104. :key="app.id"
  105. :app="app"
  106. :category="category"
  107. :list-view="false" />
  108. </ul>
  109. </div>
  110. <div id="apps-list-search" class="apps-list installed">
  111. <div class="apps-list-container">
  112. <template v-if="search !== '' && searchApps.length > 0">
  113. <div class="section">
  114. <div />
  115. <td colspan="5">
  116. <h2>{{ t('settings', 'Results from other categories') }}</h2>
  117. </td>
  118. </div>
  119. <AppItem v-for="app in searchApps"
  120. :key="app.id"
  121. :app="app"
  122. :category="category" />
  123. </template>
  124. </div>
  125. </div>
  126. <div v-if="search !== '' && !loading && searchApps.length === 0 && apps.length === 0" id="apps-list-empty" class="emptycontent emptycontent-search">
  127. <div id="app-list-empty-icon" class="icon-settings-dark" />
  128. <h2>{{ t('settings', 'No apps found for your version') }}</h2>
  129. </div>
  130. <div id="searchresults" />
  131. </div>
  132. </template>
  133. <script>
  134. import { subscribe, unsubscribe } from '@nextcloud/event-bus'
  135. import AppItem from './AppList/AppItem.vue'
  136. import pLimit from 'p-limit'
  137. import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
  138. export default {
  139. name: 'AppList',
  140. components: {
  141. AppItem,
  142. NcButton,
  143. },
  144. props: {
  145. category: {
  146. type: String,
  147. required: true,
  148. },
  149. },
  150. data() {
  151. return {
  152. search: '',
  153. }
  154. },
  155. computed: {
  156. counter() {
  157. return this.apps.filter(app => app.update).length
  158. },
  159. loading() {
  160. return this.$store.getters.loading('list')
  161. },
  162. hasPendingUpdate() {
  163. return this.apps.filter(app => app.update).length > 0
  164. },
  165. showUpdateAll() {
  166. return this.hasPendingUpdate && this.useListView
  167. },
  168. apps() {
  169. const apps = this.$store.getters.getAllApps
  170. .filter(app => app.name.toLowerCase().search(this.search.toLowerCase()) !== -1)
  171. .sort(function(a, b) {
  172. const sortStringA = '' + (a.active ? 0 : 1) + (a.update ? 0 : 1) + a.name
  173. const sortStringB = '' + (b.active ? 0 : 1) + (b.update ? 0 : 1) + b.name
  174. return OC.Util.naturalSortCompare(sortStringA, sortStringB)
  175. })
  176. if (this.category === 'installed') {
  177. return apps.filter(app => app.installed)
  178. }
  179. if (this.category === 'enabled') {
  180. return apps.filter(app => app.active && app.installed)
  181. }
  182. if (this.category === 'disabled') {
  183. return apps.filter(app => !app.active && app.installed)
  184. }
  185. if (this.category === 'app-bundles') {
  186. return apps.filter(app => app.bundles)
  187. }
  188. if (this.category === 'updates') {
  189. return apps.filter(app => app.update)
  190. }
  191. if (this.category === 'supported') {
  192. // For customers of the Nextcloud GmbH the app level will be set to `300` for apps that are supported in their subscription
  193. return apps.filter(app => app.level === 300)
  194. }
  195. if (this.category === 'featured') {
  196. // An app level of `200` will be set for apps featured on the app store
  197. return apps.filter(app => app.level === 200)
  198. }
  199. // filter app store categories
  200. return apps.filter(app => {
  201. return app.appstore && app.category !== undefined
  202. && (app.category === this.category || app.category.indexOf(this.category) > -1)
  203. })
  204. },
  205. bundles() {
  206. return this.$store.getters.getAppBundles.filter(bundle => this.bundleApps(bundle.id).length > 0)
  207. },
  208. bundleApps() {
  209. return function(bundle) {
  210. return this.$store.getters.getAllApps
  211. .filter(app => {
  212. return app.bundleIds !== undefined && app.bundleIds.includes(bundle)
  213. })
  214. }
  215. },
  216. searchApps() {
  217. if (this.search === '') {
  218. return []
  219. }
  220. return this.$store.getters.getAllApps
  221. .filter(app => {
  222. if (app.name.toLowerCase().search(this.search.toLowerCase()) !== -1) {
  223. return (!this.apps.find(_app => _app.id === app.id))
  224. }
  225. return false
  226. })
  227. },
  228. useAppStoreView() {
  229. return !this.useListView && !this.useBundleView
  230. },
  231. useListView() {
  232. return (this.category === 'installed' || this.category === 'enabled' || this.category === 'disabled' || this.category === 'updates' || this.category === 'featured' || this.category === 'supported')
  233. },
  234. useBundleView() {
  235. return (this.category === 'app-bundles')
  236. },
  237. allBundlesEnabled() {
  238. return (id) => {
  239. return this.bundleApps(id).filter(app => !app.active).length === 0
  240. }
  241. },
  242. bundleToggleText() {
  243. return (id) => {
  244. if (this.allBundlesEnabled(id)) {
  245. return t('settings', 'Disable all')
  246. }
  247. return t('settings', 'Download and enable all')
  248. }
  249. },
  250. },
  251. beforeDestroy() {
  252. unsubscribe('nextcloud:unified-search.search', this.setSearch)
  253. unsubscribe('nextcloud:unified-search.reset', this.resetSearch)
  254. },
  255. mounted() {
  256. subscribe('nextcloud:unified-search.search', this.setSearch)
  257. subscribe('nextcloud:unified-search.reset', this.resetSearch)
  258. },
  259. methods: {
  260. setSearch({ query }) {
  261. this.search = query
  262. },
  263. resetSearch() {
  264. this.search = ''
  265. },
  266. toggleBundle(id) {
  267. if (this.allBundlesEnabled(id)) {
  268. return this.disableBundle(id)
  269. }
  270. return this.enableBundle(id)
  271. },
  272. enableBundle(id) {
  273. const apps = this.bundleApps(id).map(app => app.id)
  274. this.$store.dispatch('enableApp', { appId: apps, groups: [] })
  275. .catch((error) => {
  276. console.error(error)
  277. OC.Notification.show(error)
  278. })
  279. },
  280. disableBundle(id) {
  281. const apps = this.bundleApps(id).map(app => app.id)
  282. this.$store.dispatch('disableApp', { appId: apps, groups: [] })
  283. .catch((error) => {
  284. OC.Notification.show(error)
  285. })
  286. },
  287. updateAll() {
  288. const limit = pLimit(1)
  289. this.apps
  290. .filter(app => app.update)
  291. .map(app => limit(() => this.$store.dispatch('updateApp', { appId: app.id })),
  292. )
  293. },
  294. },
  295. }
  296. </script>
  297. <style lang="scss" scoped>
  298. .app-bundle-heading {
  299. display: flex;
  300. align-items: center;
  301. margin: 20px 10px 20px 0;
  302. }
  303. .app-bundle-header {
  304. margin: 0 10px 0 50px;
  305. font-weight: bold;
  306. font-size: 20px;
  307. line-height: 30px;
  308. color: var(--color-text-light);
  309. }
  310. .apps-store-view {
  311. display: flex;
  312. flex-wrap: wrap;
  313. }
  314. </style>