NewTabPage/js/editBookmarks.js

326 lines
8.4 KiB
JavaScript
Raw Normal View History

$(document).ready(function () {
2017-12-14 22:13:26 +00:00
$("#btnEdit").click(toggleEditing);
});
2017-12-14 22:13:26 +00:00
2018-01-02 00:55:44 +00:00
function toggleEditing () {
2017-12-30 04:51:47 +00:00
if ($("#btnEdit").hasClass("btn-warning"))
disableEditing();
else
enableEditing();
2017-12-14 22:13:26 +00:00
}
2017-12-28 02:58:57 +00:00
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");
2018-01-02 00:55:44 +00:00
openDBRequest.onsuccess = function (openEvt) {
var db = openEvt.target.result;
2017-12-28 02:58:57 +00:00
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();
}
2017-12-28 02:58:57 +00:00
}
}
2018-01-02 00:55:44 +00:00
openDBRequest.onerror = function (err) {
console.log(err);
window.alert("Error moving group");
}
2017-12-28 02:58:57 +00:00
}
}
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);
}
2017-12-30 04:24:32 +00:00
function bookmarkMoved(dropEvt) {
var oldIndex = dropEvt.oldIndex;
var newIndex = dropEvt.newIndex;
if (dropEvt.from != dropEvt.to) {
2017-12-30 05:09:20 +00:00
var oldGroupIndex = $(dropEvt.from).parent().data("group-index");
var newGroupIndex = $(dropEvt.to).parent().data("group-index");
2017-12-30 04:24:32 +00:00
var item = $(dropEvt.item);
var itemData = {name: item.data("name"), address: item.data("address")};
var openDBRequest = window.indexedDB.open("bookmarks");
2018-01-02 00:55:44 +00:00
openDBRequest.onsuccess = function (openEvt) {
var db = openEvt.target.result;
2017-12-30 04:24:32 +00:00
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
groupsStore.get(oldGroupIndex).onsuccess = function (getOldEvt) {
var oldGroupData = getOldEvt.target.result;
2017-12-30 04:24:32 +00:00
groupsStore.get(newGroupIndex).onsuccess = function (getNewEvt) {
var newGroupData = getNewEvt.target.result;
2017-12-30 04:24:32 +00:00
oldGroupData.bookmarks = removeFromArray(oldGroupData.bookmarks, oldIndex);
groupsStore.put(oldGroupData);
2017-12-30 04:24:32 +00:00
newGroupData.bookmarks = addToArray(newGroupData.bookmarks, itemData, newIndex);
groupsStore.put(newGroupData);
2017-12-30 04:24:32 +00:00
db.close();
}
2017-12-30 04:24:32 +00:00
}
}
2018-01-02 00:55:44 +00:00
openDBRequest.onerror = function (err) {
console.error(err);
window.alert("Error moving bookmark");
}
2017-12-30 04:24:32 +00:00
} else if (oldIndex != newIndex) {
2017-12-30 05:09:20 +00:00
var groupIndex = $(dropEvt.from).parent().data("group-index");
2017-12-30 04:24:32 +00:00
var openDBRequest = window.indexedDB.open("bookmarks");
2018-01-02 00:55:44 +00:00
openDBRequest.onsuccess = function (openEvt) {
var db = openEvt.target.result;
2017-12-30 04:24:32 +00:00
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
2018-01-02 00:55:44 +00:00
groupsStore.get(groupIndex).onsuccess = function (getEvt) {
var groupData = getEvt.target.result;
2017-12-30 04:24:32 +00:00
var item = groupData.bookmarks[oldIndex];
groupData.bookmarks = removeFromArray(groupData.bookmarks, oldIndex);
groupData.bookmarks = addToArray(groupData.bookmarks, item, newIndex);
groupsStore.put(groupData);
db.close();
}
}
2018-01-02 00:55:44 +00:00
openDBRequest.onerror = function (err) {
console.error(err);
window.alert("Error moving bookmark");
}
2017-12-30 04:24:32 +00:00
}
}
2017-12-14 22:13:26 +00:00
function deleteBookmark(e) {
2017-12-30 04:52:32 +00:00
var item = $(this).parent();
2017-12-30 05:09:20 +00:00
var groupName = item.parent().parent().data("group-name");
var groupIndex = item.parent().parent().data("group-index");
2017-12-30 04:52:32 +00:00
var bookmarkIndex = item.index();
var bookmarkItem = $("#" + groupName + "-" + bookmarkIndex);
2017-12-14 22:13:26 +00:00
var openDBRequest = window.indexedDB.open("bookmarks");
2018-01-02 00:55:44 +00:00
openDBRequest.onsuccess = function (openEvt) {
var db = openEvt.target.result;
2017-12-27 23:27:08 +00:00
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
groupsStore.get(groupIndex).onsuccess = function (getEvt) {
var groupData = getEvt.target.result;
2017-12-30 04:52:32 +00:00
groupData.bookmarks = removeFromArray(groupData.bookmarks, bookmarkIndex);
2017-12-27 23:27:08 +00:00
groupsStore.put(groupData);
bookmarkItem.hide(300, "swing", function () { bookmarkItem.remove(); });
2018-01-02 00:55:44 +00:00
db.close();
2017-12-14 22:13:26 +00:00
}
}
2018-01-02 00:55:44 +00:00
openDBRequest.onerror = function (err) {
console.log("Error", err);
2017-12-27 23:27:08 +00:00
window.alert("There was an error deleting the bookmark");
2017-12-14 22:13:26 +00:00
}
}
2018-01-02 00:55:44 +00:00
function deleteGroup() {
2017-12-14 22:13:26 +00:00
var group = $(this); // the delete group button
2017-12-30 05:09:20 +00:00
var groupIndex = group.parent().parent().data("group-index");
2017-12-14 22:13:26 +00:00
2017-12-27 23:27:08 +00:00
var openDBRequest = window.indexedDB.open("bookmarks");
2017-12-14 22:13:26 +00:00
2018-01-02 00:55:44 +00:00
openDBRequest.onsuccess = function (openEvt) {
var db = openEvt.target.result;
2017-12-27 23:27:08 +00:00
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();
}
2017-12-27 23:27:08 +00:00
}
2017-12-14 22:13:26 +00:00
}
2018-01-02 00:55:44 +00:00
openDBRequest.onerror = function (err) {
console.error(err);
2017-12-27 23:27:08 +00:00
window.alert("There was an error deleting the group");
2017-12-14 22:13:26 +00:00
}
}
2017-12-30 04:51:47 +00:00
function enableEditing() {
$("#btnEdit").removeClass("btn-light btn-dark").addClass("btn-warning");
$("#btnImport").prop("disabled", true);
$("#btnAdd").prop("disabled", true);
2018-01-02 00:55:44 +00:00
$(".bookmarkGroup").each(function (index, item) {
var item = $(item);
2017-12-30 04:51:47 +00:00
item.sortable({
group: { name: "bookmarkLists", pull: true, put: true },
draggable: ".bookmark",
handle: ".dragHandle",
animation: 100,
onEnd: bookmarkMoved
});
});
$("#cardList").sortable({
group: { name: "bookmarksGroups" },
draggable: ".bookmarkGroupContainer",
handle: ".dragGroupHandle",
animation: 100,
onEnd: groupMoved
});
$(".btnDel").show(200);
$(".btnDelGroup").show(200);
$(".dragHandle").show(200);
$(".dragGroupHandle").show(200);
$(".bookmark").click(disableLink);
$(".btnDel").click(deleteBookmark);
$(".btnDelGroup").click(deleteGroup);
}
function disableEditing() {
2018-01-02 00:55:44 +00:00
$(".bookmarkGroup").each(function (index, item) {
$(item).sortable("destroy");
2017-12-30 04:51:47 +00:00
});
$("#cardList").sortable("destroy");
$("#btnEdit").removeClass("btn-warning");
$("#btnImport").prop("disabled", false);
$("#btnAdd").prop("disabled", false);
if ($("#btnAbout").hasClass("btn-light")) {
$("#btnEdit").addClass("btn-light");
} else {
$("#btnEdit").addClass("btn-dark");
}
$(".btnDel").hide(200);
$(".btnDelGroup").hide(200);
$(".dragHandle").hide(200);
$(".dragGroupHandle").hide(200);
$(".bookmark").off("click", disableLink);
$(".btnDel").off("click", deleteBookmark);
$(".btnDelGroup").off("click", deleteGroup);
}
2017-12-30 04:24:32 +00:00
function removeFromArray(arr, index) {
var newArr = [];
2018-01-02 00:55:44 +00:00
arr.forEach(function (item, i) {
2017-12-30 04:24:32 +00:00
if (i != index)
2018-01-02 00:55:44 +00:00
newArr.push(item);
});
2017-12-30 04:24:32 +00:00
return newArr;
}
2018-01-02 00:55:44 +00:00
function addToArray(arr, itemToAdd, index) {
2017-12-30 04:24:32 +00:00
if (index == arr.length) {
arr.push(item);
return arr;
}
var newArr = [];
2018-01-02 00:55:44 +00:00
arr.forEach(function (item, i) {
2017-12-30 04:24:32 +00:00
if (i == index)
2018-01-02 00:55:44 +00:00
newArr.push(itemToAdd);
2017-12-30 04:24:32 +00:00
2018-01-02 00:55:44 +00:00
newArr.push(item);
});
2017-12-30 04:24:32 +00:00
return newArr;
}
2017-12-14 22:13:26 +00:00
function disableLink(e) {
e.preventDefault();
}