/* global markdown */
/* global Defiant */
var currentDictionary = {
name: "New",
description: "A new dictionary.",
words: [],
nextWordId: 1,
settings: {
allowDuplicates: false,
caseSensitive: false,
partsOfSpeech: "Noun,Adjective,Verb,Adverb,Preposition,Pronoun,Conjunction",
sortByEquivalent: false,
isComplete: false
}
}
var defaultDictionaryJSON = JSON.stringify(currentDictionary); //Saves a stringifyed default dictionary.
var savedScroll = {
x: 0,
y: 0
}
var aboutText, termsText, privacyText;
window.onload = function () {
LoadDictionary();
ClearForm();
GetTextFile("README.md");
GetTextFile("TERMS.md");
GetTextFile("PRIVACY.md");
}
function GetTextFile(filename) {
var readmeFileRequest = new XMLHttpRequest();
readmeFileRequest.open('GET', filename);
readmeFileRequest.onreadystatechange = function() {
if (readmeFileRequest.readyState == 4 && readmeFileRequest.status == 200) {
if (filename == "TERMS.md") {
termsText = markdown.toHTML(readmeFileRequest.responseText);
} else if (filename == "PRIVACY.md") {
privacyText = markdown.toHTML(readmeFileRequest.responseText);
} else {
aboutText = markdown.toHTML(readmeFileRequest.responseText);
}
}
}
readmeFileRequest.send();
}
function AddWord() {
var word = htmlEntities(document.getElementById("word").value).trim();
var pronunciation = htmlEntities(document.getElementById("pronunciation").value).trim();
var partOfSpeech = htmlEntities(document.getElementById("partOfSpeech").value).trim();
var simpleDefinition = htmlEntities(document.getElementById("simpleDefinition").value).trim();
var longDefinition = htmlEntities(document.getElementById("longDefinition").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 = (!currentDictionary.settings.allowDuplicates) ? WordIndex(word) : -1;
if (editIndex != "") {
if (WordAtIndexWasChanged(editIndex, word, pronunciation, partOfSpeech, simpleDefinition, longDefinition)) {
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 (WordAtIndexWasChanged(wordIndex, word, pronunciation, partOfSpeech, simpleDefinition, longDefinition)) {
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 += " ","").replace("
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({name: word, pronunciation: pronunciation, partOfSpeech: partOfSpeech, simpleDefinition: simpleDefinition, longDefinition: longDefinition, wordId: currentDictionary.nextWordId++});
FocusAfterAddingNewWord();
NewWordNotification(word);
SaveAndUpdateDictionary(false);
}
errorMessageArea.innerHTML = "";
} 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, pronunciation, partOfSpeech, simpleDefinition, longDefinition) {
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)].pronunciation != pronunciation ||
currentDictionary.words[parseInt(indexString)].partOfSpeech != partOfSpeech ||
currentDictionary.words[parseInt(indexString)].simpleDefinition != simpleDefinition ||
currentDictionary.words[parseInt(indexString)].longDefinition != longDefinition;
}
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("pronunciation").value = htmlEntitiesParse(currentDictionary.words[index].pronunciation);
document.getElementById("partOfSpeech").value = htmlEntitiesParse(currentDictionary.words[index].partOfSpeech);
document.getElementById("simpleDefinition").value = htmlEntitiesParse(currentDictionary.words[index].simpleDefinition);
document.getElementById("longDefinition").value = htmlEntitiesParse(currentDictionary.words[index].longDefinition);
document.getElementById("newWordButtonArea").style.display = "none";
document.getElementById("editWordButtonArea").style.display = "block";
}
function SaveAndUpdateDictionary(keepFormContents) {
if (!currentDictionary.settings.sortByEquivalent) {
currentDictionary.words.sort(dynamicSort("name"));
} else {
currentDictionary.words.sort(dynamicSort("simpleDefinition"));
}
SaveDictionary();
ShowDictionary();
if (!keepFormContents) {
ClearForm();
}
CloseUpdateConflictArea();
}
function UpdateWord(wordIndex, word, pronunciation, partOfSpeech, simpleDefinition, longDefinition) {
currentDictionary.words[wordIndex].name = word;
currentDictionary.words[wordIndex].pronunciation = pronunciation;
currentDictionary.words[wordIndex].partOfSpeech = partOfSpeech;
currentDictionary.words[wordIndex].simpleDefinition = simpleDefinition;
currentDictionary.words[wordIndex].longDefinition = longDefinition;
SaveAndUpdateDictionary();
window.scroll(savedScroll.x, savedScroll.y);
}
function DeleteWord(index) {
if (document.getElementById("editIndex").value != "")
ClearForm();
currentDictionary.words.splice(index, 1);
SaveAndUpdateDictionary(true);
}
function UpdateFilter() {
ShowDictionary();
}
function ShowDictionary() {
var filter = document.getElementById("wordFilter").value;
var searchResults = [];
var search = htmlEntities(document.getElementById("searchBox").value);
if (search != "") {
var xpath = [];
if (document.getElementById("searchOptionWord").checked) {
xpath.push('contains(name, "'+ search +'")');
}
if (document.getElementById("searchOptionSimple").checked) {
xpath.push('contains(simpleDefinition, "'+ search +'")');
}
if (document.getElementById("searchOptionLong").checked) {
xpath.push('contains(longDefinition, "'+ search +'")');
}
searchResults = JSON.search(currentDictionary, '//words['+ xpath.join(' or ') +']/name');
}
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)) {
if (search == "" || (search != "" && searchResults.indexOf(htmlEntities(currentDictionary.words[i].name)) >= 0)) {
if (!currentDictionary.words[i].hasOwnProperty("pronunciation")) {
currentDictionary.words[i].pronunciation = ""; //Account for new property
}
if (!currentDictionary.words[i].hasOwnProperty("wordId")) {
currentDictionary.words[i].wordId = i + 1; //Account for new property
}
dictionaryText += DictionaryEntry(i);
}
}
}
} else {
dictionaryText = "There are no entries in the dictionary."
}
dictionaryArea.innerHTML = dictionaryText;
}
function DictionaryEntry(itemIndex) {
var entryText = "
";
if (currentDictionary.words[itemIndex].simpleDefinition != "") {
entryText += "