Fixed Search by removing Word function

Cleaned up some duplicate lines into SaveAndUpdateDictionary()
Removed UpdateFilter()
This commit is contained in:
Robbie Antenesse 2015-10-30 15:56:05 -06:00
parent 8f6a3e3a99
commit 4f511c72a4
2 changed files with 40 additions and 49 deletions

View File

@ -65,16 +65,16 @@
<div id="searchArea" style="display:block;"> <div id="searchArea" style="display:block;">
<label style="margin-top:10px;"> <label style="margin-top:10px;">
<span>Search</span> <span>Search</span>
<input type="text" id="searchBox" onclick="this.select();" onchange="UpdateFilter()" style="display:block;" /> <input type="text" id="searchBox" onclick="this.select();" onchange="ShowDictionary()" style="display:block;" />
<div id="searchOptions" style="font-size:12px;"> <div id="searchOptions" style="font-size:12px;">
<label style="display:inline;margin:0;">Word <input type="checkbox" id="searchOptionWord" checked="checked" /></label>&nbsp;&nbsp; <label style="display:inline;margin:0;">Word <input type="checkbox" id="searchOptionWord" checked="checked" onchange="ShowDictionary()" /></label>&nbsp;&nbsp;
<label style="display:inline;margin:0;">Equivalent <input type="checkbox" id="searchOptionSimple" checked="checked" /></label>&nbsp;&nbsp; <label style="display:inline;margin:0;">Equivalent <input type="checkbox" id="searchOptionSimple" checked="checked" onchange="ShowDictionary()" /></label>&nbsp;&nbsp;
<label style="display:inline;margin:0;">Explanation <input type="checkbox" id="searchOptionLong" checked="checked" /></label> <label style="display:inline;margin:0;">Explanation <input type="checkbox" id="searchOptionLong" checked="checked" onchange="ShowDictionary()" /></label>
</div> </div>
</label> </label>
</div> </div>
<label style="display:block;"><b>Filter Words </b><select id="wordFilter" onchange="UpdateFilter()"> <label style="display:block;"><b>Filter Words </b><select id="wordFilter" onchange="ShowDictionary()">
<option value="">All</option> <option value="">All</option>
</select> </select>
</label> </label>

View File

