This repository was archived by the owner on Dec 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbase.js
More file actions
284 lines (250 loc) · 8.29 KB
/
base.js
File metadata and controls
284 lines (250 loc) · 8.29 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
var snapURL = location.origin + '/snap/snap.html',
snapDevURL = location.origin + '/snapsource/dev/snap.html',
baseURL = location.href.replace(/(.*)\/.*/, '$1'),
modules = [], // compatibility with cloud.js
nop = function () {},
localizer = new Localizer(),
buttonDefaults =
{ done: { text: 'Ok', default: true }, cancel: { text: 'Cancel' } };
function getUrlParameter (param) {
var regex = new RegExp('[?&]' + param + '(=([^&#]*)|&|#|$)'),
results = regex.exec(location.href);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
};
function pageUser () {
return getUrlParameter('user');
};
function pageProject () {
return getUrlParameter('project');
};
// Permissions
function hasAnyOfRoles (roleList) {
return roleList.indexOf(sessionStorage.role) > -1
};
// Data insertion
function fillVisitorFields () {
if (sessionStorage.username) {
document.querySelectorAll('.visitor').forEach(function (each) {
each.innerHTML = escapeHtml(sessionStorage.username);
});
}
};
function fillUsernameFields () {
var username = pageUser();
if (username) {
document.querySelectorAll('.username').forEach(function (each) {
each.innerHTML = escapeHtml(username);
});
}
};
function setTitle (newTitle) {
document.title = newTitle;
};
// Element creation
function authorSpan (author, newTab) {
var span = document.createElement('span');
span.classList.add('author');
span.innerHTML = localizer.localize(' by ');
span.appendChild(userAnchor(author, newTab));
return span;
};
function userAnchor (username, newTab) {
var anchor = document.createElement('a');
anchor.href = 'user?user=' + encodeURIComponent(username);
anchor.target = newTab ? '_blank' : '';
anchor.innerHTML = '<strong>' + escapeHtml(username) + '</strong>';
return anchor;
};
function flagCountSpan (count) {
var span = document.createElement('span');
span.innerHTML =
'Flagged <strong>' + count + '</strong> time' +
((count > 1) ? 's' : '');
return span;
};
function flagSpan (flag) {
var span = document.createElement('span'),
dateSpan = document.createElement('span'),
headerSpan = document.createElement('span'),
reasonSpan = document.createElement('span'),
flaggedText = document.createElement('span'),
onText = document.createElement('span'),
removeAnchor = document.createElement('a'),
icon = document.createElement('i');
span.classList.add('flag');
headerSpan.classList.add('header');
reasonSpan.classList.add('reason');
reasonSpan.classList.add('warning');
reasonSpan.innerText = {
coc: 'Code of Conduct violation',
hack: 'Security vulnerability exploit',
dmca: 'DMCA policy violation'
}[flag.reason];
headerSpan.appendChild(reasonSpan);
icon.classList.add('fas');
icon.classList.add('fa-times-circle');
removeAnchor.classList.add('remove');
removeAnchor.classList.add('clickable');
removeAnchor.appendChild(icon);
removeAnchor.onclick = function () {
SnapCloud.withCredentialsRequest(
'DELETE',
'/projects/' +
encodeURIComponent(pageUser()) + '/' +
encodeURIComponent(pageProject()) +
'/flag?flagger=' + encodeURIComponent(flag.username),
function () {
span.classList.add('warning-flash');
setTimeout( function () { span.remove(); }, 1000);
},
genericError,
'Could not unflag project'
);
};
headerSpan.appendChild(removeAnchor);
span.appendChild(headerSpan);
flaggedText.innerText = localizer.localize('Flagged');
span.appendChild(flaggedText);
span.appendChild(authorSpan(flag.username, true));
onText.innerText = localizer.localize(' on ');
span.appendChild(onText);
dateSpan.innerHTML = formatDate(flag.created_at);
span.appendChild(dateSpan);
span.title = flag.notes;
return span;
};
function isPublicSpan (isPublic) {
var span = document.createElement('span'),
tooltip = isPublic ?
'This item can be shared via URL' :
'This item is private',
faClass = isPublic ? 'fa-link' : 'fa-unlink';
span.classList.add('is-public');
span.innerHTML = '<small><i class="fas ' + faClass +
'" aria-hidden="true"></i></small>';
span.title = localizer.localize(tooltip);
return span;
};
function isPublishedSpan (isPublished) {
var span = document.createElement('span'),
tooltip = isPublished ?
'This item is published' :
'This item is unpublished',
faClass = isPublished ? 'fa-eye' : 'fa-eye-slash';
span.classList.add('is-published');
span.innerHTML = '<small><i class="fas ' + faClass +
'" aria-hidden="true"></i></small>';
span.title = localizer.localize(tooltip);
return span;
};
function projectCodeButtonSpan (project) {
var span = document.createElement('span');
var tooltip = 'See Code';
span.classList.add('project-code-button');
span.innerHTML = '<a style="color: inherit;" ' +
'target="_blank" rel="noopener noreferrer" ' +
'href="' +
projectURL(
project.username,
project.projectname,
getUrlParameter('devVersion') !== null) +
'&editMode&noRun">' +
'<i class="fas fa-lambda' +
'" aria-hidden="true"></i></a>';
span.title = localizer.localize(tooltip);
return span;
};
function projectURL (author, project, devVersion) {
return (devVersion ? snapDevURL : snapURL) +
'#present:Username=' + encodeURIComponent(author) +
'&ProjectName=' + encodeURIComponent(project);
};
function projectSpan (author, project) {
var span = document.createElement('span');
span.classList.add('project-link');
span.innerHTML =
'<a href="project?user=' + encodeURIComponent(author) +
'&project=' + encodeURIComponent(project) + '">' +
escapeHtml(project) + '</a>';
return span;
};
function collectionURL (author, name) {
return 'collection?' +
SnapCloud.encodeDict({
user: author,
collection: name
});
};
// Error handling
function genericError (errorString, title) {
doneLoading();
alert(
localizer.localize(errorString || 'Unknown error'),
{ title: localizer.localize(title || 'Error')}
);
console.error(errorString);
};
// Page loading
function beganLoading (selector) {
var loader;
if (selector) {
loader = document.createElement('div');
loader.className = 'loader';
loader.innerHTML =
'<i class="fa fa-spinner fa-spin fa-3x" aria-hidden="true"></i>';
document.querySelector(selector).appendChild(loader);
}
};
function doneLoading (selector) {
var element = document.querySelector(
selector ?
(selector + '> .loader') :
'#loading');
localizer.localizePage();
if (element) {
element.hidden = true;
}
};
// Other goodies
function formatDate (dateString) {
return (new Date(dateString + ':00')).toLocaleString(
localizer.locale || 'en-us',
{ month: 'long', day: 'numeric', year: 'numeric' }
);
};
function escapeHtml (text) {
// Based on an answer by Kip @ StackOverflow
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text ? text.replace(/[&<>"']/g, function (m) { return map[m]; }) : ''
};
// JS additions
Array.prototype.sortBy = function (parameter, reverse) {
return this.sort(
function (a, b) {
if (reverse) {
return (a[parameter] > b[parameter]) ? 1 : -1;
} else {
return (a[parameter] > b[parameter]) ? -1 : 1;
}
}
);
};
// CustomAlert helpers
function confirmTitle (title) {
// there's a bug in customalert.js preventing us from
// using a custom title unless we also specify text for
// the ok and cancel buttons
return {
title: localizer.localize(title),
done: localizer.localize('Ok'),
cancel: localizer.localize('Cancel')
};
}