<commit_before>extends ../layouts/backend

block topnav
    form.nav.navbar-form.navbar-left(method="get", action="/admin/post")
        div.form-group
            input.form-control(name="q", placeholder="Search for posts", style="width: 300px", value="#{q}")
        button.btn.btn-primary.btn-sm(type="submit")
            i.fa.fa-search

    div.nav.navbar-form.navbar-left
        a.btn.btn-sm.btn-info(href="/admin/post/add")
            i.fa.fa-plus
            |  WRITE NEW POST

block content
    ol.breadcrumb
        li.active
            a(href="/admin/post") Posts
        li
            span
                if total == 0
                    | There are no posts
                else
                    | Found #{total} posts

    div.btn-group
        a.btn.btn-sm(class=!criteria.status ? 'active btn-success' : 'btn-info', href="#{url(req, { page: 1 }, ['status'])}")
            i.fa.fa-th-list
            | &nbsp;ALL
        a.btn.btn-sm(class=criteria.status == 'activated' ? 'active btn-success' : 'btn-info', href="#{url(req, { page: 1, status: 'activated' })}")
            i.fa.fa-check
            | &nbsp;PUBLISHED
        a.btn.btn-sm(class=criteria.status == 'deactivated' ? 'active btn-success' : 'btn-info', href="#{url(req, { page: 1, status: 'deactivated' })}")
            i.fa.fa-ban
            | &nbsp;NOT YET PUBLISHED
        a.btn.btn-sm(class=criteria.status == 'draft' ? 'active btn-success' : 'btn-info', href="#{url(req, { page: 1, status: 'draft' })}")
            i.fa.fa-edit
            | &nbsp;DRAFT

    if total > 0
        table.table(style="margin-top: 20px;")
            thead
                tr
                    th
                        a(href="#{url(req, { sort: (1 == sortDirection ? '-' : '') + 'title' })}")
                            if ('title' == sortBy && 1 == sortDirection)
                                | &uarr;&nbsp;
                            else if ('title' == sortBy && -1 == sortDirection)
                                | &darr;&nbsp;
                            | Title
                    th
                        a(href="#{url(req, { sort: (1 == sortDirection ? '-' : '') + 'created_date' })}")
                            if ('created_date' == sortBy && 1 == sortDirection)
                                | &uarr;&nbsp;
                            else if ('created_date' == sortBy && -1 == sortDirection)
                                | &darr;&nbsp;
                            | Created date
                    th
                        a(href="#{url(req, { sort: (1 == sortDirection ? '-' : '') + 'created_user.username' })}")
                            if ('created_user.username' == sortBy && 1 == sortDirection)
                                | &uarr;&nbsp;
                            else if ('created_user.username' == sortBy && -1 == sortDirection)
                                | &darr;&nbsp;
                            | Created by
                    th
                        a(href="#{url(req, { sort: (1 == sortDirection ? '-' : '') + 'status' })}")
                            if ('status' == sortBy && 1 == sortDirection)
                                | &uarr;&nbsp;
                            else if ('status' == sortBy && -1 == sortDirection)
                                | &darr;&nbsp;
                            | Status
                    th
                        a(href="#{url(req, { sort: (1 == sortDirection ? '-' : '') + 'pdf_downloads' })}")
                            if ('pdf_downloads' == sortBy && 1 == sortDirection)
                                | &uarr;&nbsp;
                            else if ('pdf_downloads' == sortBy && -1 == sortDirection)
                                | &darr;&nbsp;
                            | # PDF downloads
                    th(width="400px") Actions
            tbody#posts
                for post in posts
                    tr(data-id="#{post._id}")
                        td #{post.title}
                        td #{moment(post.created_date).format('YYYY-MM-DD HH:mm')}
                        td #{post.created_user.username}
                        td
                            if post.status == 'activated'
                                | Published
                            else if post.status == 'deactivated'
                                | Not yet published
                            else
                                | Draft
                        td #{post.pdf_downloads}
                        td
                            div.btn-group
                                a.btn.btn-default.btn-sm(href="/admin/post/edit/#{post._id}") EDIT
                                button.btn.btn-warning.btn-sm.removeButton(type="button") DELETE
                                button.btn.btn-default.btn-sm.publishButton(type="button", data-status="#{post.status}")
                                    if post.status == 'activated'
                                        | UNPUBLISH
                                    else
                                        | PUBLISH

                            a.btn.btn-sm.btn-link.viewButton(class=post.status != 'activated' ? 'hide' : '', href="/post/#{post.slug}") VIEW

        // Pagination
        if (numPages > 1)
            div.text-center
                ul.pagination
                    li(class=page == 1 ? 'disabled' : '')
                        a(href=page == 1 ? '#' : url(req, { page: 1 })) &larr;
                    li(class=page == 1 ? 'disabled' : '')
                        a(href=page == 1 ? '#' : url(req, { page: parseInt(page) - 1 })) &laquo;
                    - for (i = startRange; i <= endRange; i++)
                        li(class=(i == page) ? 'active' : '')
                            a(href="#{url(req, { page: i })}") #{i}
                    li(class=page == numPages ? 'disabled' : '')
                        a(href=page == numPages ? '#' : url(req, { page: parseInt(page) + 1 })) &raquo;
                    li(class=page == numPages ? 'disabled' : '')
                        a(href=page == numPages ? '#' : url(req, { page: numPages })) &rarr;

    div#deleteDialog.modal.fade(aria-hidden="true")
        div.modal-dialog
            div.modal-content
                div.modal-header
                    button.close(type="button", data-dismiss="modal", aria-hidden="true") &times;
                    h4.modal-title Delete post
                div.modal-body Are you sure you want to delete the post?
                div.modal-footer
                    button.btn.btn-danger.yesButton(type="button") YES
                    button.btn(type="button", data-dismiss="modal") NO

