1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-12-23 16:40:31 +00:00
calibre-web/cps/static/js/table.js

384 lines
14 KiB
JavaScript
Raw Normal View History

/* This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
* Copyright (C) 2020 OzzieIsaacs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2020-06-11 19:19:09 +00:00
/* exported TableActions, RestrictionActions, EbookActions, responseHandler */
2021-03-14 13:29:40 +00:00
/* global getPath, confirmDialog */
2020-06-11 19:19:09 +00:00
var selections = [];
2018-08-28 07:42:19 +00:00
$(function() {
2020-06-11 19:19:09 +00:00
$("#books-table").on("check.bs.table check-all.bs.table uncheck.bs.table uncheck-all.bs.table",
function (e, rowsAfter, rowsBefore) {
var rows = rowsAfter;
if (e.type === "uncheck-all") {
rows = rowsBefore;
}
var ids = $.map(!$.isArray(rows) ? [rows] : rows, function (row) {
return row.id;
});
var func = $.inArray(e.type, ["check", "check-all"]) > -1 ? "union" : "difference";
selections = window._[func](selections, ids);
if (selections.length >= 2) {
$("#merge_books").removeClass("disabled");
$("#merge_books").attr("aria-disabled", false);
} else {
$("#merge_books").addClass("disabled");
$("#merge_books").attr("aria-disabled", true);
}
2020-06-12 14:15:54 +00:00
if (selections.length < 1) {
$("#delete_selection").addClass("disabled");
$("#delete_selection").attr("aria-disabled", true);
} else {
2020-06-12 14:15:54 +00:00
$("#delete_selection").removeClass("disabled");
$("#delete_selection").attr("aria-disabled", false);
}
2020-06-11 19:19:09 +00:00
});
2020-06-12 14:15:54 +00:00
$("#delete_selection").click(function() {
$("#books-table").bootstrapTable("uncheckAll");
2020-06-12 14:15:54 +00:00
});
2020-06-11 19:19:09 +00:00
2020-06-20 11:46:12 +00:00
$("#merge_confirm").click(function() {
2020-06-11 19:19:09 +00:00
$.ajax({
method:"post",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: window.location.pathname + "/../../ajax/mergebooks",
data: JSON.stringify({"Merge_books":selections}),
success: function success() {
$("#books-table").bootstrapTable("refresh");
$("#books-table").bootstrapTable("uncheckAll");
}
});
});
$("#merge_books").click(function() {
$.ajax({
method:"post",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: window.location.pathname + "/../../ajax/simulatemerge",
data: JSON.stringify({"Merge_books":selections}),
success: function success(booTitles) {
$.each(booTitles.from, function(i, item) {
$("<span>- " + item + "</span>").appendTo("#merge_from");
});
$("#merge_to").text("- " + booTitles.to);
2020-06-11 19:19:09 +00:00
}
});
});
var column = [];
$("#books-table > thead > tr > th").each(function() {
var element = {};
if ($(this).attr("data-edit")) {
element = {
editable: {
mode: "inline",
emptytext: "<span class='glyphicon glyphicon-plus'></span>",
}
};
}
var validateText = $(this).attr("data-edit-validate");
if (validateText) {
element.editable.validate = function (value) {
if ($.trim(value) === "") return validateText;
};
2020-06-11 19:19:09 +00:00
}
column.push(element);
2020-06-11 19:19:09 +00:00
});
2020-06-12 11:45:07 +00:00
$("#books-table").bootstrapTable({
2020-06-11 19:19:09 +00:00
sidePagination: "server",
pagination: true,
paginationLoop: false,
2020-06-11 19:19:09 +00:00
paginationDetailHAlign: " hidden",
paginationHAlign: "left",
idField: "id",
2020-06-12 11:45:07 +00:00
uniqueId: "id",
2020-06-11 19:19:09 +00:00
search: true,
showColumns: true,
searchAlign: "left",
showSearchButton : false,
searchOnEnterKey: true,
checkboxHeader: false,
maintainMetaData: true,
responseHandler: responseHandler,
columns: column,
2020-06-06 19:21:10 +00:00
formatNoMatches: function () {
return "";
2020-06-11 19:19:09 +00:00
},
// eslint-disable-next-line no-unused-vars
2020-06-12 11:45:07 +00:00
onEditableSave: function (field, row, oldvalue, $el) {
if (field === "title" || field === "authors") {
$.ajax({
method:"get",
dataType: "json",
url: window.location.pathname + "/../../ajax/sort_value/" + field + "/" + row.id,
success: function success(data) {
var key = Object.keys(data)[0];
$("#books-table").bootstrapTable("updateCellByUniqueId", {
id: row.id,
field: key,
value: data[key]
});
// console.log(data);
}
});
}
2020-06-12 11:45:07 +00:00
},
// eslint-disable-next-line no-unused-vars
2020-06-12 11:45:07 +00:00
onColumnSwitch: function (field, checked) {
var visible = $("#books-table").bootstrapTable("getVisibleColumns");
var hidden = $("#books-table").bootstrapTable("getHiddenColumns");
var st = "";
2020-06-12 14:15:54 +00:00
visible.forEach(function(item) {
st += "\"" + item.field + "\":\"" + "true" + "\",";
2020-06-12 14:15:54 +00:00
});
hidden.forEach(function(item) {
st += "\"" + item.field + "\":\"" + "false" + "\",";
2020-06-12 14:15:54 +00:00
});
st = st.slice(0, -1);
2020-06-12 11:45:07 +00:00
$.ajax({
method:"post",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: window.location.pathname + "/../../ajax/table_settings",
2020-06-12 14:15:54 +00:00
data: "{" + st + "}",
});
},
2020-06-06 19:21:10 +00:00
});
2020-06-12 11:45:07 +00:00
$("#domain_allow_submit").click(function(event) {
2018-08-28 07:42:19 +00:00
event.preventDefault();
$("#domain_add_allow").ajaxForm();
2018-08-28 07:42:19 +00:00
$(this).closest("form").submit();
$.ajax ({
2018-08-28 07:42:19 +00:00
method:"get",
url: window.location.pathname + "/../../ajax/domainlist/1",
2018-08-28 07:42:19 +00:00
async: true,
timeout: 900,
success:function(data) {
$("#domain-allow-table").bootstrapTable("load", data);
2018-08-28 07:42:19 +00:00
}
});
});
$("#domain-allow-table").bootstrapTable({
formatNoMatches: function () {
return "";
},
striped: false
});
$("#domain_deny_submit").click(function(event) {
event.preventDefault();
$("#domain_add_deny").ajaxForm();
$(this).closest("form").submit();
$.ajax ({
method:"get",
url: window.location.pathname + "/../../ajax/domainlist/0",
async: true,
timeout: 900,
success:function(data) {
$("#domain-deny-table").bootstrapTable("load", data);
}
});
});
$("#domain-deny-table").bootstrapTable({
formatNoMatches: function () {
return "";
2019-03-05 15:31:09 +00:00
},
striped: false
2018-08-28 07:42:19 +00:00
});
2021-03-14 13:29:40 +00:00
function domainHandle(domainId) {
2018-08-28 07:42:19 +00:00
$.ajax({
method:"post",
url: window.location.pathname + "/../../ajax/deletedomain",
data: {"domainid":domainId}
});
$.ajax({
method:"get",
url: window.location.pathname + "/../../ajax/domainlist/1",
2018-08-28 07:42:19 +00:00
async: true,
timeout: 900,
2019-03-05 15:31:09 +00:00
success:function(data) {
$("#domain-allow-table").bootstrapTable("load", data);
}
});
$.ajax({
method:"get",
url: window.location.pathname + "/../../ajax/domainlist/0",
async: true,
timeout: 900,
success:function(data) {
$("#domain-deny-table").bootstrapTable("load", data);
2018-08-28 07:42:19 +00:00
}
});
}
$("#domain-allow-table").on("click-cell.bs.table", function (field, value, row, $element) {
if (value === 2) {
2021-03-14 13:29:40 +00:00
confirmDialog("btndeletedomain", $element.id, domainHandle);
}
2018-08-28 07:42:19 +00:00
});
$("#domain-deny-table").on("click-cell.bs.table", function (field, value, row, $element) {
if (value === 2) {
2021-03-14 13:29:40 +00:00
confirmDialog("btndeletedomain", $element.id, domainHandle);
}
2018-08-28 07:42:19 +00:00
});
2020-01-01 16:26:47 +00:00
$("#restrictModal").on("hidden.bs.modal", function () {
// Destroy table and remove hooks for buttons
$("#restrict-elements-table").unbind();
$("#restrict-elements-table").bootstrapTable("destroy");
$("[id^=submit_]").unbind();
$("#h1").addClass("hidden");
$("#h2").addClass("hidden");
$("#h3").addClass("hidden");
$("#h4").addClass("hidden");
2020-01-01 16:26:47 +00:00
});
2021-03-14 13:29:40 +00:00
function startTable(type, userId) {
2020-01-01 16:26:47 +00:00
$("#restrict-elements-table").bootstrapTable({
formatNoMatches: function () {
return "";
},
2021-03-14 13:29:40 +00:00
url: getPath() + "/ajax/listrestriction/" + type + "/" + userId,
2020-04-27 18:01:13 +00:00
rowStyle: function(row) {
// console.log('Reihe :' + row + " Index :" + index);
if (row.id.charAt(0) === "a") {
return {classes: "bg-primary"};
} else {
return {classes: "bg-dark-danger"};
}
},
2020-04-27 18:01:13 +00:00
onClickCell: function (field, value, row) {
if (field === 3) {
2020-01-01 16:26:47 +00:00
$.ajax ({
type: "Post",
data: "id=" + row.id + "&type=" + row.type + "&Element=" + encodeURIComponent(row.Element),
2021-03-14 13:29:40 +00:00
url: getPath() + "/ajax/deleterestriction/" + type + "/" + userId,
2020-01-01 16:26:47 +00:00
async: true,
timeout: 900,
2020-04-27 18:01:13 +00:00
success:function() {
2020-01-01 16:26:47 +00:00
$.ajax({
method:"get",
2021-03-14 13:29:40 +00:00
url: getPath() + "/ajax/listrestriction/" + type + "/" + userId,
2020-01-01 16:26:47 +00:00
async: true,
timeout: 900,
success:function(data) {
$("#restrict-elements-table").bootstrapTable("load", data);
}
});
}
});
2020-01-01 16:26:47 +00:00
}
},
striped: false
});
$("#restrict-elements-table").removeClass("table-hover");
2020-04-27 18:01:13 +00:00
$("#restrict-elements-table").on("editable-save.bs.table", function (e, field, row) {
$.ajax({
2021-03-14 13:29:40 +00:00
url: getPath() + "/ajax/editrestriction/" + type + "/" + userId,
type: "Post",
data: row
});
});
2020-04-27 18:01:13 +00:00
$("[id^=submit_]").click(function() {
$(this)[0].blur();
2020-01-01 16:26:47 +00:00
$.ajax({
2021-03-14 13:29:40 +00:00
url: getPath() + "/ajax/addrestriction/" + type + "/" + userId,
type: "Post",
2020-01-01 16:26:47 +00:00
data: $(this).closest("form").serialize() + "&" + $(this)[0].name + "=",
success: function () {
$.ajax ({
method:"get",
2021-03-14 13:29:40 +00:00
url: getPath() + "/ajax/listrestriction/" + type + "/" + userId,
async: true,
timeout: 900,
success:function(data) {
$("#restrict-elements-table").bootstrapTable("load", data);
}
2020-01-01 16:26:47 +00:00
});
}
});
return;
});
}
$("#get_column_values").on("click", function() {
startTable(1, 0);
$("#h2").removeClass("hidden");
2020-01-01 16:26:47 +00:00
});
$("#get_tags").on("click", function() {
startTable(0, 0);
$("#h1").removeClass("hidden");
});
$("#get_user_column_values").on("click", function() {
2021-03-14 13:29:40 +00:00
startTable(3, $(this).data("id"));
$("#h4").removeClass("hidden");
2020-01-01 16:26:47 +00:00
});
$("#get_user_tags").on("click", function() {
2021-03-14 13:29:40 +00:00
startTable(2, $(this).data("id"));
$(this)[0].blur();
$("#h3").removeClass("hidden");
});
2018-08-28 07:42:19 +00:00
});
/* Function for deleting domain restrictions */
2020-04-27 18:01:13 +00:00
function TableActions (value, row) {
2018-08-28 07:42:19 +00:00
return [
"<a class=\"danger remove\" data-value=\"" + row.id
+ "\" title=\"Remove\">",
"<i class=\"glyphicon glyphicon-trash\"></i>",
"</a>"
].join("");
}
2020-01-01 16:26:47 +00:00
2020-06-11 19:19:09 +00:00
2020-01-01 16:26:47 +00:00
/* Function for deleting domain restrictions */
2020-04-27 18:01:13 +00:00
function RestrictionActions (value, row) {
2020-01-01 16:26:47 +00:00
return [
"<div class=\"danger remove\" data-restriction-id=\"" + row.id + "\" title=\"Remove\">",
"<i class=\"glyphicon glyphicon-trash\"></i>",
"</div>"
].join("");
}
2020-06-06 19:21:10 +00:00
/* Function for deleting books */
function EbookActions (value, row) {
return [
2020-06-18 18:39:45 +00:00
"<div class=\"book-remove\" data-toggle=\"modal\" data-target=\"#deleteModal\" data-ajax=\"1\" data-delete-id=\"" + row.id + "\" title=\"Remove\">",
2020-06-06 19:21:10 +00:00
"<i class=\"glyphicon glyphicon-trash\"></i>",
"</div>"
].join("");
}
2020-06-11 19:19:09 +00:00
/* Function for keeping checked rows */
function responseHandler(res) {
$.each(res.rows, function (i, row) {
row.state = $.inArray(row.id, selections) !== -1;
});
return res;
}