Skip to content

Commit b168e97

Browse files
authored
Add interactive calendar widget to ServiceNow portal (#1598)
- Added calendar structure with month navigation, current day, and weekend highlights - Implemented `$scope.loadCalendar()` function to dynamically load days for selected month and year - CSS styling for calendar layout - JavaScript for month navigation and day calculations - Included `ng-init` to load calendar on widget initialization - Known issue: Ensure `ng-init` directive is present if dates do not load initially
1 parent d251cb2 commit b168e97

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Calendar Widget for ServiceNow Portal
2+
This widget creates a simple, interactive calendar for the ServiceNow portal, allowing users to navigate through months and view the current day highlighted. It displays the days of the month in a grid layout.
3+
4+
## Features
5+
Monthly Navigation: Buttons for navigating between months.
6+
Current Day Highlighting: The current date is highlighted in light blue.
7+
Responsive Layout: The calendar adjusts to fit within the widget container.
8+
9+
## Usage
10+
11+
HTML Structure: The main HTML structure displays month navigation buttons and the days in a grid layout.
12+
13+
CSS Styling: CSS styles define the appearance of the calendar, including a shadowed border, day alignment.
14+
15+
JavaScript Controller:
16+
17+
Defines the calendar's navigation functionality.
18+
Calculates and displays the days of the selected month.
19+
Highlights today’s date if it is within the selected month.
20+
21+
## Code Structure
22+
### Controller (api.controller):
23+
Initializes the month and year to display the current month.
24+
Provides functions to navigate to the previous and next month.
25+
Calculates the days of the selected month and highlights the current date.
26+
### CSS:
27+
.calendar-widget: The main container for the calendar.
28+
.calendar-header: Contains the month name and navigation buttons.
29+
30+
## Customization
31+
### Highlight Colors:
32+
Modify .current-day in the CSS to change the color for today’s date.
33+
Month Names and Day Names:
34+
Update the $scope.monthNames and $scope.dayNames arrays to localize or customize the labels.
35+
36+
### Example Usage
37+
In the ServiceNow portal, add this widget to display an interactive calendar. This can be embedded in any dashboard or custom page where date visualization is needed.
38+
39+
## Known Issues
40+
Initial Load: If dates do not display immediately, ensure the ng-init="loadCalendar()" directive is included in the main container.
41+
Date Accuracy: The calendar currently starts with today's date. If dates appear incorrect, check the $scope.loadCalendar() function for accurate month and day calculations.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<div class="calendar-widget" ng-init="loadCalendar()">
2+
<div class="calendar-header">
3+
<button ng-click="prevMonth()">&#9664;</button>
4+
<span>{{ monthNames[currentMonth] }} {{ currentYear }}</span>
5+
<button ng-click="nextMonth()">&#9654;</button>
6+
</div>
7+
8+
<div class="calendar-days">
9+
<div class="day-name" ng-repeat="day in dayNames">{{ day }}</div>
10+
<div
11+
class="day"
12+
ng-repeat="day in days track by $index"
13+
ng-class="{'current-day': isCurrentDay(day), 'weekend': isWeekend($index), 'regular-day': day}">
14+
{{ day }}
15+
</div>
16+
</div>
17+
</div>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
api.controller = function($scope) {
2+
// Month and day names
3+
$scope.monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
4+
$scope.dayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
5+
6+
// Initialize the current month, year, and days
7+
var today = new Date();
8+
$scope.currentMonth = today.getMonth();
9+
$scope.currentYear = today.getFullYear();
10+
$scope.days = [];
11+
12+
// Load the calendar days
13+
$scope.loadCalendar = function() {
14+
$scope.days = [];
15+
16+
// Calculate the first and last day of the month
17+
var firstDay = new Date($scope.currentYear, $scope.currentMonth, 1).getDay();
18+
var lastDate = new Date($scope.currentYear, $scope.currentMonth + 1, 0).getDate();
19+
20+
// Add empty days for alignment (for days before the 1st of the month)
21+
for (var i = 0; i < firstDay; i++) {
22+
$scope.days.push('');
23+
}
24+
25+
// Add days of the month
26+
for (var day = 1; day <= lastDate; day++) {
27+
$scope.days.push(day);
28+
}
29+
};
30+
31+
// Check if the day is today
32+
$scope.isCurrentDay = function(day) {
33+
return day == today.getDate() && $scope.currentMonth == today.getMonth() && $scope.currentYear == today.getFullYear();
34+
};
35+
36+
// Navigate to the previous month
37+
$scope.prevMonth = function() {
38+
if ($scope.currentMonth === 0) {
39+
$scope.currentMonth = 11;
40+
$scope.currentYear--;
41+
} else {
42+
$scope.currentMonth--;
43+
}
44+
$scope.loadCalendar();
45+
};
46+
47+
// Navigate to the next month
48+
$scope.nextMonth = function() {
49+
if ($scope.currentMonth === 11) {
50+
$scope.currentMonth = 0;
51+
$scope.currentYear++;
52+
} else {
53+
$scope.currentMonth++;
54+
}
55+
$scope.loadCalendar();
56+
};
57+
58+
// Initialize the calendar for the current month
59+
$scope.loadCalendar();
60+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.calendar-widget {
2+
width: 100%;
3+
max-width: 300px;
4+
margin: auto;
5+
font-family: Arial, sans-serif;
6+
border: 1px solid #ddd;
7+
box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.1);
8+
}
9+
10+
.calendar-header {
11+
display: flex;
12+
justify-content: space-between;
13+
align-items: center;
14+
padding: 10px;
15+
background-color: #f4f4f4;
16+
font-weight: bold;
17+
}
18+
19+
.calendar-header button {
20+
background: none;
21+
border: none;
22+
font-size: 1.2em;
23+
cursor: pointer;
24+
}
25+
26+
.calendar-days {
27+
display: grid;
28+
grid-template-columns: repeat(7, 1fr);
29+
text-align: center;
30+
}
31+
32+
.day-name {
33+
font-weight: bold;
34+
padding: 5px 0;
35+
color: #555;
36+
}
37+
38+
.day {
39+
padding: 10px 0;
40+
border: 1px solid #f0f0f0;
41+
}
42+
43+
.current-day {
44+
background-color: #d3e9ff;
45+
font-weight: bold;
46+
color: #333;
47+
border-radius: 50%;
48+
}

0 commit comments

Comments
 (0)