📄 paginator.js
字号:
if (!currentPage || numPages === 0 || totalPages === 0 || (totalPages === UNLIMITED && numPages === UNLIMITED)) { return [0,-1]; } // Limit requested pageLinks if there are fewer totalPages if (totalPages !== UNLIMITED) { numPages = numPages === UNLIMITED ? totalPages : Math.min(numPages,totalPages); } // Determine start and end, trying to keep current in the middle start = Math.max(1,Math.ceil(currentPage - (numPages/2))); if (totalPages === UNLIMITED) { end = start + numPages - 1; } else { end = Math.min(totalPages, start + numPages - 1); } // Adjust the start index when approaching the last page delta = numPages - (end - start + 1); start = Math.max(1, start - delta); return [start,end];};Paginator.ui.PageLinks.prototype = { /** * Current page * @property current * @type number * @private */ current : 0, /** * Span node containing the page links * @property container * @type HTMLElement * @private */ container : null, /** * Generate the nodes and return the container node containing page links * appropriate to 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; // Set up container this.container = document.createElement('span'); this.container.id = id_base + '-pages'; this.container.className = p.get('pageLinksContainerClass'); YAHOO.util.Event.on(this.container,'click',this.onClick,this,true); // Call update, flagging a need to rebuild this.update({newValue : null, rebuild : true}); return this.container; }, /** * Update the links if appropriate * @method update * @param e {CustomEvent} The calling change event */ update : function (e) { if (e && e.prevValue === e.newValue) { return; } var p = this.paginator, currentPage = p.getCurrentPage(); // Replace content if there's been a change if (this.current !== currentPage || !currentPage || e.rebuild) { var labelBuilder = p.get('pageLabelBuilder'), range = Paginator.ui.PageLinks.calculateRange( currentPage, p.getTotalPages(), p.get('pageLinks')), start = range[0], end = range[1], content = '', linkTemplate,i; linkTemplate = '<a href="#" class="' + p.get('pageLinkClass') + '" page="'; for (i = start; i <= end; ++i) { if (i === currentPage) { content += '<span class="' + p.get('currentPageClass') + ' ' + p.get('pageLinkClass') + '">' + labelBuilder(i,p) + '</span>'; } else { content += linkTemplate + i + '">' + labelBuilder(i,p) + '</a>'; } } this.container.innerHTML = content; } }, /** * Force a rebuild of the page links. * @method rebuild * @param e {CustomEvent} The calling change event */ rebuild : function (e) { e.rebuild = true; this.update(e); }, /** * Removes the page links container node and clears event listeners * @method destroy * @private */ destroy : function () { YAHOO.util.Event.purgeElement(this.container,true); this.container.parentNode.removeChild(this.container); this.container = null; }, /** * Listener for the container's onclick event. Looks for qualifying link * clicks, and pulls the page number from the link's page attribute. * Sends link's page attribute to the Paginator's setPage method. * @method onClick * @param e {DOMEvent} The click event */ onClick : function (e) { var t = YAHOO.util.Event.getTarget(e); if (t && YAHOO.util.Dom.hasClass(t, this.paginator.get('pageLinkClass'))) { YAHOO.util.Event.stopEvent(e); this.paginator.setPage(parseInt(t.getAttribute('page'),10)); } }};})();(function () {var Paginator = YAHOO.widget.Paginator, l = YAHOO.lang;/** * ui Component to generate the link to jump to the first page. * * @namespace YAHOO.widget.Paginator.ui * @class FirstPageLink * @for YAHOO.widget.Paginator * * @constructor * @param p {Pagintor} Paginator instance to attach to */Paginator.ui.FirstPageLink = function (p) { this.paginator = p; p.createEvent('firstPageLinkLabelChange'); p.createEvent('firstPageLinkClassChange'); 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('firstPageLinkLabelChange',this.update,this,true); p.subscribe('firstPageLinkClassChange',this.update,this,true);};/** * Decorates Paginator instances with new attributes. Called during * Paginator instantiation. * @method init * @param p {Paginator} Paginator instance to decorate * @static */Paginator.ui.FirstPageLink.init = function (p) { /** * Used as innerHTML for the first page link/span. * @attribute firstPageLinkLabel * @default '<< first' */ p.setAttributeConfig('firstPageLinkLabel', { value : '<< first', validator : l.isString }); /** * CSS class assigned to the link/span * @attribute firstPageLinkClass * @default 'yui-pg-first' */ p.setAttributeConfig('firstPageLinkClass', { value : 'yui-pg-first', validator : l.isString });};// Instance members and methodsPaginator.ui.FirstPageLink.prototype = { /** * The currently placed HTMLElement node * @property current * @type HTMLElement * @private */ current : null, /** * Link node * @property link * @type HTMLElement * @private */ link : null, /** * Span node (inactive link) * @property span * @type HTMLElement * @private */ span : 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('firstPageLinkClass'), label = p.get('firstPageLinkLabel'); this.link = document.createElement('a'); this.span = document.createElement('span'); this.link.id = id_base + '-first-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 + '-first-span'; this.span.className = c; this.span.innerHTML = label; this.current = p.get('recordOffset') < 1 ? this.span : this.link; return this.current; }, /** * Swap the link and span nodes if appropriate. * @method update * @param e {CustomEvent} The calling change event */ update : function (e) { if (e && e.prevValue === e.newValue) { return; } var par = this.current ? this.current.parentNode : null; if (this.paginator.get('recordOffset') < 1) { if (par && this.current === this.link) { par.replaceChild(this.span,this.current); this.current = this.span; } } else { if (par && this.current === this.span) { par.replaceChild(this.link,this.current); this.current = this.link; } } }, /** * Removes the link/span node and clears event listeners * removal. * @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. Pass new value to setPage method. * @method onClick * @param e {DOMEvent} The click event */ onClick : function (e) { YAHOO.util.Event.stopEvent(e); this.paginator.setPage(1); }};})();(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.createEvent('lastPageLinkLabelChange'); p.createEvent('lastPageLinkClassChange'); 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 >>' */ p.setAttributeConfig('lastPageLinkLabel', { value : 'last >>', 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -