UNPKG

devextreme

Version:

HTML5 JavaScript Component Suite for Responsive Web Development

199 lines (179 loc) • 6.75 kB
"use strict"; var modules = require("./ui.grid_core.modules"), Pager = require("../pager"), inArray = require("../../core/utils/array").inArray, isDefined = require("../../core/utils/type").isDefined, hasWindow = require("../../core/utils/window").hasWindow(); var PAGER_CLASS = "pager", MAX_PAGES_COUNT = 10; var PagerView = modules.View.inherit({ init: function init() { var that = this, dataController = that.getController("data"); that._isVisible = false; dataController.changed.add(function (e) { if (!e || e.changeType !== "update") { that.render(); } }); }, _getPager: function _getPager() { var $element = this.element(); return $element && $element.data("dxPager"); }, _renderCore: function _renderCore() { var that = this, $element = that.element().addClass(that.addWidgetPrefix(PAGER_CLASS)), pagerOptions = that.option("pager") || {}, dataController = that.getController("data"), options = { maxPagesCount: MAX_PAGES_COUNT, pageIndex: 1 + (parseInt(dataController.pageIndex()) || 0), pageCount: dataController.pageCount(), pageSize: dataController.pageSize(), showPageSizes: pagerOptions.showPageSizeSelector, showInfo: pagerOptions.showInfo, pagesNavigatorVisible: pagerOptions.visible, showNavigationButtons: pagerOptions.showNavigationButtons, pageSizes: that.getPageSizes(), totalCount: dataController.totalCount(), hasKnownLastPage: dataController.hasKnownLastPage(), pageIndexChanged: function pageIndexChanged(pageIndex) { if (dataController.pageIndex() !== pageIndex - 1) { setTimeout(function () { dataController.pageIndex(pageIndex - 1); }); } }, pageSizeChanged: function pageSizeChanged(pageSize) { setTimeout(function () { dataController.pageSize(pageSize); }); } }; if (isDefined(pagerOptions.infoText)) { options.infoText = pagerOptions.infoText; } that._createComponent($element, Pager, options); }, getPageSizes: function getPageSizes() { var that = this, dataController = that.getController("data"), pagerOptions = that.option("pager"), allowedPageSizes = pagerOptions && pagerOptions.allowedPageSizes, pageSize = dataController.pageSize(); if (!isDefined(that._pageSizes) || inArray(pageSize, that._pageSizes) === -1) { that._pageSizes = []; if (pagerOptions) { if (Array.isArray(allowedPageSizes)) { that._pageSizes = allowedPageSizes; } else if (allowedPageSizes && pageSize > 1) { that._pageSizes = [Math.floor(pageSize / 2), pageSize, pageSize * 2]; } } } return that._pageSizes; }, isVisible: function isVisible() { var that = this, dataController = that.getController("data"), pagerOptions = that.option("pager"), pagerVisible = pagerOptions && pagerOptions.visible, scrolling = that.option("scrolling"); if (that._isVisible) { return true; } if (pagerVisible === "auto") { if (scrolling && (scrolling.mode === "virtual" || scrolling.mode === "infinite")) { pagerVisible = false; } else { pagerVisible = dataController.pageCount() > 1 || dataController.isLoaded() && !dataController.hasKnownLastPage(); } } that._isVisible = pagerVisible; return pagerVisible; }, getHeight: function getHeight() { return this.getElementHeight(); }, optionChanged: function optionChanged(args) { var that = this, name = args.name, isPager = name === "pager", isPaging = name === "paging", isDataSource = name === "dataSource", isScrolling = name === "scrolling", dataController = that.getController("data"); if (isPager || isPaging || isScrolling || isDataSource) { args.handled = true; if (dataController.skipProcessingPagingChange(args.fullName)) { return; } if (isPager || isPaging) { that._pageSizes = null; } if (isPager || isPaging || isScrolling) { that._isVisible = false; } if (!isDataSource) { that._invalidate(); if (hasWindow && isPager && that.component) { that.component.resize(); } } } } }); module.exports = { defaultOptions: function defaultOptions() { return { /** * @name GridBaseOptions.pager * @publicName pager * @type object */ pager: { /** * @name GridBaseOptions.pager.visible * @publicName visible * @type boolean */ visible: "auto", /** * @name GridBaseOptions.pager.showPageSizeSelector * @publicName showPageSizeSelector * @type boolean * @default false */ showPageSizeSelector: false, /** * @name GridBaseOptions.pager.allowedPageSizes * @publicName allowedPageSizes * @type Array<number> */ allowedPageSizes: "auto" /** * @name GridBaseOptions.pager.showNavigationButtons * @publicName showNavigationButtons * @type boolean * @default false */ /** * @name GridBaseOptions.pager.showInfo * @publicName showInfo * @type boolean * @default false */ /** * @name GridBaseOptions.pager.infoText * @publicName infoText * @type string * @default "Page {0} of {1} ({2} items)" */ } }; }, views: { pagerView: PagerView } };