From d2babfd4851a2fe09aedf4e4495c5bf6cb04e21e Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Sun, 25 Oct 2015 22:41:25 -0600 Subject: [PATCH] Initial Commit. Basic add and save functionality, no delete. --- css/styles.css | 55 +++++++++++++++++ index.html | 41 +++++++++++++ js/dictionaryBuilder.js | 132 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+) create mode 100644 css/styles.css create mode 100644 index.html create mode 100644 js/dictionaryBuilder.js diff --git a/css/styles.css b/css/styles.css new file mode 100644 index 0000000..5e00aec --- /dev/null +++ b/css/styles.css @@ -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; +} \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..40ca39f --- /dev/null +++ b/index.html @@ -0,0 +1,41 @@ + + + + + DictionaryBuilder.Windows + + + + + + +
+ + + + + +
+ +
+

Dictionary

+
+
+ + diff --git a/js/dictionaryBuilder.js b/js/dictionaryBuilder.js new file mode 100644 index 0000000..e633b77 --- /dev/null +++ b/js/dictionaryBuilder.js @@ -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 += ""; + + dictionaryText += "" + currentDictionary.words[i].name + ""; + + if (currentDictionary.words[i].partOfSpeech != "") { + dictionaryText += " " + currentDictionary.words[i].partOfSpeech + ""; + } + + if (currentDictionary.words[i].simpleDefinition != "") { + dictionaryText += "
==> " + currentDictionary.words[i].simpleDefinition + ""; + } + + if (currentDictionary.words[i].longDefinition != "") { + dictionaryText += "
" + currentDictionary.words[i].longDefinition + ""; + } + + dictionaryText += "
"; + } + + 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; +} \ No newline at end of file