API Docs for: 1.0.0
Show:

File: src/gallery-paginator/js/CurrentPageReport.js

/*
Copyright (c) 2009, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
*/

/**
 * @module gallery-paginator
 */

/**
 * ui Component to generate the textual report of current pagination status.
 * E.g. "Now viewing page 1 of 13".
 *
 * @class Paginator.ui.CurrentPageReport
 * @constructor
 * @param p {Pagintor} Paginator instance to attach to
 */
Paginator.ui.CurrentPageReport = function (p) {
    this.paginator = p;

    p.on('destroy',this.destroy,this);
    p.after('recordOffsetChange', this.update,this);
    p.after('rowsPerPageChange', this.update,this);
    p.after('totalRecordsChange',this.update,this);

    p.after('pageReportClassChange', this.update,this);
    p.after('pageReportTemplateChange', this.update,this);
};

/**
 * CSS class assigned to the span containing the info.
 * @attribute pageReportClass
 * @default 'yui-paginator-current'
 */
Paginator.ATTRS.pageReportClass =
{
    value : Y.ClassNameManager.getClassName(Paginator.NAME, 'current'),
    validator : Y.Lang.isString
};

/**
 * Used as innerHTML for the span.  Place holders in the form of {name}
 * will be replaced with the so named value from the key:value map
 * generated by the function held in the pageReportValueGenerator attribute.
 * @attribute pageReportTemplate
 * @default '({currentPage} of {totalPages})'
 * @see pageReportValueGenerator attribute
 */
Paginator.ATTRS.pageReportTemplate =
{
    value : '({currentPage} of {totalPages})',
    validator : Y.Lang.isString
};

/**
 * Function to generate the value map used to populate the
 * pageReportTemplate.  The function is passed the Paginator instance as a
 * parameter.  The default function returns a map with the following keys:
 * <ul>
 * <li>currentPage</li>
 * <li>totalPages</li>
 * <li>startIndex</li>
 * <li>endIndex</li>
 * <li>startRecord</li>
 * <li>endRecord</li>
 * <li>totalRecords</li>
 * </ul>
 * @attribute pageReportValueGenarator
 */
Paginator.ATTRS.pageReportValueGenerator =
{
    value : function (paginator) {
        var curPage = paginator.getCurrentPage(),
            records = paginator.getPageRecords();

        return {
            'currentPage' : records ? curPage : 0,
            'totalPages'  : paginator.getTotalPages(),
            'startIndex'  : records ? records[0] : 0,
            'endIndex'    : records ? records[1] : 0,
            'startRecord' : records ? records[0] + 1 : 0,
            'endRecord'   : records ? records[1] + 1 : 0,
            'totalRecords': paginator.get('totalRecords')
        };
    },
    validator : Y.Lang.isFunction
};

/**
 * Replace place holders in a string with the named values found in an
 * object literal.
 * @static
 * @method sprintf
 * @param template {string} The content string containing place holders
 * @param values {object} The key:value pairs used to replace the place holders
 * @return {string}
 */
Paginator.ui.CurrentPageReport.sprintf = function (template, values) {
    return template.replace(/\{([\w\s\-]+)\}/g, function (x,key) {
            return (key in values) ? values[key] : '';
        });
};

Paginator.ui.CurrentPageReport.prototype = {

    /**
     * Span node containing the formatted info
     * @property span
     * @type HTMLElement
     * @private
     */
    span : null,


    /**
     * Removes the link/span node and clears event listeners
     * removal.
     * @method destroy
     * @private
     */
    destroy : function () {
        this.span.remove(true);
        this.span = null;
    },

    /**
     * Generate the span containing info formatted per the pageReportTemplate
     * attribute.
     * @method render
     * @param id_base {string} used to create unique ids for generated nodes
     * @return {HTMLElement}
     */
    render : function (id_base) {
        if (this.span) {
            this.span.remove(true);
        }

        this.span = Y.Node.create(
            '<span id="'+id_base+'-page-report"></span>');
        this.span.set('className', this.paginator.get('pageReportClass'));
        this.update();

        return this.span;
    },
    
    /**
     * Regenerate the content of the span if appropriate. Calls
     * CurrentPageReport.sprintf with the value of the pageReportTemplate
     * attribute and the value map returned from pageReportValueGenerator
     * function.
     * @method update
     * @param e {CustomEvent} The calling change event
     */
    update : function (e) {
        if (e && e.prevVal === e.newVal) {
            return;
        }

        this.span.set('className', this.paginator.get('pageReportClass'));
        this.span.set('innerHTML', Paginator.ui.CurrentPageReport.sprintf(
            this.paginator.get('pageReportTemplate'),
            this.paginator.get('pageReportValueGenerator')(this.paginator)));
    }
};