Skip to content

Commit 7713258

Browse files
committed
Tweaked so that displayInColumn: true works
Also added sample markup/styling for this so that it side scrolls Changes default Timezone to UTC
1 parent a8cc421 commit 7713258

File tree

7 files changed

+143
-22
lines changed

7 files changed

+143
-22
lines changed

calus.js

+22-11
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ let defaultTemplate = `
3939
</div>
4040
</div>
4141
</div>
42-
`
42+
`;
43+
4344
export default function calus(options) {
4445
let el = options.el || '#calendar'
4546
let docEl = el instanceof HTMLElement ? el : document.querySelector(el)
@@ -50,7 +51,7 @@ export default function calus(options) {
5051
docEl.innerHTML = defaultTemplate
5152
}
5253

53-
let timezone = options.timezone || 'America/Los_Angeles'
54+
let timezone = options.timezone || 'utc';
5455

5556
return new Vue({
5657
el: el,
@@ -59,11 +60,14 @@ export default function calus(options) {
5960
// set this if you want to provide a list of ISO dates instead of Luxon
6061
// date objects)
6162
availableDates: options.availableDates || [],
63+
6264
// currently selected date. this is reset when changing available dates
6365
selected: null,
66+
6467
// whether to show all the months in a column, or a single month with
6568
// controls to change which month is shown
6669
displayInColumn: options.displayInColumn || false,
70+
6771
// which month is currently shown on screen (only used when
6872
// `displayInColumn` is false)
6973
currentDisplayedMonth: DateTime.local().setZone(timezone, { keepLocalTime: true }).startOf('month'),
@@ -73,6 +77,7 @@ export default function calus(options) {
7377

7478
// callback for when an available date is clicked
7579
onSelect: options.onSelect || function (day) { },
80+
7681
// callback for when selected month is changed with button
7782
onChangeMonth: options.onChangeMonth || function(prev, current) { },
7883
},
@@ -87,8 +92,8 @@ export default function calus(options) {
8792
: this.now.plus({ months: 2 })
8893
},
8994
months: function () {
90-
let months = []
91-
let date = null
95+
let months = [];
96+
let date = null;
9297

9398
if (this.displayInColumn) {
9499
date = this.firstAvailable < this.now ? this.firstAvailable : this.now
@@ -98,16 +103,22 @@ export default function calus(options) {
98103

99104
let startOfCurrentlyDisplayed = this.availableDates.findIndex(x => x > date)
100105
let available = this.availableDates.slice(this.displayInColumn ? 0 : startOfCurrentlyDisplayed)
101-
102106
let end = (this.displayInColumn ? this.lastAvailable : this.currentDisplayedMonth)
103107

104-
let startOfToday = this.now.startOf('day')
108+
let startOfToday = this.now.startOf('day');
105109

106110
while (date.startOf('month') <= end) {
107111
let days = []
108-
109-
let monthStart = date.startOf('month').startOf('week').plus({ days: this.weekStartOffset })
110-
let monthEnd = date.set({ day: date.daysInMonth }).plus({ weeks: 1 }).startOf('week').plus({ days: this.weekStartOffset })
112+
let monthStart;
113+
let monthEnd;
114+
115+
if (this.displayInColumn) {
116+
monthStart = date.hasSame(this.now, 'month') ? date : date.startOf('month');
117+
monthEnd = date.endOf('month');
118+
} else {
119+
monthStart = date.startOf('month').startOf('week').plus({ days: this.weekStartOffset });
120+
monthEnd = date.endOf('month').endOf('week').plus({ days: this.weekStartOffset });
121+
}
111122

112123
for (let day = monthStart; day <= monthEnd; day = day.plus({ days: 1 })) {
113124
while(available.length && day > available[0]) {
@@ -134,8 +145,8 @@ export default function calus(options) {
134145

135146
months.push({
136147
time: date.startOf('month'),
137-
isCurrentMonth: days[15].time.startOf('month').toFormat('y-MMM') === startOfToday.startOf('month').toFormat('y-MMM'),
138-
isInCurrentYear: days[15].time.startOf('year').toFormat('y') === startOfToday.startOf('year').toFormat('y'),
148+
isCurrentMonth: (this.displayInColumn ? days[0] : days[15]).time.startOf('month').toFormat('y-MMM') === startOfToday.startOf('month').toFormat('y-MMM'),
149+
isInCurrentYear: (this.displayInColumn ? days[0] : days[15]).time.startOf('year').toFormat('y') === startOfToday.startOf('year').toFormat('y'),
139150
days: days
140151
})
141152

demo/script.js

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
document.addEventListener('DOMContentLoaded', () => {
2-
let calendar = new Calus({
3-
el: '#calendar',
2+
let calendar1 = new Calus({
3+
el: '#calendar1',
44
useDefaultTemplate: true,
5-
})
5+
});
66

7-
console.log(calendar)
7+
console.log(calendar1);
8+
9+
let calendar2 = new Calus({
10+
el: '#calendar2',
11+
availableDates: [
12+
window.luxon.DateTime.local(),
13+
window.luxon.DateTime.local().plus({days: 1}),
14+
window.luxon.DateTime.local().plus({days: 2}),
15+
window.luxon.DateTime.local().plus({weeks: 1}),
16+
window.luxon.DateTime.local().plus({weeks: 2}),
17+
window.luxon.DateTime.local().plus({months: 2}),
18+
]
19+
});
20+
21+
console.log(calendar2);
22+
23+
let calendar3 = new Calus({
24+
el: '#calendar3',
25+
displayInColumn: true,
26+
useDefaultTemplate: true,
27+
});
28+
29+
console.log(calendar3);
830
})

demo/style.css

+42-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,53 @@
1414
}
1515

1616
.month__days .day {
17-
padding: 0.25em 0;
17+
padding: 0.25em;
1818
}
1919

2020
.month__days .day.is-past {
21-
opacity: 0.5;
21+
opacity: 0.6;
22+
}
23+
.month__days .day.is-different-month {
24+
opacity: 0.15;
25+
}
26+
27+
.month__days .day.is-available {
28+
background-color: #000;
29+
border-radius: 100%;
30+
color: #fff;
31+
font-weight: bold;
32+
}
33+
.month__days .day.is-this-month.is-available {
34+
cursor: pointer;
2235
}
2336

2437
.month__days .day.is-today {
2538
text-decoration: underline;
39+
}
40+
41+
42+
43+
.month-container--column {
44+
display: flex;
45+
flex-wrap: nowrap;
46+
overflow-x: auto;
47+
width: auto;
48+
}
49+
.month-container--column .month {
50+
flex: 0 0 auto;
51+
}
52+
.month-container--column .month__header {
53+
display: block;
54+
text-align: left;
55+
}
56+
.month-container--column .month__text {
57+
left: 0;
58+
position: sticky;
59+
}
60+
.month-container--column .month__days {
61+
display: block;
62+
white-space: nowrap;
63+
}
64+
.month-container--column .day {
65+
display: inline-block;
2666
}

dist/calus.lib.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

+50-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,61 @@
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7-
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
7+
<script src="dist/calus.lib.js"></script>
8+
9+
<!-- Below includes are only required for Demo purposes -->
810
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/global/luxon.min.js"></script>
911
<link href="demo/style.css" rel="stylesheet">
10-
<script src="dist/calus.lib.js"></script>
1112
<script src="demo/script.js"></script>
1213
<title>Document</title>
1314
</head>
1415
<body>
15-
<div id="calendar"></div>
16+
<h1>Example with <code>useDefaultTemplate: true</code> as an option</h1>
17+
<div id="calendar1"></div>
18+
19+
<h1>Example with custom template</h1>
20+
<p>Also makes use of the <code>availableDates</code> option to mark dates as available</p>
21+
<div id="calendar2">
22+
<div class="month-container" v-bind:class="{ 'month-container--column': displayInColumn }">
23+
<div class="month" v-for="month in months" v-bind:data-month="month.time.toFormat('MM/y')">
24+
<div class="month__header">
25+
<button type="button" class="month__control month__control--prev" v-if="!displayInColumn" v-on:click="scrollMonth(-1)">
26+
27+
</button>
28+
<div class="month__text">
29+
{{ month.time.toFormat(month.isInCurrentYear ? 'MMMM' : 'MMMM y') }}
30+
</div>
31+
<button type="button" class="month__control month__control--prev" v-if="!displayInColumn" v-on:click="scrollMonth(1)">
32+
33+
</button>
34+
</div>
35+
<div class="month__days">
36+
<div class="day"
37+
v-for="day in month.days"
38+
v-bind:data-date="+day.time"
39+
v-bind:class="{
40+
'is-today': day.isToday,
41+
'is-not-today': !day.isToday,
42+
'is-available': day.isAvailable,
43+
'is-not-available': !day.isAvailable,
44+
'is-past': day.isPast,
45+
'is-future': day.isFuture,
46+
'is-selected': day.isSelected,
47+
'is-this-month': day.isThisMonth,
48+
'is-different-month': !day.isThisMonth,
49+
}"
50+
v-on:click="select(day)"
51+
>
52+
<div class="day__inner">
53+
{{ day.time.day }}
54+
</div>
55+
</div>
56+
</div>
57+
</div>
58+
</div>
59+
</div>
60+
61+
<h1>Example of the calendar displayed as a column</h1>
62+
<div id="calendar3"></div>
1663
</body>
1764
</html>

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"build": "webpack",
88
"start": "npx http-server . -d",
99
"test": "echo \"Error: no test specified\" && exit 1",
10-
"watch": "webpack --watch"
10+
"watch": "NODE_ENV=development webpack --watch"
1111
},
1212
"typings": "./index.d.ts",
1313
"author": "",

webpack.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports = {
2626
}
2727
]
2828
},
29+
mode: process.env.NODE_ENV ? process.env.NODE_ENV : 'production',
2930
resolve: {
3031
alias: {
3132
"vue$": "vue/dist/vue.esm.js",

0 commit comments

Comments
 (0)