@ -1,4 +1,6 @@
//Requires Markdown.js parser /* global markdown */
/* global Defiant */
//Requires Markdown.js parser
var currentVersion = 0.2; var currentVersion = 0.2;
var currentDictionary = { var currentDictionary = {
@ -16,10 +18,6 @@ var currentDictionary = {
var defaultDictionaryJSON = JSON.stringify(currentDictionary); //Saves a stringifyed default dictionary. var defaultDictionaryJSON = JSON.stringify(currentDictionary); //Saves a stringifyed default dictionary.
var dictionarySearchSnapshot;
var searchResults = [];
var savedScroll = { var savedScroll = {
x: 0, x: 0,
y: 0 y: 0
@ -30,14 +28,6 @@ window.onload = function () {
ClearForm(); 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() { function AddWord() {
var word = htmlEntities(document.getElementById("word").value); var word = htmlEntities(document.getElementById("word").value);
var simpleDefinition = htmlEntities(document.getElementById("simpleDefinition").value); var simpleDefinition = htmlEntities(document.getElementById("simpleDefinition").value);
@ -93,15 +83,12 @@ function AddWord() {
} }
} }
} else { } else {
currentDictionary.words.push(new Word(word, simpleDefinition, longDefinition, partOfSpeech)); currentDictionary.words.push({name: word, simpleDefinition: simpleDefinition, longDefinition: longDefinition, partOfSpeech: partOfSpeech});
ClearForm(); SaveAndUpdateDictionary(false);
} }
currentDictionary.words.sort(dynamicSort("name"));
errorMessageArea.innerHTML = "";
ShowDictionary(document.getElementById("wordFilter").value); errorMessageArea.innerHTML = "";
SaveDictionary();
} else { } else {
if (word == "") { if (word == "") {
errorMessage += "Word cannot be blank"; errorMessage += "Word cannot be blank";
@ -151,15 +138,23 @@ function EditWord(index) {
document.getElementById("editWordButtonArea").style.display = "block"; document.getElementById("editWordButtonArea").style.display = "block";
} }
function SaveAndUpdateDictionary(keepFormContents) {
currentDictionary.words.sort(dynamicSort("name"));
SaveDictionary();
ShowDictionary();
if (!keepFormContents) {
ClearForm();
}
CloseUpdateConflictArea();
}
function UpdateWord(wordIndex, word, simpleDefinition, longDefinition, partOfSpeech) { function UpdateWord(wordIndex, word, simpleDefinition, longDefinition, partOfSpeech) {
currentDictionary.words[wordIndex].name = word; currentDictionary.words[wordIndex].name = word;
currentDictionary.words[wordIndex].simpleDefinition = simpleDefinition; currentDictionary.words[wordIndex].simpleDefinition = simpleDefinition;
currentDictionary.words[wordIndex].longDefinition = longDefinition; currentDictionary.words[wordIndex].longDefinition = longDefinition;
currentDictionary.words[wordIndex].partOfSpeech = partOfSpeech; currentDictionary.words[wordIndex].partOfSpeech = partOfSpeech;
ShowDictionary(document.getElementById("wordFilter").value);
SaveDictionary(); SaveAndUpdateDictionary();
ClearForm();
CloseUpdateConflictArea();
window.scroll(savedScroll.x, savedScroll.y); window.scroll(savedScroll.x, savedScroll.y);
} }
@ -169,10 +164,8 @@ function DeleteWord(index) {
ClearForm(); ClearForm();
currentDictionary.words.splice(index, 1); currentDictionary.words.splice(index, 1);
ShowDictionary(document.getElementById("wordFilter").value);
SaveDictionary();
CloseUpdateConflictArea();
SaveAndUpdateDictionary(true);
} }
function CloseUpdateConflictArea() { function CloseUpdateConflictArea() {
@ -193,21 +186,23 @@ function ClearForm() {
} }
function UpdateFilter() { function UpdateFilter() {
ShowDictionary(document.getElementById("wordFilter").value); ShowDictionary();
} }
function ShowDictionary(filter) { function ShowDictionary() {
filter = (typeof filter !== 'undefined') ? filter : ""; var filter = document.getElementById("wordFilter").value;
searchResults = []; var searchResults = [];
var search = document.getElementById("searchBox").value; var search = htmlEntities(document.getElementById("searchBox").value);
if (search != "") { if (search != "") {
if (document.getElementById("searchOptionWord").checked) { if (document.getElementById("searchOptionWord").checked) {
var wordSearch = JSON.search(dictionarySearchSnapshot, '//words[contains(name, "' + search + '")]/name'); var wordNameSearch = JSON.search(currentDictionary, '//words[contains(name, "' + search + '")]/name');
searchResults.push(wordSearch); for (var i = 0; i < wordNameSearch.length; i++) {
searchResults.push(wordNameSearch[i]);
}
} }
if (document.getElementById("searchOptionSimple").checked) { if (document.getElementById("searchOptionSimple").checked) {
var simpleDefinitionSearch = JSON.search(dictionarySearchSnapshot, '//words[contains(simpleDefinition, "' + search + '")]/name'); var simpleDefinitionSearch = JSON.search(currentDictionary, '//words[contains(simpleDefinition, "' + search + '")]/name');
for (var i = 0; i < simpleDefinitionSearch.length; i++) { for (var i = 0; i < simpleDefinitionSearch.length; i++) {
if (searchResults.indexOf(simpleDefinitionSearch[i]) < 0) { if (searchResults.indexOf(simpleDefinitionSearch[i]) < 0) {
searchResults.push(simpleDefinitionSearch[i]); searchResults.push(simpleDefinitionSearch[i]);
@ -215,7 +210,7 @@ function ShowDictionary(filter) {
} }
} }
if (document.getElementById("searchOptionLong").checked) { if (document.getElementById("searchOptionLong").checked) {
var longDefinitionSearch = JSON.search(dictionarySearchSnapshot, '//words[contains(longDefinition, "' + search + '")]/name'); var longDefinitionSearch = JSON.search(currentDictionary, '//words[contains(longDefinition, "' + search + '")]/name');
for (var i = 0; i < longDefinitionSearch.length; i++) { for (var i = 0; i < longDefinitionSearch.length; i++) {
if (searchResults.indexOf(longDefinitionSearch[i]) < 0) { if (searchResults.indexOf(longDefinitionSearch[i]) < 0) {
searchResults.push(longDefinitionSearch[i]); searchResults.push(longDefinitionSearch[i]);
@ -348,8 +343,7 @@ function SaveSettings() {
HideSettingsWhenComplete(); HideSettingsWhenComplete();
ShowDictionary(document.getElementById("wordFilter").value); SaveAndUpdateDictionary(true);
SaveDictionary();
} }
function HideSettingsWhenComplete() { function HideSettingsWhenComplete() {
@ -400,8 +394,7 @@ function HideSettings() {
function EmptyWholeDictionary() { 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?")) { 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); currentDictionary = JSON.parse(defaultDictionaryJSON);
ShowDictionary(""); SaveAndUpdateDictionary(false);
SaveDictionary();
SetPartsOfSpeech(); SetPartsOfSpeech();
HideSettings(); HideSettings();
} }
@ -409,9 +402,7 @@ function EmptyWholeDictionary() {
function SaveDictionary() { function SaveDictionary() {
localStorage.setItem('dictionary', JSON.stringify(currentDictionary)); localStorage.setItem('dictionary', JSON.stringify(currentDictionary));
//location.reload();
// Update search snapshot
dictionarySearchSnapshot = Defiant.getSnapshot(currentDictionary);
} }
function LoadDictionary() { function LoadDictionary() {
@ -434,7 +425,7 @@ function LoadDictionary() {
} }
// Update search snapshot // Update search snapshot
dictionarySearchSnapshot = Defiant.getSnapshot(currentDictionary); //dictionarySearchSnapshot = Defiant.getSnapshot(currentDictionary);
} }
function ExportDictionary() { function ExportDictionary() {