-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnaval_letter.js
413 lines (367 loc) · 13.4 KB
/
naval_letter.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
const docx = require("docx");
const fs = require("fs");
module.exports = {
generateDoc: generate,
addVia: AddViaTextBox,
addRef: AddRefTextBox,
addEnc: AddEnclTextBox,
addBody: AddBodyTextBox,
addCopy: AddCopyTextBox,
showHideDiv: ShowHideDiv,
removeVia: RemoveViaTextBox,
removeEnc: RemoveEnclTextBox,
removeRef: RemoveRefTextBox,
removeBody: RemoveBodyTextBox,
removeCopy: RemoveCopyTextBox
}
function generate() {
var filename = document.getElementById("filename").value;
if (filename === "") {
filename = "NavalLetterGeneratedFile.docx";
}
if (!filename.endsWith(".docx")) {
filename = filename + ".docx";
}
const doc = new docx.Document();
var section = {
properties: {},
children: [],
margins: {
top: "1in",
bottom: "1in",
right: "1in",
left: "1in",
}
};
section.headers = {
default: new docx.Header({
children: makeHeaderEntities(doc,
document.getElementById("line1").value,
document.getElementById("line2").value,
document.getElementById("line3").value
),
}),
};
// document content
section.children.push.apply(section.children, makeHeaderSection(
document.getElementById("ssic").value,
document.getElementById("reply").value,
document.getElementById("date").value
));
section.children.push.apply(section.children, makeReplyBlock(
document.getElementById("from").value,
document.getElementById("to").value,
document.getElementById("subj").value,
//Get Vias, there can be multiple vias
document.getElementsByName("ViaTextBox"),
//Get Refs, there can be multiple refs
document.getElementsByName("RefTextBox"),
//Get Enclosures, there can be multiple encls.
document.getElementsByName("EnclTextBox")
));
// references, enclosures, vias here
bodyTexts = Array.from(document.querySelectorAll('[id=BodyBlocks]'));
bodyLevels = Array.from(document.querySelectorAll('[id=BodyLevel]'));
section.children.push.apply(section.children, makeBodies(bodyTexts, bodyLevels));
section.children.push.apply(section.children, makeSignatureSection(document.getElementById("sig").value));
// copy to's here
doc.addSection(section);
docx.Packer.toBlob(doc).then(blob => {
saveAs(blob, filename);
console.log("document downloaded");
});
}
function makeTextRun(text, font, size) {
return new docx.TextRun({
text: text,
font: font,
size: size,
});
}
function makeDefaultTextRun(text) {
return new docx.TextRun({
text: text,
font: "Times New Roman",
size: 24,
});
}
function makeSignatureSection(sig) {
paragraphs = [];
// two returns (body paragaph will leave one return above)
paragraphs.push(new docx.Paragraph({ text: "" }));
paragraphs.push(new docx.Paragraph({ text: "" }));
paragraphs.push(new docx.Paragraph({
children: [makeDefaultTextRun(sig)],
indent: {
start: "3.25in",
},
}));
return paragraphs;
}
function makeBodies(bodyTexts, bodyLevels) {
bodyZipped = bodyTexts.map(function (e, i) {
return [e, bodyLevels[i]];
});
paragraphs = [];
topLevelNum = 1;
midLevelNum = 97;
botLevelNum = 1;
bodyZipped.forEach(element => {
var selector = element[1];
var level = selector.options[selector.selectedIndex].value;
if (level === "1") {
var start = topLevelNum.toString() + ". ";
makeBodyPara(paragraphs, start, element[0].value);
// update indenting
topLevelNum++;
midLevelNum = 97;
botLevelNum = 1;
} else if (level == "2") {
var start = " " + String.fromCharCode(midLevelNum) + ". ";
makeBodyPara(paragraphs, start, element[0].value);
// update indenting
midLevelNum++;
botLevelNum = 1;
} else if (level == "3") {
var start = " (" + botLevelNum.toString() + ") ";
makeBodyPara(paragraphs, start, element[0].value);
// update indenting
botLevelNum++;
}
});
return paragraphs;
}
function makeBodyPara(paragraphs, start, value) {
var para = new docx.Paragraph({
children: [makeDefaultTextRun(start + value)],
});
paragraphs.push(para);
paragraphs.push(new docx.Paragraph({ text: "" }));
}
function makeReplyBlock(from, to, subj, vias, refs, encls) {
var output = [];
output.push(new docx.Paragraph({ children: [makeDefaultTextRun("From: " + from)], }));
output.push(new docx.Paragraph({ children: [makeDefaultTextRun("To: " + to)], }));
//Add Vias
//Check if via yes box is checked
if (document.getElementById("rad1").checked) {
console.log("Num vias detected: " + vias.length );
for (i = 0; i < vias.length; i++) { //Add a via line for every via Box
if(vias.length == 1) {//If only 1 via, no numbers
output.push(new docx.Paragraph({ children: [makeDefaultTextRun("Via: " + vias[i].value)], }));
}
else if(vias.length > 1) {//2 or more vias, add numbers
if(i == 0) {
output.push(new docx.Paragraph({ children: [makeDefaultTextRun("Via: (" + (i+1) + ") " + vias[i].value)], }));
} else {
output.push(new docx.Paragraph({ children: [makeDefaultTextRun("(" + (i+1) + ") " + vias[i].value)],
indent: {
start: ".5in",
}
}));
}
}
}
}
//Add Refs
//Check if refs yes box is checked
if (document.getElementById("rad3").checked) {
console.log("Num refs detected: " + refs.length );
for (i = 0; i < refs.length; i++) { //Add a via line for every via Box
var outputLetterBlock;
if(i < 26) {
var letter = String.fromCharCode(i+97); //Add 97 to get to ASCI code a, which is (97)
outputLetterBlock = "(" + letter + ")";
}
if(i >= 26) {
var column = Math.floor(i / 26);
var remainder = i % 26;
var letter = String.fromCharCode(remainder+97); //Add 97 to get to ASCI code a, which is (97)
outputLetterBlock = "(";
for(y = 0; y < column; y++) {
outputLetterBlock += "a";
}
outputLetterBlock += letter + ")"
}
if(i == 0) {
output.push(new docx.Paragraph({ children: [makeDefaultTextRun("Ref: " + outputLetterBlock + " " + refs[i].value)], }));
} else {
output.push(new docx.Paragraph({ children: [makeDefaultTextRun(outputLetterBlock + refs[i].value)],
indent: {
start: ".5in",
}
}));
}
}
}
//Add enclosures
//Check if encls yes box is checked
if (document.getElementById("rad5").checked) {
console.log("Num encls detected: " + encls.length );
output.push(new docx.Paragraph({ text: "" }));
for (i = 0; i < encls.length; i++) { //Add a via line for every via Box
if(i == 0) {
output.push(new docx.Paragraph({ children: [makeDefaultTextRun("Encl: (" + (i+1) + ") " + encls[i].value)], }));
} else {
output.push(new docx.Paragraph({ children: [makeDefaultTextRun("(" + (i+1) + ") " + encls[i].value)],
indent: {
start: ".5in",
}
}));
}
}
}
output.push(new docx.Paragraph({ text: "" }));
output.push(new docx.Paragraph({ children: [makeDefaultTextRun("Subj: " + subj.toUpperCase())], }));
output.push(new docx.Paragraph({ text: "" }));
return output;
}
function makeHeaderSection(ssic, replyCode, date) {
return [
new docx.Paragraph({ text: "" }),
new docx.Paragraph({
children: [makeTextRun("IN REPLY REFER TO:", "Times New Roman", 10)],
indent: {
start: "5.19in",
}
}),
new docx.Paragraph({
children: [makeDefaultTextRun(ssic)],
indent: {
start: "5.19in",
}
}),
new docx.Paragraph({
children: [makeDefaultTextRun(replyCode)],
indent: {
start: "5.19in"
}
}),
new docx.Paragraph({
children: [makeDefaultTextRun(date)],
indent: {
start: "5.19in"
}
}),
new docx.Paragraph({ text: "" }),
];
}
function makeHeaderEntities(doc, line1, line2, line3) {
// return a list
entities = [];
const image = docx.Media.addImage(doc, fs.readFileSync("../../images/DoD Header Seal.png"), 101.92, 100, {
floating: {
horizontalPosition: {
offset: 457200,
},
verticalPosition: {
offset: 457200,
},
},
});
entities.push(new docx.Paragraph(image));
entities.push(new docx.Paragraph({
children: [new docx.TextRun({
text: "UNITED STATES MARINE CORPS",
bold: true,
font: "Times New Roman",
size: 20,
})],
alignment: docx.AlignmentType.CENTER,
}));
entities.push(new docx.Paragraph({
children: [new docx.TextRun({
text: line1,
font: "Times New Roman",
size: 16,
})],
alignment: docx.AlignmentType.CENTER,
}));
entities.push(new docx.Paragraph({
children: [new docx.TextRun({
text: line2,
font: "Times New Roman",
size: 16,
alignment: docx.AlignmentType.CENTER,
})],
alignment: docx.AlignmentType.CENTER,
}));
entities.push(new docx.Paragraph({
children: [new docx.TextRun({
text: line3,
font: "Times New Roman",
size: 16,
alignment: docx.AlignmentType.CENTER,
})],
alignment: docx.AlignmentType.CENTER,
}));
return entities;
}
function GetDynamicViaTextBox(value) {
return '<input name = "ViaTextBox" size="60" type="text" value = "' + value + '" >' +
'<input type="button" value="Remove" onclick = "generatorBundle.removeVia(this)" >'
}
function ShowHideDiv(Id, Id2) {
var chkYes = document.getElementById(Id);
var dvPassport = document.getElementById(Id2);
dvPassport.style.display = chkYes.checked ? "block" : "none";
}
function AddViaTextBox() {
var div = document.createElement('DIV');
div.innerHTML = GetDynamicViaTextBox("");
document.getElementById("ViaTextBoxContainer").appendChild(div);
}
function RemoveViaTextBox(div) {
document.getElementById("ViaTextBoxContainer").removeChild(div.parentNode);
}
//Ref Text Boxes
function GetDynamicRefTextBox(value) {
return '<input name = "RefTextBox" size="60" type="text" value = "' + value + '" >' +
'<input type="button" value="Remove" onclick = "generatorBundle.removeRef(this)" >'
}
function AddRefTextBox() {
var div = document.createElement('DIV');
div.innerHTML = GetDynamicRefTextBox("");
document.getElementById("RefTextBoxContainer").appendChild(div);
}
function RemoveRefTextBox(div) {
document.getElementById("RefTextBoxContainer").removeChild(div.parentNode);
}
//Encl Text Boxes
function GetDynamicEnclTextBox(value) {
return '<input name = "EnclTextBox" size="60" type="text" value = "' + value + '" >' +
'<input type="button" value="Remove" onclick = "generatorBundle.removeEnc(this)" >'
}
function AddEnclTextBox() {
var div = document.createElement('DIV');
div.innerHTML = GetDynamicEnclTextBox("");
document.getElementById("EnclTextBoxContainer").appendChild(div);
}
function RemoveEnclTextBox(div) {
document.getElementById("EnclTextBoxContainer").removeChild(div.parentNode);
}
//Copy Text Boxes
function GetDynamicCopyTextBox(value) {
return '<input name = "CopyTextBox" size="60" type="text" value = "' + value + '" >' +
'<input type="button" value="Remove" onclick = "generatorBundle.removeCopy(this)" >'
}
function AddCopyTextBox() {
var div = document.createElement('DIV');
div.innerHTML = GetDynamicCopyTextBox("");
document.getElementById("CopyTextBoxContainer").appendChild(div);
}
function RemoveCopyTextBox(div) {
document.getElementById("CopyTextBoxContainer").removeChild(div.parentNode);
}
//Body Text Boxes
function GetDynamicBodyTextBox(value) {
return '<textarea rows = "8" cols = "80" id="BodyBlocks" name="BodyBlocks"> </textarea>' + '<label for = "bodylvl"> Select the body level: </label>' + '<select id="BodyLevel" name="BodyLevel" >' + '<option SELECTED value=1>1</option>' + '<option value=2>2</option>' + '<option value=3>3</option>' + '</select>' + '<input type="button" value="Remove Paragraph" onclick = "generatorBundle.removeBody(this)" >'
}
function AddBodyTextBox() {
var div = document.createElement('DIV');
div.innerHTML = GetDynamicBodyTextBox("");
document.getElementById("BodyTextBoxContainer").appendChild(div);
}
function RemoveBodyTextBox(div) {
document.getElementById("BodyTextBoxContainer").removeChild(div.parentNode);
}