Better organize code

This commit is contained in:
Neil Brommer 2018-01-09 09:06:35 -08:00
parent 9c84a3512e
commit f579c5a204
5 changed files with 257 additions and 257 deletions

View file

@ -178,6 +178,8 @@
<script src="js/main.js"></script> <script src="js/main.js"></script>
<script src="js/loadBookmarks.js"></script> <script src="js/loadBookmarks.js"></script>
<script src="js/addBookmarks.js"></script> <script src="js/addBookmarks.js"></script>
<script src="js/moveBookmarks.js"></script>
<script src="js/deleteBookmarks.js"></script>
<script src="js/editBookmarks.js"></script> <script src="js/editBookmarks.js"></script>
<script src="js/importExport.js"></script> <script src="js/importExport.js"></script>
</body> </body>

76
js/deleteBookmarks.js Normal file
View file

@ -0,0 +1,76 @@
function deleteGroup() {
var group = $(this); // the delete group button
var groupIndex = group.parent().parent().data("group-index");
var openDBRequest = window.indexedDB.open("bookmarks");
openDBRequest.onsuccess = function (openEvt) {
var db = openEvt.target.result;
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
var lastIndex = -1;
groupsStore.openCursor().onsuccess = function (cursorEvt) {
var cursor = cursorEvt.target.result;
if (cursor) {
var item = cursor.value;
var cardContainer = $("#group-" + item.groupIndex);
if (item.groupIndex > lastIndex)
lastIndex = item.groupIndex;
if (item.groupIndex == groupIndex) {
cardContainer.hide(300, "swing", function () { cardContainer.remove(); });
}
if (item.groupIndex > groupIndex) {
item.groupIndex--;
groupsStore.put(item);
$(cardContainer.children()[0]).attr("data-group-index", item.groupIndex);
cardContainer.attr("id", "group-" + item.groupIndex);
}
cursor.continue();
} else {
groupsStore.delete(lastIndex);
db.close();
}
}
}
openDBRequest.onerror = function (err) {
console.error(err);
window.alert("There was an error deleting the group");
}
}
function deleteBookmark(e) {
var item = $(this).parent();
var groupName = item.parent().parent().data("group-name");
var groupIndex = item.parent().parent().data("group-index");
var bookmarkIndex = item.index();
var bookmarkItem = $("#" + groupName + "-" + bookmarkIndex);
var openDBRequest = window.indexedDB.open("bookmarks");
openDBRequest.onsuccess = function (openEvt) {
var db = openEvt.target.result;
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
groupsStore.get(groupIndex).onsuccess = function (getEvt) {
var groupData = getEvt.target.result;
groupData.bookmarks = removeFromArray(groupData.bookmarks, bookmarkIndex);
groupsStore.put(groupData);
bookmarkItem.hide(300, "swing", function () { bookmarkItem.remove(); });
db.close();
}
}
openDBRequest.onerror = function (err) {
console.log("Error", err);
window.alert("There was an error deleting the bookmark");
}
}

View file

@ -9,232 +9,6 @@ function toggleEditing () {
enableEditing(); enableEditing();
} }
function groupMoved(dropEvt) {
var newIndex = dropEvt.newIndex;
var oldIndex = dropEvt.oldIndex;
var movedCard = $($(dropEvt.item).children()[0]);
var groupName = movedCard.data("group-name");
if (oldIndex != newIndex) {
var openDBRequest = window.indexedDB.open("bookmarks");
openDBRequest.onsuccess = function (openEvt) {
var db = openEvt.target.result;
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
// build an array of all groups
var groups = [];
groupsStore.openCursor().onsuccess = function (cursorEvt) {
var cursor = cursorEvt.target.result;
if (cursor) {
groups.push(cursor.value);
cursor.continue();
} else {
rearrangeGroups(groupsStore, groups, newIndex, oldIndex);
db.close();
}
}
}
openDBRequest.onerror = function (err) {
console.log(err);
window.alert("Error moving group");
}
}
}
function rearrangeGroups(groupsStore, groups, newIndex, oldIndex) {
var movedGroup = $("#group-" + oldIndex);
if (newIndex > oldIndex) {
for (var i = 0; i < groups.length; i++) {
var g = groups[i];
if (g.groupIndex > oldIndex && g.groupIndex <= newIndex) {
g.groupIndex--;
groupsStore.put(g);
// modify the group's card
var cardContainer = $("#group-" + (g.groupIndex + 1))
.attr("id", "group" + g.groupIndex);
$(cardContainer.children()[0]) // the card
.attr("data-group-index", g.groupIndex);
}
}
} else { // oldIndex > newIndex
for (var i = 0; i < groups.length; i++) {
var g = groups[i];
if (g.groupIndex < oldIndex && g.groupIndex >= newIndex) {
g.groupIndex++;
groupsStore.put(g);
// modify the group's card
var cardContainer = $("#group-" + (g.groupIndex - 1))
.attr("id", "group" + g.groupIndex);
$(cardContainer.children()[0]) // the card
.attr("data-group-index", g.groupIndex);
}
}
}
// update the moved group's data
var movedGroupData = groups[oldIndex];
movedGroupData.groupIndex = newIndex;
groupsStore.put(movedGroupData);
// update the group's card
movedGroup.attr("id", "group" + newIndex);
$(movedGroup.children()[0]).attr("data-group-index", newIndex);
}
function bookmarkMoved(dropEvt) {
var oldIndex = dropEvt.oldIndex;
var newIndex = dropEvt.newIndex;
if (dropEvt.from != dropEvt.to) {
var oldGroupIndex = $(dropEvt.from).parent().data("group-index");
var newGroupIndex = $(dropEvt.to).parent().data("group-index");
var item = $(dropEvt.item);
var itemData = {name: item.data("name"), address: item.data("address")};
var openDBRequest = window.indexedDB.open("bookmarks");
openDBRequest.onsuccess = function (openEvt) {
var db = openEvt.target.result;
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
groupsStore.get(oldGroupIndex).onsuccess = function (getOldEvt) {
var oldGroupData = getOldEvt.target.result;
groupsStore.get(newGroupIndex).onsuccess = function (getNewEvt) {
var newGroupData = getNewEvt.target.result;
oldGroupData.bookmarks = removeFromArray(oldGroupData.bookmarks, oldIndex);
groupsStore.put(oldGroupData);
newGroupData.bookmarks = addToArray(newGroupData.bookmarks, itemData, newIndex);
groupsStore.put(newGroupData);
db.close();
}
}
}
openDBRequest.onerror = function (err) {
console.error(err);
window.alert("Error moving bookmark");
}
} else if (oldIndex != newIndex) {
var groupIndex = $(dropEvt.from).parent().data("group-index");
var openDBRequest = window.indexedDB.open("bookmarks");
openDBRequest.onsuccess = function (openEvt) {
var db = openEvt.target.result;
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
groupsStore.get(groupIndex).onsuccess = function (getEvt) {
var groupData = getEvt.target.result;
var item = groupData.bookmarks[oldIndex];
groupData.bookmarks = removeFromArray(groupData.bookmarks, oldIndex);
groupData.bookmarks = addToArray(groupData.bookmarks, item, newIndex);
groupsStore.put(groupData);
db.close();
}
}
openDBRequest.onerror = function (err) {
console.error(err);
window.alert("Error moving bookmark");
}
}
}
function deleteBookmark(e) {
var item = $(this).parent();
var groupName = item.parent().parent().data("group-name");
var groupIndex = item.parent().parent().data("group-index");
var bookmarkIndex = item.index();
var bookmarkItem = $("#" + groupName + "-" + bookmarkIndex);
var openDBRequest = window.indexedDB.open("bookmarks");
openDBRequest.onsuccess = function (openEvt) {
var db = openEvt.target.result;
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
groupsStore.get(groupIndex).onsuccess = function (getEvt) {
var groupData = getEvt.target.result;
groupData.bookmarks = removeFromArray(groupData.bookmarks, bookmarkIndex);
groupsStore.put(groupData);
bookmarkItem.hide(300, "swing", function () { bookmarkItem.remove(); });
db.close();
}
}
openDBRequest.onerror = function (err) {
console.log("Error", err);
window.alert("There was an error deleting the bookmark");
}
}
function deleteGroup() {
var group = $(this); // the delete group button
var groupIndex = group.parent().parent().data("group-index");
var openDBRequest = window.indexedDB.open("bookmarks");
openDBRequest.onsuccess = function (openEvt) {
var db = openEvt.target.result;
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
var lastIndex = -1;
groupsStore.openCursor().onsuccess = function (cursorEvt) {
var cursor = cursorEvt.target.result;
if (cursor) {
var item = cursor.value;
var cardContainer = $("#group-" + item.groupIndex);
if (item.groupIndex > lastIndex)
lastIndex = item.groupIndex;
if (item.groupIndex == groupIndex) {
cardContainer.hide(300, "swing", function () { cardContainer.remove(); });
}
if (item.groupIndex > groupIndex) {
item.groupIndex--;
groupsStore.put(item);
$(cardContainer.children()[0]).attr("data-group-index", item.groupIndex);
cardContainer.attr("id", "group-" + item.groupIndex);
}
cursor.continue();
} else {
groupsStore.delete(lastIndex);
db.close();
}
}
}
openDBRequest.onerror = function (err) {
console.error(err);
window.alert("There was an error deleting the group");
}
}
function enableEditing() { function enableEditing() {
$("#btnEdit").removeClass("btn-light btn-dark").addClass("btn-warning"); $("#btnEdit").removeClass("btn-light btn-dark").addClass("btn-warning");
$("#btnImport").prop("disabled", true); $("#btnImport").prop("disabled", true);
@ -292,34 +66,3 @@ function disableEditing() {
$(".btnDel").off("click", deleteBookmark); $(".btnDel").off("click", deleteBookmark);
$(".btnDelGroup").off("click", deleteGroup); $(".btnDelGroup").off("click", deleteGroup);
} }
function removeFromArray(arr, index) {
var newArr = [];
arr.forEach(function (item, i) {
if (i != index)
newArr.push(item);
});
return newArr;
}
function addToArray(arr, itemToAdd, index) {
if (index == arr.length) {
arr.push(item);
return arr;
}
var newArr = [];
arr.forEach(function (item, i) {
if (i == index)
newArr.push(itemToAdd);
newArr.push(item);
});
return newArr;
}
function disableLink(e) {
e.preventDefault();
}

View file

@ -55,3 +55,34 @@ function setBackgroundByTime(hours, mins) {
var index = Math.floor(total / period); var index = Math.floor(total / period);
setBackground(index); setBackground(index);
} }
function removeFromArray(arr, index) {
var newArr = [];
arr.forEach(function (item, i) {
if (i != index)
newArr.push(item);
});
return newArr;
}
function addToArray(arr, itemToAdd, index) {
if (index == arr.length) {
arr.push(item);
return arr;
}
var newArr = [];
arr.forEach(function (item, i) {
if (i == index)
newArr.push(itemToAdd);
newArr.push(item);
});
return newArr;
}
function disableLink(e) {
e.preventDefault();
}

