page-selector.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <template lang="pug">
  2. v-dialog(
  3. v-model='isShown'
  4. max-width='850px'
  5. overlay-color='blue darken-4'
  6. overlay-opacity='.7'
  7. )
  8. v-card.page-selector
  9. .dialog-header.is-blue
  10. v-icon.mr-3(color='white') mdi-page-next-outline
  11. .body-1(v-if='mode === `create`') Select New Page Location
  12. .body-1(v-else-if='mode === `move`') Move / Rename Page Location
  13. v-spacer
  14. v-progress-circular(
  15. indeterminate
  16. color='white'
  17. :size='20'
  18. :width='2'
  19. v-show='searchLoading'
  20. )
  21. .d-flex
  22. v-flex.grey(xs5, :class='darkMode ? `darken-4` : `lighten-3`')
  23. v-toolbar(color='grey darken-3', dark, dense, flat)
  24. .body-2 Virtual Folders
  25. v-spacer
  26. v-btn(icon, tile, href='https://docs.requarks.io/guide/pages#folders', target='_blank')
  27. v-icon mdi-help-box
  28. div(style='height:400px;')
  29. vue-scroll(:ops='scrollStyle')
  30. v-treeview(
  31. :active.sync='currentNode'
  32. :open.sync='openNodes'
  33. :items='tree'
  34. :load-children='fetchFolders'
  35. dense
  36. expand-icon='mdi-menu-down-outline'
  37. item-id='path'
  38. item-text='title'
  39. activatable
  40. hoverable
  41. )
  42. template(slot='prepend', slot-scope='{ item, open, leaf }')
  43. v-icon mdi-{{ open ? 'folder-open' : 'folder' }}
  44. v-flex(xs7)
  45. v-toolbar(color='blue darken-2', dark, dense, flat)
  46. .body-2 Pages
  47. v-spacer
  48. v-btn(icon, tile, disabled): v-icon mdi-content-save-move-outline
  49. v-btn(icon, tile, disabled): v-icon mdi-trash-can-outline
  50. div(v-if='currentPages.length > 0', style='height:400px;')
  51. vue-scroll(:ops='scrollStyle')
  52. v-list.py-0(dense)
  53. v-list-item-group(
  54. v-model='currentPage'
  55. color='primary'
  56. )
  57. template(v-for='(page, idx) of currentPages')
  58. v-list-item(:key='page.id', :value='page.path')
  59. v-list-item-icon: v-icon mdi-file-document-box
  60. v-list-item-title {{page.title}}
  61. v-divider(v-if='idx < pages.length - 1')
  62. v-alert.animated.fadeIn(
  63. v-else
  64. text
  65. color='orange'
  66. prominent
  67. icon='mdi-alert'
  68. )
  69. .body-2 This folder is empty.
  70. v-card-actions.grey.pa-2(:class='darkMode ? `darken-2` : `lighten-1`')
  71. v-select(
  72. solo
  73. dark
  74. flat
  75. background-color='grey darken-3-d2'
  76. hide-details
  77. single-line
  78. :items='namespaces'
  79. style='flex: 0 0 100px; border-radius: 4px 0 0 4px;'
  80. v-model='currentLocale'
  81. )
  82. v-text-field(
  83. ref='pathIpt'
  84. solo
  85. hide-details
  86. prefix='/'
  87. v-model='currentPath'
  88. flat
  89. clearable
  90. style='border-radius: 0 4px 4px 0;'
  91. )
  92. v-card-chin
  93. v-spacer
  94. v-btn(text, @click='close') Cancel
  95. v-btn.px-4(color='primary', @click='open', :disabled='!isValidPath')
  96. v-icon(left) mdi-check
  97. span Select
  98. </template>
  99. <script>
  100. import _ from 'lodash'
  101. import { get } from 'vuex-pathify'
  102. import pageTreeQuery from 'gql/common/common-pages-query-tree.gql'
  103. const localeSegmentRegex = /^[A-Z]{2}(-[A-Z]{2})?$/i
  104. /* global siteLangs, siteConfig */
  105. export default {
  106. props: {
  107. value: {
  108. type: Boolean,
  109. default: false
  110. },
  111. path: {
  112. type: String,
  113. default: 'new-page'
  114. },
  115. locale: {
  116. type: String,
  117. default: 'en'
  118. },
  119. mode: {
  120. type: String,
  121. default: 'create'
  122. },
  123. openHandler: {
  124. type: Function,
  125. default: () => {}
  126. }
  127. },
  128. data() {
  129. return {
  130. searchLoading: false,
  131. currentLocale: siteConfig.lang,
  132. currentFolderPath: '',
  133. currentPath: 'new-page',
  134. currentPage: null,
  135. currentNode: [0],
  136. openNodes: [0],
  137. tree: [
  138. {
  139. id: 0,
  140. title: '/ (root',
  141. children: []
  142. }
  143. ],
  144. pages: [],
  145. all: [],
  146. namespaces: siteLangs.length ? siteLangs.map(ns => ns.code) : [siteConfig.lang],
  147. scrollStyle: {
  148. vuescroll: {},
  149. scrollPanel: {
  150. initialScrollX: 0.01, // fix scrollbar not disappearing on load
  151. scrollingX: false,
  152. speed: 50
  153. },
  154. rail: {
  155. gutterOfEnds: '2px'
  156. },
  157. bar: {
  158. onlyShowBarOnScroll: false,
  159. background: '#999',
  160. hoverStyle: {
  161. background: '#64B5F6'
  162. }
  163. }
  164. }
  165. }
  166. },
  167. computed: {
  168. darkMode: get('site/dark'),
  169. isShown: {
  170. get() { return this.value },
  171. set(val) { this.$emit('input', val) }
  172. },
  173. currentPages () {
  174. return _.sortBy(_.filter(this.pages, ['parent', _.head(this.currentNode) || 0]), ['title', 'path'])
  175. },
  176. isValidPath () {
  177. if (!this.currentPath) {
  178. return false
  179. }
  180. const firstSection = _.head(this.currentPath.split('/'))
  181. if (firstSection.length <= 1) {
  182. return false
  183. } else if (localeSegmentRegex.test(firstSection)) {
  184. return false
  185. } else if (
  186. _.some(['login', 'logout', 'register', 'verify', 'favicons', 'fonts', 'img', 'js', 'svg'], p => {
  187. return p === firstSection
  188. })) {
  189. return false
  190. } else {
  191. return true
  192. }
  193. }
  194. },
  195. watch: {
  196. isShown (newValue, oldValue) {
  197. if (newValue && !oldValue) {
  198. this.currentPath = this.path
  199. this.currentLocale = this.locale
  200. _.delay(() => {
  201. this.$refs.pathIpt.focus()
  202. })
  203. }
  204. },
  205. currentNode (newValue, oldValue) {
  206. if (newValue.length < 1) { // force a selection
  207. this.$nextTick(() => {
  208. this.currentNode = oldValue
  209. })
  210. } else {
  211. const current = _.find(this.all, ['id', newValue[0]])
  212. if (this.openNodes.indexOf(newValue[0]) < 0) { // auto open and load children
  213. if (current) {
  214. if (this.openNodes.indexOf(current.parent) < 0) {
  215. this.$nextTick(() => {
  216. this.openNodes.push(current.parent)
  217. })
  218. }
  219. }
  220. this.$nextTick(() => {
  221. this.openNodes.push(newValue[0])
  222. })
  223. }
  224. this.currentPath = _.compact([_.get(current, 'path', ''), _.last(this.currentPath.split('/'))]).join('/')
  225. }
  226. },
  227. currentPage (newValue, oldValue) {
  228. if (!_.isEmpty(newValue)) {
  229. this.currentPath = newValue
  230. }
  231. }
  232. },
  233. methods: {
  234. close() {
  235. this.isShown = false
  236. },
  237. open() {
  238. const exit = this.openHandler({
  239. locale: this.currentLocale,
  240. path: this.currentPath
  241. })
  242. if (exit !== false) {
  243. this.close()
  244. }
  245. },
  246. async fetchFolders (item) {
  247. this.searchLoading = true
  248. const resp = await this.$apollo.query({
  249. query: pageTreeQuery,
  250. fetchPolicy: 'network-only',
  251. variables: {
  252. parent: item.id,
  253. mode: 'ALL',
  254. locale: this.currentLocale
  255. }
  256. })
  257. const items = _.get(resp, 'data.pages.tree', [])
  258. const itemFolders = _.filter(items, ['isFolder', true]).map(f => ({...f, children: []}))
  259. const itemPages = _.filter(items, i => i.pageId > 0)
  260. if (itemFolders.length > 0) {
  261. item.children = itemFolders
  262. } else {
  263. item.children = undefined
  264. }
  265. this.pages.push(...itemPages)
  266. this.all.push(...items)
  267. this.searchLoading = false
  268. }
  269. }
  270. }
  271. </script>
  272. <style lang='scss'>
  273. .page-selector {
  274. .v-treeview-node__label {
  275. font-size: 13px;
  276. }
  277. .v-treeview-node__content {
  278. cursor: pointer;
  279. }
  280. }
  281. </style>