-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
vogsphere.js
363 lines (312 loc) · 10.5 KB
/
vogsphere.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
==========
VOGSPHERE
The Claim Code Generator
A Magrathea project
https://github.com/magratheaguide/vogsphere
-----
Purpose:
Convert member-provided answers from the associated form into the code admins need to update the various claims lists.
How To Guide:
https://github.com/magratheaguide/vogsphere/docs/01-getting-started.md
==========
*/
(function () {
"use strict";
const runBtn = document.getElementById("js-vogsphere__run");
// get a handle on the place the resulting code needs to go
const resultBox = document
.getElementById("js-vogsphere__result")
.querySelector("code"); // TODO: demo version, comment out when actually using
// .querySelector("td#code"); // TODO: real version, works with Jcink's [code] tags
const formId = runBtn.getAttribute("form");
const form = document.getElementById(formId);
const newline = "\n";
// doHTML codes
// square brackets must be escaped or else they get processed right away by Jcink
const leftBracket = "[";
const rightBracket = "]";
function openDohtml(tag) {
return `${leftBracket}${tag}${rightBracket}`;
} // returns [tag]
function openEqualsDohtml(tag, param) {
return `${leftBracket}${tag}="${param}"${rightBracket}`;
} // returns [tag=param]
function closeDohtml(tag) {
return `${leftBracket}/${tag}${rightBracket}`;
} // returns [/tag]
const postBbcodeName = "pathfinder"; // TODO: should be the name of your site's default bbcode for posting
const postBbcodeOpen = openDohtml(postBbcodeName);
const postBbcodeClose = closeDohtml(postBbcodeName);
const codeBbcodeOpen = openDohtml("code");
const codeBbcodeClose = closeDohtml("code");
function formatBold(content) {
return `${openDohtml("b")}${content}${closeDohtml("b")}`;
}
function formatUrl(address) {
return `${openEqualsDohtml("url", `${address}`)}${address}${closeDohtml(
"url"
)}`;
}
// TODO: names of form fields (as specified by the "name" attribute in the html)
const expectedFormFields = {
text: [
"characterName",
"faceClaim",
"labDescription",
"labName",
"memberGroup",
"occupation",
"profileUrl",
"requester",
"requestLocation",
"writerAlias",
],
bool: ["isLabLead", "isNewLab", "isRequested"],
};
let input = {};
let errors = [];
// how text fields are processed
class textInput {
constructor(name) {
this.value = form.elements[name].value;
this.required = form.elements[name].required;
}
}
// how boolean fields are processed
class boolInput {
constructor(name) {
this.value = form.elements[name].value === "true";
}
}
// TODO: update/create classes to match the actual claim codes needed for the site
class faceClaim {
constructor(
characterName,
faceClaim,
memberGroup,
profileUrl,
writerAlias
) {
this.code = `<div class="claim-row">
<span class="detail-alitus"><b>${faceClaim}</b></span> as
<span class="detail-alitus no-bg text-color-${memberGroup}">
<a href="${profileUrl}" title="played by ${writerAlias}">${characterName}</a>
</span>
</div>`;
}
}
class occupationClaim {
constructor(characterName, memberGroup, occupation, profileUrl) {
this.code = `<div class="list-item level-3">
<span class="list-taken-by text-color-${memberGroup}">
<a href="${profileUrl}">${characterName}</a>
</span> ${
occupation === ""
? ""
: `<span class="list-aside">(${occupation})</span>`
}
</div>`;
}
}
class labClaim {
constructor(isLabLead, labName, labDescription, occupationClaim) {
// labs are in the occupation claim list, so the occupation claim code is inserted into the lab claim
this.code = `<div class="list-item level-1">
<span class="heading-dinorwic">${labName}</span>
</div>
<div class="textblock-aniak left list-item level-2">
${labDescription}
</div>
<div class="list-item level-2">
<span class="heading-dollfus">Lead</span>
<span class="pill-gusev">Limit 1</span>
</div>
${isLabLead ? occupationClaim.code : ""}
<div class="list-item level-2">
<span class="heading-dollfus">Staff</span>
</div>
${isLabLead ? "" : occupationClaim.code}`;
}
}
// TODO: update to create the post you want members to reply with
class claimPost {
constructor(
faceClaim,
labClaim,
occupationClaim,
labName,
memberGroup,
requester,
requestLocation,
isLabLead,
isNewLab,
isRequested
) {
// prettier-ignore
this.content = `${postBbcodeOpen}
Face claim:
${codeBbcodeOpen}${faceClaim.code}${codeBbcodeClose}
Occupation claim: ${
memberGroup == "scientist"
? `
Add to ${labName} as ${isLabLead ? "Lead" : "Staff"}`
: ""
}
${codeBbcodeOpen}${
isNewLab ? labClaim.code : occupationClaim.code
}${codeBbcodeClose} ${
isRequested
? `
${formatBold("REQUESTED CHARACTER")} ${
requester
? `
Requested by: ${requester}`
: ""
} ${
requestLocation
? `
Request location: ${
requestLocation &&
/^http/.test(requestLocation)
? formatUrl(requestLocation)
: requestLocation
}`
: ""
}`
: ""
}
${postBbcodeClose}`;
}
}
function isInForm(name) {
return !!form.elements[name];
}
function resetGenerator() {
// clear any past results
resultBox.innerHTML = "";
// clear past errors
errors = [];
}
function getInput() {
for (const type in expectedFormFields) {
expectedFormFields[type].forEach((fieldName) => {
if (!isInForm(fieldName)) {
errors.push(
`ERROR: Could not find field with name "${fieldName}" in form. Contact admin`
);
} else {
switch (type) {
case "text":
input[fieldName] = new textInput(fieldName);
break;
case "bool":
input[fieldName] = new boolInput(fieldName);
break;
default:
errors.push(
`ERROR: Form field type "${type}" is unsupported. Contact admin`
);
break;
}
}
});
}
}
function validateInput() {
// check that required input is present
for (const x in input) {
if (input[x].required && !input[x].value) {
errors.push(`ERROR: Missing ${x}`);
}
}
// check that information about requester or request location is provided for requested characters
if (
input.isRequested.value &&
!input.requester.value &&
!input.requestLocation.value
) {
errors.push(
"ERROR: Requested character, need requester name or request location"
);
}
// TODO: check for context-sensitive errors (e.g. if member group is A, need to also have provided B)
if (
input.memberGroup.value == "scientist" &&
input.isNewLab.value &&
!input.labDescription.value
) {
errors.push("ERROR: Missing lab description");
}
//Check for selection on Member group
if (!input.memberGroup.value) {
errors.push("ERROR: Missing Member group selection");
}
if (input.memberGroup.value == "scientist" && !input.labName.value) {
errors.push("ERROR: Missing name of lab");
}
}
// TODO: list all the different claims you need and the pieces they need to be filled in
function fillInClaims() {
let completeFaceClaim = new faceClaim(
input.characterName.value,
input.faceClaim.value,
input.memberGroup.value,
input.profileUrl.value,
input.writerAlias.value
);
let completeOccupationClaim = new occupationClaim(
input.characterName.value,
input.memberGroup.value,
input.occupation.value,
input.profileUrl.value
);
// note that the labClaim needs to be handed the occupationClaim
let completeLabClaim = new labClaim(
input.isLabLead.value,
input.labName.value,
input.labDescription.value,
completeOccupationClaim
);
return {
faceClaim: completeFaceClaim,
occupationClaim: completeOccupationClaim,
labClaim: completeLabClaim,
};
}
// TODO: Update to match class claimPost
function compileClaimPost(claims) {
let post = new claimPost(
claims.faceClaim,
claims.labClaim,
claims.occupationClaim,
input.labName.value,
input.memberGroup.value,
input.requester.value,
input.requestLocation.value,
input.isLabLead.value,
input.isNewLab.value,
input.isRequested.value
);
return post.content;
}
function generateClaim() {
let claims;
let post;
resetGenerator();
getInput();
validateInput();
// stop if input errors were found
if (errors.length > 0) {
errors.forEach(
(element) => (resultBox.textContent += element + newline)
);
return;
}
claims = fillInClaims();
post = compileClaimPost(claims);
resultBox.textContent = post;
return;
}
runBtn.addEventListener("click", generateClaim, false);
})();