wikijs-fork/client/js/modals/move.js

48 lines
1.5 KiB
JavaScript
Raw Normal View History

/* global $, _, alerts, currentBasePath */
2016-09-09 23:22:18 +00:00
2017-02-09 01:52:37 +00:00
// -> Move Existing Document
2016-09-09 23:22:18 +00:00
2017-02-09 01:52:37 +00:00
if (currentBasePath !== '') {
$('.btn-move-prompt').removeClass('is-hidden')
2016-09-09 23:22:18 +00:00
}
2017-02-09 01:52:37 +00:00
let moveInitialDocument = _.lastIndexOf(currentBasePath, '/') + 1
2016-09-09 23:22:18 +00:00
$('.btn-move-prompt').on('click', (ev) => {
2017-02-09 01:52:37 +00:00
$('#txt-move-prompt').val(currentBasePath)
$('#modal-move-prompt').toggleClass('is-active')
setInputSelection($('#txt-move-prompt').get(0), moveInitialDocument, currentBasePath.length) // eslint-disable-line no-undef
2017-02-09 01:52:37 +00:00
$('#txt-move-prompt').removeClass('is-danger').next().addClass('is-hidden')
})
2016-09-09 23:22:18 +00:00
$('#txt-move-prompt').on('keypress', (ev) => {
2017-02-09 01:52:37 +00:00
if (ev.which === 13) {
$('.btn-move-go').trigger('click')
}
})
2016-09-09 23:22:18 +00:00
$('.btn-move-go').on('click', (ev) => {
let newDocPath = makeSafePath($('#txt-move-prompt').val()) // eslint-disable-line no-undef
2017-02-09 01:52:37 +00:00
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.')
})
}
})