-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmadScripts.js
More file actions
151 lines (142 loc) · 4.99 KB
/
Copy pathmadScripts.js
File metadata and controls
151 lines (142 loc) · 4.99 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
//madScripts.js
//Author:Alexander Corley
var story;
/**
* @brief prepare the page
*/
$(document).ready(function(){
var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
if (sPage == "madLibs.html") {
var title = getCookie("madLib");
story = getMadLib(title);
fillNavBox();
var inputBox = document.getElementById("getWords");
if (story == undefined) {
var error = document.createElement("p");
error.appendChild(document.createTextNode("Looks like you came here out of order... "
+ "try clicking one of the stories on the side to fix this and move on."));
var errorImg = document.createElement("img");
errorImg.setAttribute("src", "error-512.png");
errorImg.setAttribute("alt", "a picture of something broken");
errorImg.setAttribute("style", "width:50%;")
inputBox.setAttribute("style", "text-align:center;");
inputBox.appendChild(error);
inputBox.appendChild(errorImg);
}
else {
fillInputBox(title, inputBox);
document.getElementById("title").innerHTML = story.title + " Mad Lib™";
}
} else fillNavBox();
});
/**
* @brief fills in the navigation box automatically with the stories that the user can choose
*/
function fillNavBox() {
var nav = document.getElementById("nav");
var list1 = document.createElement("ul");
var listItem = document.createElement("li");
listItem.appendChild(document.createTextNode("Stories"));
list1.appendChild(listItem);
var list2 = document.createElement("ul");
madLibs.sort(compareTo);
for (var i = 0; i < madLibs.length; i++) {
//add story titles to nav bar
listItem = document.createElement("li");
listItem.appendChild(document.createTextNode(madLibs[i].title));
if (story != undefined && story.title == madLibs[i].title) listItem.classList.add("active");
listItem.setAttribute("onclick", "setCookie(\"madLib\", this.innerHTML, .5);window.location = \"madLibs.html\";");
list2.appendChild(listItem);
}
list1.appendChild(list2);
listItem = document.createElement("li");
var linkk = document.createElement("a");
var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
if (sPage == "madLibs.html") linkk.setAttribute("href", "index.html");
else linkk.setAttribute("href", "#");
linkk.appendChild(document.createTextNode("Home"));
listItem.appendChild(linkk);
list1.appendChild(listItem);
nav.appendChild(list1);
}
/**
* @brief generate the input boxes for the user to enter words into
*/
function fillInputBox(title, inputBox) {
var para;
var wordIn;
var textNode;
for (var index = 0; index < story.words.length; index++) {
//create word inputs
para = document.createElement("p");
wordIn = document.createElement("input");
para.appendChild(document.createTextNode(story.words[index]));
wordIn.classList.add("wordInputs");
wordIn.setAttribute("placeholder", "Enter a word HERE");
para.appendChild(wordIn);
inputBox.appendChild(para);
}
//create submit button
var butt = document.createElement("button");
butt.appendChild(document.createTextNode("Mad-Lib!!!"));
butt.classList.add("submitButton");
butt.setAttribute("onclick", "fillMadLib(); document.getElementById(\"getWords\").appendChild(document.createTextNode(\"Story MadLib'ed\"));");
inputBox.appendChild(butt);
}
/**
* @brief fill in the mad lib for the user and display it
*/
function fillMadLib() {
var storyArray = story.Story.split("[Word Not Submitted]");
var outputBox = document.getElementById("displayLib");
var spann;
var textt;
for (var i = 0; i < storyArray.length; i++) {
textt = document.createTextNode(storyArray[i]);
outputBox.appendChild(textt);
if (i == storyArray.length-1) {
break;
}
spann = document.createElement("span");
spann.classList.add("wordOutputs");
outputBox.appendChild(spann);
}
var storyText = document.createTextNode(story.Story);
var inputs = document.getElementsByClassName("wordInputs");
var outputs = document.getElementsByClassName("wordOutputs");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].value.trim() != "")
outputs[i].innerHTML = inputs[i].value;
else outputs[i].innerHTML = "Word Not Submitted";
}
}
/**
* @brief save a cookie
* @source http://www.w3schools.com/js/js_cookies.asp
*/
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
/**
* @brief get a cookie from saved cookies
* @source http://www.w3schools.com/js/js_cookies.asp
*/
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length,c.length);
}
}
return "";
}