page-selector.vue 8.34 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
        .body-1(v-if='mode === `create`') Select New Page Location
        .body-1(v-else-if='mode === `move`') Move / Rename Page Location
13 14 15 16 17 18 19 20
        v-spacer
        v-progress-circular(
          indeterminate
          color='white'
          :size='20'
          :width='2'
          v-show='searchLoading'
          )
NGPixel's avatar
NGPixel committed
21
      .d-flex
22
        v-flex.grey(xs5, :class='darkMode ? `darken-4` : `lighten-3`')
23
          v-toolbar(color='grey darken-3', dark, dense, flat)
NGPixel's avatar
NGPixel committed
24 25
            .body-2 Virtual Folders
            v-spacer
26
            v-btn(icon, tile, href='https://docs.requarks.io/guide/pages#folders', target='_blank')
NGPixel's avatar
NGPixel committed
27
              v-icon mdi-help-box
NGPixel's avatar
NGPixel committed
28 29 30
          div(style='height:400px;')
            vue-scroll(:ops='scrollStyle')
              v-treeview(
31
                :key='`pageTree-` + treeViewCacheId'
NGPixel's avatar
NGPixel committed
32 33 34 35 36 37 38 39 40 41 42 43 44
                :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' }}
45
        v-flex(xs7)
NGPixel's avatar
NGPixel committed
46
          v-toolbar(color='blue darken-2', dark, dense, flat)
47
            .body-2 Pages
48 49 50
            //- 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
51 52 53 54 55 56 57 58
          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')
59 60
                    v-list-item(:key='`page-` + page.id', :value='page.path')
                      v-list-item-icon: v-icon mdi-text-box
NGPixel's avatar
NGPixel committed
61 62
                      v-list-item-title {{page.title}}
                    v-divider(v-if='idx < pages.length - 1')
NGPixel's avatar
NGPixel committed
63 64 65 66 67 68 69 70 71
          v-alert.animated.fadeIn(
            v-else
            text
            color='orange'
            prominent
            icon='mdi-alert'
            )
            .body-2 This folder is empty.
      v-card-actions.grey.pa-2(:class='darkMode ? `darken-2` : `lighten-1`')
72 73 74
        v-select(
          solo
          dark
NGPixel's avatar
NGPixel committed
75
          flat
76 77 78 79
          background-color='grey darken-3-d2'
          hide-details
          single-line
          :items='namespaces'
80
          style='flex: 0 0 100px; border-radius: 4px 0 0 4px;'
81 82
          v-model='currentLocale'
          )
83
        v-text-field(
NGPixel's avatar
NGPixel committed
84
          ref='pathIpt'
85 86
          solo
          hide-details
87 88
          prefix='/'
          v-model='currentPath'
89 90
          flat
          clearable
91
          style='border-radius: 0 4px 4px 0;'
92 93 94
        )
      v-card-chin
        v-spacer
95
        v-btn(text, @click='close') Cancel
NGPixel's avatar
NGPixel committed
96
        v-btn.px-4(color='primary', @click='open', :disabled='!isValidPath')
97
          v-icon(left) mdi-check
98 99 100 101
          span Select
</template>

<script>
NGPixel's avatar
NGPixel committed
102
import _ from 'lodash'
103
import { get } from 'vuex-pathify'
NGPixel's avatar
NGPixel committed
104
import pageTreeQuery from 'gql/common/common-pages-query-tree.gql'
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
    }
  },
  data() {
    return {
135
      treeViewCacheId: 0,
Nicolas Giard's avatar
Nicolas Giard committed
136
      searchLoading: false,
137
      currentLocale: siteConfig.lang,
138
      currentFolderPath: '',
139
      currentPath: 'new-page',
NGPixel's avatar
NGPixel committed
140 141 142
      currentPage: null,
      currentNode: [0],
      openNodes: [0],
NGPixel's avatar
NGPixel committed
143 144 145
      tree: [
        {
          id: 0,
146
          title: '/ (root)',
NGPixel's avatar
NGPixel committed
147 148 149
          children: []
        }
      ],
NGPixel's avatar
NGPixel committed
150
      pages: [],
NGPixel's avatar
NGPixel committed
151
      all: [],
NGPixel's avatar
NGPixel committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
      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'
          }
        }
      }
