📄 paginator.js
字号:
// Listen for changes to totalRecords and alwaysVisible this.subscribe('totalRecordsChange',this.updateVisibility,this,true); this.subscribe('alwaysVisibleChange',this.updateVisibility,this,true); // Fire the pageChange event when appropriate this.subscribe('totalRecordsChange',this._handleStateChange,this,true); this.subscribe('recordOffsetChange',this._handleStateChange,this,true); this.subscribe('rowsPerPageChange',this._handleStateChange,this,true); // Update recordOffset when totalRecords is reduced below this.subscribe('totalRecordsChange',this._syncRecordOffset,this,true); }, /** * Sets recordOffset to the starting index of the previous page when * totalRecords is reduced below the current recordOffset. * @method _syncRecordOffset * @param e {Event} totalRecordsChange event * @protected */ _syncRecordOffset : function (e) { var v = e.newValue,rpp,state; if (e.prevValue !== v) { if (v !== YAHOO.widget.Paginator.VALUE_UNLIMITED) { rpp = this.get('rowsPerPage'); if (rpp && this.get('recordOffset') >= v) { state = this.getState({ totalRecords : e.prevValue, recordOffset : this.get('recordOffset') }); this.set('recordOffset', state.before.recordOffset); this._firePageChange(state); } } } }, /** * Fires the pageChange event when the state attributes have changed in * such a way as to locate the current recordOffset on a new page. * @method _handleStateChange * @param e {Event} the attribute change event * @protected */ _handleStateChange : function (e) { if (e.prevValue !== e.newValue) { var change = this._state || {}, state; change[e.type.replace(/Change$/,'')] = e.prevValue; state = this.getState(change); if (state.page !== state.before.page) { if (this._batch) { this._pageChanged = true; } else { this._firePageChange(state); } } } }, /** * Fires a pageChange event in the form of a standard attribute change * event with additional properties prevState and newState. * @method _firePageChange * @param state {Object} the result of getState(oldState) * @protected */ _firePageChange : function (state) { if (YAHOO.lang.isObject(state)) { var current = state.before; delete state.before; this.fireEvent('pageChange',{ type : 'pageChange', prevValue : state.page, newValue : current.page, prevState : state, newState : current }); } }, /** * Render the pagination controls per the format attribute into the * specified container nodes. * @method render */ render : function () { if (this.get('rendered')) { return; } // Forgo rendering if only one page and alwaysVisible is off var totalRecords = this.get('totalRecords'); if (totalRecords !== YAHOO.widget.Paginator.VALUE_UNLIMITED && totalRecords < this.get('rowsPerPage') && !this.get('alwaysVisible')) { return; } var Dom = YAHOO.util.Dom, template = this.get('template'), containerClass = this.get('containerClass'); // add marker spans to the template html to indicate drop zones // for ui components template = template.replace(/\{([a-z0-9_ \-]+)\}/gi, '<span class="yui-pg-ui $1"></span>'); for (var i = 0, len = this._containers.length; i < len; ++i) { var c = this._containers[i], // ex. yui-pg0-1 (first paginator, second container) id_base = YAHOO.widget.Paginator.ID_BASE + this.get('id') + '-' + i; if (!c) { continue; } // Hide the container while its contents are rendered c.style.display = 'none'; Dom.addClass(c,containerClass); // Place the template innerHTML c.innerHTML = template; // Replace each marker with the ui component's render() output var markers = Dom.getElementsByClassName('yui-pg-ui','span',c); for (var j = 0, jlen = markers.length; j < jlen; ++j) { var m = markers[j], mp = m.parentNode, name = m.className.replace(/\s*yui-pg-ui\s+/g,''), UIComp = YAHOO.widget.Paginator.ui[name]; if (YAHOO.lang.isFunction(UIComp)) { var comp = new UIComp(this); if (YAHOO.lang.isFunction(comp.render)) { mp.replaceChild(comp.render(id_base),m); } } } // Show the container allowing page reflow c.style.display = ''; } // Set render attribute manually to support its readOnly contract if (this._containers.length) { this.setAttributeConfig('rendered',{value:true}); this.fireEvent('render',this.getState()); // For backward compatibility this.fireEvent('rendered',this.getState()); } }, /** * Removes controls from the page and unhooks events. * @method destroy */ destroy : function () { this.fireEvent('beforeDestroy'); this.fireEvent('destroy'); this.setAttributeConfig('rendered',{value:false}); }, /** * Hides the containers if there is only one page of data and attribute * alwaysVisible is false. Conversely, it displays the containers if either * there is more than one page worth of data or alwaysVisible is turned on. * @method updateVisibility */ updateVisibility : function (e) { var alwaysVisible = this.get('alwaysVisible'); if (e.type === 'alwaysVisibleChange' || !alwaysVisible) { var totalRecords = this.get('totalRecords'), visible = true, rpp = this.get('rowsPerPage'), rppOptions = this.get('rowsPerPageOptions'), i,len; if (YAHOO.lang.isArray(rppOptions)) { for (i = 0, len = rppOptions.length; i < len; ++i) { rpp = Math.min(rpp,rppOptions[i]); } } if (totalRecords !== YAHOO.widget.Paginator.VALUE_UNLIMITED && totalRecords <= rpp) { visible = false; } visible = visible || alwaysVisible; for (i = 0, len = this._containers.length; i < len; ++i) { YAHOO.util.Dom.setStyle(this._containers[i],'display', visible ? '' : 'none'); } } }, /** * Get the configured container nodes * @method getContainerNodes * @return {Array} array of HTMLElement nodes */ getContainerNodes : function () { return this._containers; }, /** * Get the total number of pages in the data set according to the current * rowsPerPage and totalRecords values. If totalRecords is not set, or * set to YAHOO.widget.Paginator.VALUE_UNLIMITED, returns * YAHOO.widget.Paginator.VALUE_UNLIMITED. * @method getTotalPages * @return {number} */ getTotalPages : function () { var records = this.get('totalRecords'); var perPage = this.get('rowsPerPage'); // rowsPerPage not set. Can't calculate if (!perPage) { return null; } if (records === YAHOO.widget.Paginator.VALUE_UNLIMITED) { return YAHOO.widget.Paginator.VALUE_UNLIMITED; } return Math.ceil(records/perPage); }, /** * Does the requested page have any records? * @method hasPage * @param page {number} the page in question * @return {boolean} */ hasPage : function (page) { if (!YAHOO.lang.isNumber(page) || page < 1) { return false; } var totalPages = this.getTotalPages(); return (totalPages === YAHOO.widget.Paginator.VALUE_UNLIMITED || totalPages >= page); }, /** * Get the page number corresponding to the current record offset. * @method getCurrentPage * @return {number} */ getCurrentPage : function () { var perPage = this.get('rowsPerPage'); if (!perPage || !this.get('totalRecords')) { return 0; } return Math.floor(this.get('recordOffset') / perPage) + 1; }, /** * Are there records on the next page? * @method hasNextPage * @return {boolean} */ hasNextPage : function () { var currentPage = this.getCurrentPage(), totalPages = this.getTotalPages(); return currentPage && (totalPages === YAHOO.widget.Paginator.VALUE_UNLIMITED || currentPage < totalPages); }, /** * Get the page number of the next page, or null if the current page is the * last page. * @method getNextPage * @return {number} */ getNextPage : function () { return this.hasNextPage() ? this.getCurrentPage() + 1 : null; }, /** * Is there a page before the current page? * @method hasPreviousPage * @return {boolean} */ hasPreviousPage : function () { return (this.getCurrentPage() > 1); }, /** * Get the page number of the previous page, or null if the current page * is the first page. * @method getPreviousPage * @return {number} */ getPreviousPage : function () { return (this.hasPreviousPage() ? this.getCurrentPage() - 1 : 1); }, /** * Get the start and end record indexes of the specified page. * @method getPageRecords * @param page {number} (optional) The page (current page if not specified) * @return {Array} [start_index, end_index] */ getPageRecords : function (page) { if (!YAHOO.lang.isNumber(page)) { page = this.getCurrentPage(); } var perPage = this.get('rowsPerPage'), records = this.get('totalRecords'), start, end; if (!page || !perPage) { return null; } start = (page - 1) * perPage; if (records !== YAHOO.widget.Paginator.VALUE_UNLIMITED) { if (start >= records) { return null; } end = Math.min(start + perPage, records) - 1; } else { end = start + perPage - 1; } return [start,end]; }, /** * Set the current page to the provided page number if possible. * @method setPage * @param newPage {number} the new page number * @param silent {boolean} whether to forcibly avoid firing the * changeRequest event */ setPage : function (page,silent) { if (this.hasPage(page) && page !== this.getCurrentPage()) { if (this.get('updateOnChange') || silent) { this.set('recordOffset', (page - 1) * this.get('rowsPerPage')); } else { this.fireEvent('changeRequest',this.getState({'page':page})); } } }, /** * Get the number of rows per page. * @method getRowsPerPage * @return {number} the current setting of the rowsPerPage attribute */ getRowsPerPage : function () { return this.get('rowsPerPage'); }, /** * Set the number of rows per page. * @method setRowsPerPage * @param rpp {number} the new number of rows per page * @param silent {boolean} whether to forcibly avoid firing the * changeRequest event */ setRowsPerPage : function (rpp,silent) { if (YAHOO.lang.isNumber(rpp) && rpp > 0 && rpp !== this.get('rowsPerPage')) { if (this.get('updateOnChange') || silent) { this.set('rowsPerPage',rpp); } else { this.fireEvent('changeRequest', this.getState({'rowsPerPage':rpp})); } } }, /** * Get the total number of records. * @method getTotalRecords * @return {number} the current setting of totalRecords attribute */ getTotalRecords : function () { return this.get('totalRecords'); }, /** * Set the total number of records. * @method setTotalRecords * @param total {number} the new total number of records * @param silent {boolean} whether to forcibly avoid firing the changeRequest event */ setTotalRecords : function (total,silent) { if (YAHOO.lang.isNumber(total) && total >= 0 && total !== this.get('totalRecords')) { if (this.get('updateOnChange') || silent) { this.set('totalRecords',total); } else { this.fireEvent('changeRequest', this.getState({'totalRecords':total})); } } }, /** * Get the index of the first record on the current page * @method getStartIndex * @return {number} the index of the first record on the current page */ getStartIndex : function () { return this.get('recordOffset'); }, /** * Move the record offset to a new starting index. This will likely cause * the calculated current page to change. You should probably use setPage. * @method setStartIndex * @param offset {number} the new record offset * @param silent {boolean} whether to forcibly avoid firing the changeRequest event */ setStartIndex : function (offset,silent) { if (YAHOO.lang.isNumber(offset) && offset >= 0 && offset !== this.get('recordOffset')) { if (this.get('updateOnChange') || silent) { this.set('recordOffset',offset); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -