diff --git a/index.html b/index.html index a352165..c0b1151 100644 --- a/index.html +++ b/index.html @@ -178,6 +178,8 @@ + + diff --git a/js/deleteBookmarks.js b/js/deleteBookmarks.js new file mode 100644 index 0000000..82936a9 --- /dev/null +++ b/js/deleteBookmarks.js @@ -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"); + } +} diff --git a/js/editBookmarks.js b/js/editBookmarks.js index 579bb3a..ecaf163 100644 --- a/js/editBookmarks.js +++ b/js/editBookmarks.js @@ -9,232 +9,6 @@ function toggleEditing () { 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() { $("#btnEdit").removeClass("btn-light btn-dark").addClass("btn-warning"); $("#btnImport").prop("disabled", true); @@ -292,34 +66,3 @@ function disableEditing() { $(".btnDel").off("click", deleteBookmark); $(".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(); -} diff --git a/js/main.js b/js/main.js index 912d239..cfe329d 100644 --- a/js/main.js +++ b/js/main.js @@ -55,3 +55,34 @@ function setBackgroundByTime(hours, mins) { var index = Math.floor(total / period); 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(); +} diff --git a/js/moveBookmarks.js b/js/moveBookmarks.js new file mode 100644 index 0000000..67ba87b --- /dev/null +++ b/js/moveBookmarks.js @@ -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"); + } + } +}