block footerScript
    script(type="text/javascript").
        $(document).ready(function() {
            $('#posts')
                // Removing post handler
                .on('click', '.removeButton', function() {
                    var $that = $(this),
                        $tr   = $that.parents('tr'),
                        id    = $tr.attr('data-id');
                    $that.attr('disabled', 'disabled');

                    var deleteDialog = $('#deleteDialog');
                    deleteDialog
                        // No button click handler
                        .on('hidden.bs.modal', function() {
                            $that.removeAttr('disabled');
                        })
                        // Yes button click handler
                        .find('.yesButton')
                            .off('click')
                            .on('click', function() {
                                $.ajax({
                                    type: 'post',
                                    url: '/admin/post/remove',
                                    data: {
                                        id: id
                                    },
                                    dataType: 'json'
                                }).done(function(response) {
                                    $that.removeAttr('disabled');
                                    if ('ok' == response.result) {
                                        $tr.remove();
                                    }
                                    deleteDialog.modal('hide');
                                });
                            })
                            .end()
                        .modal('show');
                })
                // Publishing post handler
                .on('click', '.publishButton', function() {
                    var $that  = $(this),
                        id     = $that.parents('tr').attr('data-id');
                    $that.attr('disabled', 'disabled');
                    $.ajax({
                        type: 'post',
                        url: '/admin/post/activate',
                        data: {
                            id: id
                        },
                        dataType: 'json'
                    }).done(function(response) {
                        $that.removeAttr('disabled');
                        if ('ok' == response.result) {
                            var status = ('activated' == $that.attr('data-status')) ? 'deactivated' : 'activated';
                            $that
                                .attr('data-status', status)
                                .html('activated' == status ? 'UNPUBLISH' : 'PUBLISH')
                                .parents('tr')
                                    .find('td')
                                        .eq(3).html('activated' == status ? 'Published' : 'Not yet published')
                                        .end()
                                    .find('.viewButton')
                                        .toggleClass('hide');
                        }
                    });
                });
        });<commit_msg>#1: remove post after publish/unpublish<commit_after>extends ../layouts/backend

