const commit = window.gitlist.commit;
const branches = window.gitlist.branches;
const Cookies = require('js-cookie')
const errorHandler = window.gitlist.ajaxErrorHandler;
const invalidSnackbarCommit = () => {
$.snackbar({
content: 'The commit form data is invalid..'
})
}
const validCommit = () => {
if (!branches.includes(commit)) {
let branchInfo;
if (branches.length === 1) {
branchInfo = `Only the <strong>${branches.join(', ')}</strong> branch is editable.`
} else {
branchInfo = `Only the <strong>${branches.join(', ')}</strong> branches are editable.`
}
$.snackbar({
htmlAllowed: true,
content: `This commit <strong>${commit}</strong> is not changeable.<br/>
${branchInfo}
`,
})
return false;
} {
return true
}
}
const preLoadCommits = (options) => {
const { inputs, commentCookie } = options
for(let inputKey in inputs) {
const input = inputs[inputKey]
//console.log(inputKey, commentCookie)
let cookieName = `p3x-gitlist-commit-${inputKey}`;
if (inputKey === 'comment') {
cookieName = `p3x-gitlist-commit-${commentCookie }-${inputKey}`;
}
const cookie = Cookies.get(cookieName)
if (cookie) {
input.val(cookie.trim());
}
input.change(() => {
const val = input.val().trim();
Cookies.set(cookieName, val);
input.val(val);
})
}
}
const paths = window.gitlist.getPaths();
filename = paths.slice(4).join('/');
const gitHelperAjax = async (options) => {
const { modal, action, inputs, value } = options
modal.modal('hide')
const url = `${window.gitlist.basepath}/${window.gitlist.repo}/git-helper/${window.gitlist.branch}/${action}`
const response = await $.ajax({
url: url,
type: 'POST',
data: {
value: value,
email: inputs.email.val(),
name: inputs.name.val(),
comment: inputs.comment.val(),
filename: filename
}
})
const json = JSON.parse(response)
if (json.hasOwnProperty('output') && json.output !== '') {
$.snackbar({
htmlAllowed: true,
content: json.output,
})
}
if (json.error === true) {
errorHandler(json);
}
return json;
}
const $modalDelete = $('#p3x-gitlist-modal-delete')
const $buttonDelete = $('#p3x-gitlist-file-delete')
const $buttonDeleteSure = $('#p3x-gitlist-modal-delete-confirm')
const $formDeleteForm = $('#p3x-gitlist-modal-delete-form')
const $deleteInputName = $('#p3x-gitlist-modal-delete-name');
const $deleteInputEmail = $('#p3x-gitlist-modal-delete-email');
const $deleteInputComment = $('#p3x-gitlist-modal-delete-comment');
const inputs = {
name: $deleteInputName,
email: $deleteInputEmail,
comment: $deleteInputComment,
}
preLoadCommits({
inputs: inputs,
commentCookie: 'delete'
})
$buttonDelete.click(() => {
if (!validCommit()) {
return
}
$modalDelete.modal('show')
})
$buttonDeleteSure.click(async() => {
if($formDeleteForm[0].checkValidity() === false) {
invalidSnackbarCommit()
return;
}
try {
const json = await gitHelperAjax({
modal: $modalDelete,
action: 'delete',
inputs: inputs,
value: undefined,
})
const newLocation = `${window.gitlist.basepath}/${paths[1]}/commit/${json.output}?snack=` + encodeURIComponent(`The "${filename}" file is deleted. You are switched to the page where you can see the last commit.`)
// console.log(json, newLocation)
location = newLocation
/*
close();
$.snackbar({ewLo
htmlAllowed: true,
content: '<i class="fas fa-check"></i> The file is saved.',
})
*/
} catch(e) {
errorHandler(e)
}
})