admin-navigation.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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` || current.targetType === `externalblank`'
  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.externalblank'), value: 'externalblank' },
  296. { text: this.$t('navigation.navType.home'), value: 'home' },
  297. { text: this.$t('navigation.navType.page'), value: 'page' }
  298. // { text: this.$t('navigation.navType.searchQuery'), value: 'search' }
  299. ]
  300. },
  301. locales () {
  302. return _.intersectionBy(this.allLocales, _.unionBy(siteLangs, [{ code: 'en' }, { code: siteConfig.lang }], 'code'), 'code')
  303. },
  304. currentTree: {
  305. get () {
  306. return _.get(_.find(this.trees, ['locale', this.currentLang]), 'items', null) || []
  307. },
  308. set (val) {
  309. const tree = _.find(this.trees, ['locale', this.currentLang])
  310. if (tree) {
  311. tree.items = val
  312. } else {
  313. this.trees = [...this.trees, {
  314. locale: this.currentLang,
  315. items: val
  316. }]
  317. }
  318. }
  319. }
  320. },
  321. watch: {
  322. currentLang (newValue, oldValue) {
  323. this.$nextTick(() => {
  324. if (this.currentTree.length > 0) {
  325. this.current = this.currentTree[0]
  326. } else {
  327. this.current = {}
  328. }
  329. })
  330. }
  331. },
  332. methods: {
  333. addItem(kind) {
  334. let newItem = {
  335. id: uuid(),
  336. kind,
  337. visibilityMode: 'all',
  338. visibilityGroups: []
  339. }
  340. switch (kind) {
  341. case 'link':
  342. newItem = {
  343. ...newItem,
  344. label: this.$t('navigation.untitled', { kind: this.$t(`navigation.link`) }),
  345. icon: 'mdi-chevron-right',
  346. targetType: 'home',
  347. target: ''
  348. }
  349. break
  350. case 'header':
  351. newItem.label = this.$t('navigation.untitled', { kind: this.$t(`navigation.header`) })
  352. break
  353. }
  354. this.currentTree = [...this.currentTree, newItem]
  355. this.current = newItem
  356. },
  357. deleteItem(item) {
  358. this.currentTree = _.pull(this.currentTree, item)
  359. this.current = {}
  360. },
  361. selectItem(item) {
  362. this.current = item
  363. },
  364. selectPage() {
  365. this.selectPageModal = true
  366. },
  367. selectPageHandle ({ path, locale }) {
  368. this.current.target = `/${locale}/${path}`
  369. },
  370. copyFromLocale () {
  371. this.copyFromLocaleDialogIsShown = false
  372. this.currentTree = [...this.currentTree, ..._.get(_.find(this.trees, ['locale', this.copyFromLocaleCode]), 'items', null) || []]
  373. },
  374. async save() {
  375. this.$store.commit(`loadingStart`, 'admin-navigation-save')
  376. try {
  377. const resp = await this.$apollo.mutate({
  378. mutation: gql`
  379. mutation ($tree: [NavigationTreeInput]!, $mode: NavigationMode!) {
  380. navigation{
  381. updateTree(tree: $tree) {
  382. responseResult {
  383. succeeded
  384. errorCode
  385. slug
  386. message
  387. }
  388. },
  389. updateConfig(mode: $mode) {
  390. responseResult {
  391. succeeded
  392. errorCode
  393. slug
  394. message
  395. }
  396. }
  397. }
  398. }
  399. `,
  400. variables: {
  401. tree: this.trees,
  402. mode: this.config.mode
  403. }
  404. })
  405. if (_.get(resp, 'data.navigation.updateTree.responseResult.succeeded', false) && _.get(resp, 'data.navigation.updateConfig.responseResult.succeeded', false)) {
  406. this.$store.commit('showNotification', {
  407. message: this.$t('navigation.saveSuccess'),
  408. style: 'success',
  409. icon: 'check'
  410. })
  411. } else {
  412. throw new Error(_.get(resp, 'data.navigation.updateTree.responseResult.message', 'An unexpected error occured.'))
  413. }
  414. } catch (err) {
  415. this.$store.commit('pushGraphError', err)
  416. }
  417. this.$store.commit(`loadingStop`, 'admin-navigation-save')
  418. },
  419. async refresh() {
  420. await this.$apollo.queries.trees.refetch()
  421. this.current = {}
  422. this.$store.commit('showNotification', {
  423. message: 'Navigation has been refreshed.',
  424. style: 'success',
  425. icon: 'cached'
  426. })
  427. }
  428. },
  429. apollo: {
  430. config: {
  431. query: gql`
  432. {
  433. navigation {
  434. config {
  435. mode
  436. }
  437. }
  438. }
  439. `,
  440. fetchPolicy: 'network-only',
  441. update: (data) => _.cloneDeep(data.navigation.config),
  442. watchLoading (isLoading) {
  443. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-navigation-config')
  444. }
  445. },
  446. trees: {
  447. query: gql`
  448. {
  449. navigation {
  450. tree {
  451. locale
  452. items {
  453. id
  454. kind
  455. label
  456. icon
  457. targetType
  458. target
  459. visibilityMode
  460. visibilityGroups
  461. }
  462. }
  463. }
  464. }
  465. `,
  466. fetchPolicy: 'network-only',
  467. update: (data) => _.cloneDeep(data.navigation.tree),
  468. watchLoading (isLoading) {
  469. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-navigation-tree')
  470. }
  471. },
  472. groups: {
  473. query: groupsQuery,
  474. fetchPolicy: 'network-only',
  475. update: (data) => data.groups.list,
  476. watchLoading (isLoading) {
  477. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-navigation-groups')
  478. }
  479. },
  480. allLocales: {
  481. query: gql`
  482. {
  483. localization {
  484. locales {
  485. code
  486. name
  487. nativeName
  488. }
  489. }
  490. }
  491. `,
  492. fetchPolicy: 'network-only',
  493. update: (data) => data.localization.locales,
  494. watchLoading (isLoading) {
  495. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-navigation-locales')
  496. }
  497. }
  498. }
  499. }
  500. </script>
  501. <style lang='scss' scoped>
  502. .clickable {
  503. cursor: pointer;
  504. &:hover {
  505. background-color: rgba(mc('blue', '500'), .25);
  506. }
  507. }
  508. </style>