NewTabPage/js/loadBookmarks.js

119 lines
3.2 KiB
JavaScript
Raw Normal View History

$(document).ready(function () {
loadBookmarks();
});
function loadBookmarks() {
$("#cardList").empty();
var openDBRequest = window.indexedDB.open("bookmarks");
2018-01-02 00:55:44 +00:00
openDBRequest.onsuccess = function (openEvt) {
db = openEvt.target.result;
var groupsStore = db.transaction("Groups", "readonly").objectStore("Groups");
groupsStore.count().onsuccess = function (countEvt) {
var numGroups = countEvt.target.result;
// use a placeholder to prevent problems with sortable
2017-12-27 23:27:08 +00:00
var cardList = $("#cardList");
for (var i = 0; i < numGroups; i++) {
$("<div>").attr("id", "group-" + i)
2017-12-27 23:27:08 +00:00
.addClass("bookmarkGroupContainer")
.appendTo(cardList);
}
groupsStore.openCursor().onsuccess = function (cursorEvt) {
var cursor = cursorEvt.target.result;
if (cursor) {
var groupData = cursor.value;
buildCard(groupData.title, groupData.groupIndex, groupData.bookmarks)
.appendTo($("#group-" + groupData.groupIndex));
cursor.continue();
} else {
db.close();
}
}
}
}
2018-01-02 00:55:44 +00:00
openDBRequest.onupgradeneeded = function (openEvt) {
db = openEvt.target.result;
2018-01-02 00:55:44 +00:00
var groupStore = db.createObjectStore("Groups", { keyPath: "groupIndex" });
groupStore.createIndex("groupIndex", "groupIndex", { unique: true });
groupStore.createIndex("title", "title", { unique: false });
groupStore.createIndex("bookmarks", "bookmarks", { unique: false });
2017-12-27 23:27:08 +00:00
var groupData = {
groupIndex: 0,
title: "Examples",
bookmarks: [
{
"name": "Github",
"address": "https://github.com/"
},
{
"name": "This project on Github",
"address": "https://github.com/NeilBrommer/NewTabPage"
}
]
};
2017-12-27 23:27:08 +00:00
groupStore.add(groupData);
$("#aboutModal").modal("show");
}
2018-01-02 00:55:44 +00:00
openDBRequest.onerror = function (err) {
2018-01-02 01:05:28 +00:00
console.error(err);
2018-01-02 00:55:44 +00:00
window.alert("Error loading bookmarks");
}
2017-12-14 22:13:26 +00:00
}
2017-12-27 23:27:08 +00:00
function buildCard(title, groupIndex, itemList) {
2017-12-14 22:13:26 +00:00
var card = $("<div>");
card.attr({
2017-12-30 04:24:32 +00:00
"id": "group-" + title,
2017-12-27 23:27:08 +00:00
"class": "card",
"data-group-name": title,
"data-group-index": groupIndex
});
2018-01-02 00:55:44 +00:00
var cardHead = $("<div>").addClass("card-header").text(title).appendTo(card);
$("<span>") // use wrapper, sortable has issues with <svg> dragHandle
.addClass("mr-2 start-hidden dragGroupHandle")
.append($("<span>").addClass("fas fa-bars")) // move group icon
.prependTo(cardHead);
$("<span>") // delete group button
.addClass("btnDelGroup far fa-trash-alt float-right mt-1 start-hidden text-danger clickable")
.appendTo(cardHead);
var cardList = $("<div>")
.addClass("list-group list-group-flush bookmarkGroup")
.appendTo(card);
itemList.forEach(function (item, i) {
var handle = $("<span>") // use wrapper, sortable has issues with <svg> dragHandle
.addClass("mr-2 start-hidden dragHandle")
2017-12-27 23:27:08 +00:00
.append($("<span>").addClass("fas fa-bars"));
2017-12-14 22:13:26 +00:00
var del = $("<span>")
.addClass("btnDel far fa-trash-alt float-right mt-1 start-hidden text-danger");
del.css("cursor: pointer;");
$("<a>")
.attr({
2017-12-27 23:27:08 +00:00
"id": title + "-" + i,
2017-12-14 22:13:26 +00:00
"class": "list-group-item list-group-item-action bookmark",
"href": item.address,
2017-12-27 23:27:08 +00:00
"data-name": item.name,
"data-address": item.address
2017-12-14 22:13:26 +00:00
})
.text(item.name)
2017-12-27 23:27:08 +00:00
.prepend(handle)
2017-12-14 22:13:26 +00:00
.append(del)
.appendTo(cardList);
2018-01-02 00:55:44 +00:00
});
return card;
}