Skip to content

Commit

Permalink
Import bug fixet, design bug in options fixed, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dirtmaxim committed Aug 17, 2016
1 parent 61a3f24 commit d7e50b9
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 64 deletions.
2 changes: 1 addition & 1 deletion background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var settingsArray,
intervalIdShowing,
timeoutIdNotification,
intervalTime,
stopSign;
stopSign;

function showNotification(word, translation, settingsArray) {
"use strict";
Expand Down
5 changes: 3 additions & 2 deletions commonFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,11 @@ function loadShareButtonScript() {
Array.prototype.indexOfObject = function (object) {
"use strict";
var i,
flag;
flag,
key;
for (i = 0; i < this.length; i++) {
flag = true;
for (var key in object) {
for (key in object) {
if (object.hasOwnProperty(key)) {
if (this[i][key] !== object[key]) {
flag = false;
Expand Down
4 changes: 2 additions & 2 deletions embeddedWindowAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function showSimpleWindow(text, textAlign, width, height) {
simpleWindowContent,
frameDocument;
simpleWindow = document.getElementById("simpleWindow");

// In case of buttonWindow is already shown.
if (simpleWindow !== null) {
simpleWindow.remove();
Expand Down Expand Up @@ -58,7 +58,7 @@ function showButtonWindow(text, buttonText, textAlign, width, height, finalActio
buttonWindowContent,
buttonWindowButton;
buttonWindow = document.getElementById("buttonWindow");

// In case of buttonWindow is already shown.
if (buttonWindow !== null) {
buttonWindow.remove();
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "EachWord - expand your vocabulary",
"short_name": "EachWord",
"version": "1.1.9",
"version": "1.1.10",
"description": "Add new words when you watch movies, series or read books and they will be occasionally shown as push notifications.",
"icons": {
"16": "images/icons/icon16.png",
Expand Down
6 changes: 3 additions & 3 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@
<p>Restore the Dictionary that you exported earlier:</p>
<p>
<button id="importDictionary">
<input type="file" id="inputImport" accept="application/json" tabindex="-1"/>
<input type="file" id="inputImport" accept=".json" tabindex="-1"/>
<span>Import the Dictionary<span>
</button><br>
<input type="radio" id="inputAppend" name="importType" value="append" checked>Append
<input type="radio" id="inputReplace" name="importType" value="replace">Replace
<label><input type="radio" id="inputAppend" name="importType" value="append" checked>Append</label>
<label><input type="radio" id="inputReplace" name="importType" value="replace">Replace</label>
</p>
</div>
<div class="rowSettings">
Expand Down
26 changes: 13 additions & 13 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function controlCheckedTypesOfThemes() {
var count,
lastIndex;
count = 0;
for (var i = 0; i < checkBoxTheme.length && count < 2; i++) {
for (i = 0; i < checkBoxTheme.length && count < 2; i++) {
if (checkBoxTheme[i].checked === true) {
count++;
lastIndex = i;
Expand Down Expand Up @@ -98,6 +98,7 @@ function showSettingsStateDelayed(state, delay) {

// It shows warning massege when user switch "Append" to "Replace".
function showReplaceWarning() {
"use strict";
var span;
importType = document.getElementsByName("importType");
span = document.createElement("span");
Expand All @@ -108,6 +109,7 @@ function showReplaceWarning() {

// It hides warning massege when user switch "Replace" to "Append".
function hideReplaceWarning() {
"use strict";
var span;
span = document.getElementById("warningMessage");
if (span) {
Expand All @@ -120,7 +122,7 @@ function exportDictionaryFile() {
"use strict";
var a;
a = document.createElement("a");
a.href = URL.createObjectURL(new Blob([localStorage.getItem("dictionaryArray")], {type: "application/json"}));
a.href = URL.createObjectURL(new Blob([localStorage.getItem("dictionaryArray")]));
a.download = "yourDictionary.json";
document.body.appendChild(a);
a.click();
Expand All @@ -131,11 +133,10 @@ function exportDictionaryFile() {
// Replace or merge dictionary.
function importDictionaryFile(e) {
"use strict";
var i,
file,
var file,
fileReader;
file = e.target.files[0];
if (file !== undefined && file.type === "application/json") {
if (file !== undefined && file.name.endsWith(".json")) {
fileReader = new FileReader();
fileReader.onload = function (e) {
var result,
Expand All @@ -162,7 +163,7 @@ function importDictionaryFile(e) {
}
importType = document.getElementsByName("importType");
if (importType[0].checked) {
for(i = 0; i < result.length; i++) {
for (i = 0; i < result.length; i++) {
dictionaryArray.push(result[i]);
}
localStorage.setItem("dictionaryArray", JSON.stringify(dictionaryArray));
Expand All @@ -172,16 +173,15 @@ function importDictionaryFile(e) {
localStorage.setItem("dictionaryArrayQueue", JSON.stringify([]));
}
showSettingsStateDelayed("Dictionary have been imported!", 500);
} catch(error) {
} catch (error) {
showSettingsStateDelayed("It is not a Dictionary format file!", 500);
} finally {
// It allows to load the same file twise.
inputImport.value = null;
}
};
fileReader.readAsText(file);
}
else {
} else {
showSettingsStateDelayed("It is not a Dictionary format file!", 500);
}
}
Expand All @@ -207,7 +207,7 @@ function save() {
settingsArray.showNotificationCardsDisabled = showNotificationCards.disabled;
settingsArray.showTimeline = showTimeline.checked;
selectedThemes = [];
for (var i = 0; i < checkBoxTheme.length; i++) {
for (i = 0; i < checkBoxTheme.length; i++) {
if (checkBoxTheme[i].checked) {
selectedThemes.push(i);
}
Expand Down Expand Up @@ -274,9 +274,9 @@ window.onload = function () {
span = document.createElement("span");
span.className = "colorSquare";
span.id = "color" + i;

// RegExp to set up "background-color" of themes rounds in "options.js".
span.style.backgroundColor = themes[i].match(/#wordCard8730011\s?{\s?background-color\s?:\s?(#\w{3,6})\s?;\s?}/)[1];
span.style.backgroundColor = themes[i].match(/#wordCard8730011\s?\{\s?background-color\s?:\s?(#\w{3,6})\s?;\s?\}/)[1];
input = document.createElement("input");
input.type = "checkbox";
input.className = "checkBoxTheme";
Expand All @@ -290,7 +290,7 @@ window.onload = function () {
for (i = 0; i < selectedThemes.length; i++) {
checkBoxTheme[selectedThemes[i]].checked = true;
}
if (selectedThemes.length == 1) {
if (selectedThemes.length === 1) {
controlCheckedTypesOfThemes.lastIndex = selectedThemes[0];
checkBoxTheme[selectedThemes[0]].disabled = true;
}
Expand Down
103 changes: 61 additions & 42 deletions popup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
var nothingToShowText,
welcomeText,
updateText;
var nothingToShowMessage,
welcomeMessage,
updateMessage,
isCrucialUpdate;

// It affects either updateMessage will be shown or not.
isCrucialUpdate = false;

// Text message initialisation.
nothingToShowMessage = "We have nothing to show you <img src=\"images/smiles/confused.svg\" width=\"18\"><br>First of all, add some words.";
Expand Down Expand Up @@ -33,7 +37,7 @@ function switchButtonChangeState() {
switchButton.title = "Turn on push cards";
switchButton.classList.remove("colorFirst");
switchButton.classList.add("colorSecond");

// Change color icon to icon without color to indicate state of extension.
chrome.browserAction.setIcon({path: "images/default_icons/icon38_(without_color).png"});
if (JSON.parse(localStorage.getItem("dictionaryArray")).length !== 0) {
Expand All @@ -55,6 +59,40 @@ function switchButtonChangeState() {
return false;
}

// It checks if extension was updated in case of it is not first installed version.
function isExtensionUpdated() {
"use strict";
var versionArray;
versionArray = JSON.parse(localStorage.getItem("versionArray"));
if (versionArray.length === 0) {
localStorage.setItem("welcomeIsShown", JSON.stringify(true));
versionArray.push(chrome.app.getDetails().version);
localStorage.setItem("versionArray", JSON.stringify(versionArray));
} else if (versionArray[versionArray.length - 1] !== chrome.app.getDetails().version) {
return true;
}
return false;
}

// It checks cases to show different type of messages.
function checkCasesToShowMessage() {
"use strict";
var dictionaryArray;
dictionaryArray = localStorage.getItem("dictionaryArray");
dictionaryArray = JSON.parse(dictionaryArray);
if (JSON.parse(localStorage.getItem("welcomeIsShown"))) {
showButtonWindow(welcomeMessage, "Got it", "left", "80%", "80%", function () {
dictionaryArray = JSON.parse(localStorage.getItem("dictionaryArray"));
localStorage.setItem("welcomeIsShown", JSON.stringify(false));
if (dictionaryArray.length === 0) {
showSimpleWindow(nothingToShowMessage, "center", "80%", "25%");
}
});
} else if (dictionaryArray.length === 0) {
showSimpleWindow(nothingToShowMessage, "center", "80%", "25%");
}
}

// Function to play word when user clicks to speaker.
function playWord() {
"use strict";
Expand Down Expand Up @@ -213,22 +251,6 @@ document.onkeyup = function (e) {
}
};

// It checks if extension was updated in case of it is not first installed version.
function isExtensionUpdated() {
"use strict";
var versionArray;
versionArray = JSON.parse(localStorage.getItem("versionArray"));
if (versionArray.length === 0) {
localStorage.setItem("welcomeIsShown", JSON.stringify(true));
versionArray.push(chrome.app.getDetails().version);
localStorage.setItem("versionArray", JSON.stringify(versionArray));
}
else if (versionArray[versionArray.length - 1] !== chrome.app.getDetails().version) {
return true;
}
return false;
}

// Fill in extension window.
window.onload = function () {
"use strict";
Expand All @@ -238,29 +260,30 @@ window.onload = function () {
intoLanguage,
switchState,
dictionaryArray,
versionArray,
i,
word,
translation,
frameDocument,
tr,
td,
a;
loadShareButtonScript();

loadShareButtonScript();
addButton = document.getElementById("addButton");
switchButton = document.getElementById("switchButton");
fromLanguage = document.getElementById("fromLanguage");
intoLanguage = document.getElementById("intoLanguage");

// State of extension: "Turn on" or "Turn off".
switchState = localStorage.getItem("switchState");
addButton.onclick = addWord;
switchButton.onclick = switchButtonChangeState;

// Save inputed letters in fields when extension closes.
fromLanguage.oninput = fromLanguageSave;
intoLanguage.oninput = intoLanguageSave;

// Load saved letters from localStorage into fields.
fromLanguage.value = JSON.parse(localStorage.getItem("fromLanguage"));
intoLanguage.value = JSON.parse(localStorage.getItem("intoLanguage"));
Expand All @@ -276,11 +299,11 @@ window.onload = function () {
switchButton.classList.remove("colorFirst");
switchButton.classList.add("colorSecond");
}

// Array of words in localStorage.
dictionaryArray = localStorage.getItem("dictionaryArray");
dictionaryArray = JSON.parse(dictionaryArray);

// Fill in words, translation and "deleteButton" into "iframe".
for (i = 0; i < dictionaryArray.length; i++) {
word = dictionaryArray[i].word;
Expand Down Expand Up @@ -319,9 +342,8 @@ window.onload = function () {
tr.scrollIntoView(true);
}
if (isExtensionUpdated()) {
showButtonWindow(updateMessage, "Got it", "left", "80%", "80%", function () {
var versionArray,
dictionaryArray;
if (isCrucialUpdate) {
showButtonWindow(updateMessage, "Got it", "left", "80%", "80%", function () {
versionArray = JSON.parse(localStorage.getItem("versionArray"));
dictionaryArray = JSON.parse(localStorage.getItem("dictionaryArray"));
versionArray.push(chrome.app.getDetails().version);
Expand All @@ -330,18 +352,15 @@ window.onload = function () {
if (dictionaryArray.length === 0) {
showSimpleWindow(nothingToShowMessage, "center", "80%", "25%");
}
});
} else if (JSON.parse(localStorage.getItem("welcomeIsShown"))) {
showButtonWindow(welcomeMessage, "Got it", "left", "80%", "80%", function () {
var dictionaryArray;
dictionaryArray = JSON.parse(localStorage.getItem("dictionaryArray"));
localStorage.setItem("welcomeIsShown", JSON.stringify(false));
if (dictionaryArray.length === 0) {
showSimpleWindow(nothingToShowMessage, "center", "80%", "25%");
}
});
} else if (dictionaryArray.length === 0) {
showSimpleWindow(nothingToShowMessage, "center", "80%", "25%");
});
} else {
versionArray = JSON.parse(localStorage.getItem("versionArray"));
versionArray.push(chrome.app.getDetails().version);
localStorage.setItem("versionArray", JSON.stringify(versionArray));
checkCasesToShowMessage();
}
} else {
checkCasesToShowMessage();
}
document.getElementById("fromLanguage").focus();
};

0 comments on commit d7e50b9

Please sign in to comment.