admin-navigation.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.animated.fadeInUp(src='/svg/icon-triangle-arrow.svg', alt='Navigation', style='width: 80px;')
  7. .admin-header-title
  8. .headline.primary--text.animated.fadeInLeft {{$t('navigation.title')}}
  9. .subheading.grey--text.animated.fadeInLeft.wait-p4s {{$t('navigation.subtitle')}}
  10. v-spacer
  11. v-btn.animated.fadeInDown.wait-p2s(outline, color='grey', @click='refresh', large)
  12. v-icon refresh
  13. v-btn.animated.fadeInDown(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.animated.fadeInUp
  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.animated.fadeInUp.wait-p2s
  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. hide-details
  80. )
  81. .caption.pt-2.pl-5 Refer to the #[a(href='https://material.io/tools/icons/?style=baseline', target='_blank') Material Design Icons Reference] for the list of all possible values.
  82. v-select.mt-4(
  83. outline
  84. :label='$t("navigation.targetType")'
  85. prepend-icon='near_me'
  86. :items='navTypes'
  87. v-model='current.targetType'
  88. )
  89. v-text-field(
  90. v-if='current.targetType === `external`'
  91. outline
  92. :label='$t("navigation.target")'
  93. prepend-icon='near_me'
  94. v-model='current.target'
  95. )
  96. v-btn(
  97. v-else-if='current.targetType === "page"'
  98. color='indigo'
  99. dark
  100. @click='selectPage'
  101. )
  102. v-icon(left) search
  103. span Select Page...
  104. v-text-field(
  105. v-else-if='current.targetType === `search`'
  106. outline
  107. :label='$t("navigation.navType.searchQuery")'
  108. prepend-icon='search'
  109. v-model='current.target'
  110. )
  111. v-card-chin
  112. v-spacer
  113. v-btn(color='red', outline, @click='deleteItem(current)')
  114. v-icon(left) delete
  115. span {{$t('navigation.delete', { kind: $t('navigation.link') })}}
  116. v-card(v-else-if='current.kind === "header"')
  117. v-toolbar(dense, color='blue', flat, dark)
  118. .subheading {{$t('navigation.edit', { kind: $t('navigation.header') })}}
  119. v-card-text
  120. v-text-field(
  121. outline
  122. background-color='grey lighten-2'
  123. :label='$t("navigation.label")'
  124. prepend-icon='title'
  125. v-model='current.label'
  126. )
  127. v-card-chin
  128. v-spacer
  129. v-btn(color='red', outline, @click='deleteItem(current)')
  130. v-icon(left) delete
  131. span {{$t('navigation.delete', { kind: $t('navigation.header') })}}
  132. div(v-else-if='current.kind === "divider"')
  133. v-btn.mt-0(color='red', outline, @click='deleteItem(current)')
  134. v-icon(left) delete
  135. span {{$t('navigation.delete', { kind: $t('navigation.divider') })}}
  136. v-card(v-else)
  137. v-card-text.grey--text(v-if='navTree.length > 0') {{$t('navigation.noSelectionText')}}
  138. v-card-text.grey--text(v-else) {{$t('navigation.noItemsText')}}
  139. </template>
  140. <script>
  141. import _ from 'lodash'
  142. import uuid from 'uuid/v4'
  143. import treeSaveMutation from 'gql/admin/navigation/navigation-mutation-save-tree.gql'
  144. import treeQuery from 'gql/admin/navigation/navigation-query-tree.gql'
  145. import draggable from 'vuedraggable'
  146. export default {
  147. components: {
  148. draggable
  149. },
  150. data() {
  151. return {
  152. navTree: [],
  153. current: {}
  154. }
  155. },
  156. computed: {
  157. navTypes() {
  158. return [
  159. { text: this.$t('navigation.navType.external'), value: 'external' },
  160. { text: this.$t('navigation.navType.home'), value: 'home' },
  161. { text: this.$t('navigation.navType.page'), value: 'page' }
  162. // { text: this.$t('navigation.navType.searchQuery'), value: 'search' }
  163. ]
  164. }
  165. },
  166. methods: {
  167. addItem(kind) {
  168. let newItem = {
  169. id: uuid(),
  170. kind
  171. }
  172. switch (kind) {
  173. case 'link':
  174. newItem = {
  175. ...newItem,
  176. label: this.$t('navigation.untitled', { kind: this.$t(`navigation.link`) }),
  177. icon: 'chevron_right',
  178. targetType: 'home',
  179. target: '/'
  180. }
  181. break
  182. case 'header':
  183. newItem.label = this.$t('navigation.untitled', { kind: this.$t(`navigation.header`) })
  184. break
  185. }
  186. this.navTree.push(newItem)
  187. this.current = newItem
  188. },
  189. deleteItem(item) {
  190. this.navTree = _.pull(this.navTree, item)
  191. this.current = {}
  192. },
  193. selectItem(item) {
  194. this.current = item
  195. },
  196. selectPage() {
  197. window.alert(`Coming soon. Use External Link for now (you can still specify internal links).`)
  198. },
  199. async save() {
  200. this.$store.commit(`loadingStart`, 'admin-navigation-save')
  201. try {
  202. const resp = await this.$apollo.mutate({
  203. mutation: treeSaveMutation,
  204. variables: {
  205. tree: this.navTree
  206. }
  207. })
  208. if (_.get(resp, 'data.navigation.updateTree.responseResult.succeeded', false)) {
  209. this.$store.commit('showNotification', {
  210. message: this.$t('navigation.saveSuccess'),
  211. style: 'success',
  212. icon: 'check'
  213. })
  214. } else {
  215. throw new Error(_.get(resp, 'data.navigation.updateTree.responseResult.message', 'An unexpected error occured.'))
  216. }
  217. } catch (err) {
  218. this.$store.commit('pushGraphError', err)
  219. }
  220. this.$store.commit(`loadingStop`, 'admin-navigation-save')
  221. },
  222. async refresh() {
  223. await this.$apollo.queries.navTree.refetch()
  224. this.current = {}
  225. this.$store.commit('showNotification', {
  226. message: 'Navigation has been refreshed.',
  227. style: 'success',
  228. icon: 'cached'
  229. })
  230. }
  231. },
  232. apollo: {
  233. navTree: {
  234. query: treeQuery,
  235. fetchPolicy: 'network-only',
  236. update: (data) => _.cloneDeep(data.navigation.tree),
  237. watchLoading (isLoading) {
  238. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-navigation-tree')
  239. }
  240. }
  241. }
  242. }
  243. </script>
  244. <style lang='scss' scoped>
  245. .clickable {
  246. cursor: pointer;
  247. &:hover {
  248. background-color: rgba(mc('blue', '500'), .25);
  249. }
  250. }
  251. </style>