/*
@config : next999.json
@filename : ${psFileBasename}
@title : 3.予定編集ウインドウ
@name :
@siteIds : 310511
@siteTitles : イオン多摩平シフト管理
@disabled: false
*/
//$(document).on('click', 'div.item', function (e) {
$p.ex.changeTimeDataWindow = function (dataId, pageX, pageY) {
const $item = $('div.item[data-id="' + dataId + '"]');
$('#edit-modal').remove();
const itemId = $item.attr('data-id');
const posX = pageX;
const posY = pageY;
// 既存データ取得
const textParts = $item.text().trim().split('-');
const startTime = textParts[1] ? textParts[1].trim() : '';
const endTime = textParts[2] ? textParts[2].trim() : '';
// 休憩時間(NumD)がデータに含まれていれば取得、なければ自動計算
let restTime = '';
if (textParts[3]) {
restTime = textParts[3].trim();
} else if (startTime !== '' && endTime !== '') {
const diff = parseInt(endTime, 10) - parseInt(startTime, 10);
restTime = (diff > 4) ? 1 : 0;
} else {
restTime = 0;
}
// モーダルHTML(休憩時間も追加)
const modalHtml = `
`;
$('body').append(modalHtml);
$('body').css('overflow', 'hidden');
// 入力制限ロジック
$('#edit-start, #edit-end').on('input', function () {
let start = parseInt($('#edit-start').val(), 10);
let end = parseInt($('#edit-end').val(), 10);
// startTimeはendTimeより大きくできない
if (!isNaN(end)) {
$('#edit-start').attr('max', end);
} else {
$('#edit-start').attr('max', 23);
}
// endTimeはstartTimeより小さくできない
if (!isNaN(start)) {
$('#edit-end').attr('min', start);
} else {
$('#edit-end').attr('min', 0);
}
// 休憩時間自動計算
if (!isNaN(start) && !isNaN(end)) {
const diff = end - start;
const rest = (diff > 4) ? 1 : 0;
$('#edit-rest').val(rest);
} else {
$('#edit-rest').val(0);
}
});
setTimeout(() => {
$(document).on('mousedown.editModal', async function (event) {
if (!$(event.target).closest('#edit-modal').length) {
// 入力値取得
const newStart = $('#edit-start').val();
const newEnd = $('#edit-end').val();
const newRest = $('#edit-rest').val();
// 変更がない場合は何もせず閉じる
if (newStart === startTime && newEnd === endTime && newRest == restTime) {
$('#edit-modal').remove();
$(document).off('mousedown.editModal');
$('body').css('overflow', '');
return;
}
// 更新リクエスト送信
const pleasanterUrl = 'https://' + location.hostname + '/pleasanter/api/items/' + itemId + '/update';
const jsonBody = {
'ApiVersion': 1.1,
'ApiKey': apiKey,
'NumHash': {
'NumA': newStart,
'NumB': newEnd,
'NumD': newRest // 休憩時間を追加
}
};
try {
const pleasanterRes = await fetch(pleasanterUrl, {
'method': 'POST',
'headers': { 'Content-Type': 'application/json' },
'body': JSON.stringify(jsonBody)
});
const resData = await pleasanterRes.json();
if (resData.StatusCode === 200) {
$('#edit-modal').remove();
$(document).off('mousedown.editModal');
$('body').css('overflow', '');
location.reload();
} else {
alert('更新に失敗しました');
$('#edit-modal').remove();
$(document).off('mousedown.editModal');
$('body').css('overflow', '');
}
} catch (err) {
alert('通信エラーが発生しました');
$('#edit-modal').remove();
$(document).off('mousedown.editModal');
$('body').css('overflow', '');
}
}
});
}, 10);
}
// モーダル内クリック時は閉じない
$(document).on('click', '#edit-modal', function (e) {
e.stopPropagation();
});