block topnav
    form.nav.navbar-form.navbar-left(method="get", action="/admin/post")
        div.form-group
            input.form-control(name="q", placeholder="Search for posts", style="width: 300px", value="#{q}")
        button.btn.btn-primary.btn-sm(type="submit")
            i.fa.fa-search

    div.nav.navbar-form.navbar-left
        a.btn.btn-sm.btn-info(href="/admin/post/add")
            i.fa.fa-plus
            |  WRITE NEW POST

block content
    ol.breadcrumb
        li.active
            a(href="/admin/post") Posts
        li
            span
                if total == 0
                    | There are no posts
                else
                    | Found #{total} posts

    div.btn-group
        a.btn.btn-sm(class=!criteria.status ? 'active btn-success' : 'btn-info', href="#{url(req, { page: 1 }, ['status'])}")
            i.fa.fa-th-list
            | &nbsp;ALL
        a.btn.btn-sm(class=criteria.status == 'activated' ? 'active btn-success' : 'btn-info', href="#{url(req, { page: 1, status: 'activated' })}")
            i.fa.fa-check
            | &nbsp;PUBLISHED
        a.btn.btn-sm(class=criteria.status == 'deactivated' ? 'active btn-success' : 'btn-info', href="#{url(req, { page: 1, status: 'deactivated' })}")
            i.fa.fa-ban
            | &nbsp;NOT YET PUBLISHED
        a.btn.btn-sm(class=criteria.status == 'draft' ? 'active btn-success' : 'btn-info', href="#{url(req, { page: 1, status: 'draft' })}")
            i.fa.fa-edit
            | &nbsp;DRAFT

    if total > 0
        table.table(style="margin-top: 20px;")
            thead
                tr
                    th
                        a(href="#{url(req, { sort: (1 == sortDirection ? '-' : '') + 'title' })}")
                            if ('title' == sortBy && 1 == sortDirection)
                                | &uarr;&nbsp;
                            else if ('title' == sortBy && -1 == sortDirection)
                                | &darr;&nbsp;
                            | Title
                    th
                        a(href="#{url(req, { sort: (1 == sortDirection ? '-' : '') + 'created_date' })}")
                            if ('created_date' == sortBy && 1 == sortDirection)
                                | &uarr;&nbsp;
                            else if ('created_date' == sortBy && -1 == sortDirection)
                                | &darr;&nbsp;
                            | Created date
                    th
                        a(href="#{url(req, { sort: (1 == sortDirection ? '-' : '') + 'created_user.username' })}")
                            if ('created_user.username' == sortBy && 1 == sortDirection)
                                | &uarr;&nbsp;
                            else if ('created_user.username' == sortBy && -1 == sortDirection)
                                | &darr;&nbsp;
                            | Created by
                    th
                        a(href="#{url(req, { sort: (1 == sortDirection ? '-' : '') + 'status' })}")
                            if ('status' == sortBy && 1 == sortDirection)
                                | &uarr;&nbsp;
                            else if ('status' == sortBy && -1 == sortDirection)
                                | &darr;&nbsp;
                            | Status
                    th
                        a(href="#{url(req, { sort: (1 == sortDirection ? '-' : '') + 'pdf_downloads' })}")
                            if ('pdf_downloads' == sortBy && 1 == sortDirection)
                                | &uarr;&nbsp;
                            else if ('pdf_downloads' == sortBy && -1 == sortDirection)
                                | &darr;&nbsp;
                            | # PDF downloads
                    th(width="400px") Actions
            tbody#posts
                for post in posts
                    tr(data-id="#{post._id}")
                        td #{post.title}
                        td #{moment(post.created_date).format('YYYY-MM-DD HH:mm')}
                        td #{post.created_user.username}
                        td
                            if post.status == 'activated'
                                | Published
                            else if post.status == 'deactivated'
                                | Not yet published
                            else
                                | Draft
                        td #{post.pdf_downloads}
                        td
                            div.btn-group
                                a.btn.btn-default.btn-sm(href="/admin/post/edit/#{post._id}") EDIT
                                button.btn.btn-warning.btn-sm.removeButton(type="button") DELETE
                                button.btn.btn-default.btn-sm.publishButton(type="button", data-status="#{post.status}")
                                    if post.status == 'activated'
                                        | UNPUBLISH
                                    else
                                        | PUBLISH

                            a.btn.btn-sm.btn-link.viewButton(class=post.status != 'activated' ? 'hide' : '', href="/post/#{post.slug}") VIEW

        // Pagination
        if (numPages > 1)
            div.text-center
                ul.pagination
                    li(class=page == 1 ? 'disabled' : '')
                        a(href=page == 1 ? '#' : url(req, { page: 1 })) &larr;
                    li(class=page == 1 ? 'disabled' : '')
                        a(href=page == 1 ? '#' : url(req, { page: parseInt(page) - 1 })) &laquo;
                    - for (i = startRange; i <= endRange; i++)
                        li(class=(i == page) ? 'active' : '')
                            a(href="#{url(req, { page: i })}") #{i}
                    li(class=page == numPages ? 'disabled' : '')
                        a(href=page == numPages ? '#' : url(req, { page: parseInt(page) + 1 })) &raquo;
                    li(class=page == numPages ? 'disabled' : '')
                        a(href=page == numPages ? '#' : url(req, { page: numPages })) &rarr;

    div#deleteDialog.modal.fade(aria-hidden="true")
        div.modal-dialog
            div.modal-content
                div.modal-header
                    button.close(type="button", data-dismiss="modal", aria-hidden="true") &times;
                    h4.modal-title Delete post
                div.modal-body Are you sure you want to delete the post?
                div.modal-footer
                    button.btn.btn-danger.yesButton(type="button") YES
                    button.btn(type="button", data-dismiss="modal") NO

