Skip to content

Commit

Permalink
modify AvailabilityScale interface to provide a function and remove f…
Browse files Browse the repository at this point in the history
…unctions from AvailabilityLevel
  • Loading branch information
Jihou Tsao committed Sep 16, 2022
1 parent bf9ebc8 commit 5b48edf
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 57 deletions.
39 changes: 9 additions & 30 deletions src/AvailabilityLevel.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,17 @@
//import { lighten } from 'color2k';
import { AvailabilityScale } from './models/availability/AvailabilityScale';
import { defaultAvailabilityScale } from './models/availability/DefaultAvailabilityScale';

export class AvailabilityLevel {
public level = 1; // one-indexed
public readonly scale: AvailabilityScale;
public readonly level: number; // one-indexed
public readonly label: string;
public readonly color: string;

constructor(level = 1, scale = defaultAvailabilityScale) {
constructor(
level = 1,
scale: AvailabilityScale = defaultAvailabilityScale
) {
this.level = level;
this.scale = scale;
}

get color(): string {
if (this.level in this.scale.colors) {
return this.scale.colors[this.level];
}

return 'black';
// TODO: rewrite function to interpolate color
/*
const midpoint: number = (this.scale.levels + 1) / 2;
if (this.level === midpoint) return NEUTRAL_COLOR;
if (this.level === 1) return BUSY_COLOR;
if (this.level === this.scale.levels) return AVAILABLE_COLOR;
//const adjustment = Math.abs(midpoint - this.level) / this.max;
if (this.level < midpoint) {
const adjustment = (1.5 * (this.level - 1)) / this.scale.levels;
return lighten(BUSY_COLOR, adjustment);
} else {
const adjustment =
(1.5 * (this.scale.levels - this.level)) / this.scale.levels;
return lighten(AVAILABLE_COLOR, adjustment);
}
*/
this.label = scale.label(this.level);
this.color = scale.color(this.level);
}
}
5 changes: 2 additions & 3 deletions src/BlockData.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Interval } from 'luxon';
import { AvailabilityLevel } from './AvailabilityLevel';

export class BlockData {
public readonly interval: Interval;
public readonly level: AvailabilityLevel;
public level: number;

constructor(interval: Interval, level: AvailabilityLevel) {
constructor(interval: Interval, level: number) {
this.interval = interval;
this.level = level;
}
Expand Down
10 changes: 8 additions & 2 deletions src/DetailPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div>
<select v-model="selectedLevel">
<option v-for="level in levels" :value="level" :key="level">
{{ level }}
{{ levelLabels[level - 1] }}
</option>
</select>
</div>
Expand Down Expand Up @@ -46,11 +46,17 @@ const gridState = useGridStateStore();
const levels = computed((): number[] => {
return Array.from(
{ length: gridState.maxLevel },
{ length: gridState.scale.levels },
(value, index) => index + 1
);
});
const levelLabels = computed((): string[] => {
return levels.value.map((level) => {
return gridState.scale.label(level);
});
});
const intervalString = computed(() => {
let result = '';
let intervals: Interval[] = [];
Expand Down
8 changes: 2 additions & 6 deletions src/models/availability/AvailabilityScale.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
export interface AvailabilityScale {
levels: number;
labels: {
[level: number]: string;
};
colors: {
[level: number]: string;
};
label(level: number): string;
color(level: number): string;
}
30 changes: 22 additions & 8 deletions src/models/availability/DefaultAvailabilityScale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,28 @@ import { AvailabilityScale } from './AvailabilityScale';

export const defaultAvailabilityScale: AvailabilityScale = {
levels: 3,
colors: {
'1': BUSY_COLOR,
'2': NEUTRAL_COLOR,
'3': AVAILABLE_COLOR,
color(level: number) {
switch (level) {
case 1:
return BUSY_COLOR;
case 2:
return NEUTRAL_COLOR;
case 3:
return AVAILABLE_COLOR;
default:
return '';
}
},
labels: {
'1': 'Busy',
'2': 'Maybe',
'3': 'Available',
label(level: number) {
switch (level) {
case 1:
return 'Busy';
case 2:
return 'Maybe';
case 3:
return 'Available';
default:
return 'Unknown';
}
},
};
15 changes: 7 additions & 8 deletions src/stores/gridState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const useGridStateStore = defineStore('gridState', {
state: () => {
return {
blockData: [] as BlockData[],
maxLevel: 3,
scale: defaultAvailabilityScale,
};
},

Expand All @@ -25,17 +25,13 @@ export const useGridStateStore = defineStore('gridState', {
let intervalStart = startTime;

for (let i = 0; i < blockCount; i++) {
const defaultLevel = new AvailabilityLevel(
DEFAULT_INITIAL_LEVEL,
defaultAvailabilityScale
);
const blockInterval: Interval = Interval.after(
intervalStart,
intervalDuration
);
const blockData: BlockData = new BlockData(
blockInterval,
defaultLevel
DEFAULT_INITIAL_LEVEL
);
this.blockData.push(blockData);

Expand All @@ -48,11 +44,14 @@ export const useGridStateStore = defineStore('gridState', {
return new AvailabilityLevel();
}

return this.blockData[index].level;
return new AvailabilityLevel(
this.blockData[index].level,
this.scale
);
},

changeLevel(blockIndex: number, level: number) {
this.blockData[blockIndex].level.level = level;
this.blockData[blockIndex].level = level;
},

changeMultipleLevels(blocks: number[], level: number) {
Expand Down

0 comments on commit 5b48edf

Please sign in to comment.