admin-navigation.vue 23.1 KB
Newer Older
1
<template lang='pug'>
2
  v-container(fluid, grid-list-lg)
3 4
    v-layout(row wrap)
      v-flex(xs12)
5
        .admin-header
6
          img.animated.fadeInUp(src='/_assets/svg/icon-triangle-arrow.svg', alt='Navigation', style='width: 80px;')
7
          .admin-header-title
Nick's avatar
Nick committed
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 12 13
          v-btn.animated.fadeInDown.wait-p3s(icon, outlined, color='grey', href='https://docs.requarks.io/navigation', target='_blank')
            v-icon mdi-help-circle
          v-btn.mx-3.animated.fadeInDown.wait-p2s.mr-3(icon, outlined, color='grey', @click='refresh')
14
            v-icon mdi-refresh
Nick's avatar
Nick committed
15
          v-btn.animated.fadeInDown(color='success', depressed, @click='save', large)
16
            v-icon(left) mdi-check
17
            span {{$t('common:actions.apply')}}
18
        v-container.pa-0.mt-3(fluid, grid-list-lg)
NGPixel's avatar
NGPixel committed
19 20
          v-row(dense)
            v-col(cols='3')
Nick's avatar
Nick committed
21
              v-card.animated.fadeInUp
NGPixel's avatar
NGPixel committed
22 23 24
                v-toolbar(color='teal', dark, dense, flat, height='56')
                  v-toolbar-title.subtitle-1 {{$t('admin:navigation.mode')}}
                v-list(nav, two-line)
25 26
                  v-list-item-group(v-model='config.mode', mandatory, :color='$vuetify.theme.dark ? `teal lighten-3` : `teal`')
                    v-list-item(value='TREE')
NGPixel's avatar
NGPixel committed
27
                      v-list-item-avatar
28
                        img(src='/_assets/svg/icon-tree-structure-dotted.svg', alt='Site Tree')
NGPixel's avatar
NGPixel committed
29 30 31 32
                      v-list-item-content
                        v-list-item-title {{$t('admin:navigation.modeSiteTree.title')}}
                        v-list-item-subtitle {{$t('admin:navigation.modeSiteTree.description')}}
                      v-list-item-avatar
33 34
                        v-icon(v-if='$vuetify.theme.dark', :color='config.mode === `TREE` ? `teal lighten-3` : `grey darken-2`') mdi-check-circle
                        v-icon(v-else, :color='config.mode === `TREE` ? `teal` : `grey lighten-3`') mdi-check-circle
35 36
                    v-list-item(value='STATIC')
                      v-list-item-avatar
37
                        img(src='/_assets/svg/icon-features-list.svg', alt='Static Navigation')
38 39 40 41 42 43
                      v-list-item-content
                        v-list-item-title {{$t('admin:navigation.modeStatic.title')}}
                        v-list-item-subtitle {{$t('admin:navigation.modeStatic.description')}}
                      v-list-item-avatar
                        v-icon(v-if='$vuetify.theme.dark', :color='config.mode === `STATIC` ? `teal lighten-3` : `grey darken-2`') mdi-check-circle
                        v-icon(v-else, :color='config.mode === `STATIC` ? `teal` : `grey lighten-3`') mdi-check-circle
44 45 46 47 48 49 50 51 52
                    v-list-item(value='MIXED')
                      v-list-item-avatar
                        img(src='/_assets/svg/icon-user-menu-male-dotted.svg', alt='Custom Navigation')
                      v-list-item-content
                        v-list-item-title {{$t('admin:navigation.modeCustom.title')}}
                        v-list-item-subtitle {{$t('admin:navigation.modeCustom.description')}}
                      v-list-item-avatar
                        v-icon(v-if='$vuetify.theme.dark', :color='config.mode === `MIXED` ? `teal lighten-3` : `grey darken-2`') mdi-check-circle
                        v-icon(v-else, :color='config.mode === `MIXED` ? `teal` : `grey lighten-3`') mdi-check-circle
53
                    v-list-item(value='NONE')
NGPixel's avatar
NGPixel committed
54
                      v-list-item-avatar
55
                        img(src='/_assets/svg/icon-cancel-dotted.svg', alt='None')
NGPixel's avatar
NGPixel committed
56 57 58 59
                      v-list-item-content
                        v-list-item-title {{$t('admin:navigation.modeNone.title')}}
                        v-list-item-subtitle {{$t('admin:navigation.modeNone.description')}}
                      v-list-item-avatar
60 61
                        v-icon(v-if='$vuetify.theme.dark', :color='config.mode === `none` ? `teal lighten-3` : `grey darken-2`') mdi-check-circle
                        v-icon(v-else, :color='config.mode === `none` ? `teal` : `grey lighten-3`') mdi-check-circle
62
            v-col(cols='9', v-if='config.mode === `MIXED` || config.mode === `STATIC`')
NGPixel's avatar
NGPixel committed
63 64 65 66
              v-card.animated.fadeInUp.wait-p2s
                v-row(no-gutters, align='stretch')
                  v-col(style='flex: 0 0 350px;')
                    v-card.grey(flat, style='height: 100%; border-radius: 4px 0 0 4px;', :class='$vuetify.theme.dark ? `darken-4-l5` : `lighten-3`')
