Correct year date navigation logic

This commit is contained in:
Sergio Crisostomo 2018-01-19 10:59:44 +01:00
parent 8f6aeda4be
commit 77e43f2b87
2 changed files with 34 additions and 16 deletions

View file

@ -218,30 +218,44 @@
labels: labels.map(obj => ((obj.handler = handler(obj.type)), obj)) labels: labels.map(obj => ((obj.handler = handler(obj.type)), obj))
}; };
}, },
prevYear (direction) { prevYear (panel) {
this.changePanelDate(direction, 'FullYear', -1); console.log(this)
const increment = this.currentView === 'year' ? -10 : -1;
this.changePanelDate(panel, 'FullYear', increment);
}, },
nextYear (direction) { nextYear (panel) {
this.changePanelDate(direction, 'FullYear', 1); const increment = this.currentView === 'year' ? 10 : 1;
this.changePanelDate(panel, 'FullYear', increment);
}, },
prevMonth(direction){ prevMonth(panel){
this.changePanelDate(direction, 'Month', -1); this.changePanelDate(panel, 'Month', -1);
}, },
nextMonth(direction){ nextMonth(panel){
this.changePanelDate(direction, 'Month', 1); this.changePanelDate(panel, 'Month', 1);
}, },
changePanelDate(panel, type, increment){ changePanelDate(panel, type, increment){
const current = new Date(this[`${panel}PanelDate`]); const current = new Date(this[`${panel}PanelDate`]);
current[`set${type}`](current[`get${type}`]() + increment); current[`set${type}`](current[`get${type}`]() + increment);
this[`${panel}PanelDate`] = current; this[`${panel}PanelDate`] = current;
// change other panels if they overlap
const otherPanel = panel === 'left' ? 'right' : 'left'; if (this.splitPanels){
if (panel === 'left' && this.leftPanelDate >= this.rightPanelDate){ // change other panel if dates overlap
this.changePanelDate(otherPanel, type, 1); const otherPanel = panel === 'left' ? 'right' : 'left';
} if (panel === 'left' && this.leftPanelDate >= this.rightPanelDate){
if (panel === 'right' && this.rightPanelDate <= this.leftPanelDate){ this.changePanelDate(otherPanel, type, 1);
this.changePanelDate(otherPanel, type, -1); }
if (panel === 'right' && this.rightPanelDate <= this.leftPanelDate){
this.changePanelDate(otherPanel, type, -1);
}
} else {
// keep the panels together
const otherPanel = panel === 'left' ? 'right' : 'left';
const otherCurrent = new Date(this[`${otherPanel}PanelDate`]);
otherCurrent[`set${type}`](otherCurrent[`get${type}`]() + increment);
if (current[`get${type}`]() !== otherCurrent[`get${type}`]()){
this[`${otherPanel}PanelDate`] = otherCurrent;
}
} }
}, },
showYearPicker () { showYearPicker () {

View file

@ -145,7 +145,11 @@
}, },
methods: { methods: {
changeYear(dir){ changeYear(dir){
this.panelDate = siblingMonth(this.panelDate, dir * 12); if (this.selectionMode === 'year'){
this.panelDate = new Date(this.panelDate.getFullYear() + dir * 10, 0, 1);
} else {
this.panelDate = siblingMonth(this.panelDate, dir * 12);
}
}, },
changeMonth(dir){ changeMonth(dir){
this.panelDate = siblingMonth(this.panelDate, dir); this.panelDate = siblingMonth(this.panelDate, dir);