move.js 1.65 KB
Newer Older
1 2 3 4
'use strict'

import $ from 'jquery'
import _ from 'lodash'
NGPixel's avatar
NGPixel committed
5 6
import { setInputSelection } from '../helpers/form'
import { makeSafePath } from '../helpers/pages'
NGPixel's avatar
NGPixel committed
7

8
// -> Move Existing Document
NGPixel's avatar
NGPixel committed
9

10 11 12 13
module.exports = (currentBasePath, alerts) => {
  if (currentBasePath !== '') {
    $('.btn-move-prompt').removeClass('is-hidden')
  }
NGPixel's avatar
NGPixel committed
14

15
  let moveInitialDocument = _.lastIndexOf(currentBasePath, '/') + 1
NGPixel's avatar
NGPixel committed
16

17 18 19 20 21 22
  $('.btn-move-prompt').on('click', (ev) => {
    $('#txt-move-prompt').val(currentBasePath)
    $('#modal-move-prompt').toggleClass('is-active')
    setInputSelection($('#txt-move-prompt').get(0), moveInitialDocument, currentBasePath.length)
    $('#txt-move-prompt').removeClass('is-danger').next().addClass('is-hidden')
  })
NGPixel's avatar
NGPixel committed
23

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  $('#txt-move-prompt').on('keypress', (ev) => {
    if (ev.which === 13) {
      $('.btn-move-go').trigger('click')
    }
  })

  $('.btn-move-go').on('click', (ev) => {
    let newDocPath = makeSafePath($('#txt-move-prompt').val())
    if (_.isEmpty(newDocPath) || newDocPath === currentBasePath || newDocPath === 'home') {
      $('#txt-move-prompt').addClass('is-danger').next().removeClass('is-hidden')
    } else {
      $('#txt-move-prompt').parent().addClass('is-loading')

      $.ajax(window.location.href, {
        data: {
          move: newDocPath
        },
        dataType: 'json',
        method: 'PUT'
      }).then((rData, rStatus, rXHR) => {
        if (rData.ok) {
          window.location.assign('/' + newDocPath)
        } else {
          alerts.pushError('Something went wrong', rData.error)
        }
      }, (rXHR, rStatus, err) => {
        alerts.pushError('Something went wrong', 'Save operation failed.')
      })
    }
  })
}