block footerScript
    script(type="text/javascript").
        $(document).ready(function() {
            $('#posts')
                // Removing post handler
                .on('click', '.removeButton', function() {
                    var $that = $(this),
                        $tr   = $that.parents('tr'),
                        id    = $tr.attr('data-id');
                    $that.attr('disabled', 'disabled');

                    var deleteDialog = $('#deleteDialog');
                    deleteDialog
                        // No button click handler
                        .on('hidden.bs.modal', function() {
                            $that.removeAttr('disabled');
                        })
                        // Yes button click handler
                        .find('.yesButton')
                            .off('click')
                            .on('click', function() {
                                $.ajax({
                                    type: 'post',
                                    url: '/admin/post/remove',
                                    data: {
                                        id: id
                                    },
                                    dataType: 'json'
                                }).done(function(response) {
                                    $that.removeAttr('disabled');
                                    if ('ok' == response.result) {
                                        $tr.remove();
                                    }
                                    deleteDialog.modal('hide');
                                });
                            })
                            .end()
                        .modal('show');
                })
                // Publishing post handler
                .on('click', '.publishButton', function() {
                    var $that  = $(this),
                        $tr    = $that.parents('tr'),
                        id     = $tr.attr('data-id');
                    $that.attr('disabled', 'disabled');
                    $.ajax({
                        type: 'post',
                        url: '/admin/post/activate',
                        data: {
                            id: id
                        },
                        dataType: 'json'
                    }).done(function(response) {
                        $that.removeAttr('disabled');
                        if ('ok' == response.result) {
                            $tr.remove();
                            var status = ('activated' == $that.attr('data-status')) ? 'deactivated' : 'activated';
                            $that
                                .attr('data-status', status)
                                .html('activated' == status ? 'UNPUBLISH' : 'PUBLISH')
                                .parents('tr')
                                    .find('td')
                                        .eq(3).html('activated' == status ? 'Published' : 'Not yet published')
                                        .end()
                                    .find('.viewButton')
                                        .toggleClass('hide');
                        }
                    });
                });
        });
{'fraction_non_alphanumeric': 0.13454179254783485, 'fraction_numerical': 0.003977844914400805, 'mean_word_length': 0.9263821532492725, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 84, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1052102', 'n_tokens_mistral': 5032, 'n_tokens_neox': 4662, 'n_words': 895}