-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
235 lines (213 loc) · 9.45 KB
/
index.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap-notify@3/bootstrap-notify.min.js"></script>
<script src="https://use.fontawesome.com/releases/v5.15.4/js/all.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootswatch@4/dist/materia/bootstrap.min.css" rel="stylesheet" />
<style>body{padding-top:40px}</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-12">
<div class="card border-light mb-3">
<div class="card-header">
<span class="fas fa-users"></span> <span class="fas fa-angle-right"></span> <span class="fas fa-users"></span> Google Group Duplicator
<div class="float-right">
<button type="button" id="refresh" class="btn btn-primary btn-sm"><span class="fas fa-sync"></span></button>
</div>
</div>
<div class="card-body">
<form>
<div class="form-group">
<fieldset>
<label class="control-label" for="groups"><span class="fas fa-users"></span> Group to Copy From</label>
<select id="groups" class="custom-select">
<option selected>Loading...</option>
</select>
</fieldset>
</div>
<div class="row text-center" style="padding-bottom:24px">
<div class="col-12">
<span class="fas fa-long-arrow-alt-down fa-3x"></span>
</div>
</div>
<div class="row">
<div id="newGroupIdContainer" class="form-group col">
<fieldset>
<label class="control-label" for="newGroupId" required><span class="fas fa-envelope"></span> New Group ID</label>
<input id="newGroupId" class="form-control" placeholder="[email protected]" />
</fieldset>
</div>
<div id="newGroupNameContainer" class="form-group col" >
<fieldset>
<label class="control-label" for="newGroupName" required><span class="fas fa-address-card"></span> New Group Name</label>
<input id="newGroupName" class="form-control" placeholder="Name" />
</fieldset>
</div>
</div>
<div class="row">
<div id="newGroupNameContainer" class="form-group col" >
<fieldset>
<label class="control-label" for="newGroupDescription" required><span class="fas fa-pencil-alt"></span> New Group Description</label>
<textarea id="newGroupDescription" rows="3" class="form-control" placeholder="Description"></textarea>
</fieldset>
</div>
</div>
<div class="col-auto">
<button id="submit" type="submit" class="btn btn-block btn-primary">Duplicate Group</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function() {
getGroups();
disableSubmit("Invalid Group ID");
});
// the function to update the "groups" dropdown.
// happens on load and when the refresh button is pressed.
function getGroups() {
console.log("Updating all groups...");
disableRefresh();
$('#groups').find('option').remove().append('<option selected>Refreshing...</option>');
google.script.run.withSuccessHandler(updateGroupsDropdown).withFailureHandler(handleError).getAllGroups();
}
// getGroups() has run successfully
// this updates the dropdown with all of the groups as choices
function updateGroupsDropdown(groups) {
$('#groups').find('option').remove();
for (var i in groups) {
$('#groups').append('<option value="' + groups[i].email + '">' + groups[i].name + ' (' + groups[i].email + ')</option>');
}
console.log("Updated!");
var groupId = $('#groups option:selected').val();
getGroupInfo();
enableRefresh();
}
// handles all errors that might happen from any of the server-side apps script functions
function handleError(error) {
$.notify({
"icon": "fas fa-exclamation-triangle",
"delay": 30000,
"message": error
},{
"type": "danger"
});
enableSubmit();
enableRefresh();
}
// this is what happens when the form is submitted.
// disable the buttons, get all of the field values,
// then run copyGroup() server-side to create the new group.
function copySubmit(event) {
event.preventDefault();
disableSubmit('Duplicate Group');
disableRefresh();
var groupId = $('#groups option:selected').val();
var newGroupId = $('#newGroupId').val();
var newGroupName = $('#newGroupName').val();
var newGroupDescription = $('#newGroupDescription').val();
console.log("Creating " + newGroupName + " <" + newGroupId + "> from " + groupId + "...");
google.script.run.withSuccessHandler(copySuccessful).withFailureHandler(handleError).copyGroup(groupId, newGroupId, newGroupName, newGroupDescription);
}
// once the above function runs successfully,
// this resets the form, sends a "success" notification,
// and re-enables the buttons
function copySuccessful(newGroup) {
console.log("Group created successfully!");
$.notify({
"icon": "fas fa-thumbs-up",
"delay": 30000,
"message": "New group created!"
},{
"type": "success"
});
enableSubmit();
enableRefresh();
}
// every time the textboxes get a new update,
// check to see if it matches regex for your domain's email address
$('#newGroupId, #newGroupDescription').keyup(function() {
verify();
});
// if the id and description are valid, enable the buttons and highlight the input green.
// if not, disable and highlight red.
function verify() {
var newGroupId = $('#newGroupId').val();
$('#newGroupIdContainer').removeClass('has-success has-danger');
// check to see if the new group id exists as a value in any of the existing options in the groups dropdown
// if it does, that means that the id already exists in another group
if ($('#groups option[value="' + newGroupId + '"]').length > 0) {
$('#newGroupIdContainer').addClass('has-danger');
disableSubmit("ID Must Be Unique");
return;
}
var domainEmail = /^[a-zA-Z0-9]+@yourdomain\.com$/;
if (!domainEmail.test(newGroupId)) {
$('#newGroupIdContainer').addClass('has-danger');
disableSubmit("Invalid Group ID");
return;
}
$('#newGroupIdContainer').addClass('has-success');
$('#newDescription').removeClass('has-success has-danger');
if ($('#newGroupDescription').val().length >= 4096) {
$('#newDescription').addClass('has-danger');
disableSubmit("Description is Too Long");
return;
}
$('#newDescription').addClass('has-success');
enableSubmit();
}
// presumably, nobody would be using this tool unless they want the new group
// to be pretty similar to the old, existing group they're copying from.
// when a different group is selected from the dropdown, we want to get it's settings.
// then, we populate the new group's fields in the form with the existing group's info,
// to make changing the details trivial.
$('#groups').change(function() {
getGroupInfo();
});
// when a different group is selected, get it's info...
function getGroupInfo() {
var groupId = $('#groups option:selected').val();
console.log("Getting group info for " + groupId);
google.script.run.withSuccessHandler(fillInNewGroupInfo).withFailureHandler(handleError).getGroup(groupId);
}
// ...and fill it into the form.
function fillInNewGroupInfo(group) {
$('#newGroupId').val(group.email);
$('#newGroupName').val(group.name);
$('#newGroupDescription').val(group.description);
verify();
}
//
//
function disableSubmit(buttonText) {
$('#submit').addClass('disabled').html(buttonText).off().click(function(event){event.preventDefault()});
}
function enableSubmit() {
$('#submit').removeClass('disabled').html('Duplicate Group').off().click(copySubmit);
}
function disableRefresh() {
$('#refresh').addClass('disabled').html('<span class="fas fa-spin fa-sync"></span>').off().click(function(event){event.preventDefault()});
}
function enableRefresh() {
$('#refresh').removeClass('disabled').html('<span class="fas fa-sync"></span>').off().click(refresh);
}
//
//
// the click listener for the refresh
function refresh() {
if (!$('#refresh').hasClass('disabled')) {
getGroups();
}
}
</script>
</body>
</html>