171 172 173
    }
  },
  computed: {
174
    darkMode: get('site/dark'),
175 176 177
    isShown: {
      get() { return this.value },
      set(val) { this.$emit('input', val) }
Nicolas Giard's avatar
Nicolas Giard committed
178
    },
NGPixel's avatar
NGPixel committed
179
    currentPages () {
180
      return _.sortBy(_.filter(this.pages, ['parent', _.head(this.currentNode) || 0]), ['title', 'path'])
NGPixel's avatar
NGPixel committed
181 182
    },
    isValidPath () {
NGPixel's avatar
NGPixel committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
      if (!this.currentPath) {
        return false
      }
      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
      }
199 200
    }
  },
201
  watch: {
NGPixel's avatar
NGPixel committed
202
    isShown (newValue, oldValue) {
203 204 205
      if (newValue && !oldValue) {
        this.currentPath = this.path
        this.currentLocale = this.locale
NGPixel's avatar
NGPixel committed
206 207 208 209 210 211 212 213 214 215
        _.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
216
      } else {
217 218
        const current = _.find(this.all, ['id', newValue[0]])

NGPixel's avatar
NGPixel committed
219 220 221 222 223 224 225 226 227 228 229 230
        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])
          })
        }
231 232

        this.currentPath = _.compact([_.get(current, 'path', ''), _.last(this.currentPath.split('/'))]).join('/')
NGPixel's avatar
NGPixel committed
233 234 235 236 237
      }
    },
    currentPage (newValue, oldValue) {
      if (!_.isEmpty(newValue)) {
        this.currentPath = newValue
238
      }
239 240 241 242 243 244
    },
    currentLocale (newValue, oldValue) {
      this.$nextTick(() => {
        this.tree = [
          {
            id: 0,
245
            title: '/ (root)',
246 247 248 249 250 251 252 253 254
            children: []
          }
        ]
        this.currentNode = [0]
        this.openNodes = [0]
        this.pages = []
        this.all = []
        this.treeViewCacheId += 1
      })
255 256
    }
  },
257 258 259
  methods: {
    close() {
      this.isShown = false
Nicolas Giard's avatar
Nicolas Giard committed
260 261
    },
    open() {
262 263 264 265 266 267
      const exit = this.openHandler({
        locale: this.currentLocale,
        path: this.currentPath
      })
      if (exit !== false) {
        this.close()
Nicolas Giard's avatar
Nicolas Giard committed
268 269
      }
    },
NGPixel's avatar
NGPixel committed
270 271 272 273 274 275 276 277 278 279 280 281 282
    async fetchFolders (item) {
      this.searchLoading = true
      const resp = await this.$apollo.query({
        query: pageTreeQuery,
        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: []}))
283
      const itemPages = _.filter(items, i => i.pageId > 0)
NGPixel's avatar
NGPixel committed
284 285 286 287 288
      if (itemFolders.length > 0) {
        item.children = itemFolders
      } else {
        item.children = undefined
      }
289 290
      this.pages = _.unionBy(this.pages, itemPages, 'id')
      this.all = _.unionBy(this.all, items, 'id')
NGPixel's avatar
NGPixel committed
291

NGPixel's avatar
NGPixel committed
292
      this.searchLoading = false
293 294 295 296
    }
  }
}
</script>
Nicolas Giard's avatar
Nicolas Giard committed
297 298 299 300 301 302 303

<style lang='scss'>

.page-selector {
  .v-treeview-node__label {
    font-size: 13px;
  }
304 305 306
  .v-treeview-node__content {
    cursor: pointer;
  }
Nicolas Giard's avatar
Nicolas Giard committed
307 308 309
}

</style>