67
                      .teal.lighten-1.pa-2.d-flex(style='margin-bottom: 1px; height:56px;')
NGPixel's avatar
NGPixel committed
68
                        v-select(
69
                          :disabled='locales.length < 2'
NGPixel's avatar
NGPixel committed
70 71 72 73 74 75 76 77 78
                          label='Locale'
                          hide-details
                          solo
                          flat
                          background-color='teal darken-2'
                          dark
                          dense
                          v-model='currentLang'
                          :items='locales'
79
                          item-text='nativeName'
NGPixel's avatar
NGPixel committed
80
                          item-value='code'
81
                        )
82 83 84 85 86
                        v-tooltip(top)
                          template(v-slot:activator='{ on }')
                            v-btn.ml-2(icon, tile, color='white', v-on='on', @click='copyFromLocaleDialogIsShown = true')
                              v-icon mdi-arrange-send-backward
                          span {{$t('admin:navigation.copyFromLocale')}}
NGPixel's avatar
NGPixel committed
87
                      v-list.py-2(dense, nav, dark, class='blue darken-2', style='border-radius: 0;')
88
                        v-list-item(v-if='currentTree.length < 1')
NGPixel's avatar
NGPixel committed
89 90 91
                          v-list-item-avatar(size='24'): v-icon(color='blue lighten-3') mdi-alert
                          v-list-item-content
                            em.caption.blue--text.text--lighten-4 {{$t('navigation.emptyList')}}
92 93
                        draggable(v-model='currentTree')
                          template(v-for='navItem in currentTree')
NGPixel's avatar
NGPixel committed
94 95 96 97 98 99
                            v-list-item(
                              v-if='navItem.kind === "link"'
                              :key='navItem.id'
                              :class='(navItem === current) ? "blue" : ""'
                              @click='selectItem(navItem)'
                              )
100 101 102
                              v-list-item-avatar(size='24', tile)
                                v-icon(v-if='navItem.icon.match(/fa[a-z] fa-/)', size='19') {{ navItem.icon }}
                                v-icon(v-else) {{ navItem.icon }}
NGPixel's avatar
NGPixel committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
                              v-list-item-title {{navItem.label}}
                            .py-2.clickable(
                              v-else-if='navItem.kind === "divider"'
                              :key='navItem.id'
                              :class='(navItem === current) ? "blue" : ""'
                              @click='selectItem(navItem)'
                              )
                              v-divider
                            v-subheader.pl-4.clickable(
                              v-else-if='navItem.kind === "header"'
                              :key='navItem.id'
                              :class='(navItem === current) ? "blue" : ""'
                              @click='selectItem(navItem)'
                              ) {{navItem.label}}
                      v-card-chin
                        v-menu(offset-y, bottom, min-width='200px', style='flex: 1 1;')
                          template(v-slot:activator='{ on }')
                            v-btn(v-on='on', color='primary', depressed, block)
                              v-icon(left) mdi-plus
                              span {{$t('common:actions.add')}}
                          v-list
                            v-list-item(@click='addItem("link")')
                              v-list-item-avatar(size='24'): v-icon mdi-link
                              v-list-item-title {{$t('navigation.link')}}
                            v-list-item(@click='addItem("header")')
                              v-list-item-avatar(size='24'): v-icon mdi-format-title
                              v-list-item-title {{$t('navigation.header')}}
                            v-list-item(@click='addItem("divider")')
                              v-list-item-avatar(size='24'): v-icon mdi-minus
                              v-list-item-title {{$t('navigation.divider')}}
                  v-col
                    v-card(flat, style='border-radius: 0 4px 4px 0;')
                      template(v-if='current.kind === "link"')
                        v-toolbar(height='56', color='teal lighten-1', flat, dark)
                          .subtitle-1 {{$t('navigation.edit', { kind: $t('navigation.link') })}}
                          v-spacer
                          v-btn.px-5(color='white', outlined, @click='deleteItem(current)')
                            v-icon(left) mdi-delete
                            span {{$t('navigation.delete', { kind: $t('navigation.link') })}}
                        v-card-text
                          v-text-field(
                            outlined
                            :label='$t("navigation.label")'
                            prepend-icon='mdi-format-title'
                            v-model='current.label'
                            counter='255'
                          )
                          v-text-field(
                            outlined
                            :label='$t("navigation.icon")'
                            prepend-icon='mdi-dice-5'
                            v-model='current.icon'
                            hide-details
                          )
                          .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.
                          .caption.pt-3.pl-5: strong Material Design Icons
                          .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]
                          .caption.pt-3.pl-5: strong Font Awesome 5
                          .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]).
                          .caption.pt-3.pl-5: strong Font Awesome 4
                          .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]
                        v-divider
                        v-card-text
                          v-select(
                            outlined
                            :label='$t("navigation.targetType")'
                            prepend-icon='mdi-near-me'
                            :items='navTypes'
                            v-model='current.targetType'
                            hide-details
                          )
                          v-text-field.mt-4(
175
                            v-if='current.targetType === `external` || current.targetType === `externalblank`'
NGPixel's avatar
NGPixel committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
                            outlined
                            :label='$t("navigation.target")'
                            prepend-icon='mdi-near-me'
                            v-model='current.target'
                            hide-details
                          )
                          .d-flex.align-center.mt-4(v-else-if='current.targetType === "page"')
                            v-btn.ml-8(
                              color='primary'
                              dark
                              @click='selectPage'
                              )
                              v-icon(left) mdi-magnify
                              span {{$t('admin:navigation.selectPageButton')}}
                            .caption.ml-4.primary--text {{current.target}}
                          v-text-field(
                            v-else-if='current.targetType === `search`'
                            outlined
                            :label='$t("navigation.navType.searchQuery")'
                            prepend-icon='search'
                            v-model='current.target'
                          )
                        v-divider

                      template(v-else-if='current.kind === "header"')
                        v-toolbar(height='56', color='teal lighten-1', flat, dark)
                          .subtitle-1 {{$t('navigation.edit', { kind: $t('navigation.header') })}}
                          v-spacer
                          v-btn.px-5(color='white', outlined, @click='deleteItem(current)')
                            v-icon(left) mdi-delete
                            span {{$t('navigation.delete', { kind: $t('navigation.header') })}}
                        v-card-text
                          v-text-field(
                            outlined
                            :label='$t("navigation.label")'
                            prepend-icon='mdi-format-title'
                            v-model='current.label'
                          )
