Initial Commit. Basic add and save functionality, no delete.

This commit is contained in:
Robbie Antenesse 2015-10-25 22:41:25 -06:00
commit d2babfd485
3 changed files with 228 additions and 0 deletions

55
css/styles.css Normal file
View File

@ -0,0 +1,55 @@
body {
padding: 0;
margin: 0;
border: none;
}
form {
margin: 15px;
float: left;
}
label {
display: block;
margin-bottom: 10px;
}
label span {
display: block;
font-weight: bold;
}
input, textarea {
display: block;
padding-left: 5px;
}
label textarea {
width: 350px;
height: 200px;
}
#dictionaryContainer {
margin: 15px;
width: 50%;
min-width: 350px;
float: right;
}
entry {
display: block;
margin-bottom: 10px;
}
word {
font-weight: bold;
}
partofspeech {
font-style: italic;
font-size: 10px;
}
longdefinition {
margin-left: 20px;
}

41
index.html Normal file
View File

@ -0,0 +1,41 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>DictionaryBuilder.Windows</title>
<!-- DictionaryBuilder.Windows references -->
<link href="css/styles.css" rel="stylesheet" />
<script src="js/dictionaryBuilder.js"></script>
</head>
<body>
<form>
<label><span>Word</span>
<input type="text" id="word" />
</label>
<label><span>Equivalent Word</span>
<input type="text" id="simpleDefinition" />
</label>
<label><span>Long Definition</span>
<textarea id="longDefinition"></textarea>
</label>
<label><span>Part of Speech</span>
<select id="partOfSpeech">
<option value="noun">Noun</option>
<option value="adjective">Adjective</option>
<option value="verb">Verb</option>
<option value="adverb">Adverb</option>
<option value="preposition">Preposition</option>
<option value="pronoun">Pronoun</option>
<option value="conjunction">Conjunction</option>
</select>
</label>
<button type="button" onclick="AddWord(); return false;">Add Word</button>
</form>
<div id="dictionaryContainer">
<h2>Dictionary</h2>
<div id="theDictionary"></div>
</div>
</body>
</html>

132
js/dictionaryBuilder.js Normal file
View File

@ -0,0 +1,132 @@
var currentDictionary = {
name: "Current Dictionary",
//index: 0,
words: [],
settings: {
caseSensitive: false,
preferUpperCase: false
}
}
window.onload = function () {
LoadDictionary();
}
var Word = function (word, simpleDefinition, longDefinition, partOfSpeech) {
//this.index = currentDictionary.index++;
this.name = word;
this.simpleDefinition = simpleDefinition;
this.longDefinition = longDefinition;
this.partOfSpeech = partOfSpeech;
}
function AddWord() {
var word = document.getElementById("word").value;
var simpleDefinition = document.getElementById("simpleDefinition").value;
var longDefinition = document.getElementById("longDefinition").value;
var partOfSpeech = document.getElementById("partOfSpeech").value;
if (word != "" && (simpleDefinition != "" || longDefinition != "")) {
if (!currentDictionary.settings.caseSensitive) {
if (currentDictionary.settings.preferUpperCase) {
word = word.toUpperCase();
} else {
word = word.toLowerCase();
}
}
var wordIndex = WordIndex(word);
if (wordIndex >= 0) {
if (confirm("\"" + word + "\" is already in the dictionary. Click OK if you want to update it.")) {
UpdateWord(wordIndex, simpleDefinition, longDefinition, partOfSpeech);
}
} else {
currentDictionary.words.push(new Word(word, simpleDefinition, longDefinition, partOfSpeech));
}
currentDictionary.words.sort(dynamicSort("name"));
ShowDictionary();
SaveDictionary();
} else {
var alertMessage = "";
if (word == "") {
alertMessage += "Word cannot be blank";
if (simpleDefinition == "" && longDefinition == "") {
alertMessage += " and you need at least one definition.";
} else {
alertMessage += ".";
}
} else if (simpleDefinition == "" && longDefinition == "") {
alertMessage += "You need at least one definition."
}
}
}
function UpdateWord(wordIndex, simpleDefinition, longDefinition, partOfSpeech) {
currentDictionary.words[wordIndex].simpleDefinition = simpleDefinition;
currentDictionary.words[wordIndex].longDefinition = longDefinition;
currentDictionary.words[wordIndex].partOfSpeech = partOfSpeech;
}
function ShowDictionary() {
var dictionaryArea = document.getElementById("theDictionary");
var dictionaryText = "";
for (var i = 0; i < currentDictionary.words.length; i++) {
dictionaryText += "<entry>";
dictionaryText += "<word>" + currentDictionary.words[i].name + "</word>";
if (currentDictionary.words[i].partOfSpeech != "") {
dictionaryText += " <partofspeech>" + currentDictionary.words[i].partOfSpeech + "</partofspeech>";
}
if (currentDictionary.words[i].simpleDefinition != "") {
dictionaryText += "<br><simpledefinition> ==> " + currentDictionary.words[i].simpleDefinition + "</simpledefinition>";
}
if (currentDictionary.words[i].longDefinition != "") {
dictionaryText += "<br><longdefinition>" + currentDictionary.words[i].longDefinition + "</longdefinition>";
}
dictionaryText += "</entry>";
}
dictionaryArea.innerHTML = dictionaryText;
}
function dynamicSort(property) {
/* Retrieved from http://stackoverflow.com/a/4760279
Usage: theArray.sort(dynamicSort("objectProperty"));*/
var sortOrder = 1;
if (property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a, b) {
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
}
}
function SaveDictionary() {
localStorage.setItem('dictionary', JSON.stringify(currentDictionary));
}
function LoadDictionary() {
if (localStorage.getItem('dictionary')) {
currentDictionary = JSON.parse(localStorage.getItem('dictionary'));
}
ShowDictionary();
}
function WordIndex(word) {
for (var i = 0; i < currentDictionary.words.length; i++)
{
if (currentDictionary.words[i] == word) {
return i;
}
}
return -1;
}