Added lots of stuff.

Editing/deleting entries, Dictionary import/export, settings, etc.
This commit is contained in:
Robbie Antenesse 2015-10-26 15:50:09 -06:00
parent d2babfd485
commit b8e822eaa7
3 changed files with 369 additions and 45 deletions

View File

@ -7,6 +7,9 @@
form {
margin: 15px;
float: left;
max-width: 400px;
padding: 15px;
border: outset 3px;
}
label {
@ -29,16 +32,26 @@ label textarea {
height: 200px;
}
#errorMessage, #updateConflictMessage {
display: block;
color: red;
font-weight: bold;
}
#dictionaryContainer {
margin: 15px;
width: 50%;
min-width: 350px;
float: right;
float: left;
}
entry {
display: block;
margin-bottom: 10px;
width: 50%;
min-width: 300px;
padding: 10px 10px 3px;
border: outset 3px;
margin-bottom: 5px;
}
word {
@ -50,6 +63,52 @@ partofspeech {
font-size: 10px;
}
shortdefinition {
display: block;
}
longdefinition {
display: block;
margin-left: 20px;
}
.management {
display: block;
right: 5px;
width: 100px;
padding: 3px;
border: inset 3px;
margin: 10px;
}
.editButton, .deleteButton, .deleteConfirmButton, .deleteCancelButton {
display: inline;
font-size: 10px;
font-weight: bold;
cursor: pointer;
margin: 5px;
}
.deleteConfirm {
display: block;
font-size: 10px;
}
#settingsScreen {
position: fixed;
top: 0px;
left: 0px;
width: 90%;
height: 90%;
padding: 5%;
background: #ffffff;
border-radius: 5px;
border: 1px solid black;
}
#settingsButton, #settingsScreenCloseButton {
float: right;
cursor: pointer;
font-size: 12px;
font-weight: bold;
}

View File

@ -2,21 +2,20 @@
<html>
<head>
<meta charset="utf-8" />
<title>DictionaryBuilder.Windows</title>
<title>Dictionary Builder</title>
<!-- DictionaryBuilder.Windows references -->
<link href="css/styles.css" rel="stylesheet" />
<script src="js/dictionaryBuilder.js"></script>
</head>
<body>
<form>
<form id="wordEntryForm">
<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>
<label><span>Explanation</span>
<textarea id="longDefinition"></textarea>
</label>
<label><span>Part of Speech</span>
@ -30,12 +29,47 @@
<option value="conjunction">Conjunction</option>
</select>
</label>
<input type="hidden" id="editIndex" />
<span id="errorMessage"></span>
<div id="newWordButtonArea" style="display: block;">
<button type="button" onclick="AddWord(); return false;">Add Word</button>
</div>
<div id="editWordButtonArea" style="display: none;">
<button type="button" onclick="AddWord(); return false;">Edit Word</button> <button type="button" onclick="ClearForm(); return false;">Cancel</button>
</div>
<div id="updateConflict" style="display: none;"></div>
</form>
<div id="dictionaryContainer">
<h2>Dictionary</h2>
<span id="settingsButton" onclick="ShowSettings()">Settings</span>
<h2 id="dictionaryName"></h2>
<div id="theDictionary"></div>
</div>
<div id="settingsScreen" style="display:none;">
<span id="settingsScreenCloseButton" onclick="HideSettings()">Close</span>
<h2>Dictionary Settings</h2>
<form>
<label>
<span>Dictionary name</span>
<input type="text" id="dictionaryNameEdit" />
</label>
<label>
<span>Dictionary is complete</span>
<input type="checkbox" id="dictionaryIsComplete" />
</label>
<label><button type="button" onclick="ExportDictionary()" style="cursor:pointer;">Export Current Dictionary</button></label>
<label>
<span>Import Dictionary</span>
<input type="file" id="importFile" />
<button type="button" onclick="ImportDictionary(); return false;">Import</button>
</label>
<label><button type="button" onclick="EmptyWholeDictionary()" style="cursor:pointer;">Empty Current Dictionary</button></label>
<br>
<span id="settingsErrorMessage"></span>
<button type="button" onclick="SaveSettings(); return false;">Save Settings</button><br>
<button type="button" onclick="SaveSettings(); HideSettings(); return false;">Save and Close</button>
</form>
</div>
</body>
</html>

View File

