modal-move-page.vue 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <template lang="pug">
  2. .modal(v-bind:class='{ "is-active": isShown }')
  3. .modal-background
  4. .modal-container
  5. .modal-content
  6. header.is-indigo Move document
  7. section
  8. label.label Enter the new document path:
  9. p.control.is-fullwidth(v-bind:class='{ "is-loading": isLoading }')
  10. input.input(type='text', placeholder='page-name', v-model='movePath', ref='movePageInput', @keyup.enter='move', @keyup.esc='cancel')
  11. span.help.is-red(v-show='isInvalid') This document path is invalid or not allowed!
  12. span.note Note that moving or renaming documents can lead to broken links. Make sure to edit any page that links to this document afterwards!
  13. footer
  14. a.button.is-grey.is-outlined(v-on:click='cancel') Discard
  15. a.button.is-indigo(v-on:click='move') Move
  16. </template>
  17. <script>
  18. export default {
  19. name: 'modal-move-page',
  20. props: ['currentPath'],
  21. data () {
  22. return {
  23. movePath: '',
  24. isLoading: false,
  25. isInvalid: false
  26. }
  27. },
  28. computed: {
  29. isShown () {
  30. if(this.$store.state.modalMovePage.shown) {
  31. this.movePath = this.currentPath
  32. this.makeSelection()
  33. }
  34. return this.$store.state.modalMovePage.shown
  35. }
  36. },
  37. methods: {
  38. makeSelection: function () {
  39. let self = this;
  40. self._.delay(() => {
  41. let startPos = (self._.includes(self.currentPath, '/') ? self._.lastIndexOf(self.movePath, '/') + 1 : 0
  42. self.$helpers.form.setInputSelection(self.$refs.movePageInput, startPos, self.movePath.length)
  43. }, 100)
  44. },
  45. cancel: function () {
  46. this.$store.dispatch('modalMovePage/close')
  47. },
  48. move: function () {
  49. this.isInvalid = false
  50. let newDocPath = this.$helpers.pages.makeSafePath(this.movePath)
  51. if (this._.isEmpty(newDocPath) || newDocPath === this.currentPath || newDocPath === 'home') {) {
  52. this.isInvalid = true
  53. } else {
  54. this.isLoading = true
  55. this.$http.put(window.location.href, {
  56. move: newDocPath
  57. }).then(resp => {
  58. return resp.json()
  59. }).then(resp => {
  60. if (resp.ok) {
  61. window.location.assign('/' + newDocPath)
  62. } else {
  63. this.loading = false
  64. self.$store.dispatch('alert', {
  65. style: 'red',
  66. icon: 'square-cross',
  67. msg: resp.msg
  68. })
  69. }
  70. }).catch(err => {
  71. this.loading = false
  72. self.$store.dispatch('alert', {
  73. style: 'red',
  74. icon: 'square-cross',
  75. msg: 'Error: ' + err.body.msg
  76. })
  77. })
  78. }
  79. }
  80. }
  81. }
  82. </script>