2017-12-13 00:41:59 +00:00
|
|
|
$(document).ready(function () {
|
2017-12-14 22:13:26 +00:00
|
|
|
$("#btnEdit").click(toggleEditing);
|
2017-12-13 00:41:59 +00:00
|
|
|
});
|
2017-12-14 22:13:26 +00:00
|
|
|
|
|
|
|
function toggleEditing (e) {
|
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");
|
|
|
|
var groupIndex = movedCard.data("group-index");
|
|
|
|
|
|
|
|
if (oldIndex != newIndex) {
|
|
|
|
var openDBRequest = window.indexedDB.open("bookmarks");
|
|
|
|
|
|
|
|
openDBRequest.onsuccess = function (e) {
|
|
|
|
var db = e.target.result;
|
|
|
|
|
|
|
|
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
|
|
|
|
groupsStore.getAll().onsuccess = function (evt) {
|
|
|
|
var groups = evt.target.result;
|
|
|
|
|
2017-12-28 21:41:36 +00:00
|
|
|
if (newIndex > oldIndex) {
|
|
|
|
for (let g of groups) {
|
|
|
|
if (g.groupIndex > oldIndex && g.groupIndex <= newIndex) {
|
|
|
|
g.groupIndex--;
|
|
|
|
groupsStore.put(g);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // oldIndex > newIndex
|
|
|
|
for (let g of groups) {
|
|
|
|
if (g.groupIndex < oldIndex && g.groupIndex >= newIndex) {
|
|
|
|
g.groupIndex++;
|
|
|
|
groupsStore.put(g);
|
|
|
|
}
|
2017-12-28 02:58:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
groups[oldIndex].groupIndex = newIndex;
|
|
|
|
groupsStore.put(groups[oldIndex]);
|
|
|
|
|
|
|
|
db.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
openDBRequest.onerror = function (err) { console.log(err); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
openDBRequest.onsuccess = function (dbe) {
|
|
|
|
var db = dbe.target.result;
|
|
|
|
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
|
|
|
|
|
|
|
|
groupsStore.getAll().onsuccess = function (gete) {
|
|
|
|
var groups = gete.target.result;
|
|
|
|
|
|
|
|
var oldGroupData = groups[oldGroupIndex];
|
|
|
|
var newGroupData = groups[newGroupIndex];
|
|
|
|
|
|
|
|
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); }
|
|
|
|
} 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");
|
|
|
|
openDBRequest.onsuccess = function (dbe) {
|
|
|
|
var db = dbe.target.result;
|
|
|
|
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
|
|
|
|
|
|
|
|
groupsStore.get(groupIndex).onsuccess = function (gete) {
|
|
|
|
var groupData = gete.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); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
openDBRequest.onsuccess = function (e) {
|
|
|
|
var db = e.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.remove();
|
2017-12-14 22:13:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
openDBRequest.onerror = function (evt) {
|
|
|
|
console.log("Error", evt);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteGroup(e) {
|
|
|
|
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
|
|
|
|
2017-12-27 23:27:08 +00:00
|
|
|
openDBRequest.onsuccess = function (dbe) {
|
|
|
|
var db = dbe.target.result;
|
2017-12-14 22:13:26 +00:00
|
|
|
|
2017-12-27 23:27:08 +00:00
|
|
|
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
|
2017-12-28 00:01:12 +00:00
|
|
|
|
|
|
|
groupsStore.getAll().onsuccess = function (getEvt) {
|
|
|
|
var groups = getEvt.target.result;
|
|
|
|
|
|
|
|
var lastIndex = -1;
|
2017-12-28 02:58:57 +00:00
|
|
|
for (let item of groups) {
|
2017-12-28 00:01:12 +00:00
|
|
|
if (item.groupIndex > groupIndex) {
|
|
|
|
lastIndex = item.groupIndex;
|
|
|
|
item.groupIndex--;
|
|
|
|
groupsStore.put(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
groupsStore.delete(lastIndex);
|
|
|
|
loadBookmarks();
|
2017-12-27 23:27:08 +00:00
|
|
|
}
|
2017-12-14 22:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
openDBRequest.onerror = function (e) {
|
|
|
|
console.error(e);
|
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);
|
|
|
|
|
|
|
|
$(".bookmarkGroup").each(function (index) {
|
|
|
|
var item = $(this);
|
|
|
|
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() {
|
|
|
|
$(".bookmarkGroup").each(function (index) {
|
|
|
|
$(this).sortable("destroy");
|
|
|
|
});
|
|
|
|
$("#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 = [];
|
|
|
|
for (var i = 0; i < arr.length; i++) {
|
|
|
|
if (i != index)
|
|
|
|
newArr.push(arr[i]);
|
|
|
|
}
|
|
|
|
return newArr;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addToArray(arr, item, index) {
|
|
|
|
if (index == arr.length) {
|
|
|
|
arr.push(item);
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
var newArr = [];
|
|
|
|
for (var i = 0; i < arr.length; i++) {
|
|
|
|
if (i == index)
|
|
|
|
newArr.push(item);
|
|
|
|
|
|
|
|
newArr.push(arr[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newArr;
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:13:26 +00:00
|
|
|
function disableLink(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|