Added Ajax handler for book info call

This commit is contained in:
Raelon Masters
2020-07-31 02:27:07 -04:00
parent be2c525d9a
commit 5e36fc1d16
3 changed files with 72 additions and 31 deletions

View File

@@ -18,7 +18,7 @@ $(document).ready(function(){
var max_height = win_height - (hdr_height + ftr_height) - (scr_height - win_height); // Set our available height var max_height = win_height - (hdr_height + ftr_height) - (scr_height - win_height); // Set our available height
var u_string = "Username"; var u_string = "Username";
var p_string = "Password"; var p_string = "Password";
var s_string = "search by Title, Author, Tags, or Collections" var s_string = "search by Title, Author, Tags, or Collections";
customlog([cmp_height]); customlog([cmp_height]);
$(".search_submit").click(function(){ $(".search_submit").click(function(){
var query = $('.nav_search').val(); var query = $('.nav_search').val();
@@ -35,8 +35,8 @@ $(document).ready(function(){
}); });
$('.nav_link').on('mouseover', function (e){ $('.nav_link').on('mouseover', function (e){
var popover_str = $(this).attr('alt'); var popover_str = $(this).attr('alt');
x = $(this).offset().left x = $(this).offset().left;
y = $(this).offset().top y = $(this).offset().to;
$('.popover').html(popover_str); $('.popover').html(popover_str);
$('.popover').css('left', x+nav_width); $('.popover').css('left', x+nav_width);
$('.popover').css('top', y); $('.popover').css('top', y);
@@ -44,15 +44,15 @@ $(document).ready(function(){
}); });
$('.nav_link').on('mouseout', function (e){ $('.nav_link').on('mouseout', function (e){
var popover_str = $(this).attr('alt'); var popover_str = $(this).attr('alt');
x = $(this).offset().left x = $(this).offset().left;
y = $(this).offset().top y = $(this).offset().top;
$('.popover').html(popover_str); $('.popover').html(popover_str);
$('.popover').css('left', x); $('.popover').css('left', x);
$('.popover').css('top', y); $('.popover').css('top', y);
$('.popover').css('display','none'); $('.popover').css('display','none');
}); });
$('#btn_collections').on('click', function (e){ $('#btn_collections').on('click', function (e){
$('.hidden.vert-nav.collections').toggle() $('.hidden.vert-nav.collections').toggle();
}); });
$('.input_box').on('click', function(){ $('.input_box').on('click', function(){
$(this).attr("value",""); $(this).attr("value","");
@@ -78,16 +78,16 @@ $(document).ready(function(){
var optionSelected = $(this).find("option:selected"); var optionSelected = $(this).find("option:selected");
var valueSelected = optionSelected.val(); var valueSelected = optionSelected.val();
var textSelected = optionSelected.text(); var textSelected = optionSelected.text();
window.location.href="/"+valueSelected window.location.href="/"+valueSelected;
}); });
$('#btn-home').on("click", function(){ $('#btn-home').on("click", function(){
_location = $(this).attr('data-location'); _location = $(this).attr('data-location');
window.location.href=_location; window.location.href=_location;
}); });
$('#flip_sort').on("click", function(){ $('#flip_sort').on("click", function(){
window.location.href="/flip_sort/"+$("#_order").val() window.location.href="/flip_sort/"+$("#_order").val();
}); });
$('#search_string').html("<i> "+$('#_search').val().substr(0,15)+"</i>") $('#search_string').html("<i> "+$('#_search').val().substr(0,15)+"</i>");
$('#pop_over_0').dialog({ autoOpen: false }); $('#pop_over_0').dialog({ autoOpen: false });
resize_search(); resize_search();
@@ -97,10 +97,10 @@ $(document).ready(function(){
window.location.href = '/show_collection/'+$(this).attr('data'); window.location.href = '/show_collection/'+$(this).attr('data');
}); });
$('#btn_login').on('click', function() { $('#btn_login').on('click', function() {
var isopen = $('#pop_over_0').dialog("isOpen") var isopen = $('#pop_over_0').dialog("isOpen");
if (isopen) { if (isopen) {
$('#pop_over_0').dialog("close"); $('#pop_over_0').dialog("close");
return return;
} }
customlog(['Login Clicked']); customlog(['Login Clicked']);
$.ajax({ $.ajax({
@@ -138,10 +138,10 @@ $(document).ready(function(){
window.location.href = '/logout'; window.location.href = '/logout';
}); });
$('#coll_button').on('click', function(){ $('#coll_button').on('click', function(){
var isopen = $('#pop_over_0').dialog("isOpen") var isopen = $('#pop_over_0').dialog("isOpen");
if (isopen){ if (isopen){
$('#pop_over_0').dialog("close"); $('#pop_over_0').dialog("close");
return return;
} }
customlog(['Collections Clicked']); customlog(['Collections Clicked']);
$.ajax({ $.ajax({
@@ -171,7 +171,43 @@ $(document).ready(function(){
error: function(response){ error: function(response){
customlog(["Failure", response]); customlog(["Failure", response]);
} }
}) });
});
$('.info-button').on('click', function(){
var isopen = $('#pop_over_0').dialog("isOpen");
if (isopen){
$('#pop_over_0').dialog("close");
return;
}
customlog(['Book Details Clicked', $(this).attr('data')]);
$.ajax({
type: "GET", url: "/live", data: {hook: 'details', pk:$(this).attr('data')},
success: function(response){
// Set the dialog title
$('#pop_over_0').dialog({
title: "Collections",
maxHeight: (win_height-100),
minWidth: $("#horiz_nav_main").width(),
hide: { effect: "blind", duration: 1000 },
show: { effect: "blind", duration: 1000 },
position: { my: "top", at: "bottom", of: $("#horiz_nav_main")
}
});
// clear and create a new container
$('#pop_over_0').html('<div id=collections>');
// Populate the container from response.data
$.each(response.data, function(index, value){
$('#collections').append("<div class=collection data='"+value+"/"+$('#_set').val()+"'>"+value+"</div>");
});
// Close the container
$('#pop_over').append('</div>');
// Now open this dialog
$('#pop_over_0').dialog("open");
},
error: function(response){
customlog(["Failure", response]);
}
});
}); });
}); });
function resize_search(win_width){ function resize_search(win_width){

View File

@@ -120,8 +120,8 @@
<span class="share-button controls"> <span class="share-button controls">
<a href="{% url 'share' pk=book.pk %}" class="book_link"><i class="fas fa-share icon"></i></a> <a href="{% url 'share' pk=book.pk %}" class="book_link"><i class="fas fa-share icon"></i></a>
</span> </span>
<span class="info-button controls"> <span class="info-button controls" data="{{ book.pk }}">
<a href="{% url 'info' pk=book.pk %}" class="book_link"><i class="fas fa-info icon"></i></a> <i class="fas fa-info icon"></i>
</span> </span>
</li> </li>
</ul> </ul>

View File

@@ -1,22 +1,22 @@
import json
import os import os
from base64 import b64decode, b64encode from base64 import b64decode, b64encode
from pathlib import Path from pathlib import Path
from backend.lib.config import Config from backend.lib.config import Config
from django.conf import settings
from django.contrib import auth
from django.contrib.auth import authenticate, get_user_model, login, logout
from django.contrib.auth.models import User
from django.db import models from django.db import models
from django.http import JsonResponse from django.http import JsonResponse
from django.shortcuts import HttpResponse, render, redirect # render_to_response from django.shortcuts import HttpResponse, redirect, render # render_to_response
from django.utils.text import slugify
from django.contrib.auth import login, authenticate, logout
from django.contrib import auth
from django.contrib.auth.models import User
from django.conf import settings
from django.contrib.auth import get_user_model
from django.utils.datastructures import MultiValueDictKeyError
import json
from .forms import SignUpForm, UserLoginForm
from .models import Books, Collections, Navigation, Favorites, User
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.utils.datastructures import MultiValueDictKeyError
from django.utils.text import slugify
from .forms import SignUpForm, UserLoginForm
from .models import Books, Collections, Favorites, Navigation, User
config = Config(Path("../")) config = Config(Path("../"))
@@ -380,6 +380,12 @@ def collections_list():
def live(request, **kwargs): def live(request, **kwargs):
"""
Respond to live requests. Primarily used as a response object for Ajax calls
:param GET['hook']: collection_listing, book_details, register
:param kwargs['pk']: Primary key of requested object
:return: JsonResponse Object, status response code
"""
err_txt = {"err": "There is no responder for your request"} err_txt = {"err": "There is no responder for your request"}
try: hook = request.GET['hook'] try: hook = request.GET['hook']
except MultiValueDictKeyError as e: return JsonResponse(err_txt, status=404) except MultiValueDictKeyError as e: return JsonResponse(err_txt, status=404)
@@ -445,10 +451,10 @@ def payload(request, query, _set, _limit, _order, **kwargs):
_r, _r_len = \ _r, _r_len = \
_results.order_by(_order)[_set_min:_set_max],\ _results.order_by(_order)[_set_min:_set_max],\
_results.count() _results.count()
else: else: # No new query was passed
try: try:
query = request.session['cached_query'] # Is there a cached query? query = request.session['cached_query'] # Is there a cached query?
if query == None: raise KeyError if query == None: raise KeyError # No cached query exists jump to KeyError
if request.session['ascending']: if request.session['ascending']:
_results = Books().generic_search(query) _results = Books().generic_search(query)
else: _results = Books().generic_search(query).reverse() else: _results = Books().generic_search(query).reverse()
@@ -464,7 +470,7 @@ def payload(request, query, _set, _limit, _order, **kwargs):
_bookstats = Books.objects.all().count() _bookstats = Books.objects.all().count()
if (_r_len): _btotal = str(_r_len) if (_r_len): _btotal = str(_r_len)
else: _btotal = str(_bookstats) else: _btotal = str(_bookstats)
# Format the payload and return it to the view
return { return {
"Books": _r, "Books": _r,
"Set": str(_set), "Set": str(_set),
@@ -479,4 +485,3 @@ def payload(request, query, _set, _limit, _order, **kwargs):
"SearchLen": _r_len, "SearchLen": _r_len,
"Order": _order, "Order": _order,
} }