148
js/moveBookmarks.js Normal file
View file

@ -0,0 +1,148 @@
function groupMoved(dropEvt) {
var newIndex = dropEvt.newIndex;
var oldIndex = dropEvt.oldIndex;
var movedCard = $($(dropEvt.item).children()[0]);
var groupName = movedCard.data("group-name");
if (oldIndex != newIndex) {
var openDBRequest = window.indexedDB.open("bookmarks");
openDBRequest.onsuccess = function (openEvt) {
var db = openEvt.target.result;
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
// build an array of all groups
var groups = [];
groupsStore.openCursor().onsuccess = function (cursorEvt) {
var cursor = cursorEvt.target.result;
if (cursor) {
groups.push(cursor.value);
cursor.continue();
} else {
rearrangeGroups(groupsStore, groups, newIndex, oldIndex);
db.close();
}
}
}
openDBRequest.onerror = function (err) {
console.log(err);
window.alert("Error moving group");
}
}
}
function rearrangeGroups(groupsStore, groups, newIndex, oldIndex) {
var movedGroup = $("#group-" + oldIndex);
if (newIndex > oldIndex) {
for (var i = 0; i < groups.length; i++) {
var g = groups[i];
if (g.groupIndex > oldIndex && g.groupIndex <= newIndex) {
g.groupIndex--;
groupsStore.put(g);
// modify the group's card
var cardContainer = $("#group-" + (g.groupIndex + 1))
.attr("id", "group" + g.groupIndex);
$(cardContainer.children()[0]) // the card
.attr("data-group-index", g.groupIndex);
}
}
} else { // oldIndex > newIndex
for (var i = 0; i < groups.length; i++) {
var g = groups[i];
if (g.groupIndex < oldIndex && g.groupIndex >= newIndex) {
g.groupIndex++;
groupsStore.put(g);
// modify the group's card
var cardContainer = $("#group-" + (g.groupIndex - 1))
.attr("id", "group" + g.groupIndex);
$(cardContainer.children()[0]) // the card
.attr("data-group-index", g.groupIndex);
}
}
}
// update the moved group's data
var movedGroupData = groups[oldIndex];
movedGroupData.groupIndex = newIndex;
groupsStore.put(movedGroupData);
// update the group's card
movedGroup.attr("id", "group" + newIndex);
$(movedGroup.children()[0]).attr("data-group-index", newIndex);
}
function bookmarkMoved(dropEvt) {
var oldIndex = dropEvt.oldIndex;
var newIndex = dropEvt.newIndex;
if (dropEvt.from != dropEvt.to) {
var oldGroupIndex = $(dropEvt.from).parent().data("group-index");
var newGroupIndex = $(dropEvt.to).parent().data("group-index");
var item = $(dropEvt.item);
var itemData = {name: item.data("name"), address: item.data("address")};
var openDBRequest = window.indexedDB.open("bookmarks");
openDBRequest.onsuccess = function (openEvt) {
var db = openEvt.target.result;
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
groupsStore.get(oldGroupIndex).onsuccess = function (getOldEvt) {
var oldGroupData = getOldEvt.target.result;
groupsStore.get(newGroupIndex).onsuccess = function (getNewEvt) {
var newGroupData = getNewEvt.target.result;
oldGroupData.bookmarks = removeFromArray(oldGroupData.bookmarks, oldIndex);
groupsStore.put(oldGroupData);
newGroupData.bookmarks = addToArray(newGroupData.bookmarks, itemData, newIndex);
groupsStore.put(newGroupData);
db.close();
}
}
}
openDBRequest.onerror = function (err) {
console.error(err);
window.alert("Error moving bookmark");
}
} else if (oldIndex != newIndex) {
var groupIndex = $(dropEvt.from).parent().data("group-index");
var openDBRequest = window.indexedDB.open("bookmarks");
openDBRequest.onsuccess = function (openEvt) {
var db = openEvt.target.result;
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
groupsStore.get(groupIndex).onsuccess = function (getEvt) {
var groupData = getEvt.target.result;
var item = groupData.bookmarks[oldIndex];
groupData.bookmarks = removeFromArray(groupData.bookmarks, oldIndex);
groupData.bookmarks = addToArray(groupData.bookmarks, item, newIndex);
groupsStore.put(groupData);
db.close();
}
}
openDBRequest.onerror = function (err) {
console.error(err);
window.alert("Error moving bookmark");
}
}
}