//Requires Markdown.js parser
var currentVersion = 0.2;
var currentDictionary = {
name: "New",
description: "A new dictionary.",
words: [],
settings: {
allowDuplicates: false,
caseSensitive: false,
partsOfSpeech: "Noun,Adjective,Verb,Adverb,Preposition,Pronoun,Conjunction",
isComplete: false
},
dictionaryImportVersion: currentVersion // This needs to always be last.
}
var defaultDictionaryJSON = JSON.stringify(currentDictionary); //Saves a stringifyed default dictionary.
var savedScroll = {
x: 0,
y: 0
}
window.onload = function () {
LoadDictionary();
ClearForm();
}
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 = htmlEntities(document.getElementById("word").value);
var simpleDefinition = htmlEntities(document.getElementById("simpleDefinition").value);
var longDefinition = htmlEntities(document.getElementById("longDefinition").value);
var partOfSpeech = htmlEntities(document.getElementById("partOfSpeech").value);
var editIndex = htmlEntities(document.getElementById("editIndex").value);
var errorMessageArea = document.getElementById("errorMessage");
var errorMessage = "";
var updateConflictArea = document.getElementById("updateConflict");
if (word != "" && (simpleDefinition != "" || longDefinition != "")) {
var wordIndex = WordIndex(word);
if (editIndex != "") {
if (WordAtIndexWasChanged(editIndex, word, simpleDefinition, longDefinition, partOfSpeech)) {
updateConflictArea.style.display = "block";
updateConflictArea.innerHTML = "Do you really want to change the word \"" + currentDictionary.words[parseInt(editIndex)].name + "\" to what you have set above?";
updateConflictArea.innerHTML += '';
updateConflictArea.innerHTML += '';
} else {
errorMessage = "No change has been made to \"" + word + "\"";
if (currentDictionary.words[parseInt(editIndex)].name != word) {
errorMessage += ". (Your dictionary is currently set to ignore case.)"
}
}
} else if (wordIndex >= 0) {
if (currentDictionary.words[wordIndex].simpleDefinition != simpleDefinition || currentDictionary.words[wordIndex].longDefinition != longDefinition || currentDictionary.words[wordIndex].partOfSpeech != partOfSpeech) {
updateConflictArea.style.display = "block";
var updateConflictText = "\"" + word + "\" is already in the dictionary";
if (currentDictionary.words[wordIndex].name != word) {
updateConflictText += " as \"" + currentDictionary.words[wordIndex].name + "\", and your dictionary is set to ignore case.";
} else {
updateConflictText += "."
}
updateConflictText += "
Do you want to update it to what you have set above?";
updateConflictText += '';
updateConflictText += ' ';
updateConflictArea.innerHTML = updateConflictText;
} else {
errorMessage = "\"" + word + "\" is already in the dictionary exactly as it is written above";
if (currentDictionary.words[wordIndex].name != word) {
errorMessage += ". (Your dictionary is currently set to ignore case.)"
}
}
} else {
currentDictionary.words.push(new Word(word, simpleDefinition, longDefinition, partOfSpeech));
ClearForm();
}
currentDictionary.words.sort(dynamicSort("name"));
errorMessageArea.innerHTML = "";
ShowDictionary(document.getElementById("wordFilter").value);
SaveDictionary();
} else {
if (word == "") {
errorMessage += "Word cannot be blank";
if (simpleDefinition == "" && longDefinition == "") {
errorMessage += " and you need at least one definition.";
} else {
errorMessage += ".";
}
} else if (simpleDefinition == "" && longDefinition == "") {
errorMessage += "You need at least one definition."
}
}
errorMessageArea.innerHTML = errorMessage;
}
function WordAtIndexWasChanged(indexString, word, simpleDefinition, longDefinition, partOfSpeech) {
return (!currentDictionary.settings.caseSensitive && currentDictionary.words[parseInt(indexString)].name.toLowerCase() != word.toLowerCase()) ||
(currentDictionary.settings.caseSensitive && currentDictionary.words[parseInt(indexString)].name != word) ||
currentDictionary.words[parseInt(indexString)].simpleDefinition != simpleDefinition ||
currentDictionary.words[parseInt(indexString)].longDefinition != longDefinition ||
currentDictionary.words[parseInt(indexString)].partOfSpeech != partOfSpeech;
}
function SaveScroll() {
var doc = document.documentElement;
var left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
savedScroll.x = left;
savedScroll.y = top;
}
function EditWord(index) {
SaveScroll();
window.scroll(0, 0);
ClearForm();
document.getElementById("editIndex").value = index.toString();
document.getElementById("word").value = htmlEntitiesParse(currentDictionary.words[index].name);
document.getElementById("simpleDefinition").value = htmlEntitiesParse(currentDictionary.words[index].simpleDefinition);
document.getElementById("longDefinition").value = htmlEntitiesParse(currentDictionary.words[index].longDefinition);
document.getElementById("partOfSpeech").value = htmlEntitiesParse(currentDictionary.words[index].partOfSpeech);
document.getElementById("newWordButtonArea").style.display = "none";
document.getElementById("editWordButtonArea").style.display = "block";
}
function UpdateWord(wordIndex, word, simpleDefinition, longDefinition, partOfSpeech) {
currentDictionary.words[wordIndex].name = word;
currentDictionary.words[wordIndex].simpleDefinition = simpleDefinition;
currentDictionary.words[wordIndex].longDefinition = longDefinition;
currentDictionary.words[wordIndex].partOfSpeech = partOfSpeech;
ShowDictionary(document.getElementById("wordFilter").value);
SaveDictionary();
ClearForm();
CloseUpdateConflictArea();
window.scroll(savedScroll.x, savedScroll.y);
}
function DeleteWord(index) {
if (document.getElementById("editIndex").value != "")
ClearForm();
currentDictionary.words.splice(index, 1);
ShowDictionary(document.getElementById("wordFilter").value);
SaveDictionary();
CloseUpdateConflictArea();
}
function CloseUpdateConflictArea() {
document.getElementById("updateConflict").style.display = "none";
}
function ClearForm() {
document.getElementById("word").value = "";
document.getElementById("simpleDefinition").value = "";
document.getElementById("longDefinition").value = "";
document.getElementById("partOfSpeech").value = "";
document.getElementById("editIndex").value = "";
document.getElementById("newWordButtonArea").style.display = "block";
document.getElementById("editWordButtonArea").style.display = "none";
document.getElementById("errorMessage").innerHTML = "";
document.getElementById("updateConflict").style.display = "none";
}
function UpdateFilter() {
ShowDictionary(document.getElementById("wordFilter").value);
}
function ShowDictionary(filter) {
filter = (typeof filter !== 'undefined') ? filter : "";
var dictionaryNameArea = document.getElementById("dictionaryName");
dictionaryNameArea.innerHTML = htmlEntitiesParse(currentDictionary.name) + " Dictionary";
var dictionaryDescriptionArea = document.getElementById("dictionaryDescription");
dictionaryDescriptionArea.innerHTML = markdown.toHTML(htmlEntitiesParse(currentDictionary.description));
var dictionaryArea = document.getElementById("theDictionary");
var dictionaryText = "";
if (currentDictionary.words.length > 0) {
for (var i = 0; i < currentDictionary.words.length; i++) {
if (filter == "" || (filter != "" && currentDictionary.words[i].partOfSpeech == filter)) {
dictionaryText += DictionaryEntry(i);
}
}
} else {
dictionaryText = "There are no entries in the dictionary."
}
dictionaryArea.innerHTML = dictionaryText;
}
function ToggleDescription() {
var descriptionToggle = document.getElementById("descriptionToggle");
var descriptionArea = document.getElementById("dictionaryDescription");
if (descriptionArea.style.display == "none") {
descriptionArea.style.display = "block";
descriptionToggle.innerHTML = "Hide Description";
} else {
descriptionArea.style.display = "none";
descriptionToggle.innerHTML = "Show Description";
}
}
function DictionaryEntry(itemIndex) {
var entryText = "
";
if (currentDictionary.words[itemIndex].simpleDefinition != "") {
entryText += "