admin-navigation.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. .subtitle-1.grey--text.animated.fadeInLeft.wait-p4s {{$t('navigation.subtitle')}}
  10. v-spacer
  11. v-btn.animated.fadeInDown.wait-p2s.mr-3(outlined, color='grey', @click='refresh', large)
  12. v-icon mdi-refresh
  13. v-btn.animated.fadeInDown(color='success', depressed, @click='save', large)
  14. v-icon(left) mdi-check
  15. span {{$t('common:actions.apply')}}
  16. v-container.pa-0.mt-3(fluid, grid-list-lg)
  17. v-row(dense)
  18. v-col(cols='3')
  19. v-card.animated.fadeInUp
  20. v-toolbar(color='teal', dark, dense, flat, height='56')
  21. v-toolbar-title.subtitle-1 {{$t('admin:navigation.mode')}}
  22. v-list(nav, two-line)
  23. v-list-item-group(v-model='config.mode', mandatory, :color='$vuetify.theme.dark ? `teal lighten-3` : `teal`')
  24. v-list-item(value='TREE')
  25. v-list-item-avatar
  26. img(src='/svg/icon-tree-structure-dotted.svg', alt='Site Tree')
  27. v-list-item-content
  28. v-list-item-title {{$t('admin:navigation.modeSiteTree.title')}}
  29. v-list-item-subtitle {{$t('admin:navigation.modeSiteTree.description')}}
  30. v-list-item-avatar
  31. v-icon(v-if='$vuetify.theme.dark', :color='config.mode === `TREE` ? `teal lighten-3` : `grey darken-2`') mdi-check-circle
  32. v-icon(v-else, :color='config.mode === `TREE` ? `teal` : `grey lighten-3`') mdi-check-circle
  33. v-list-item(value='MIXED')
  34. v-list-item-avatar
  35. img(src='/svg/icon-user-menu-male-dotted.svg', alt='Custom Navigation')
  36. v-list-item-content
  37. v-list-item-title {{$t('admin:navigation.modeCustom.title')}}
  38. v-list-item-subtitle {{$t('admin:navigation.modeCustom.description')}}
  39. v-list-item-avatar
  40. v-icon(v-if='$vuetify.theme.dark', :color='config.mode === `MIXED` ? `teal lighten-3` : `grey darken-2`') mdi-check-circle
  41. v-icon(v-else, :color='config.mode === `MIXED` ? `teal` : `grey lighten-3`') mdi-check-circle
  42. v-list-item(value='NONE')
  43. v-list-item-avatar
  44. img(src='/svg/icon-cancel-dotted.svg', alt='None')
  45. v-list-item-content
  46. v-list-item-title {{$t('admin:navigation.modeNone.title')}}
  47. v-list-item-subtitle {{$t('admin:navigation.modeNone.description')}}
  48. v-list-item-avatar
  49. v-icon(v-if='$vuetify.theme.dark', :color='config.mode === `none` ? `teal lighten-3` : `grey darken-2`') mdi-check-circle
  50. v-icon(v-else, :color='config.mode === `none` ? `teal` : `grey lighten-3`') mdi-check-circle
  51. v-col(cols='9', v-if='config.mode === `MIXED`')
  52. v-card.animated.fadeInUp.wait-p2s
  53. v-row(no-gutters, align='stretch')
  54. v-col(style='flex: 0 0 350px;')
  55. v-card.grey(flat, style='height: 100%; border-radius: 4px 0 0 4px;', :class='$vuetify.theme.dark ? `darken-4-l5` : `lighten-3`')
  56. .teal.pa-2(style='margin-bottom: 1px; height: 56px;')
  57. v-select(
  58. v-if='locales.length > 0'
  59. label='Locale'
  60. hide-details
  61. solo
  62. flat
  63. background-color='teal darken-2'
  64. dark
  65. dense
  66. v-model='currentLang'
  67. :items='locales'
  68. item-text='name'
  69. item-value='code'
  70. )
  71. v-list.py-2(dense, nav, dark, class='blue darken-2', style='border-radius: 0;')
  72. v-list-item(v-if='navTree.length < 1')
  73. v-list-item-avatar(size='24'): v-icon(color='blue lighten-3') mdi-alert
  74. v-list-item-content
  75. em.caption.blue--text.text--lighten-4 {{$t('navigation.emptyList')}}
  76. draggable(v-model='navTree')
  77. template(v-for='navItem in navTree')
  78. v-list-item(
  79. v-if='navItem.kind === "link"'
  80. :key='navItem.id'
  81. :class='(navItem === current) ? "blue" : ""'
  82. @click='selectItem(navItem)'
  83. )
  84. v-list-item-avatar(size='24'): v-icon {{navItem.icon}}
  85. v-list-item-title {{navItem.label}}
  86. .py-2.clickable(
  87. v-else-if='navItem.kind === "divider"'
  88. :key='navItem.id'
  89. :class='(navItem === current) ? "blue" : ""'
  90. @click='selectItem(navItem)'
  91. )
  92. v-divider
  93. v-subheader.pl-4.clickable(
  94. v-else-if='navItem.kind === "header"'
  95. :key='navItem.id'
  96. :class='(navItem === current) ? "blue" : ""'
  97. @click='selectItem(navItem)'
  98. ) {{navItem.label}}
  99. v-card-chin
  100. v-menu(offset-y, bottom, min-width='200px', style='flex: 1 1;')
  101. template(v-slot:activator='{ on }')
  102. v-btn(v-on='on', color='primary', depressed, block)
  103. v-icon(left) mdi-plus
  104. span {{$t('common:actions.add')}}
  105. v-list
  106. v-list-item(@click='addItem("link")')
  107. v-list-item-avatar(size='24'): v-icon mdi-link
  108. v-list-item-title {{$t('navigation.link')}}
  109. v-list-item(@click='addItem("header")')
  110. v-list-item-avatar(size='24'): v-icon mdi-format-title
  111. v-list-item-title {{$t('navigation.header')}}
  112. v-list-item(@click='addItem("divider")')
  113. v-list-item-avatar(size='24'): v-icon mdi-minus
  114. v-list-item-title {{$t('navigation.divider')}}
  115. v-col
  116. v-card(flat, style='border-radius: 0 4px 4px 0;')
  117. template(v-if='current.kind === "link"')
  118. v-toolbar(height='56', color='teal lighten-1', flat, dark)
  119. .subtitle-1 {{$t('navigation.edit', { kind: $t('navigation.link') })}}
  120. v-spacer
  121. v-btn.px-5(color='white', outlined, @click='deleteItem(current)')
  122. v-icon(left) mdi-delete
  123. span {{$t('navigation.delete', { kind: $t('navigation.link') })}}
  124. v-card-text
  125. v-text-field(
  126. outlined
  127. :label='$t("navigation.label")'
  128. prepend-icon='mdi-format-title'
  129. v-model='current.label'
  130. counter='255'
  131. )
  132. v-text-field(
  133. outlined
  134. :label='$t("navigation.icon")'
  135. prepend-icon='mdi-dice-5'
  136. v-model='current.icon'
  137. hide-details
  138. )
  139. .caption.pt-3.pl-5 The default icon set is #[strong Material Design Icons]. In order to use another icon set, you must first select it in the Theme administration section.
  140. .caption.pt-3.pl-5: strong Material Design Icons
  141. .caption.pl-5 Refer to the #[a(href='https://materialdesignicons.com/', target='_blank') Material Design Icons Reference] for the list of all possible values. You must prefix all values with #[code mdi-], e.g. #[code mdi-home]
  142. .caption.pt-3.pl-5: strong Font Awesome 5
  143. .caption.pl-5 Refer to the #[a(href='https://fontawesome.com/icons?d=gallery&m=free', target='_blank') Font Awesome 5 Reference] for the list of all possible values. You must prefix all values with #[code fas fa-], e.g. #[code fas fa-home]. Note that some icons use different prefixes (e.g. #[code fab], #[code fad], #[code fal], #[code far]).
  144. .caption.pt-3.pl-5: strong Font Awesome 4
  145. .caption.pl-5 Refer to the #[a(href='https://fontawesome.com/v4.7.0/icons/', target='_blank') Font Awesome 4 Reference] for the list of all possible values. You must prefix all values with #[code fa fa-], e.g. #[code fa fa-home]
  146. v-divider
  147. v-card-text
  148. v-select(
  149. outlined
  150. :label='$t("navigation.targetType")'
  151. prepend-icon='mdi-near-me'
  152. :items='navTypes'
  153. v-model='current.targetType'
  154. hide-details
  155. )
  156. v-text-field.mt-4(
  157. v-if='current.targetType === `external`'
  158. outlined
  159. :label='$t("navigation.target")'
  160. prepend-icon='mdi-near-me'
  161. v-model='current.target'
  162. hide-details
  163. )
  164. .d-flex.align-center.mt-4(v-else-if='current.targetType === "page"')
  165. v-btn.ml-8(
  166. color='primary'
  167. dark
  168. @click='selectPage'
  169. )
  170. v-icon(left) mdi-magnify
  171. span {{$t('admin:navigation.selectPageButton')}}
  172. .caption.ml-4.primary--text {{current.target}}
  173. v-text-field(
  174. v-else-if='current.targetType === `search`'
  175. outlined
  176. :label='$t("navigation.navType.searchQuery")'
  177. prepend-icon='search'
  178. v-model='current.target'
  179. )
  180. v-divider
  181. template(v-else-if='current.kind === "header"')
  182. v-toolbar(height='56', color='teal lighten-1', flat, dark)
  183. .subtitle-1 {{$t('navigation.edit', { kind: $t('navigation.header') })}}
  184. v-spacer
  185. v-btn.px-5(color='white', outlined, @click='deleteItem(current)')
  186. v-icon(left) mdi-delete
  187. span {{$t('navigation.delete', { kind: $t('navigation.header') })}}
  188. v-card-text
  189. v-text-field(
  190. outlined
  191. :label='$t("navigation.label")'
  192. prepend-icon='mdi-format-title'
  193. v-model='current.label'
  194. )
  195. v-divider
  196. div(v-else-if='current.kind === "divider"')
  197. v-toolbar(height='56', color='teal lighten-1', flat, dark)
  198. .subtitle-1 {{$t('navigation.edit', { kind: $t('navigation.divider') })}}
  199. v-spacer
  200. v-btn.px-5(color='white', outlined, @click='deleteItem(current)')
  201. v-icon(left) mdi-delete
  202. span {{$t('navigation.delete', { kind: $t('navigation.divider') })}}
  203. v-card-text(v-if='current.kind')
  204. v-radio-group.pl-8(v-model='current.visibilityMode', mandatory, hide-details)
  205. v-radio(:label='$t("admin:navigation.visibilityMode.all")', value='all', color='primary')
  206. v-radio.mt-3(:label='$t("admin:navigation.visibilityMode.restricted")', value='restricted', color='primary')
  207. .pl-8
  208. v-select.pl-8.mt-3(
  209. item-text='name'
  210. item-value='id'
  211. outlined
  212. prepend-icon='mdi-account-group'
  213. label='Groups'
  214. :disabled='current.visibilityMode !== `restricted`'
  215. v-model='current.visibilityGroups'
  216. :items='groups'
  217. persistent-hint
  218. clearable
  219. multiple
  220. )
  221. template(v-else)
  222. v-toolbar(height='56', color='teal lighten-1', flat, dark)
  223. v-card-text.grey--text(v-if='navTree.length > 0') {{$t('navigation.noSelectionText')}}
  224. v-card-text.grey--text(v-else) {{$t('navigation.noItemsText')}}
  225. page-selector(mode='select', v-model='selectPageModal', :open-handler='selectPageHandle', path='home', :locale='currentLang')
  226. </template>
  227. <script>
  228. import _ from 'lodash'
  229. import gql from 'graphql-tag'
  230. import { v4 as uuid } from 'uuid'
  231. import groupsQuery from 'gql/admin/users/users-query-groups.gql'
  232. import draggable from 'vuedraggable'
  233. /* global siteConfig, siteLangs */
  234. export default {
  235. components: {
  236. draggable
  237. },
  238. data() {
  239. return {
  240. selectPageModal: false,
  241. navTree: [],
  242. current: {},
  243. currentLang: 'en',
  244. groups: [],
  245. config: {
  246. mode: 'NONE'
  247. }
  248. }
  249. },
  250. computed: {
  251. navTypes () {
  252. return [
  253. { text: this.$t('navigation.navType.external'), value: 'external' },
  254. { text: this.$t('navigation.navType.home'), value: 'home' },
  255. { text: this.$t('navigation.navType.page'), value: 'page' }
  256. // { text: this.$t('navigation.navType.searchQuery'), value: 'search' }
  257. ]
  258. },
  259. locales () {
  260. return siteLangs
  261. }
  262. },
  263. methods: {
  264. addItem(kind) {
  265. let newItem = {
  266. id: uuid(),
  267. kind,
  268. visibilityMode: 'all',
  269. visibilityGroups: []
  270. }
  271. switch (kind) {
  272. case 'link':
  273. newItem = {
  274. ...newItem,
  275. label: this.$t('navigation.untitled', { kind: this.$t(`navigation.link`) }),
  276. icon: 'mdi-chevron-right',
  277. targetType: 'home',
  278. target: '/'
  279. }
  280. break
  281. case 'header':
  282. newItem.label = this.$t('navigation.untitled', { kind: this.$t(`navigation.header`) })
  283. break
  284. }
  285. this.navTree.push(newItem)
  286. this.current = newItem
  287. },
  288. deleteItem(item) {
  289. this.navTree = _.pull(this.navTree, item)
  290. this.current = {}
  291. },
  292. selectItem(item) {
  293. this.current = item
  294. },
  295. selectPage() {
  296. this.selectPageModal = true
  297. },
  298. selectPageHandle ({ path, locale }) {
  299. this.current.target = `/${locale}/${path}`
  300. },
  301. async save() {
  302. this.$store.commit(`loadingStart`, 'admin-navigation-save')
  303. try {
  304. const resp = await this.$apollo.mutate({
  305. mutation: gql`
  306. mutation ($tree: [NavigationTreeInput]!) {
  307. navigation{
  308. updateTree(tree: $tree) {
  309. responseResult {
  310. succeeded
  311. errorCode
  312. slug
  313. message
  314. }
  315. }
  316. }
  317. }
  318. `,
  319. variables: {
  320. tree: this.navTree
  321. }
  322. })
  323. if (_.get(resp, 'data.navigation.updateTree.responseResult.succeeded', false)) {
  324. this.$store.commit('showNotification', {
  325. message: this.$t('navigation.saveSuccess'),
  326. style: 'success',
  327. icon: 'check'
  328. })
  329. } else {
  330. throw new Error(_.get(resp, 'data.navigation.updateTree.responseResult.message', 'An unexpected error occured.'))
  331. }
  332. } catch (err) {
  333. this.$store.commit('pushGraphError', err)
  334. }
  335. this.$store.commit(`loadingStop`, 'admin-navigation-save')
  336. },
  337. async refresh() {
  338. await this.$apollo.queries.navTree.refetch()
  339. this.current = {}
  340. this.$store.commit('showNotification', {
  341. message: 'Navigation has been refreshed.',
  342. style: 'success',
  343. icon: 'cached'
  344. })
  345. }
  346. },
  347. mounted () {
  348. this.currentLang = siteConfig.lang
  349. },
  350. apollo: {
  351. config: {
  352. query: gql`
  353. {
  354. navigation {
  355. config {
  356. mode
  357. }
  358. }
  359. }
  360. `,
  361. fetchPolicy: 'network-only',
  362. update: (data) => _.cloneDeep(data.navigation.config),
  363. watchLoading (isLoading) {
  364. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-navigation-config')
  365. }
  366. },
  367. navTree: {
  368. query: gql`
  369. {
  370. navigation {
  371. tree {
  372. locale
  373. items {
  374. id
  375. kind
  376. label
  377. icon
  378. targetType
  379. target
  380. }
  381. }
  382. }
  383. }
  384. `,
  385. fetchPolicy: 'network-only',
  386. update: (data) => _.cloneDeep(data.navigation.tree),
  387. watchLoading (isLoading) {
  388. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-navigation-tree')
  389. }
  390. },
  391. groups: {
  392. query: groupsQuery,
  393. fetchPolicy: 'network-only',
  394. update: (data) => data.groups.list,
  395. watchLoading (isLoading) {
  396. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-navigation-groups')
  397. }
  398. }
  399. }
  400. }
  401. </script>
  402. <style lang='scss' scoped>
  403. .clickable {
  404. cursor: pointer;
  405. &:hover {
  406. background-color: rgba(mc('blue', '500'), .25);
  407. }
  408. }
  409. </style>