paging_header.js 3.59 KB
Newer Older
1
define(["underscore", "gettext", "js/views/baseview"], function(_, gettext, BaseView) {
2

3
    var PagingHeader = BaseView.extend({
4 5 6 7 8 9 10 11 12
        events : {
            "click .next-page-link": "nextPage",
            "click .previous-page-link": "previousPage"
        },

        initialize: function(options) {
            var view = options.view,
                collection = view.collection;
            this.view = view;
13
            this.template = this.loadTemplate('paging-header');
14 15 16 17 18 19 20 21 22 23 24 25 26 27
            collection.bind('add', _.bind(this.render, this));
            collection.bind('remove', _.bind(this.render, this));
            collection.bind('reset', _.bind(this.render, this));
        },

        render: function() {
            var view = this.view,
                collection = view.collection,
                currentPage = collection.currentPage,
                lastPage = collection.totalPages - 1,
                messageHtml = this.messageHtml();
            this.$el.html(this.template({
                messageHtml: messageHtml
            }));
28 29
            this.$(".previous-page-link").toggleClass("is-disabled", currentPage === 0).attr('aria-disabled', currentPage === 0);
            this.$(".next-page-link").toggleClass("is-disabled", currentPage === lastPage).attr('aria-disabled', currentPage === lastPage);
30 31 32 33
            return this;
        },

        messageHtml: function() {
34 35
            var message;
            if (this.view.collection.sortDirection === 'asc') {
36
                // Translators: sample result: "Showing 0-9 out of 25 total, sorted by Date Added ascending"
37 38
                message = gettext('Showing %(current_item_range)s out of %(total_items_count)s, sorted by %(sort_name)s ascending');
            } else {
39
                // Translators: sample result: "Showing 0-9 out of 25 total, sorted by Date Added descending"
40 41 42 43 44 45 46 47 48 49
                message = gettext('Showing %(current_item_range)s out of %(total_items_count)s, sorted by %(sort_name)s descending');
            }
            return '<p>' + interpolate(message, {
                current_item_range: this.currentItemRangeLabel(),
                total_items_count: this.totalItemsCountLabel(),
                sort_name: this.sortNameLabel()
            }, true) + "</p>";
        },

        currentItemRangeLabel: function() {
50 51 52 53
            var view = this.view,
                collection = view.collection,
                start = collection.start,
                count = collection.size(),
54 55
                end = start + count;
            return interpolate('<span class="count-current-shown">%(start)s-%(end)s</span>', {
56
                start: Math.min(start + 1, end),
57 58 59 60 61 62
                end: end
            }, true);
        },

        totalItemsCountLabel: function() {
            var totalItemsLabel;
63
            // Translators: turns into "25 total" to be used in other sentences, e.g. "Showing 0-9 out of 25 total".
64 65 66 67 68 69 70 71 72 73 74 75
            totalItemsLabel = interpolate(gettext('%(total_items)s total'), {
                total_items: this.view.collection.totalCount
            }, true);
            return interpolate('<span class="count-total">%(total_items_label)s</span>', {
                total_items_label: totalItemsLabel
            }, true);
        },

        sortNameLabel: function() {
            return interpolate('<span class="sort-order">%(sort_name)s</span>', {
                sort_name: this.view.sortDisplayName()
            }, true);
76 77 78 79 80 81 82 83 84 85 86 87 88
        },

        nextPage: function() {
            this.view.nextPage();
        },

        previousPage: function() {
            this.view.previousPage();
        }
    });

    return PagingHeader;
}); // end define();