refactor duration.js

This commit is contained in:
Cindy Chang
2024-07-22 14:52:52 +08:00
parent 486baa6e54
commit 9f48c1d2b0
2 changed files with 111 additions and 46 deletions

View File

@@ -1259,6 +1259,29 @@ export default {
checkActivityDuration() {
return !this.selectDurationData?.length;
},
/**
* 檢查活動序列的邏輯
* @returns {boolean} 是否禁用按鈕
*/
checkActivitySequence() {
switch (this.selectedActivitySequence) {
case 'Start & End':
return checkStartAndEndSequence(this.selectCfmSeqStart, this.selectCfmSeqEnd);
case 'Sequence':
return checkSequenceMode(this.selectedMode);
default:
return true;
}
},
/**
* 檢查 Start & End 活動序列
* @param {string} start 活動開始
* @param {string} end 活動結束
* @returns {boolean} 是否禁用按鈕
*/
checkStartAndEndSequence(start, end) {
return !(start && end);
},
checkProcessingTime() {
let disabled = true;
switch (this.selectedProcessScope) {

View File

@@ -248,58 +248,100 @@ export default {
* @param {number} goUp 上箭頭的鍵盤代號
* @param {boolean} selectIt 是否已執行
*/
actionUpDown(input, goUp, selectIt = false) {
const tUnit = input.dataset.tunit;
let newVal = parseInt(input.value, 10);
actionUpDown(input, goUp, selectIt = false) {
const tUnit = input.dataset.tunit;
let newVal = this.getNewValue(input);
newVal = isNaN(newVal) ? 0 : newVal;
if(goUp) {
// 箭頭向上,數字加一
newVal += this.tUnits[tUnit].inc;
if(newVal > this.tUnits[tUnit].max) {
if(this.tUnits[tUnit].dsp === 'd'){
// 超過 maxDays 要等於最大值
this.totalSeconds = this.maxTotal;
} else {
// 超過該單位最大值時要進位為零
newVal = newVal % (this.tUnits[tUnit].max + 1);
// 前一個更大的單位要進位
if(input.dataset.index > 0) {
const prevUnit = document.querySelector(`input[data-index="${parseInt(input.dataset.index) - 1}"]`);
this.actionUpDown(prevUnit, true);
}
}
if (goUp) {
newVal = this.handleArrowUp(newVal, tUnit, input);
} else {
newVal = this.handleArrowDown(newVal, tUnit);
}
} else {
// 箭頭向下,數字減一
this.updateInputValue(input, newVal, tUnit);
if (selectIt) {
input.select();
}
this.calculateTotalSeconds();
},
/**
* 獲取新的數值
* @param {element} input 輸入的元素
* @returns {number} 新的數值
*/
getNewValue(input) {
let newVal = parseInt(input.value, 10);
return isNaN(newVal) ? 0 : newVal;
},
/**
* 處理向上箭頭的行為
* @param {number} newVal 當前數值
* @param {string} tUnit 時間單位
* @param {element} input 輸入的元素
* @returns {number} 更新後的數值
*/
handleArrowUp(newVal, tUnit, input) {
newVal += this.tUnits[tUnit].inc;
if (newVal > this.tUnits[tUnit].max) {
if (this.tUnits[tUnit].dsp === 'd') {
this.totalSeconds = this.maxTotal;
} else {
newVal = newVal % (this.tUnits[tUnit].max + 1);
this.incrementPreviousUnit(input);
}
}
return newVal;
},
/**
* 處理向下箭頭的行為
* @param {number} newVal 當前數值
* @param {string} tUnit 時間單位
* @returns {number} 更新後的數值
*/
handleArrowDown(newVal, tUnit) {
newVal -= this.tUnits[tUnit].inc;
if (newVal < 0) {
// 小於零要調整為該單位最大值,但下一個單位不動
newVal = (this.tUnits[tUnit].max + 1) - this.tUnits[tUnit].inc;
newVal = (this.tUnits[tUnit].max + 1) - this.tUnits[tUnit].inc;
}
}
return newVal;
},
// input.value = newVal.toString().padStart(2, '0'); // 前綴要補 0
input.value = newVal.toString();
switch (tUnit) {
case 'd':
this.days = input.value;
break;
case 'h':
this.hours = input.value;
break;
case 'm':
this.minutes = input.value;
break;
case 's':
this.seconds = input.value;
break;
};
if (selectIt) {
input.select();
}
this.calculateTotalSeconds();
/**
* 進位前一個更大的單位
* @param {element} input 輸入的元素
*/
incrementPreviousUnit(input) {
if (input.dataset.index > 0) {
const prevUnit = document.querySelector(`input[data-index="${parseInt(input.dataset.index) - 1}"]`);
this.actionUpDown(prevUnit, true);
}
},
/**
* 更新輸入框的數值
* @param {element} input 輸入的元素
* @param {number} newVal 新的數值
* @param {string} tUnit 時間單位
*/
updateInputValue(input, newVal, tUnit) {
input.value = newVal.toString();
switch (tUnit) {
case 'd':
this.days = input.value;
break;
case 'h':
this.hours = input.value;
break;
case 'm':
this.minutes = input.value;
break;
case 's':
this.seconds = input.value;
break;
}
},
/**
* 設定 dhms 的數值