-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-calendar-events.html
185 lines (167 loc) · 9.41 KB
/
google-calendar-events.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<script type="text/javascript">
RED.nodes.registerType('google-calendar-events', {
category: 'function', // This node belongs to the 'function' category
color: '#a6bbcf', // Set the color of the node in the editor
defaults: {
name: { value: "" }, // Node name
google: { type: "google-calendar-config", required: true }, // Google Calendar configuration node
calendar: { value: "", required: false }, // Calendar ID
startDate: { value: "", required: false }, // Start date
endDate: { value: "", required: false }, // End date
fullDayStart: { value: false }, // Full day checkbox for start date
fullDayEnd: { value: false } // Full day checkbox for end date
},
inputs: 1,
outputs: 1,
icon: "font-awesome/fa-calendar",
label: function() {
return this.name || "Google Calendar Events";
},
oneditprepare: function() {
const node = this;
// Initialize the date fields using the node values
$("#node-input-startDate").val(node.startDate);
$("#node-input-endDate").val(node.endDate);
$("#node-input-fullDayStart").prop('checked', node.fullDayStart);
$("#node-input-fullDayEnd").prop('checked', node.fullDayEnd);
// Function to handle Full Day checkbox logic for the startDate or endDate field
function handleFullDayCheckbox(startOrEnd) {
const fullDayCheckbox = $("#node-input-fullDay" + startOrEnd);
const dateTimeField = $("#node-input-" + startOrEnd.toLowerCase() + "Date");
// Toggle the input type between date and datetime-local based on checkbox status
fullDayCheckbox.on('change', function() {
if (this.checked) {
dateTimeField.attr('type', 'date'); // Only ask for date
dateTimeField.val(node[startOrEnd.toLowerCase() + 'Date'].substring(0, 10)); // Set only the date
} else {
dateTimeField.attr('type', 'datetime-local'); // Ask for date and time
dateTimeField.val(node[startOrEnd.toLowerCase() + 'Date']); // Set full datetime
}
});
// Set the initial state based on the node's configuration
if (fullDayCheckbox.is(':checked')) {
dateTimeField.attr('type', 'date'); // Set to date if checkbox is checked
if (node[startOrEnd.toLowerCase() + 'Date']) {
dateTimeField.val(node[startOrEnd.toLowerCase() + 'Date'].substring(0, 10)); // Show only the date part
}
} else {
dateTimeField.attr('type', 'datetime-local'); // Set to datetime-local if checkbox is not checked
if (node[startOrEnd.toLowerCase() + 'Date']) {
dateTimeField.val(node[startOrEnd.toLowerCase() + 'Date']); // Show full datetime
}
}
}
// Apply the Full Day logic for startDate and endDate
handleFullDayCheckbox("Start");
handleFullDayCheckbox("End");
// Function to load calendars from the Google Calendar API
function loadCalendars() {
$("#node-input-calendar").empty().append($('<option>').text("Loading calendars..."));
$.getJSON('google-calendar/get-calendars', { configNodeId: $("#node-input-google").val() }, function(data) {
$("#node-input-calendar").empty();
$("#calendar-table tbody").empty();
data.items.forEach(function(calendar) {
$("#node-input-calendar").append($("<option></option>").attr("value", calendar.id).text(calendar.summary));
// Append a row to the table with Calendar Name and Calendar ID
$("#calendar-table tbody").append(
$("<tr>")
.append($("<td>").text(calendar.summary)) // Calendar Name
.append($("<td>").text(calendar.id)) // Calendar ID
);
});
if (node.calendar) {
$("#node-input-calendar").val(node.calendar);
}
}).fail(function(jqXHR, textStatus, errorThrown) {
$("#node-input-calendar").empty().append($('<option>').text("Failed to load calendars"));
RED.notify("Failed to load calendars: " + errorThrown, "error");
});
}
// Detect changes in the Google configuration and handle it
function handleGoogleConfigChange(googleConfigId) {
if (!googleConfigId || googleConfigId === "_ADD_") {
$("#node-input-calendar").empty().append($('<option>').text("Please configure Google Calendar first."));
$("#node-input-calendar").prop('disabled', true);
} else {
loadCalendars(); // Load the calendars when Google Config is selected
$("#node-input-calendar").empty().append($('<option>').text("Select a calendar..."));
$("#node-input-calendar").prop('disabled', false);
}
}
// Apply initial logic for the calendar dropdown
let googleConfigId = $("#node-input-google").val();
handleGoogleConfigChange(googleConfigId);
// Load calendars when the calendar dropdown is focused
$("#node-input-calendar").one('focus', function() {
loadCalendars();
});
// Detect changes in the Google configuration
$("#node-input-google").on('change', function() {
googleConfigId = $(this).val();
handleGoogleConfigChange(googleConfigId);
});
},
// Save the node's configuration, including the Full Day checkboxes
oneditsave: function() {
const node = this;
// Store the value of the checkboxes and dates
node.fullDayStart = $("#node-input-fullDayStart").is(":checked");
node.fullDayEnd = $("#node-input-fullDayEnd").is(":checked");
node.startDate = $("#node-input-startDate").val();
node.endDate = $("#node-input-endDate").val();
node.calendar = $("#node-input-calendar").val();
}
});
</script>
<!-- HTML form template for the Google Calendar Events node -->
<script type="text/html" data-template-name="google-calendar-events">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name">
</div>
<div class="form-row">
<label for="node-input-google"><i class="fa fa-cogs"></i> Google Calendar Config</label>
<select id="node-input-google"></select>
</div>
<div class="form-row">
<label for="node-input-calendar"><i class="fa fa-calendar"></i> Calendar</label>
<select id="node-input-calendar"></select> <!-- Dropdown to select the specific calendar -->
</div>
<!-- Full Day checkbox and Start Date on one line -->
<div class="form-row">
<label style="display: inline-block; width: auto;">
<input type="checkbox" id="node-input-fullDayStart" style="vertical-align: middle;"> Full Day
</label>
<label for="node-input-startDate" style="margin-left: 15px;">
<i class="fa fa-clock-o"></i> Start Date
</label>
<input type="datetime-local" id="node-input-startDate" style="display: inline-block; width: auto;">
</div>
<!-- Full Day checkbox and End Date on one line -->
<div class="form-row">
<label style="display: inline-block; width: auto;">
<input type="checkbox" id="node-input-fullDayEnd" style="vertical-align: middle;"> Full Day
</label>
<label for="node-input-endDate" style="margin-left: 15px;">
<i class="fa fa-clock-o"></i> End Date
</label>
<input type="datetime-local" id="node-input-endDate" style="display: inline-block; width: auto;">
</div>
<div class="form-tips">
<p>You can override : <BR> - the Calendar (get the calendar ID from the table), <BR> - Start Date (format : YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS), <BR> - and End Date (format : YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS) <BR> <BR> by setting <code>msg.calendar</code>, <code>msg.start</code>, and <code>msg.end</code>.</p>
</div>
<!-- Table to display Calendar Names and IDs -->
<div class="form-row">
<table id="calendar-table" style="width:100%; border-collapse:collapse; margin-top: 10px;">
<thead>
<tr>
<th style="text-align:left; border-bottom:1px solid #ccc;">Calendar Name</th>
<th style="text-align:left; border-bottom:1px solid #ccc;">Calendar ID</th>
</tr>
</thead>
<tbody>
<!-- Rows will be dynamically inserted here -->
</tbody>
</table>
</div>
</script>