You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.6 KiB

  1. 'use strict'
  2. import $ from 'jquery'
  3. import _ from 'lodash'
  4. import { setInputSelection } from '../helpers/form'
  5. import { makeSafePath } from '../helpers/pages'
  6. // -> Move Existing Document
  7. module.exports = (currentBasePath, alerts) => {
  8. if (currentBasePath !== '') {
  9. $('.btn-move-prompt').removeClass('is-hidden')
  10. }
  11. let moveInitialDocument = _.lastIndexOf(currentBasePath, '/') + 1
  12. $('.btn-move-prompt').on('click', (ev) => {
  13. $('#txt-move-prompt').val(currentBasePath)
  14. $('#modal-move-prompt').toggleClass('is-active')
  15. setInputSelection($('#txt-move-prompt').get(0), moveInitialDocument, currentBasePath.length)
  16. $('#txt-move-prompt').removeClass('is-danger').next().addClass('is-hidden')
  17. })
  18. $('#txt-move-prompt').on('keypress', (ev) => {
  19. if (ev.which === 13) {
  20. $('.btn-move-go').trigger('click')
  21. }
  22. })
  23. $('.btn-move-go').on('click', (ev) => {
  24. let newDocPath = makeSafePath($('#txt-move-prompt').val())
  25. if (_.isEmpty(newDocPath) || newDocPath === currentBasePath || newDocPath === 'home') {
  26. $('#txt-move-prompt').addClass('is-danger').next().removeClass('is-hidden')
  27. } else {
  28. $('#txt-move-prompt').parent().addClass('is-loading')
  29. $.ajax(window.location.href, {
  30. data: {
  31. move: newDocPath
  32. },
  33. dataType: 'json',
  34. method: 'PUT'
  35. }).then((rData, rStatus, rXHR) => {
  36. if (rData.ok) {
  37. window.location.assign('/' + newDocPath)
  38. } else {
  39. alerts.pushError('Something went wrong', rData.error)
  40. }
  41. }, (rXHR, rStatus, err) => {
  42. alerts.pushError('Something went wrong', 'Save operation failed.')
  43. })
  44. }
  45. })
  46. }