From f6538b6110cb97a4b4496ad38a291829ea09238d Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Sun, 20 Dec 2020 19:17:29 +0100 Subject: [PATCH] New delete user button refactored confirm dialogs --- cps/admin.py | 39 ++++++++++---- cps/static/js/main.js | 87 +++++++++++++++++++++++++++----- cps/static/js/table.js | 22 ++++---- cps/templates/book_edit.html | 3 +- cps/templates/email_edit.html | 15 +----- cps/templates/layout.html | 18 +++---- cps/templates/modal_dialogs.html | 21 +++++++- cps/templates/shelf.html | 10 ++-- cps/templates/user_edit.html | 29 +++-------- 9 files changed, 159 insertions(+), 85 deletions(-) diff --git a/cps/admin.py b/cps/admin.py index 8b3ca247..5831234f 100644 --- a/cps/admin.py +++ b/cps/admin.py @@ -244,6 +244,21 @@ def update_view_configuration(): return view_configuration() +@admi.route("/ajax/loaddialogtexts/") +@login_required +def load_dialogtexts(element_id): + texts = { "header": "", "main": "" } + if element_id == "config_delete_kobo_token": + texts["main"] = _('Do you really want to delete the Kobo Token?') + elif element_id == "btndeletedomain": + texts["main"] = _('Do you really want to delete this domain?') + elif element_id == "btndeluser": + texts["main"] = _('Do you really want to delete this user?') + elif element_id == "delete_shelf": + texts["main"] = _('Are you sure you want to delete this shelf?') + return json.dumps(texts) + + @admi.route("/ajax/editdomain/", methods=['POST']) @login_required @admin_required @@ -283,20 +298,23 @@ def add_domain(allow): @login_required @admin_required def delete_domain(): - domain_id = request.form.to_dict()['domainid'].replace('*', '%').replace('?', '_').lower() - ub.session.query(ub.Registration).filter(ub.Registration.id == domain_id).delete() try: - ub.session.commit() - except OperationalError: - ub.session.rollback() - # If last domain was deleted, add all domains by default - if not ub.session.query(ub.Registration).filter(ub.Registration.allow==1).count(): - new_domain = ub.Registration(domain="%.%",allow=1) - ub.session.add(new_domain) + domain_id = request.form.to_dict()['domainid'].replace('*', '%').replace('?', '_').lower() + ub.session.query(ub.Registration).filter(ub.Registration.id == domain_id).delete() try: ub.session.commit() except OperationalError: ub.session.rollback() + # If last domain was deleted, add all domains by default + if not ub.session.query(ub.Registration).filter(ub.Registration.allow==1).count(): + new_domain = ub.Registration(domain="%.%",allow=1) + ub.session.add(new_domain) + try: + ub.session.commit() + except OperationalError: + ub.session.rollback() + except KeyError: + pass return "" @@ -569,7 +587,8 @@ def list_restriction(res_type): return response @admi.route("/basicconfig/pathchooser/") -@unconfigured +# @unconfigured +@login_required def config_pathchooser(): return pathchooser() diff --git a/cps/static/js/main.js b/cps/static/js/main.js index 3fbaed88..d02d3b58 100644 --- a/cps/static/js/main.js +++ b/cps/static/js/main.js @@ -110,6 +110,34 @@ $(document).ready(function() { } }); +function ConfirmDialog(id, dataValue, yesFn, noFn) { + var pathname = document.getElementsByTagName("script"), src = pathname[pathname.length - 1].src; + var path = src.substring(0, src.lastIndexOf("/")); + var $confirm = $("#GeneralDeleteModal"); + // var dataValue= e.data('value'); // target.data('value'); + $confirm.modal('show'); + $.ajax({ + method:"get", + dataType: "json", + url: path + "/../../ajax/loaddialogtexts/" + id, + success: function success(data) { + $("#header").html(data.header); + $("#text").html(data.main); + } + }); + + + $("#btnConfirmYes").off('click').click(function () { + yesFn(dataValue); + $confirm.modal("hide"); + }); + $("#btnConfirmNo").off('click').click(function () { + if (typeof noFn !== 'undefined') { + noFn(dataValue); + } + $confirm.modal("hide"); + }); +} $("#delete_confirm").click(function() { //get data-id attribute of the clicked element @@ -452,6 +480,52 @@ $(function() { $("#config_delete_kobo_token").show(); }); + $("#config_delete_kobo_token").click(function() { + ConfirmDialog( + $(this).attr('id'), + $(this).data('value'), + function (value) { + var pathname = document.getElementsByTagName("script"); + var src = pathname[pathname.length - 1].src; + var path = src.substring(0, src.lastIndexOf("/")); + $.ajax({ + method: "get", + url: path + "/../../kobo_auth/deleteauthtoken/" + value, + }); + $("#config_delete_kobo_token").hide(); + } + ); + }); + + + $("#btndeluser").click(function() { + ConfirmDialog( + $(this).attr('id'), + $(this).data('value'), + function(value){ + var subform = $('#user_submit').closest("form"); + subform.submit(function(eventObj) { + $(this).append(''); + return true; + }); + subform.submit(); + } + ); + }); + $("#user_submit").click(function() { + this.closest("form").submit(); + }); + + $("#delete_shelf").click(function() { + ConfirmDialog( + $(this).attr('id'), + $(this).data('value'), + function(value){ + window.location.href = window.location.pathname + "/../../shelf/delete/" + value + } + ); + + }); $("#fileModal").on("show.bs.modal", function(e) { @@ -487,19 +561,6 @@ $(function() { } }); - $("#btndeletetoken").click(function() { - //get data-id attribute of the clicked element - var pathname = document.getElementsByTagName("script"), src = pathname[pathname.length - 1].src; - var path = src.substring(0, src.lastIndexOf("/")); - $.ajax({ - method:"get", - url: path + "/../../kobo_auth/deleteauthtoken/" + this.value, - }); - $("#modalDeleteToken").modal("hide"); - $("#config_delete_kobo_token").hide(); - - }); - $(window).resize(function() { $(".discover .row").isotope("layout"); }); diff --git a/cps/static/js/table.js b/cps/static/js/table.js index eb349c2b..7fdf9e6a 100644 --- a/cps/static/js/table.js +++ b/cps/static/js/table.js @@ -208,15 +208,13 @@ $(function() { }, striped: false }); - $("#btndeletedomain").click(function() { - //get data-id attribute of the clicked element - var domainId = $(this).data("domainId"); + + function domain_handle(domainId) { $.ajax({ method:"post", url: window.location.pathname + "/../../ajax/deletedomain", data: {"domainid":domainId} }); - $("#DeleteDomain").modal("hide"); $.ajax({ method:"get", url: window.location.pathname + "/../../ajax/domainlist/1", @@ -235,12 +233,16 @@ $(function() { $("#domain-deny-table").bootstrapTable("load", data); } }); + } + $("#domain-allow-table").on("click-cell.bs.table", function (field, value, row, $element) { + if (value === 2) { + ConfirmDialog("btndeletedomain", $element.id, domain_handle); + } }); - //triggered when modal is about to be shown - $("#DeleteDomain").on("show.bs.modal", function(e) { - //get data-id attribute of the clicked element and store in button - var domainId = $(e.relatedTarget).data("domain-id"); - $(e.currentTarget).find("#btndeletedomain").data("domainId", domainId); + $("#domain-deny-table").on("click-cell.bs.table", function (field, value, row, $element) { + if (value === 2) { + ConfirmDialog("btndeletedomain", $element.id, domain_handle); + } }); $("#restrictModal").on("hidden.bs.modal", function () { @@ -347,7 +349,7 @@ $(function() { /* Function for deleting domain restrictions */ function TableActions (value, row) { return [ - "", "", "" diff --git a/cps/templates/book_edit.html b/cps/templates/book_edit.html index 003b33f9..c0fc141e 100644 --- a/cps/templates/book_edit.html +++ b/cps/templates/book_edit.html @@ -197,7 +197,8 @@ {% endblock %} {% block modal %} -{{ delete_book(book.id) }} +{{ delete_book() }} +{{ delete_confirm_modal() }} {% block modal %}{% endblock %} - - @@ -200,14 +198,7 @@ - - {% if g.current_theme == 1 %} - - - - - {% endif %} + + {% if g.current_theme == 1 %} + + + + + {% endif %} {% block js %}{% endblock %} diff --git a/cps/templates/modal_dialogs.html b/cps/templates/modal_dialogs.html index 038681b4..da83649a 100644 --- a/cps/templates/modal_dialogs.html +++ b/cps/templates/modal_dialogs.html @@ -37,7 +37,7 @@ {% endmacro %} -{% macro delete_book(bookid) %} +{% macro delete_book() %} {% if g.user.role_delete_books() %} {% endmacro %} + +{% macro delete_confirm_modal() %} + + +{% endmacro %} diff --git a/cps/templates/shelf.html b/cps/templates/shelf.html index f7e3c1ae..dab42d2f 100644 --- a/cps/templates/shelf.html +++ b/cps/templates/shelf.html @@ -7,7 +7,8 @@ {% endif %} {% if g.user.is_authenticated %} {% if (g.user.role_edit_shelfs() and shelf.is_public ) or not shelf.is_public %} -
{{ _('Delete this Shelf') }}
+ +
{{ _('Delete this Shelf') }}
{{ _('Edit Shelf') }} {% if entries.__len__() %} - @@ -125,19 +125,15 @@ {% endif %} {% endif %} - {% if g.user and g.user.role_admin() and not profile and not new_user and not content.role_anonymous() %} -
- -
- {% endif %}
- +
{{_('Save')}}
{% if not profile %} {{_('Cancel')}} {% endif %} + {% if g.user and g.user.role_admin() and not profile and not new_user and not content.role_anonymous() %} +
{{_('Delete User')}}
+ {% endif %}
@@ -157,23 +153,10 @@ - - {% endblock %} {% block modal %} {{ restrict_modal() }} +{{ delete_confirm_modal() }} {% endblock %} {% block js %}