page-selector.vue 9.07 KB
Newer Older
1
<template lang="pug">
2 3 4 5 6 7
  v-dialog(
    v-model='isShown'
    max-width='850px'
    overlay-color='blue darken-4'
    overlay-opacity='.7'
    )
Nicolas Giard's avatar
Nicolas Giard committed
8
    v-card.page-selector
NGPixel's avatar
NGPixel committed
9
      .dialog-header.is-blue
10
        v-icon.mr-3(color='white') mdi-page-next-outline
11 12 13
        .body-1(v-if='mode === `create`') {{$t('common:pageSelector.createTitle')}}
        .body-1(v-else-if='mode === `move`') {{$t('common:pageSelector.moveTitle')}}
        .body-1(v-else-if='mode === `select`') {{$t('common:pageSelector.selectTitle')}}
14 15 16 17 18 19 20 21
        v-spacer
        v-progress-circular(
          indeterminate
          color='white'
          :size='20'
          :width='2'
          v-show='searchLoading'
          )
NGPixel's avatar
NGPixel committed
22
      .d-flex
23
        v-flex.grey(xs5, :class='$vuetify.theme.dark ? `darken-4` : `lighten-3`')
24
          v-toolbar(color='grey darken-3', dark, dense, flat)
25
            .body-2 {{$t('common:pageSelector.virtualFolders')}}
NGPixel's avatar
NGPixel committed
26
            v-spacer
27
            v-btn(icon, tile, href='https://docs.requarks.io/guide/pages#folders', target='_blank')
NGPixel's avatar
NGPixel committed
28
              v-icon mdi-help-box
NGPixel's avatar
NGPixel committed
29 30 31
          div(style='height:400px;')
            vue-scroll(:ops='scrollStyle')
              v-treeview(
32
                :key='`pageTree-` + treeViewCacheId'
NGPixel's avatar
NGPixel committed
33 34 35 36 37 38 39 40 41 42 43 44 45
                :active.sync='currentNode'
                :open.sync='openNodes'
                :items='tree'
                :load-children='fetchFolders'
                dense
                expand-icon='mdi-menu-down-outline'
                item-id='path'
                item-text='title'
                activatable
                hoverable
                )
                template(slot='prepend', slot-scope='{ item, open, leaf }')
                  v-icon mdi-{{ open ? 'folder-open' : 'folder' }}
46
        v-flex(xs7)
NGPixel's avatar
NGPixel committed
47
          v-toolbar(color='blue darken-2', dark, dense, flat)
48
            .body-2 {{$t('common:pageSelector.pages')}}
49 50 51
            //- v-spacer
            //- v-btn(icon, tile, disabled): v-icon mdi-content-save-move-outline
            //- v-btn(icon, tile, disabled): v-icon mdi-trash-can-outline
NGPixel's avatar
NGPixel committed
52 53 54 55 56 57 58 59
          div(v-if='currentPages.length > 0', style='height:400px;')
            vue-scroll(:ops='scrollStyle')
              v-list.py-0(dense)
                v-list-item-group(
                  v-model='currentPage'
                  color='primary'
                  )
                  template(v-for='(page, idx) of currentPages')
60
                    v-list-item(:key='`page-` + page.id', :value='page')
61
                      v-list-item-icon: v-icon mdi-text-box
NGPixel's avatar
NGPixel committed
62 63
                      v-list-item-title {{page.title}}
                    v-divider(v-if='idx < pages.length - 1')
NGPixel's avatar
NGPixel committed
64 65 66 67 68 69 70
          v-alert.animated.fadeIn(
            v-else
            text
            color='orange'
            prominent
            icon='mdi-alert'
            )
71
            .body-2 {{$t('common:pageSelector.folderEmptyWarning')}}
72
      v-card-actions.grey.pa-2(:class='$vuetify.theme.dark ? `darken-2` : `lighten-1`', v-if='!mustExist')
73 74 75
        v-select(
          solo
          dark
NGPixel's avatar
NGPixel committed
76
          flat
77 78 79 80
          background-color='grey darken-3-d2'
          hide-details
          single-line
          :items='namespaces'
81
          style='flex: 0 0 100px; border-radius: 4px 0 0 4px;'
82 83
          v-model='currentLocale'
          )
