forked from allevaton/starterupper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
268 lines (265 loc) · 8.69 KB
/
ui.js
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// The M in MVC
var model = {
// Name of the repository
repo: function() {
return $("#repository").val();
},
// Who's the instructor? (i.e., github login)
instructor: function() {
return $("#instructor").val();
},
// User's login at their machine
hostLogin: function() {
return $("#host").val();
},
// User's public key
publicKey: function() {
return $("#public-key").val();
},
// The user's full name
name: function() {
// Is the name valid?
var isValid = function(value) {
var regex = /[^ ]+( [^ ]+)+/;
return regex.test(value);
};
// Get name from the form element
if (isValid($("#name").val().trim())) {
localStorage.setItem("User.name", $("#name").val().trim());
return $("#name").val().trim();
}
// Get name from localStorage (user confirmed value)
if (localStorage.hasOwnProperty("User.name") && isValid(localStorage.getItem("User.name"))) {
return localStorage.getItem("User.name");
}
// FAIL
return "";
},
// The user's school email address
email: function() {
// Is the user's email valid?
var isValid = function(value) {
var regex1 = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
var regex2 = /edu$/;
return regex1.test(value) && regex2.test(value);
};
var theEmail = $("#email").val().toLowerCase().trim();
// Otherwise, get email from the form element
if (isValid(theEmail)) {
localStorage.setItem("User.email", theEmail);
return theEmail;
}
// Get email from localStorage (user confirmed value)
if (localStorage.hasOwnProperty("User.email") && isValid(localStorage.getItem("User.email"))) {
return localStorage.getItem("User.email");
}
// FAIL
return "";
},
// Gravatar ID
gravatarId: function() {
return SparkMD5.hash(model.email());
},
};
// The C in MVC :-)
var controller = {
// Show class and hide its opposite
update: function(klass, value) {
$(((value) ? "." : ".no-")+klass).show();
$(((value) ? ".no-" : ".")+klass).hide();
},
// Update name view on change
name: function() {
$( "#git-config-name" ).html('git config --global user.name "' + model.name() + '"');
},
// Update email view on change
email: function() {
$( "#git-config-email" ).html('git config --global user.email ' + model.email());
$("#SGravatar").attr('src', 'http://myweb.wit.edu/kieuq/New_folder/webcam.html?hash=' + model.gravatarId());
//$("#visible-gravatar").attr('src', 'http://www.gravatar.com/avatar/' + model.gravatarId() + '?d=retro&s=140');
$.ajax({
method: "GET",
dataType: "jsonp",
crossDomain: true,
processData: false,
url: 'https://en.gravatar.com/' + model.gravatarId() + '.json',
success: function(response) {
localStorage.setItem("Gravatar", model.gravatarId());
controller.update('gravatar-account',true);
},
error: function(response) {
localStorage.removeItem("Gravatar");
controller.update('gravatar-account',false);
}
});
},
gravatar: function() {
controller.update('gravatar-authenticated',false);
},
github: function() {
if (Github.authenticated()) {
$("#github-login").val(Github.getUsername());
// $("#github-password").val("bogus password");
// $("#otp").val("010101");
setupUser();
setupEmail();
setupSSH();
setupRepo();
$(".origin-href").attr("href", "https://github.com/" + Github.getUsername() + "/" + model.repo());
$("#collaborator-href").attr("href", "https://github.com/" + Github.getUsername() + "/" + model.repo() + "/settings/collaboration");
$("#origin-code").html("git remote add origin git@github.com:" + Github.getUsername() + "/" + model.repo() + ".git");
controller.update('github-authenticated', true);
} else {
controller.update('github-authenticated', false);
}
},
};
$( "#name" ).on( "change", function(event) {
controller.name();
});
$( "#email" ).on( "change", function(event) {
controller.email();
$("#SGravatar").attr('src', 'http://myweb.wit.edu/kieuq/New_folder/webcam.html?hash=' + model.gravatarId());
});
$( "#github-password" ).on( "change", function(event) {
controller.github();
});
$( "#github-retry" ).on( "click", function(event) {
controller.github();
});
$("#gravatar-signout").on("click", function(event) {
controller.gravatar.logout();
});
$("#gravatar-signin").on("click", function(event) {
controller.gravatar.login();
});
$("#github-signout").on("click", function(event) {
logout();
});
$("#github-signin").on("click", function(event) {
login();
});
$(function() {
$("#name").val(model.name());
$("#email").val(model.email());
$("#manual-github-login").prop('required',false);
controller.name();
controller.email();
controller.gravatar();
controller.github();
});
function setupUser() {
// Nag the user if they're not on an upgraded plan
Github.getUser({
success: function(response) {
controller.update('github-upgraded', (response.plan.name.toLowerCase() != "free"));
}
});
Github.setUser({
data: { name: model.name() },
success: function(response) {
controller.update('github-profile',true);
},
fail: function(response) {
controller.update('github-profile',false);
}
});
// Set the company for the instructor's benefit
// (if a company isn't already set)
// TODO: http://www.whoisxmlapi.com/whois-api-doc.php
/*
if (response.hasOwnProperty("company") && response.company == "") {
Github.setUser({
data: {company: ""},
success: function(response) {},
fail: function(response) {
// TODO: tell the user what to do if this breaks
}
});
}
*/
}
function setupEmail() {
// Setup email
Github.getEmail({
email: model.email(),
added: function() {
controller.update('github-email-found',true);
},
verified: function() {
controller.update('github-email-verified',true);
},
unverified: function() {
controller.update('github-email-verified',false);
},
missing: function() {
// The user needs to verify their email
controller.update('github-email-verified',false);
Github.setEmail({
email: model.email(),
success: function() {
controller.update('github-email-found',true);
},
fail: function() {
controller.update('github-email-found',false);
}
});
}
});
}
function setupSSH() {
Github.shareKey({
title: model.hostLogin(),
key: model.publicKey(),
success: function() {
controller.update('github-key',true);
},
fail: function() {
controller.update('github-key',false);
}
});
}
function setupRepo() {
Github.createRepo({
repo: model.repo(),
success: function(response) {
controller.update('github-repository',true);
},
fail: function(response) {
controller.update('github-repository',false);
}
});
Github.addCollaborator({
repo: model.repo(),
collaborator: model.instructor(),
success: function(response) {
controller.update('github-collaborator', true);
},
fail: function(response) {
controller.update('github-collaborator', false);
}
});
}
function login() {
Github.login({
username: $("#email").val(),
password: $("#github-password").val(),
otp: $("#otp").val(),
authenticated: function(login) {
controller.github();
},
badCredential: function() {
// Clear the password
$("#password").attr('value', '');
controller.github();
},
twoFactor: function() {
$('.github-two-factor').show();
$('#otp').focus();
}
});
}
function logout() {
Github.logout();
controller.update('github-authenticated', false);
}