admin-navigation.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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='currentTree.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='currentTree')
  77. template(v-for='navItem in currentTree')
  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='currentTree.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. trees: [],
  242. current: {},
  243. currentLang: siteConfig.lang,
  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. currentTree: {
  263. get () {
  264. return _.get(_.find(this.trees, ['locale', this.currentLang]), 'items', null) || []
  265. },
  266. set (val) {
  267. const tree = _.find(this.trees, ['locale', this.currentLang])
  268. if (tree) {
  269. tree.items = val
  270. } else {
  271. this.trees = [...this.trees, {
  272. locale: this.currentLang,
  273. items: val
  274. }]
  275. }
  276. }
  277. }
  278. },
  279. watch: {
  280. currentLang (newValue, oldValue) {
  281. this.$nextTick(() => {
  282. if (this.currentTree.length > 0) {
  283. this.current = this.currentTree[0]
  284. } else {
  285. this.current = {}
  286. }
  287. })
  288. }
  289. },
  290. methods: {
  291. addItem(kind) {
  292. let newItem = {
  293. id: uuid(),
  294. kind,
  295. visibilityMode: 'all',
  296. visibilityGroups: []
  297. }
  298. switch (kind) {
  299. case 'link':
  300. newItem = {
  301. ...newItem,
  302. label: this.$t('navigation.untitled', { kind: this.$t(`navigation.link`) }),
  303. icon: 'mdi-chevron-right',
  304. targetType: 'home',
  305. target: '/'
  306. }
  307. break
  308. case 'header':
  309. newItem.label = this.$t('navigation.untitled', { kind: this.$t(`navigation.header`) })
  310. break
  311. }
  312. this.currentTree = [...this.currentTree, newItem]
  313. this.current = newItem
  314. },
  315. deleteItem(item) {
  316. this.currentTree = _.pull(this.currentTree, item)
  317. this.current = {}
  318. },
  319. selectItem(item) {
  320. this.current = item
  321. },
  322. selectPage() {
  323. this.selectPageModal = true
  324. },
  325. selectPageHandle ({ path, locale }) {
  326. this.current.target = `/${locale}/${path}`
  327. },
  328. async save() {
  329. this.$store.commit(`loadingStart`, 'admin-navigation-save')
  330. try {
  331. const resp = await this.$apollo.mutate({
  332. mutation: gql`
  333. mutation ($tree: [NavigationTreeInput]!) {
  334. navigation{
  335. updateTree(tree: $tree) {
  336. responseResult {
  337. succeeded
  338. errorCode
  339. slug
  340. message
  341. }
  342. }
  343. }
  344. }
  345. `,
  346. variables: {
  347. tree: this.trees
  348. }
  349. })
  350. if (_.get(resp, 'data.navigation.updateTree.responseResult.succeeded', false)) {
  351. this.$store.commit('showNotification', {
  352. message: this.$t('navigation.saveSuccess'),
  353. style: 'success',
  354. icon: 'check'
  355. })
  356. } else {
  357. throw new Error(_.get(resp, 'data.navigation.updateTree.responseResult.message', 'An unexpected error occured.'))
  358. }
  359. } catch (err) {
  360. this.$store.commit('pushGraphError', err)
  361. }
  362. this.$store.commit(`loadingStop`, 'admin-navigation-save')
  363. },
  364. async refresh() {
  365. await this.$apollo.queries.trees.refetch()
  366. this.current = {}
  367. this.$store.commit('showNotification', {
  368. message: 'Navigation has been refreshed.',
  369. style: 'success',
  370. icon: 'cached'
  371. })
  372. }
  373. },
  374. apollo: {
  375. config: {
  376. query: gql`
  377. {
  378. navigation {
  379. config {
  380. mode
  381. }
  382. }
  383. }
  384. `,
  385. fetchPolicy: 'network-only',
  386. update: (data) => _.cloneDeep(data.navigation.config),
  387. watchLoading (isLoading) {
  388. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-navigation-config')
  389. }
  390. },
  391. trees: {
  392. query: gql`
  393. {
  394. navigation {
  395. tree {
  396. locale
  397. items {
  398. id
  399. kind
  400. label
  401. icon
  402. targetType
  403. target
  404. visibilityMode
  405. visibilityGroups
  406. }
  407. }
  408. }
  409. }
  410. `,
  411. fetchPolicy: 'network-only',
  412. update: (data) => _.cloneDeep(data.navigation.tree),
  413. watchLoading (isLoading) {
  414. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-navigation-tree')
  415. }
  416. },
  417. groups: {
  418. query: groupsQuery,
  419. fetchPolicy: 'network-only',
  420. update: (data) => data.groups.list,
  421. watchLoading (isLoading) {
  422. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-navigation-groups')
  423. }
  424. }
  425. }
  426. }
  427. </script>
  428. <style lang='scss' scoped>
  429. .clickable {
  430. cursor: pointer;
  431. &:hover {
  432. background-color: rgba(mc('blue', '500'), .25);
  433. }
  434. }
  435. </style>