84
        v-text-field(
NGPixel's avatar
NGPixel committed
85
          ref='pathIpt'
86 87
          solo
          hide-details
88 89
          prefix='/'
          v-model='currentPath'
90 91
          flat
          clearable
92
          style='border-radius: 0 4px 4px 0;'
93 94 95
        )
      v-card-chin
        v-spacer
96
        v-btn(text, @click='close') {{$t('common:actions.cancel')}}
NGPixel's avatar
NGPixel committed
97
        v-btn.px-4(color='primary', @click='open', :disabled='!isValidPath')
98
          v-icon(left) mdi-check
99
          span {{$t('common:actions.select')}}
100 101 102
</template>

<script>
NGPixel's avatar
NGPixel committed
103
import _ from 'lodash'
104
import gql from 'graphql-tag'
105

NGPixel's avatar
NGPixel committed
106 107
const localeSegmentRegex = /^[A-Z]{2}(-[A-Z]{2})?$/i

108 109
/* global siteLangs, siteConfig */

110 111 112 113 114
export default {
  props: {
    value: {
      type: Boolean,
      default: false
Nicolas Giard's avatar
Nicolas Giard committed
115
    },
116 117 118 119 120 121 122 123
    path: {
      type: String,
      default: 'new-page'
    },
    locale: {
      type: String,
      default: 'en'
    },
Nicolas Giard's avatar
Nicolas Giard committed
124 125 126
    mode: {
      type: String,
      default: 'create'
127 128 129 130
    },
    openHandler: {
      type: Function,
      default: () => {}
131 132 133 134
    },
    mustExist: {
      type: Boolean,
      default: false
135 136 137 138
    }
  },
  data() {
    return {
139
      treeViewCacheId: 0,
Nicolas Giard's avatar
Nicolas Giard committed
140
      searchLoading: false,
141
      currentLocale: siteConfig.lang,
142
      currentFolderPath: '',
143
      currentPath: 'new-page',
NGPixel's avatar
NGPixel committed
144 145 146
      currentPage: null,
      currentNode: [0],
      openNodes: [0],
NGPixel's avatar
NGPixel committed
147 148 149
      tree: [
        {
          id: 0,
150
          title: '/ (root)',
NGPixel's avatar
NGPixel committed
151 152 153
          children: []
        }
      ],
NGPixel's avatar
NGPixel committed
154
      pages: [],
NGPixel's avatar
NGPixel committed
155
      all: [],
NGPixel's avatar
NGPixel committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
      namespaces: siteLangs.length ? siteLangs.map(ns => ns.code) : [siteConfig.lang],
      scrollStyle: {
        vuescroll: {},
        scrollPanel: {
          initialScrollX: 0.01, // fix scrollbar not disappearing on load
          scrollingX: false,
          speed: 50
        },
        rail: {
          gutterOfEnds: '2px'
        },
        bar: {
          onlyShowBarOnScroll: false,
          background: '#999',
          hoverStyle: {
            background: '#64B5F6'
          }
        }
      }
175 176 177 178 179 180
    }
  },
  computed: {
    isShown: {
      get() { return this.value },
      set(val) { this.$emit('input', val) }
Nicolas Giard's avatar
Nicolas Giard committed
181
    },
NGPixel's avatar
NGPixel committed
182
    currentPages () {
183
      return _.sortBy(_.filter(this.pages, ['parent', _.head(this.currentNode) || 0]), ['title', 'path'])
NGPixel's avatar
NGPixel committed
184 185
    },
    isValidPath () {
NGPixel's avatar
NGPixel committed
186 187 188
      if (!this.currentPath) {
        return false
      }
189 190 191
      if (this.mustExist && !this.currentPage) {
        return false
      }
NGPixel's avatar
NGPixel committed
192 193 194 195 196 197 198 199 200 201 202 203 204
      const firstSection = _.head(this.currentPath.split('/'))
      if (firstSection.length <= 1) {
        return false
      } else if (localeSegmentRegex.test(firstSection)) {
        return false
      } else if (
        _.some(['login', 'logout', 'register', 'verify', 'favicons', 'fonts', 'img', 'js', 'svg'], p => {
          return p === firstSection
        })) {
        return false
      } else {
        return true
      }
205 206
    }
  },
207
  watch: {
NGPixel's avatar
NGPixel committed
208
    isShown (newValue, oldValue) {
209 210 211
      if (newValue && !oldValue) {
        this.currentPath = this.path
        this.currentLocale = this.locale
NGPixel's avatar
NGPixel committed
212 213 214 215 216 217 218 219 220 221
        _.delay(() => {
          this.$refs.pathIpt.focus()
        })
      }
    },
    currentNode (newValue, oldValue) {
      if (newValue.length < 1) { // force a selection
        this.$nextTick(() => {
          this.currentNode = oldValue
        })
NGPixel's avatar
NGPixel committed
222
      } else {
223 224
        const current = _.find(this.all, ['id', newValue[0]])

NGPixel's avatar
NGPixel committed
225 226 227 228 229 230 231 232 233 234 235 236
        if (this.openNodes.indexOf(newValue[0]) < 0) { // auto open and load children
          if (current) {
            if (this.openNodes.indexOf(current.parent) < 0) {
              this.$nextTick(() => {
                this.openNodes.push(current.parent)
              })
            }
          }
          this.$nextTick(() => {
            this.openNodes.push(newValue[0])
          })
        }
237 238

        this.currentPath = _.compact([_.get(current, 'path', ''), _.last(this.currentPath.split('/'))]).join('/')
NGPixel's avatar
NGPixel committed
239 240 241 242
      }
    },
    currentPage (newValue, oldValue) {
      if (!_.isEmpty(newValue)) {
243
        this.currentPath = newValue.path
244
      }
245 246 247 248 249 250
    },
    currentLocale (newValue, oldValue) {
      this.$nextTick(() => {
        this.tree = [
          {
            id: 0,
251
            title: '/ (root)',
252 253 254 255 256 257 258 259 260
            children: []
          }
        ]
        this.currentNode = [0]
        this.openNodes = [0]
        this.pages = []
        this.all = []
        this.treeViewCacheId += 1
      })
261 262
    }
  },
263 264 265
  methods: {
    close() {
      this.isShown = false
Nicolas Giard's avatar
Nicolas Giard committed
266 267
    },
    open() {
268 269
      const exit = this.openHandler({
        locale: this.currentLocale,
270 271
        path: this.currentPath,
        id: (this.mustExist && this.currentPage) ? this.currentPage.pageId : 0
272 273 274
      })
      if (exit !== false) {
        this.close()
Nicolas Giard's avatar
Nicolas Giard committed
275 276
      }
    },
NGPixel's avatar
NGPixel committed
277 278 279
    async fetchFolders (item) {
      this.searchLoading = true
      const resp = await this.$apollo.query({
280 281 282 283 284 285 286 287 288 289 290 291 292 293
        query: gql`
          query ($parent: Int!, $mode: PageTreeMode!, $locale: String!) {
            pages {
              tree(parent: $parent, mode: $mode, locale: $locale) {
                id
                path
                title
                isFolder
                pageId
                parent
              }
            }
          }
        `,
NGPixel's avatar
NGPixel committed
294 295 296 297 298 299 300 301 302
        fetchPolicy: 'network-only',
        variables: {
          parent: item.id,
          mode: 'ALL',
          locale: this.currentLocale
        }
      })
      const items = _.get(resp, 'data.pages.tree', [])
      const itemFolders = _.filter(items, ['isFolder', true]).map(f => ({...f, children: []}))
303
      const itemPages = _.filter(items, i => i.pageId > 0)
NGPixel's avatar
NGPixel committed
304 305 306 307 308
      if (itemFolders.length > 0) {
        item.children = itemFolders
      } else {
        item.children = undefined
      }
309 310
      this.pages = _.unionBy(this.pages, itemPages, 'id')
      this.all = _.unionBy(this.all, items, 'id')
NGPixel's avatar
NGPixel committed
311

NGPixel's avatar
NGPixel committed
312
      this.searchLoading = false
313 314 315 316
    }
  }
}
</script>
Nicolas Giard's avatar
Nicolas Giard committed
317 318 319 320 321 322 323

<style lang='scss'>

.page-selector {
  .v-treeview-node__label {
    font-size: 13px;
  }
324 325 326
  .v-treeview-node__content {
    cursor: pointer;
  }
Nicolas Giard's avatar
Nicolas Giard committed
327 328 329
}

</style>