Improved export so data is wrapped in quotes and made import process more clear.

This commit is contained in:
Robbie Antenesse 2016-06-21 17:42:01 -06:00
parent f7031ab579
commit da56c43e33
1 changed files with 11 additions and 9 deletions

View File

@ -273,8 +273,10 @@ function DictionaryEntry(itemIndex) {
var wordName = wordPronunciation = wordPartOfSpeech = wordSimpleDefinition = wordLongDefinition = ""; var wordName = wordPronunciation = wordPartOfSpeech = wordSimpleDefinition = wordLongDefinition = "";
if (searchTerm != "" && searchByWord) { if (searchTerm != "" && searchByWord) {
// Parse HTML Entities while searching so the regex can search actual characters instead of HTML.
wordName += htmlEntitiesParse(currentDictionary.words[itemIndex].name).replace(searchRegEx, "<searchTerm>$1</searchterm>"); wordName += htmlEntitiesParse(currentDictionary.words[itemIndex].name).replace(searchRegEx, "<searchTerm>$1</searchterm>");
} else { } else {
// Don't need to parse if not searching because HTML displays correctly anyway!
wordName += currentDictionary.words[itemIndex].name.toString(); // Use toString() to prevent using a reference instead of the value. wordName += currentDictionary.words[itemIndex].name.toString(); // Use toString() to prevent using a reference instead of the value.
} }
@ -282,7 +284,7 @@ function DictionaryEntry(itemIndex) {
wordPronunciation += marked(htmlEntitiesParse(currentDictionary.words[itemIndex].pronunciation)).replace("<p>","").replace("</p>",""); wordPronunciation += marked(htmlEntitiesParse(currentDictionary.words[itemIndex].pronunciation)).replace("<p>","").replace("</p>","");
} }
if (currentDictionary.words[itemIndex].partOfSpeech != "") { if (currentDictionary.words[itemIndex].partOfSpeech != " " && currentDictionary.words[itemIndex].partOfSpeech != "") {
wordPartOfSpeech += currentDictionary.words[itemIndex].partOfSpeech.toString(); wordPartOfSpeech += currentDictionary.words[itemIndex].partOfSpeech.toString();
} }
@ -706,17 +708,17 @@ function ExportWords() {
var wordsCSV = "word,pronunciation,part of speech,equivalent,explanation\n"; var wordsCSV = "word,pronunciation,part of speech,equivalent,explanation\n";
for (var i = 0; i < currentDictionary.words.length; i++) { for (var i = 0; i < currentDictionary.words.length; i++) {
var word = htmlEntities(currentDictionary.words[i].name).trim(); var word = "\"" + htmlEntitiesParse(currentDictionary.words[i].name).trim().replace(/\"/g, "\"\"") + "\"";
var pronunciation = htmlEntities(currentDictionary.words[i].pronunciation).trim(); var pronunciation = "\"" + htmlEntitiesParse(currentDictionary.words[i].pronunciation).trim().replace(/\"/g, "\"\"") + "\"";
var partOfSpeech = htmlEntities(currentDictionary.words[i].partOfSpeech).trim(); var partOfSpeech = "\"" + htmlEntitiesParse(currentDictionary.words[i].partOfSpeech).trim().replace(/\"/g, "\"\"") + "\"";
var simpleDefinition = htmlEntities(currentDictionary.words[i].simpleDefinition).trim(); var simpleDefinition = "\"" + htmlEntitiesParse(currentDictionary.words[i].simpleDefinition).trim().replace(/\"/g, "\"\"") + "\"";
var longDefinition = htmlEntities(currentDictionary.words[i].longDefinition); var longDefinition = "\"" + htmlEntitiesParse(currentDictionary.words[i].longDefinition).replace(/\"/g, "\"\"") + "\"";
wordsCSV += word + "," + pronunciation + "," + partOfSpeech + "," + simpleDefinition + "," + longDefinition + "\n"; wordsCSV += word + "," + pronunciation + "," + partOfSpeech + "," + simpleDefinition + "," + longDefinition + "\n";
} }
download(downloadName + ".csv", wordsCSV); download(downloadName + ".csv", wordsCSV);
} else { } else {
alert("Dictionary must have at least 1 word to export.") alert("Dictionary must have at least 1 word to export.");
} }
} }
@ -785,7 +787,7 @@ function ImportWords() {
var file = document.getElementById("importWordsCSV").files[0]; var file = document.getElementById("importWordsCSV").files[0];
var resultsArea = document.getElementById("importOptions"); var resultsArea = document.getElementById("importOptions");
resultsArea.innerHTML = ""; resultsArea.innerHTML = "<h3>Importing Words...</h3>";
var currentRow = 0; // Because of the header, the first row of data is always on line 2. var currentRow = 0; // Because of the header, the first row of data is always on line 2.
var rowsImported = 0; var rowsImported = 0;
@ -818,7 +820,7 @@ function ImportWords() {
} }
} }
// Scroll to the bottom. // Scroll to the bottom.
document.getElementById("importOptions").scrollTop = document.getElementById("importOptions").scrollHeight; document.getElementById("infoPage").scrollTop = document.getElementById("infoPage").scrollHeight;
}, },
complete: function(results) { complete: function(results) {
SaveAndUpdateWords("all"); SaveAndUpdateWords("all");