You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
9.3 KiB

  1. <template lang='pug'>
  2. v-container(fluid, grid-list-lg)
  3. v-layout(row wrap)
  4. v-flex(xs12)
  5. .admin-header
  6. img(src='/svg/icon-triangle-arrow.svg', alt='Navigation', style='width: 80px;')
  7. .admin-header-title
  8. .headline.primary--text {{$t('navigation.title')}}
  9. .subheading.grey--text {{$t('navigation.subtitle')}}
  10. v-spacer
  11. v-btn(outline, color='grey', @click='refresh', large)
  12. v-icon refresh
  13. v-btn(color='success', depressed, @click='save', large)
  14. v-icon(left) check
  15. span {{$t('common:actions.apply')}}
  16. v-container.pa-0.mt-3(fluid, grid-list-lg)
  17. v-layout(row)
  18. v-flex(style='flex: 0 0 350px;')
  19. v-card
  20. v-list.py-2(dense, dark, :class='navTree.length < 1 ? "grey lighten-4" : "primary"')
  21. v-list-tile(v-if='navTree.length < 1')
  22. v-list-tile-avatar: v-icon(color='grey') explore_off
  23. v-list-tile-content
  24. .caption.grey--text {{$t('navigation.emptyList')}}
  25. draggable(v-model='navTree')
  26. template(v-for='navItem in navTree')
  27. v-list-tile(
  28. v-if='navItem.kind === "link"'
  29. :key='navItem.id'
  30. :class='(navItem === current) ? "blue" : ""'
  31. @click='selectItem(navItem)'
  32. )
  33. v-list-tile-avatar: v-icon {{navItem.icon}}
  34. v-list-tile-title {{navItem.label}}
  35. .py-2.clickable(
  36. v-else-if='navItem.kind === "divider"'
  37. :key='navItem.id'
  38. :class='(navItem === current) ? "blue" : ""'
  39. @click='selectItem(navItem)'
  40. )
  41. v-divider
  42. v-subheader.pl-4.clickable(
  43. v-else-if='navItem.kind === "header"'
  44. :key='navItem.id'
  45. :class='(navItem === current) ? "blue" : ""'
  46. @click='selectItem(navItem)'
  47. ) {{navItem.label}}
  48. v-card-chin
  49. v-menu(offset-y, bottom, min-width='200px', style='flex: 1 1;')
  50. v-btn(slot='activator', color='primary', depressed, block)
  51. v-icon(left) add
  52. span {{$t('common:actions.add')}}
  53. v-list
  54. v-list-tile(@click='addItem("link")')
  55. v-list-tile-avatar: v-icon link
  56. v-list-tile-title {{$t('navigation.link')}}
  57. v-list-tile(@click='addItem("header")')
  58. v-list-tile-avatar: v-icon title
  59. v-list-tile-title {{$t('navigation.header')}}
  60. v-list-tile(@click='addItem("divider")')
  61. v-list-tile-avatar: v-icon power_input
  62. v-list-tile-title {{$t('navigation.divider')}}
  63. v-flex
  64. v-card.wiki-form(v-if='current.kind === "link"')
  65. v-toolbar(dense, color='blue', flat, dark)
  66. .subheading {{$t('navigation.edit', { kind: $t('navigation.link') })}}
  67. v-card-text
  68. v-text-field(
  69. outline
  70. :label='$t("navigation.label")'
  71. prepend-icon='title'
  72. v-model='current.label'
  73. )
  74. v-text-field(
  75. outline
  76. :label='$t("navigation.icon")'
  77. prepend-icon='casino'
  78. v-model='current.icon'
  79. )
  80. v-select(
  81. outline
  82. :label='$t("navigation.targetType")'
  83. prepend-icon='near_me'
  84. :items='navTypes'
  85. v-model='current.targetType'
  86. )
  87. v-text-field(
  88. v-if='current.targetType === `external`'
  89. outline
  90. :label='$t("navigation.target")'
  91. prepend-icon='near_me'
  92. v-model='current.target'
  93. )
  94. v-btn(
  95. v-else-if='current.targetType === "page"'
  96. color='indigo'
  97. dark
  98. )
  99. v-icon(left) search
  100. span Select Page...
  101. v-text-field(
  102. v-else-if='current.targetType === `search`'
  103. outline
  104. :label='$t("navigation.navType.searchQuery")'
  105. prepend-icon='search'
  106. v-model='current.target'
  107. )
  108. v-card-chin
  109. v-spacer
  110. v-btn(color='red', outline, @click='deleteItem(current)')
  111. v-icon(left) delete
  112. span {{$t('navigation.delete', { kind: $t('navigation.link') })}}
  113. v-card(v-else-if='current.kind === "header"')
  114. v-toolbar(dense, color='blue', flat, dark)
  115. .subheading {{$t('navigation.edit', { kind: $t('navigation.header') })}}
  116. v-card-text
  117. v-text-field(
  118. outline
  119. background-color='grey lighten-2'
  120. :label='$t("navigation.label")'
  121. prepend-icon='title'
  122. v-model='current.label'
  123. )
  124. v-card-chin
  125. v-spacer
  126. v-btn(color='red', outline, @click='deleteItem(current)')
  127. v-icon(left) delete
  128. span {{$t('navigation.delete', { kind: $t('navigation.header') })}}
  129. div(v-else-if='current.kind === "divider"')
  130. v-btn.mt-0(color='red', outline, @click='deleteItem(current)')
  131. v-icon(left) delete
  132. span {{$t('navigation.delete', { kind: $t('navigation.divider') })}}
  133. v-card(v-else)
  134. v-card-text.grey--text(v-if='navTree.length > 0') {{$t('navigation.noSelectionText')}}
  135. v-card-text.grey--text(v-else) {{$t('navigation.noItemsText')}}
  136. </template>
  137. <script>
  138. import _ from 'lodash'
  139. import uuid from 'uuid/v4'
  140. import treeSaveMutation from 'gql/admin/navigation/navigation-mutation-save-tree.gql'
  141. import treeQuery from 'gql/admin/navigation/navigation-query-tree.gql'
  142. import draggable from 'vuedraggable'
  143. export default {
  144. components: {
  145. draggable
  146. },
  147. data() {
  148. return {
  149. navTree: [],
  150. current: {}
  151. }
  152. },
  153. computed: {
  154. navTypes() {
  155. return [
  156. { text: this.$t('navigation.navType.external'), value: 'external' },
  157. { text: this.$t('navigation.navType.home'), value: 'home' },
  158. { text: this.$t('navigation.navType.page'), value: 'page' },
  159. { text: this.$t('navigation.navType.searchQuery'), value: 'search' }
  160. ]
  161. }
  162. },
  163. methods: {
  164. addItem(kind) {
  165. let newItem = {
  166. id: uuid(),
  167. kind
  168. }
  169. switch (kind) {
  170. case 'link':
  171. newItem = {
  172. ...newItem,
  173. label: this.$t('navigation.untitled', { kind: this.$t(`navigation.link`) }),
  174. icon: 'chevron_right',
  175. targetType: 'home',
  176. target: '/'
  177. }
  178. break
  179. case 'header':
  180. newItem.label = this.$t('navigation.untitled', { kind: this.$t(`navigation.header`) })
  181. break
  182. }
  183. this.navTree.push(newItem)
  184. this.current = newItem
  185. },
  186. deleteItem(item) {
  187. this.navTree = _.pull(this.navTree, item)
  188. this.current = {}
  189. },
  190. selectItem(item) {
  191. this.current = item
  192. },
  193. async save() {
  194. this.$store.commit(`loadingStart`, 'admin-navigation-save')
  195. try {
  196. const resp = await this.$apollo.mutate({
  197. mutation: treeSaveMutation,
  198. variables: {
  199. tree: this.navTree
  200. }
  201. })
  202. if (_.get(resp, 'data.navigation.updateTree.responseResult.succeeded', false)) {
  203. this.$store.commit('showNotification', {
  204. message: this.$t('navigation.saveSuccess'),
  205. style: 'success',
  206. icon: 'check'
  207. })
  208. } else {
  209. throw new Error(_.get(resp, 'data.navigation.updateTree.responseResult.message', 'An unexpected error occured.'))
  210. }
  211. } catch (err) {
  212. this.$store.commit('showNotification', {
  213. message: err.message,
  214. style: 'red',
  215. icon: 'warning'
  216. })
  217. }
  218. this.$store.commit(`loadingStop`, 'admin-navigation-save')
  219. },
  220. async refresh() {
  221. await this.$apollo.queries.navTree.refetch()
  222. this.current = {}
  223. this.$store.commit('showNotification', {
  224. message: 'Navigation has been refreshed.',
  225. style: 'success',
  226. icon: 'cached'
  227. })
  228. }
  229. },
  230. apollo: {
  231. navTree: {
  232. query: treeQuery,
  233. fetchPolicy: 'network-only',
  234. update: (data) => _.cloneDeep(data.navigation.tree),
  235. watchLoading (isLoading) {
  236. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-navigation-tree')
  237. }
  238. }
  239. }
  240. }
  241. </script>
  242. <style lang='scss' scoped>
  243. .clickable {
  244. cursor: pointer;
  245. &:hover {
  246. background-color: rgba(mc('blue', '500'), .25);
  247. }
  248. }
  249. </style>