Skip to content

Commit

Permalink
3.3.2: fix print book page
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswilm committed Oct 14, 2017
1 parent 2c4c501 commit bcfc8e2
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 69 deletions.
4 changes: 2 additions & 2 deletions book/static/js/es6_modules/books/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class BookActions {
xhr.setRequestHeader("X-CSRFToken", csrfToken),
success: (data, textStatus, jqXHR) => {
this.stopBookTable()
jQuery('#Book_' + id).detach()
jQuery(`#Book_${id}`).detach()
this.bookList.bookList = this.bookList.bookList.filter(book => book.id !== id)
this.startBookTable()
},
Expand Down Expand Up @@ -92,7 +92,7 @@ export class BookActions {
)
let buttons = {}
buttons[gettext('Delete')] = function () {
ids.forEach(id => that.deleteBook(id))
ids.forEach(id => that.deleteBook(parseInt(id)))
jQuery(this).dialog("close")
addAlert('success', gettext('The book(s) have been deleted'))
}
Expand Down
48 changes: 24 additions & 24 deletions book/static/js/es6_modules/print-book/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ export class PrintBook {
}
this.documentOwners = []
this.printConfig = {
flowFromElement : document.getElementById('flow'),
enableFrontmatter : true,
enableFrontmatter: true,
sectionStartSelector: 'div.part',
sectionTitleSelector: 'h1',
chapterStartSelector: 'div.chapter',
Expand All @@ -52,22 +51,29 @@ export class PrintBook {
this.bindEvents()
}

setDocumentStyle() {
let docStyle = this.documentStyles.find(
docStyle => docStyle.filename === this.book.settings.documentstyle
)
if (docStyle) {
let styleEl = document.createElement('style')
styleEl.innerHTML = docStyle.contents
document.head.appendChild(styleEl)
}
}

setTheBook(book) {
book.settings = JSON.parse(book.settings)
book.metadata = JSON.parse(book.metadata)
for (let i = 0; i < book.chapters.length; i++) {
book.chapters[i].metadata = JSON.parse(book.chapters[
i].metadata)
book.chapters[i].settings = JSON.parse(book.chapters[
i].settings)
book.chapters[i].contents = JSON.parse(book.chapters[
i].contents)
if (this.documentOwners.indexOf(book.chapters[i].owner)===-1) {
this.documentOwners.push(book.chapters[i].owner)
}
}
this.book = book
this.setDocumentStyle(this.book.settings.documentstyle)
this.setDocumentStyle()

this.printConfig['pageHeight'] = this.pageSizes[this.book.settings.papersize].height
this.printConfig['pageWidth'] = this.pageSizes[this.book.settings.papersize].width
Expand All @@ -87,17 +93,20 @@ export class PrintBook {
crossDomain: false, // obviates need for sameOrigin test
beforeSend: (xhr, settings) =>
xhr.setRequestHeader("X-CSRFToken", csrfToken),
success: (response, textStatus, jqXHR) =>
this.setTheBook(response.book),
success: (response, textStatus, jqXHR) => {
this.citationStyles = response.citation_styles
this.citationLocales = response.citation_locales
this.documentStyles = response.document_styles
this.setTheBook(response.book)
},
error: (jqXHR, textStatus, errorThrown) =>
addAlert('error', jqXHR.responseText),
complete: () => deactivateWait()
})
}

fillPrintPage() {
jQuery(document.body).addClass(this.book.settings.documentstyle)
jQuery('#book')[0].outerHTML = bookPrintTemplate({
document.getElementById('book').outerHTML = bookPrintTemplate({
book: this.book,
docSchema
})
Expand All @@ -106,6 +115,8 @@ export class PrintBook {
document.body,
this.book.settings.citationstyle,
this.bibDB,
this.citationStyles,
this.citationLocales,
true
)
this.citRenderer.init().then(
Expand Down Expand Up @@ -133,6 +144,8 @@ export class PrintBook {
this.printConfig['frontmatterContents'] = bookPrintStartTemplate(
{book: this.book}
)
this.printConfig['flowFromElement'] = document.getElementById('flow')
this.printConfig['flowToElement'] = document.getElementById('flow')

let paginator = new PaginateForPrint(this.printConfig)
paginator.initiate()
Expand All @@ -141,19 +154,6 @@ export class PrintBook {

}

setDocumentStyle() {
let theValue = this.book.settings.documentstyle
let documentStyleLink = document.getElementById('document-style-link'),
newDocumentStyleLink = document.createElement('link')
newDocumentStyleLink.setAttribute("rel", "stylesheet")
newDocumentStyleLink.setAttribute("type", "text/css")
newDocumentStyleLink.setAttribute("id", "document-style-link")
newDocumentStyleLink.setAttribute("href", window.staticUrl+'css/document/'+theValue+'.css')

documentStyleLink.parentElement.replaceChild(newDocumentStyleLink, documentStyleLink)
}


bindEvents() {
jQuery(document).ready(() => {
let pathnameParts = window.location.pathname.split('/'),
Expand Down
68 changes: 41 additions & 27 deletions book/static/js/es6_modules/print-book/templates.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {escapeText} from "../../common"
import {DOMSerializer} from "prosemirror-model"

import {escapeText} from "../common"

/** A template for the initial pages of a book before the contents begin. */
export let bookPrintStartTemplate = ({book}) =>
Expand Down Expand Up @@ -27,32 +29,44 @@ export let bookPrintStartTemplate = ({book}) =>
<div class="pagination-pagebreak">`

/** A template for the print view of a book. */
export let bookPrintTemplate = ({book, docSchema}) =>
book.chapters.map(
chapter =>
`${
chapter.part && chapter.part.length ?
`<div class="part">
<h1>${escapeText(chapter.part)}</h1>
</div>` :
''
}
<div class="chapter">
<h1 class="title">${escapeText(chapter.title)}</h1>
${
chapter.metadata.subtitle ?
`<h2 class="metadata-subtitle">${escapeText(chapter.metadata.subtitle)}</h2>` :
''
}
${
chapter.metadata.abstract ?
`<div class="metadata-abstract">${escapeText(chapter.metadata.abstract)}</div>` :
export let bookPrintTemplate = ({book, docSchema}) => {
let serializer = DOMSerializer.fromSchema(docSchema)
return book.chapters.map(
chapter => {
let subtitleNode = docSchema.nodeFromJSON(
chapter.contents.content.find(node => node.type==="subtitle")
)
let abstractNode = docSchema.nodeFromJSON(
chapter.contents.content.find(node => node.type==="abstract")
)

return `${
chapter.part && chapter.part.length ?
`<div class="part">
<h1>${escapeText(chapter.part)}</h1>
</div>` :
''
}
${
docSchema.nodeFromJSON(
chapter.contents.content.find(node => node.type==="body")
).toDOM().innerHTML
}
</div>`
<div class="chapter">
<h1 class="title">${escapeText(chapter.title)}</h1>
${
subtitleNode.attrs.hidden ?
'' :
serializer.serializeNode(subtitleNode).outerHTML
}
${
abstractNode.attrs.hidden ?
'' :
serializer.serializeNode(abstractNode).outerHTML
}
${
serializer.serializeNode(
docSchema.nodeFromJSON(
chapter.contents.content.find(node => node.type==="body")
)
).outerHTML
}
</div>`
}
).join('')
}
14 changes: 7 additions & 7 deletions book/templates/book/print.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
<title></title>
<!-- KaTeX -->
<link type="text/css" rel="stylesheet" href="{{ STATIC_URL }}css/libs/katex/katex.min.css" />
<!-- document style -->
<link id="document-style-link" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/jsi18n/"></script>
<!-- jQuery -->
<script type="text/javascript" src="{{ JQUERY_URL }}"></script>
Expand All @@ -24,11 +22,13 @@

</head>
<body class="tex2jax_ignore">
<div id="flow">
<div id="book">
</div>
<div class="chapter">
<div id="bibliography" class="user-contents"></div>
<div id="print" class="user-contents">
<div id="flow">
<div id="book">
</div>
<div class="chapter">
<div id="bibliography" class="user-contents"></div>
</div>
</div>
</div>
</body>
Expand Down
32 changes: 24 additions & 8 deletions book/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,25 @@ def get_book_js(request):
'title': chapter.text.title,
'contents': chapter.text.contents,
'part': chapter.part,
'settings': chapter.text.settings,
'metadata': chapter.text.metadata,
'owner': chapter.text.owner.id
})
status = 200
serializer = PythonWithURLSerializer()
document_styles = serializer.serialize(
DocumentStyle.objects.all(),
use_natural_foreign_keys=True
)
response['document_styles'] = [
obj['fields'] for obj in document_styles
]
cit_styles = serializer.serialize(
CitationStyle.objects.all()
)
response['citation_styles'] = [obj['fields'] for obj in cit_styles]
cit_locales = serializer.serialize(CitationLocale.objects.all())
response['citation_locales'] = [
obj['fields'] for obj in cit_locales
]

return JsonResponse(
response,
Expand Down Expand Up @@ -348,12 +362,14 @@ def delete_js(request):
status = 405
if request.is_ajax() and request.method == 'POST':
book_id = int(request.POST['id'])
book = Book.objects.get(pk=book_id, owner=request.user)
image = book.cover_image
book.delete()
if image and image.is_deletable():
image.delete()
status = 200
book = Book.objects.filter(pk=book_id, owner=request.user)
if len(book) > 0:
book = book[0]
image = book.cover_image
book.delete()
if image and image.is_deletable():
image.delete()
status = 200
return JsonResponse(
response,
status=status
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='fiduswriter-books',
version='3.3.1',
version='3.3.2',
packages=find_packages(),
include_package_data=True,
license='AGPL License',
Expand Down

0 comments on commit bcfc8e2

Please sign in to comment.