# Yahoo! UI Library

### paginator 2.8.2r1

Yahoo! UI Library > paginator > LastPageLink.js (source view)
Search:
Filters Show Private Show Protected Show Deprecated

    (function () {

    var Paginator = YAHOO.widget.Paginator,
        l         = YAHOO.lang;

    /**
     * ui Component to generate the link to jump to the last page.
     *
     * @namespace YAHOO.widget.Paginator.ui
     * @class LastPageLink
     * @for YAHOO.widget.Paginator
     *
     * @constructor
     * @param p {Pagintor} Paginator instance to attach to
     */
    Paginator.ui.LastPageLink = function (p) {
        this.paginator = p;

        p.subscribe('recordOffsetChange',this.update,this,true);
        p.subscribe('rowsPerPageChange',this.update,this,true);
        p.subscribe('totalRecordsChange',this.update,this,true);
        p.subscribe('destroy',this.destroy,this,true);

        // TODO: make this work
        p.subscribe('lastPageLinkLabelChange',this.update,this,true);
        p.subscribe('lastPageLinkClassChange', this.update,this,true);
    };

    /**
     * Decorates Paginator instances with new attributes. Called during
     * Paginator instantiation.
     * @method init
     * @param paginator {Paginator} Paginator instance to decorate
     * @static
     */
    Paginator.ui.LastPageLink.init = function (p) {

        /**
         * Used as innerHTML for the last page link/span.
         * @attribute lastPageLinkLabel
         * @default 'last &gt;&gt;'
         */
        p.setAttributeConfig('lastPageLinkLabel', {
            value : 'last &gt;&gt;',
            validator : l.isString
        });

        /**
         * CSS class assigned to the link/span
         * @attribute lastPageLinkClass
         * @default 'yui-pg-last'
         */
        p.setAttributeConfig('lastPageLinkClass', {
            value : 'yui-pg-last',
            validator : l.isString
        });
    };

    Paginator.ui.LastPageLink.prototype = {

        /**
         * Currently placed HTMLElement node
         * @property current
         * @type HTMLElement
         * @private
         */
        current   : null,

        /**
         * Link HTMLElement node
         * @property link
         * @type HTMLElement
         * @private
         */
        link      : null,

        /**
         * Span node (inactive link)
         * @property span
         * @type HTMLElement
         * @private
         */
        span      : null,

        /**
         * Empty place holder node for when the last page link is inappropriate to
         * display in any form (unlimited paging).
         * @property na
         * @type HTMLElement
         * @private
         */
        na        : null,

        /**
         * Generate the nodes and return the appropriate node given the current
         * pagination state.
         * @method render
         * @param id_base {string} used to create unique ids for generated nodes
         * @return {HTMLElement}
         */
        render : function (id_base) {
            var p     = this.paginator,
                c     = p.get('lastPageLinkClass'),
                label = p.get('lastPageLinkLabel'),
                last  = p.getTotalPages();

            this.link = document.createElement('a');
            this.span = document.createElement('span');
            this.na   = this.span.cloneNode(false);

            this.link.id        = id_base + '-last-link';
            this.link.href      = '#';
            this.link.className = c;
            this.link.innerHTML = label;
            YAHOO.util.Event.on(this.link,'click',this.onClick,this,true);

            this.span.id        = id_base + '-last-span';
            this.span.className = c;
            this.span.innerHTML = label;

            this.na.id = id_base + '-last-na';

            switch (last) {
                case Paginator.VALUE_UNLIMITED :
                        this.current = this.na; break;
                case p.getCurrentPage() :
                        this.current = this.span; break;
                default :
                        this.current = this.link;
            }

            return this.current;
        },

        /**
         * Swap the link, span, and na nodes if appropriate.
         * @method update
         * @param e {CustomEvent} The calling change event (ignored)
         */
        update : function (e) {
            if (e && e.prevValue === e.newValue) {
                return;
            }

            var par   = this.current ? this.current.parentNode : null,
                after = this.link;

            if (par) {
                switch (this.paginator.getTotalPages()) {
                    case Paginator.VALUE_UNLIMITED :
                            after = this.na; break;
                    case this.paginator.getCurrentPage() :
                            after = this.span; break;
                }

                if (this.current !== after) {
                    par.replaceChild(after,this.current);
                    this.current = after;
                }
            }
        },

        /**
         * Removes the link/span node and clears event listeners
         * @method destroy
         * @private
         */
        destroy : function () {
            YAHOO.util.Event.purgeElement(this.link);
            this.current.parentNode.removeChild(this.current);
            this.link = this.span = null;
        },

        /**
         * Listener for the link's onclick event.  Passes to setPage method.
         * @method onClick
         * @param e {DOMEvent} The click event
         */
        onClick : function (e) {
            YAHOO.util.Event.stopEvent(e);
            this.paginator.setPage(this.paginator.getTotalPages());
        }
    };

    })();

Copyright © 2010 Yahoo! Inc. All rights reserved.