admin-navigation.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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='STATIC')
  43. v-list-item-avatar
  44. img(src='/svg/icon-features-list.svg', alt='Static Navigation')
  45. v-list-item-content
  46. v-list-item-title {{$t('admin:navigation.modeStatic.title')}}
  47. v-list-item-subtitle {{$t('admin:navigation.modeStatic.description')}}
  48. v-list-item-avatar
  49. v-icon(v-if='$vuetify.theme.dark', :color='config.mode === `STATIC` ? `teal lighten-3` : `grey darken-2`') mdi-check-circle
  50. v-icon(v-else, :color='config.mode === `STATIC` ? `teal` : `grey lighten-3`') mdi-check-circle
  51. v-list-item(value='NONE')
  52. v-list-item-avatar
  53. img(src='/svg/icon-cancel-dotted.svg', alt='None')
  54. v-list-item-content
  55. v-list-item-title {{$t('admin:navigation.modeNone.title')}}
  56. v-list-item-subtitle {{$t('admin:navigation.modeNone.description')}}
  57. v-list-item-avatar
  58. v-icon(v-if='$vuetify.theme.dark', :color='config.mode === `none` ? `teal lighten-3` : `grey darken-2`') mdi-check-circle
  59. v-icon(v-else, :color='config.mode === `none` ? `teal` : `grey lighten-3`') mdi-check-circle
  60. v-col(cols='9', v-if='config.mode === `MIXED` || config.mode === `STATIC`')
  61. v-card.animated.fadeInUp.wait-p2s
  62. v-row(no-gutters, align='stretch')
  63. v-col(style='flex: 0 0 350px;')
  64. v-card.grey(flat, style='height: 100%; border-radius: 4px 0 0 4px;', :class='$vuetify.theme.dark ? `darken-4-l5` : `lighten-3`')
  65. .teal.lighten-1.pa-2.d-flex(style='margin-bottom: 1px; height:56px;')
  66. v-select(
  67. :disabled='locales.length < 2'
  68. label='Locale'
  69. hide-details
  70. solo
  71. flat
  72. background-color='teal darken-2'
  73. dark
  74. dense
  75. v-model='currentLang'
  76. :items='locales'
  77. item-text='nativeName'
  78. item-value='code'
  79. )
  80. v-tooltip(top)
  81. template(v-slot:activator='{ on }')
  82. v-btn.ml-2(icon, tile, color='white', v-on='on', @click='copyFromLocaleDialogIsShown = true')
  83. v-icon mdi-arrange-send-backward
  84. span {{$t('admin:navigation.copyFromLocale')}}
  85. v-list.py-2(dense, nav, dark, class='blue darken-2', style='border-radius: 0;')
  86. v-list-item(v-if='currentTree.length < 1')
  87. v-list-item-avatar(size='24'): v-icon(color='blue lighten-3') mdi-alert
  88. v-list-item-content
  89. em.caption.blue--text.text--lighten-4 {{$t('navigation.emptyList')}}
  90. draggable(v-model='currentTree')
  91. template(v-for='navItem in currentTree')
  92. v-list-item(
  93. v-if='navItem.kind === "link"'
  94. :key='navItem.id'
  95. :class='(navItem === current) ? "blue" : ""'
  96. @click='selectItem(navItem)'
  97. )
  98. v-list-item-avatar(size='24', tile): v-icon {{navItem.icon}}
  99. v-list-item-title {{navItem.label}}
  100. .py-2.clickable(
  101. v-else-if='navItem.kind === "divider"'
  102. :key='navItem.id'
  103. :class='(navItem === current) ? "blue" : ""'
  104. @click='selectItem(navItem)'
  105. )
  106. v-divider
  107. v-subheader.pl-4.clickable(
  108. v-else-if='navItem.kind === "header"'
  109. :key='navItem.id'
  110. :class='(navItem === current) ? "blue" : ""'
  111. @click='selectItem(navItem)'
  112. ) {{navItem.label}}
  113. v-card-chin
  114. v-menu(offset-y, bottom, min-width='200px', style='flex: 1 1;')
  115. template(v-slot:activator='{ on }')
  116. v-btn(v-on='on', color='primary', depressed, block)
  117. v-icon(left) mdi-plus
  118. span {{$t('common:actions.add')}}
  119. v-list
  120. v-list-item(@click='addItem("link")')
  121. v-list-item-avatar(size='24'): v-icon mdi-link
  122. v-list-item-title {{$t('navigation.link')}}
  123. v-list-item(@click='addItem("header")')
  124. v-list-item-avatar(size='24'): v-icon mdi-format-title
  125. v-list-item-title {{$t('navigation.header')}}
  126. v-list-item(@click='addItem("divider")')
  127. v-list-item-avatar(size='24'): v-icon mdi-minus
  128. v-list-item-title {{$t('navigation.divider')}}
  129. v-col
  130. v-card(flat, style='border-radius: 0 4px 4px 0;')
  131. template(v-if='current.kind === "link"')
  132. v-toolbar(height='56', color='teal lighten-1', flat, dark)
  133. .subtitle-1 {{$t('navigation.edit', { kind: $t('navigation.link') })}}
  134. v-spacer
  135. v-btn.px-5(color='white', outlined, @click='deleteItem(current)')
  136. v-icon(left) mdi-delete
  137. span {{$t('navigation.delete', { kind: $t('navigation.link') })}}
  138. v-card-text
  139. v-text-field(
  140. outlined
  141. :label='$t("navigation.label")'
  142. prepend-icon='mdi-format-title'
  143. v-model='current.label'
  144. counter='255'
  145. )
  146. v-text-field(
  147. outlined
  148. :label='$t("navigation.icon")'
  149. prepend-icon='mdi-dice-5'
  150. v-model='current.icon'
  151. hide-details
  152. )
  153. .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.
  154. .caption.pt-3.pl-5: strong Material Design Icons
  155. .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]
  156. .caption.pt-3.pl-5: strong Font Awesome 5
  157. .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]).
  158. .caption.pt-3.pl-5: strong Font Awesome 4
  159. .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]
  160. v-divider
  161. v-card-text
  162. v-select(
  163. outlined
  164. :label='$t("navigation.targetType")'
  165. prepend-icon='mdi-near-me'
  166. :items='navTypes'
  167. v-model='current.targetType'
  168. hide-details
  169. )
  170. v-text-field.mt-4(
  171. v-if='current.targetType === `external`'
  172. outlined
  173. :label='$t("navigation.target")'
  174. prepend-icon='mdi-near-me'
  175. v-model='current.target'
  176. hide-details
  177. )
  178. .d-flex.align-center.mt-4(v-else-if='current.targetType === "page"')
  179. v-btn.ml-8(
  180. color='primary'
  181. dark
  182. @click='selectPage'
  183. )
  184. v-icon(left) mdi-magnify
  185. span {{$t('admin:navigation.selectPageButton')}}
  186. .caption.ml-4.primary--text {{current.target}}
  187. v-text-field(
  188. v-else-if='current.targetType === `search`'
  189. outlined
  190. :label='$t("navigation.navType.searchQuery")'
  191. prepend-icon='search'
  192. v-model='current.target'
  193. )
  194. v-divider
  195. template(v-else-if='current.kind === "header"')
  196. v-toolbar(height='56', color='teal lighten-1', flat, dark)
  197. .subtitle-1 {{$t('navigation.edit', { kind: $t('navigation.header') })}}
  198. v-spacer
  199. v-btn.px-5(color='white', outlined, @click='deleteItem(current)')
  200. v-icon(left) mdi-delete
  201. span {{$t('navigation.delete', { kind: $t('navigation.header') })}}
  202. v-card-text
  203. v-text-field(
  204. outlined
  205. :label='$t("navigation.label")'
  206. prepend-icon='mdi-format-title'
  207. v-model='current.label'
  208. )
  209. v-divider
  210. div(v-else-if='current.kind === "divider"')
  211. v-toolbar(height='56', color='teal lighten-1', flat, dark)
  212. .subtitle-1 {{$t('navigation.edit', { kind: $t('navigation.divider') })}}
  213. v-spacer
  214. v-btn.px-5(color='white', outlined, @click='deleteItem(current)')
  215. v-icon(left) mdi-delete
  216. span {{$t('navigation.delete', { kind: $t('navigation.divider') })}}
  217. v-card-text(v-if='current.kind')
  218. v-radio-group.pl-8(v-model='current.visibilityMode', mandatory, hide-details)
  219. v-radio(:label='$t("admin:navigation.visibilityMode.all")', value='all', color='primary')
  220. v-radio.mt-3(:label='$t("admin:navigation.visibilityMode.restricted")', value='restricted', color='primary')
  221. .pl-8
  222. v-select.pl-8.mt-3(
  223. item-text='name'
  224. item-value='id'
  225. outlined
  226. prepend-icon='mdi-account-group'
  227. label='Groups'
  228. :disabled='current.visibilityMode !== `restricted`'
  229. v-model='current.visibilityGroups'
  230. :items='groups'
  231. persistent-hint
  232. clearable
  233. multiple
  234. )
  235. template(v-else)
  236. v-toolbar(height='56', color='teal lighten-1', flat, dark)
  237. v-card-text.grey--text(v-if='currentTree.length > 0') {{$t('navigation.noSelectionText')}}
  238. v-card-text.grey--text(v-else) {{$t('navigation.noItemsText')}}
  239. v-dialog(v-model='copyFromLocaleDialogIsShown', max-width='650', persistent)
  240. v-card
  241. .dialog-header.is-short.is-teal
  242. v-icon.mr-3(color='white') mdi-arrange-send-backward
  243. span {{$t('admin:navigation.copyFromLocale')}}
  244. v-card-text.pt-5
  245. .body-2 {{$t('admin:navigation.copyFromLocaleInfoText')}}
  246. v-select.mt-3(
  247. :items='locales'
  248. item-text='nativeName'
  249. item-value='code'
  250. outlined
  251. prepend-icon='mdi-web'
  252. v-model='copyFromLocaleCode'
  253. :label='$t(`admin:navigation.sourceLocale`)'
  254. :hint='$t(`admin:navigation.sourceLocaleHint`)'
  255. persistent-hint
  256. )
  257. v-card-chin
  258. v-spacer
  259. v-btn(text, @click='copyFromLocaleDialogIsShown = false') {{$t('common:actions.cancel')}}
  260. v-btn.px-3(depressed, color='primary', @click='copyFromLocale')
  261. v-icon(left) mdi-chevron-right
  262. span {{$t('common:actions.copy')}}
  263. page-selector(mode='select', v-model='selectPageModal', :open-handler='selectPageHandle', path='home', :locale='currentLang')
  264. </template>
  265. <script>
  266. import _ from 'lodash'
  267. import gql from 'graphql-tag'
  268. import { v4 as uuid } from 'uuid'
  269. import groupsQuery from 'gql/admin/users/users-query-groups.gql'
  270. import draggable from 'vuedraggable'
  271. /* global siteConfig, siteLangs */
  272. export default {
  273. components: {
  274. draggable
  275. },
  276. data() {
  277. return {
  278. selectPageModal: false,
  279. trees: [],
  280. current: {},
  281. currentLang: siteConfig.lang,
  282. groups: [],
  283. copyFromLocaleDialogIsShown: false,
  284. config: {
  285. mode: 'NONE'
  286. },
  287. allLocales: [],
  288. copyFromLocaleCode: 'en'
  289. }
  290. },
  291. computed: {
  292. navTypes () {
  293. return [
  294. { text: this.$t('navigation.navType.external'), value: 'external' },
  295. { text: this.$t('navigation.navType.home'), value: 'home' },
  296. { text: this.$t('navigation.navType.page'), value: 'page' }
  297. // { text: this.$t('navigation.navType.searchQuery'), value: 'search' }
  298. ]
  299. },
  300. locales () {
  301. return _.intersectionBy(this.allLocales, _.unionBy(siteLangs, [{ code: 'en' }, { code: siteConfig.lang }], 'code'), 'code')
  302. },
  303. currentTree: {
  304. get () {
  305. return _.get(_.find(this.trees, ['locale', this.currentLang]), 'items', null) || []
  306. },
  307. set (val) {
  308. const tree = _.find(this.trees, ['locale', this.currentLang])
  309. if (tree) {
  310. tree.items = val
  311. } else {
  312. this.trees = [...this.trees, {
  313. locale: this.currentLang,
  314. items: val
  315. }]
  316. }
  317. }
  318. }
  319. },
  320. watch: {
  321. currentLang (newValue, oldValue) {
  322. this.$nextTick(() => {
  323. if (this.currentTree.length > 0) {
  324. this.current = this.currentTree[0]
  325. } else {
  326. this.current = {}
  327. }
  328. })
  329. }
  330. },
  331. methods: {
  332. addItem(kind) {
  333. let newItem = {
  334. id: uuid(),
  335. kind,
  336. visibilityMode: 'all',
  337. visibilityGroups: []
  338. }
  339. switch (kind) {
  340. case 'link':
  341. newItem = {
  342. ...newItem,
  343. label: this.$t('navigation.untitled', { kind: this.$t(`navigation.link`) }),
  344. icon: 'mdi-chevron-right',
  345. targetType: 'home',
  346. target: ''
  347. }
  348. break
  349. case 'header':
  350. newItem.label = this.$t('navigation.untitled', { kind: this.$t(`navigation.header`) })
  351. break
  352. }
  353. this.currentTree = [...this.currentTree, newItem]
  354. this.current = newItem
  355. },
  356. deleteItem(item) {
  357. this.currentTree = _.pull(this.currentTree, item)
  358. this.current = {}
  359. },
  360. selectItem(item) {
  361. this.current = item
  362. },
  363. selectPage() {
  364. this.selectPageModal = true
  365. },
  366. selectPageHandle ({ path, locale }) {
  367. this.current.target = `/${locale}/${path}`
  368. },
  369. copyFromLocale () {
  370. this.copyFromLocaleDialogIsShown = false
  371. this.currentTree = [...this.currentTree, ..._.get(_.find(this.trees, ['locale', this.copyFromLocaleCode]), 'items', null) || []]
  372. },
  373. async save() {
  374. this.$store.commit(`loadingStart`, 'admin-navigation-save')
  375. try {
  376. const resp = await this.$apollo.mutate({
  377. mutation: gql`
  378. mutation ($tree: [NavigationTreeInput]!, $mode: NavigationMode!) {
  379. navigation{
  380. updateTree(tree: $tree) {
  381. responseResult {
  382. succeeded
  383. errorCode
  384. slug
  385. message
  386. }
  387. },
  388. updateConfig(mode: $mode) {
  389. responseResult {
  390. succeeded
  391. errorCode
  392. slug
  393. message
  394. }
  395. }
  396. }
  397. }
  398. `,
  399. variables: {
  400. tree: this.trees,
  401. mode: this.config.mode
  402. }
  403. })
  404. if (_.get(resp, 'data.navigation.updateTree.responseResult.succeeded', false) && _.get(resp, 'data.navigation.updateConfig.responseResult.succeeded', false)) {
  405. this.$store.commit('showNotification', {
  406. message: this.$t('navigation.saveSuccess'),
  407. style: 'success',
  408. icon: 'check'
  409. })
  410. } else {
  411. throw new Error(_.get(resp, 'data.navigation.updateTree.responseResult.message', 'An unexpected error occured.'))
  412. }
  413. } catch (err) {
  414. this.$store.commit('pushGraphError', err)
  415. }
  416. this.$store.commit(`loadingStop`, 'admin-navigation-save')
  417. },
  418. async refresh() {
  419. await this.$apollo.queries.trees.refetch()
  420. this.current = {}
  421. this.$store.commit('showNotification', {
  422. message: 'Navigation has been refreshed.',
  423. style: 'success',
  424. icon: 'cached'
  425. })
  426. }
  427. },
  428. apollo: {
  429. config: {
  430. query: gql`
  431. {
  432. navigation {
  433. config {
  434. mode
  435. }
  436. }
  437. }
  438. `,
  439. fetchPolicy: 'network-only',
  440. update: (data) => _.cloneDeep(data.navigation.config),
  441. watchLoading (isLoading) {
  442. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-navigation-config')
  443. }
  444. },
  445. trees: {
  446. query: gql`
  447. {
  448. navigation {
  449. tree {
  450. locale
  451. items {
  452. id
  453. kind
  454. label
  455. icon
  456. targetType
  457. target
  458. visibilityMode
  459. visibilityGroups
  460. }
  461. }
  462. }
  463. }
  464. `,
  465. fetchPolicy: 'network-only',
  466. update: (data) => _.cloneDeep(data.navigation.tree),
  467. watchLoading (isLoading) {
  468. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-navigation-tree')
  469. }
  470. },
  471. groups: {
  472. query: groupsQuery,
  473. fetchPolicy: 'network-only',
  474. update: (data) => data.groups.list,
  475. watchLoading (isLoading) {
  476. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-navigation-groups')
  477. }
  478. },
  479. allLocales: {
  480. query: gql`
  481. {
  482. localization {
  483. locales {
  484. code
  485. name
  486. nativeName
  487. }
  488. }
  489. }
  490. `,
  491. fetchPolicy: 'network-only',
  492. update: (data) => data.localization.locales,
  493. watchLoading (isLoading) {
  494. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-navigation-locales')
  495. }
  496. }
  497. }
  498. }
  499. </script>
  500. <style lang='scss' scoped>
  501. .clickable {
  502. cursor: pointer;
  503. &:hover {
  504. background-color: rgba(mc('blue', '500'), .25);
  505. }
  506. }
  507. </style>