NewTabPage/js/addBookmarks.js

113 lines
2.9 KiB
JavaScript
Raw Normal View History

$(document).ready(function () {
2018-01-02 00:55:44 +00:00
$("#newBookmarkModal").on("shown.bs.modal", populateGroupList);
$("#addBookmarkForm").submit(addNewBookmark);
$("#newBookmarkGroup").on("change", selectGroupChanged);
});
2018-01-02 00:55:44 +00:00
function populateGroupList() {
var combo = $("#newBookmarkGroup");
combo.empty();
combo.append($("<option>").attr({ "value": "--" })
.text("New Group"));
var openDBRequest = window.indexedDB.open("bookmarks");
openDBRequest.onsuccess = function (openEvt) {
var db = openEvt.target.result;
var groupsStore = db.transaction("Groups", "readwrite").objectStore("Groups");
groupsStore.getAll().onsuccess = function (getAllEvt) {
var groups = getAllEvt.target.result;
for (let group of groups) {
combo.append($("<option>")
.attr({ "value": group.groupIndex })
.text(group.title));
}
$("#createGroup").prop("required", true);
db.close();
}
}
openDBRequest.onerror = function (err) {
console.error(err);
window.alert("Error building groups list");
}
}
function addNewBookmark(e) {
e.preventDefault();
// read in data from the form
var bkmkName = $("#newBookmarkName").val();
var bkmkAddress = $("#newBookmarkURL").val();
var bkmkGroup = $("#newBookmarkGroup").val();
if (bkmkGroup == "--") { // create a new group
var newGroupName = $("#newBookMarkGroupNew").val();
2017-12-27 23:27:08 +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");
2018-01-02 00:55:44 +00:00
groupsStore.count().onsuccess = function (countEvt) {
var numGroups = countEvt.target.result;
2017-12-27 23:27:08 +00:00
var newGroup = {
2018-01-02 00:55:44 +00:00
groupIndex: numGroups,
2017-12-27 23:27:08 +00:00
title: newGroupName,
bookmarks: [ { name: bkmkName, address: bkmkAddress } ]
};
groupsStore.add(newGroup);
2017-12-14 22:13:26 +00:00
db.close();
loadBookmarks();
2018-01-02 00:55:44 +00:00
$("#newBookmarkModal").modal("hide");
2017-12-14 22:13:26 +00:00
}
}
2018-01-02 00:55:44 +00:00
openDBRequest.onerror = function (err) {
console.log(err);
window.alert("There was an error creating the bookmark");
}
} else { // add to existing group
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");
2017-12-27 23:27:08 +00:00
var groupReq = groupsStore.get(parseInt(bkmkGroup));
groupReq.onsuccess = function (evt) {
var group = groupReq.result;
var newItem = { "name": bkmkName, "address": bkmkAddress };
group.bookmarks.push(newItem);
groupsStore.put(group);
2017-12-27 23:27:08 +00:00
db.close();
loadBookmarks();
$(".modal").modal("hide");
}
}
2018-01-02 00:55:44 +00:00
openDBRequest.onerror = function (err) {
console.log(err);
window.alert("There was an error creating the bookmark");
}
}
}
function selectGroupChanged(value) {
var groupText = $("#createGroup");
if (value == "--") {
groupText.show();
groupText.prop("required", true);
} else {
groupText.hide();
groupText.prop("required", false);
}
}