@ -1,15 +1,31 @@
var currentDictionary = {
name: "Current Dictionary",
//index: 0,
var currentVersion = 0.1;
var currentDictionary = {
name: "New",
words: [],
settings: {
caseSensitive: false,
preferUpperCase: false
preferUpperCase: false,
isComplete: false
},
dictionaryBuilderVersion: currentVersion
}
var defaultDictionaryJSON = JSON.stringify(currentDictionary); //Saves a stringifyed default dictionary.
//document.write(defaultDictionaryJSON);
var savedScroll = {
x: 0,
y: 0
}
window.onload = function () {
//defaultDictionaryJSON = JSON.stringify(currentDictionary); //Saves a stringifyed default dictionary.
LoadDictionary();
ClearForm();
if (currentDictionary.settings.isComplete) {
document.getElementById("wordEntryForm").style.display = "none";
}
}
var Word = function (word, simpleDefinition, longDefinition, partOfSpeech) {
@ -21,10 +37,16 @@ var Word = function (word, simpleDefinition, longDefinition, 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;
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");
var updateConflictMessageArea = document.getElementById("updateConflictMessage");
var updateConfirmButton = document.getElementById("updateConfirmButton");
if (word != "" && (simpleDefinition != "" || longDefinition != "")) {
if (!currentDictionary.settings.caseSensitive) {
@ -36,66 +58,203 @@ function AddWord() {
}
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);
if (editIndex != "") {
if (currentDictionary.words[parseInt(editIndex)].name != word || currentDictionary.words[parseInt(editIndex)].simpleDefinition != simpleDefinition || currentDictionary.words[parseInt(editIndex)].longDefinition != longDefinition || currentDictionary.words[parseInt(editIndex)].partOfSpeech != partOfSpeech) {
updateConflictArea.style.display = "block";
updateConflictArea.innerHTML = "<span id='updateConflictMessage'>Do you really want to change the word \"" + currentDictionary.words[parseInt(editIndex)].name + "\" to what you have set above?</span>";
updateConflictArea.innerHTML += '<button type="button" id="updateConfirmButton" onclick="UpdateWord(' + editIndex + ', \'' +
htmlEntities(word) + '\', \'' +
htmlEntities(simpleDefinition) + '\', \'' +
htmlEntities(longDefinition) + '\', \'' +
htmlEntities(partOfSpeech) + '\'); return false;">Yes, Update it</button>';
updateConflictArea.innerHTML += '<button type="button" id="updateCancelButton" onclick="CloseUpdateConflictArea(); return false;">No, Leave it</button>';
} else {
errorMessage = "No change has been made to \"" + word + "\"";
}
} else if (wordIndex >= 0) {
if (currentDictionary.words[parseInt(wordIndex)].simpleDefinition != simpleDefinition || currentDictionary.words[parseInt(wordIndex)].longDefinition != longDefinition || currentDictionary.words[parseInt(wordIndex)].partOfSpeech != partOfSpeech) {
updateConflictArea.style.display = "block";
updateConflictArea.innerHTML = "<span id='updateConflictMessage'>\"" + word + "\" is already in the dictionary. Do you want to update it to what you have set above?</span>";
updateConflictArea.innerHTML += '<button type="button" id="updateConfirmButton" onclick="UpdateWord(' + wordIndex + ', \'' +
htmlEntities(word) + '\', \'' +
htmlEntities(simpleDefinition) + '\', \'' +
htmlEntities(longDefinition) + '\', \'' +
htmlEntities(partOfSpeech) + '\'); return false;">Yes, Update it</button>';
updateConflictArea.innerHTML += ' <button type="button" id="updateCancelButton" onclick="CloseUpdateConflictArea(); return false;">No, Leave it</button>';
} else {
errorMessage = "\"" + word + "\" is already in the dictionary exactly as it is written above.";
}
} else {
currentDictionary.words.push(new Word(word, simpleDefinition, longDefinition, partOfSpeech));
ClearForm();
}
currentDictionary.words.sort(dynamicSort("name"));
errorMessageArea.innerHTML = "";
ShowDictionary();
SaveDictionary();
} else {
var alertMessage = "";
if (word == "") {
alertMessage += "Word cannot be blank";
errorMessage += "Word cannot be blank";
if (simpleDefinition == "" && longDefinition == "") {
alertMessage += " and you need at least one definition.";
errorMessage += " and you need at least one definition.";
} else {
alertMessage += ".";
errorMessage += ".";
}
} else if (simpleDefinition == "" && longDefinition == "") {
alertMessage += "You need at least one definition."
}
errorMessage += "You need at least one definition."
}
}
function UpdateWord(wordIndex, simpleDefinition, longDefinition, partOfSpeech) {
errorMessageArea.innerHTML = errorMessage;
}
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();
SaveDictionary();
ClearForm();
CloseUpdateConflictArea();
window.scroll(savedScroll.x, savedScroll.y);
}
function DeleteWord(index) {
if (document.getElementById("editIndex").value != "")
ClearForm();
currentDictionary.words.splice(index, 1);
ShowDictionary();
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 ShowDictionary() {
var dictionaryNameArea = document.getElementById("dictionaryName");
dictionaryNameArea.innerHTML = htmlEntitiesParse(currentDictionary.name) + " Dictionary";
var dictionaryArea = document.getElementById("theDictionary");
var dictionaryText = "";
if (currentDictionary.words.length > 0) {
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>";
dictionaryText += DictionaryEntry(i);
}
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>";
} else {
dictionaryText = "There are no entries in the dictionary."
}
dictionaryArea.innerHTML = dictionaryText;
}
function DictionaryEntry(itemIndex) {
var entryText = "<entry>";
entryText += "<word>" + currentDictionary.words[itemIndex].name + "</word>";
if (currentDictionary.words[itemIndex].partOfSpeech != "") {
entryText += " <partofspeech>" + currentDictionary.words[itemIndex].partOfSpeech + "</partofspeech>";
}
entryText += "<br>";
if (currentDictionary.words[itemIndex].simpleDefinition != "") {
entryText += "<simpledefinition> ==> " + currentDictionary.words[itemIndex].simpleDefinition + "</simpledefinition>";
}
if (currentDictionary.words[itemIndex].longDefinition != "") {
entryText += "<longdefinition>" + currentDictionary.words[itemIndex].longDefinition + "</longdefinition>";
}
entryText += ManagementArea(itemIndex);
entryText += "</entry>";
return entryText;
}
function ManagementArea(itemIndex) {
var managementHTML = "<div class='management'>";
managementHTML += "<span class='editButton' onclick='EditWord(" + itemIndex + ")'>Edit</span>";
managementHTML += "<span class='deleteButton' onclick='document.getElementById(\"delete" + itemIndex + "Confirm\").style.display = \"block\";'>Delete</span>";
managementHTML += "<div class='deleteConfirm' id='delete" + itemIndex + "Confirm' style='display:none;'>Are you sure you want to delete this entry?<br>";
managementHTML += "<span class='deleteCancelButton' onclick='document.getElementById(\"delete" + itemIndex + "Confirm\").style.display = \"none\";'>No</span>";
managementHTML += "<span class='deleteConfirmButton' onclick='DeleteWord(" + itemIndex + ")'>Yes</span>";
managementHTML += "</div>";
managementHTML += "</div>";
return managementHTML;
}
function ShowSettings() {
document.getElementById("settingsScreen").style.display = "block";
document.getElementById("dictionaryNameEdit").value = htmlEntitiesParse(currentDictionary.name);
document.getElementById("dictionaryIsComplete").checked = currentDictionary.settings.isComplete;
}
function SaveSettings() {
currentDictionary.name = htmlEntities(document.getElementById("dictionaryNameEdit").value);
currentDictionary.settings.isComplete = document.getElementById("dictionaryIsComplete").checked;
ShowDictionary();
SaveDictionary();
}
function HideSettings() {
document.getElementById("settingsScreen").style.display = "none";
document.getElementById("wordEntryForm").style.display = (currentDictionary.settings.isComplete) ? "none" : "block";
}
function dynamicSort(property) {
/* Retrieved from http://stackoverflow.com/a/4760279
Usage: theArray.sort(dynamicSort("objectProperty"));*/
@ -110,23 +269,95 @@ function dynamicSort(property) {
}
}
function EmptyWholeDictionary() {
if (confirm("This will delete the entire current dictionary. If you do not have a backed up export, you will lose it forever!\n\nDo you still want to delete?")) {
currentDictionary = JSON.parse(defaultDictionaryJSON);
ShowDictionary();
SaveDictionary();
HideSettings();
}
}
function SaveDictionary() {
localStorage.setItem('dictionary', JSON.stringify(currentDictionary));
}
function LoadDictionary() {
if (localStorage.getItem('dictionary')) {
var tmpDictionary = JSON.parse(localStorage.getItem('dictionary'));
if (tmpDictionary.words.length > 0) {
currentDictionary = JSON.parse(localStorage.getItem('dictionary'));
}
tmpDictionary = null;
}
ShowDictionary();
}
function ExportDictionary() {
download("export.dict", localStorage.getItem('dictionary'));
}
function ImportDictionary() {
if (!window.FileReader) {
alert('Your browser is not supported');
return false;
}
var reader = new FileReader();
if (document.getElementById("importFile").files.length > 0) {
var file = document.getElementById("importFile").files[0];
// Read the file
reader.readAsText(file);
// When it's loaded, process it
reader.onloadend = function () {
if (reader.result && reader.result.length) {
if (reader.result.substr(reader.result.length - 31) == '"dictionaryBuilderVersion":' + currentVersion + '}') {
localStorage.setItem('dictionary', reader.result);
document.getElementById("importFile").value = "";
LoadDictionary();
HideSettings();
} else {
alert("Uploaded file is not compatible.");
}
} else {
alert("Upload Failed");
}
reader = null;
}
} else {
alert("You must add a file to import.");
}
}
function WordIndex(word) {
for (var i = 0; i < currentDictionary.words.length; i++)
{
if (currentDictionary.words[i] == word) {
if (currentDictionary.words[i].name == word) {
return i;
}
}
return -1;
}
function htmlEntities(string) {
return String(string).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&apos;').replace(/\n/g, '<br>');
}
function htmlEntitiesParse(string) {
return String(string).replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&apos;/g, "'").replace(/<br>/g, '\n');
}
function download(filename, text) {
/* Retrieved from http://stackoverflow.com/a/18197341/3508346
Usage: download('test.txt', 'Hello world!');*/
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}