NewTabPage/js/loadBookmarks.js

126 lines
3.3 KiB
JavaScript
Raw Normal View History

$(document).ready(function () {
loadBookmarks();
});
function loadBookmarks() {
$("#cardList").empty();
var openDBRequest = window.indexedDB.open("bookmarks");
openDBRequest.onsuccess = function (dbEvt) {
db = dbEvt.target.result;
dbVersion = db.version;
2017-12-27 23:27:08 +00:00
db.transaction(["Groups"], "readonly").objectStore("Groups").getAll().onsuccess = function (groupsEvt) {
var groups = groupsEvt.target.result;
groups.sort(function (a, b) {
return a.groupIndex - b.groupIndex;
});
2017-12-27 23:27:08 +00:00
// use a placholder
var cardList = $("#cardList");
for (let groupData of groups) {
$("<div>").attr("id", "group-" + groupData.groupIndex)
.addClass("bookmarkGroupContainer")
.appendTo(cardList);
}
2017-12-27 23:27:08 +00:00
bookmarkList = [];
for (let groupData of groups) {
buildGroup(groupData, $("#group-" + groupData.groupIndex));
bookmarkList.push(groupData);
}
db.close();
};
}
openDBRequest.onerror = function (e) { console.log(e); }
openDBRequest.onupgradeneeded = function (e) {
// the database doesn't exist
console.log("Creating database");
db = e.target.result;
2017-12-27 23:27:08 +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});
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");
}
}
function buildGroup(groupInfo, placeholder) {
2017-12-27 23:27:08 +00:00
buildCard(groupInfo.title, groupInfo.groupIndex, groupInfo.bookmarks).appendTo(placeholder);
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
});
2017-12-14 22:13:26 +00:00
var cardHead = $("<div>");
cardHead.addClass("card-header");
cardHead.text(title);
2017-12-27 23:27:08 +00:00
var btnDrag = $("<span>").addClass("mr-2 start-hidden dragGroupHandle")
.append($("<span>").addClass("fas fa-bars"));
2017-12-14 22:13:26 +00:00
var btnDel = $("<span>")
.addClass("btnDelGroup far fa-trash-alt float-right mt-1 start-hidden text-danger clickable");
btnDel.appendTo(cardHead);
2017-12-27 23:27:08 +00:00
btnDrag.prependTo(cardHead);
card.append(cardHead);
2017-12-14 22:13:26 +00:00
var cardList = $("<div>");
2017-12-30 05:09:20 +00:00
cardList.addClass("list-group list-group-flush bookmarkGroup");
card.append(cardList);
for (var i = 0; i < itemList.length; i++) {
var item = itemList[i];
2017-12-27 23:27:08 +00:00
// the fa span gets replaced with an svg element, which causes problems
// with using it as a drag handle, so wrap it in the drag element
var handle = $("<span>").addClass("mr-2 start-hidden dragHandle")
.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);
}
return card;
}