214
                        v-divider
215

NGPixel's avatar
NGPixel committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
                      div(v-else-if='current.kind === "divider"')
                        v-toolbar(height='56', color='teal lighten-1', flat, dark)
                          .subtitle-1 {{$t('navigation.edit', { kind: $t('navigation.divider') })}}
                          v-spacer
                          v-btn.px-5(color='white', outlined, @click='deleteItem(current)')
                            v-icon(left) mdi-delete
                            span {{$t('navigation.delete', { kind: $t('navigation.divider') })}}

                      v-card-text(v-if='current.kind')
                        v-radio-group.pl-8(v-model='current.visibilityMode', mandatory, hide-details)
                          v-radio(:label='$t("admin:navigation.visibilityMode.all")', value='all', color='primary')
                          v-radio.mt-3(:label='$t("admin:navigation.visibilityMode.restricted")', value='restricted', color='primary')
                        .pl-8
                          v-select.pl-8.mt-3(
                            item-text='name'
                            item-value='id'
                            outlined
                            prepend-icon='mdi-account-group'
                            label='Groups'
                            :disabled='current.visibilityMode !== `restricted`'
                            v-model='current.visibilityGroups'
                            :items='groups'
                            persistent-hint
                            clearable
                            multiple
                          )
                      template(v-else)
                        v-toolbar(height='56', color='teal lighten-1', flat, dark)
244
                        v-card-text.grey--text(v-if='currentTree.length > 0') {{$t('navigation.noSelectionText')}}
NGPixel's avatar
NGPixel committed
245 246
                        v-card-text.grey--text(v-else) {{$t('navigation.noItemsText')}}

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    v-dialog(v-model='copyFromLocaleDialogIsShown', max-width='650', persistent)
      v-card
        .dialog-header.is-short.is-teal
          v-icon.mr-3(color='white') mdi-arrange-send-backward
          span {{$t('admin:navigation.copyFromLocale')}}
        v-card-text.pt-5
          .body-2 {{$t('admin:navigation.copyFromLocaleInfoText')}}
          v-select.mt-3(
            :items='locales'
            item-text='nativeName'
            item-value='code'
            outlined
            prepend-icon='mdi-web'
            v-model='copyFromLocaleCode'
            :label='$t(`admin:navigation.sourceLocale`)'
            :hint='$t(`admin:navigation.sourceLocaleHint`)'
            persistent-hint
            )
        v-card-chin
          v-spacer
          v-btn(text, @click='copyFromLocaleDialogIsShown = false') {{$t('common:actions.cancel')}}
          v-btn.px-3(depressed, color='primary', @click='copyFromLocale')
            v-icon(left) mdi-chevron-right
            span {{$t('common:actions.copy')}}

NGPixel's avatar
NGPixel committed
272
    page-selector(mode='select', v-model='selectPageModal', :open-handler='selectPageHandle', path='home', :locale='currentLang')
273 274 275
</template>

<script>
276
import _ from 'lodash'
NGPixel's avatar
NGPixel committed
277
import gql from 'graphql-tag'
278
import { v4 as uuid } from 'uuid'
279

NGPixel's avatar
NGPixel committed
280
import groupsQuery from 'gql/admin/users/users-query-groups.gql'
281

282 283
import draggable from 'vuedraggable'

NGPixel's avatar
NGPixel committed
284 285
/* global siteConfig, siteLangs */

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

<style lang='scss' scoped>

.clickable {
  cursor: pointer;

  &:hover {
    background-color: rgba(mc('blue', '500'), .25);
  }
}

</style>