pagination.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. "use strict";
  2. /**
  3. * @author cai
  4. * @date 2020-09-18
  5. */
  6. var __spreadArrays = (this && this.__spreadArrays) || function () {
  7. for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  8. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  9. for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  10. r[k] = a[j];
  11. return r;
  12. };
  13. var Pagination = /** @class */ (function () {
  14. function Pagination(options) {
  15. // 布局可选参数
  16. this._layout = ['home', 'prev', 'pager', 'total', 'sizes', 'jumper', 'next', 'last'];
  17. this.options = {
  18. // 容器
  19. element: '',
  20. // 样式类型
  21. type: 1,
  22. // 当前页
  23. pageIndex: 1,
  24. // 每页显示数量
  25. pageSize: 0,
  26. // 每页显示几条
  27. pageSizes: [],
  28. // 按钮数量
  29. pageCount: 9,
  30. // 总条数
  31. total: 0,
  32. // 页面布局
  33. layout: '',
  34. // 上一页文字
  35. prevText: '',
  36. // 下一页文字
  37. nextText: '',
  38. // 页码显示省略
  39. ellipsis: false,
  40. // 单页隐藏
  41. singlePageHide: true,
  42. // 是否禁用
  43. disabled: false,
  44. /**
  45. * @description 页码变化事件回调
  46. * @param index [number] 当前页码
  47. * @param pageSize [number] 每页显示条数 TODO: 只有使用sizes才有返回值
  48. */
  49. currentChange: function (index, pageSize) { },
  50. };
  51. this.element = null;
  52. this.pageNum = 0;
  53. this.showSelector = false;
  54. this.selectedIndex = 0;
  55. this.uniqid = Math.random().toString(36).substr(2);
  56. if (this.validate(options)) {
  57. this.init(options);
  58. }
  59. }
  60. // 初始化
  61. Pagination.prototype.init = function (options) {
  62. // 参数赋值
  63. this.setOptions(options);
  64. // 渲染
  65. this.render();
  66. };
  67. // 渲染
  68. Pagination.prototype.render = function () {
  69. var _this_1 = this;
  70. // 切换每页显示条数重新替换每页条数参数
  71. if (this.options.layout.indexOf('sizes') !== -1 && this.options.pageSizes instanceof Array) {
  72. this.options.pageSizes.forEach(function (v, k) {
  73. if (v == _this_1.options.pageSize) {
  74. _this_1.selectedIndex = k;
  75. }
  76. });
  77. if (!isNaN(this.options.pageSizes[this.selectedIndex])) {
  78. this.options.pageSize = this.options.pageSizes[this.selectedIndex];
  79. }
  80. }
  81. // 总页数
  82. this.pageNum = Math.ceil(this.options.total / this.options.pageSize);
  83. // 单页隐藏
  84. if (this.pageNum === 1 && this.options.singlePageHide) {
  85. // 清空元素
  86. this.element.innerHTML = '';
  87. return;
  88. }
  89. // 最大页码
  90. if (this.options.pageIndex > this.pageNum)
  91. this.options.pageIndex = this.pageNum;
  92. // 最小页码
  93. if (this.options.pageIndex <= 0)
  94. this.options.pageIndex = 1;
  95. var element = null;
  96. // 主体容器
  97. var container = this.createElement('div', '_page_container');
  98. // layout 渲染
  99. this.options.layout.split(',').forEach(function (v) {
  100. if (_this_1._layout.indexOf(v.trim()) !== -1) {
  101. element = _this_1[v.trim()]();
  102. element && container.appendChild(element);
  103. }
  104. });
  105. var old_container = document.querySelector(this.options.element + " ._page_container");
  106. if (old_container) {
  107. // 保存元素
  108. this.element.replaceChild(container, old_container);
  109. }
  110. else {
  111. // 保存元素
  112. this.element.appendChild(container);
  113. }
  114. };
  115. // 首页
  116. Pagination.prototype.home = function () {
  117. var _this_1 = this;
  118. // 禁用样式
  119. var disabled = this.options.pageIndex <= 1 ? ['_disabled_c'] : [];
  120. // 手势禁止
  121. if (this.options.pageIndex <= 1 && this.options.disabled)
  122. disabled.push('_disabled');
  123. var element = this.createElement('div', __spreadArrays(['_home', "_home_" + this.options.type], disabled));
  124. element.innerText = '首页';
  125. element.addEventListener('click', function () {
  126. if (_this_1.options.pageIndex > 1) {
  127. _this_1.handleChangePage(1);
  128. }
  129. });
  130. return element;
  131. };
  132. // 上一页
  133. Pagination.prototype.prev = function () {
  134. var _this_1 = this;
  135. // 禁用样式
  136. var disabled = this.options.pageIndex <= 1 ? ['_disabled_c'] : [];
  137. // 手势禁止
  138. if (this.options.pageIndex <= 1 && this.options.disabled)
  139. disabled.push('_disabled');
  140. var element = this.createElement('div', __spreadArrays(['_prev', "_prev_" + this.options.type], disabled, (this.options.prevText ? ['_prev_text'] : ['_iconfont', 'iconzuo'])));
  141. element.innerText = this.options.prevText || '';
  142. // 上一页事件
  143. element.addEventListener('click', function () {
  144. if (_this_1.options.pageIndex > 1) {
  145. _this_1.handleChangePage(_this_1.options.pageIndex - 1);
  146. }
  147. });
  148. return element;
  149. };
  150. // 页码
  151. Pagination.prototype.pager = function () {
  152. var _this_1 = this;
  153. var _this = this, li, active, attr;
  154. // 页码容器
  155. var ul = this.createElement('ul', ['_pages', "_pages_" + this.options.type]);
  156. // 区间值
  157. var between = this.getBetween();
  158. // 生成区间值
  159. var arrs = this.generateArray(between.min, between.max);
  160. // 显示省略页码
  161. if (this.options.ellipsis) {
  162. // 判断是否不存在最小页码
  163. if (arrs[0] > 1) {
  164. arrs.splice(1, 0, '...');
  165. arrs[0] = 1;
  166. }
  167. // 判断是否不存在最大页码
  168. if (arrs[arrs.length - 1] < this.pageNum) {
  169. arrs.splice(arrs.length - 1, 0, '...');
  170. arrs[arrs.length - 1] = this.pageNum;
  171. }
  172. }
  173. // 生成页码
  174. arrs.forEach(function (v, k) {
  175. active = v === _this_1.options.pageIndex ? ["_active_" + _this_1.options.type] : [];
  176. // 手势禁止
  177. if (v === _this_1.options.pageIndex && _this_1.options.disabled)
  178. active.push('_disabled');
  179. li = _this_1.createElement('li', __spreadArrays(["_pages_li_" + _this_1.options.type], active));
  180. if (isNaN(v)) {
  181. if (k <= 1) {
  182. attr = 'prev';
  183. li.classList.add('_pager_prev');
  184. }
  185. else {
  186. attr = 'next';
  187. li.classList.add('_pager_next');
  188. }
  189. li._id = attr;
  190. }
  191. else {
  192. li.innerText = v.toString();
  193. }
  194. li.addEventListener('click', function () {
  195. // 省略号向上跳转
  196. if (this._id === 'prev') {
  197. var prevIndex = _this.options.pageIndex - _this.options.pageCount + 2;
  198. _this.handleChangePage(prevIndex < 1 ? 1 : prevIndex);
  199. return;
  200. }
  201. // 省略号向下跳转
  202. if (this._id === 'next') {
  203. var nextIndex = _this.options.pageIndex + _this.options.pageCount - 2;
  204. _this.handleChangePage(nextIndex > _this.pageNum ? _this.pageNum : nextIndex);
  205. return;
  206. }
  207. if (v != _this.options.pageIndex) {
  208. _this.handleChangePage(v);
  209. }
  210. });
  211. ul.appendChild(li);
  212. });
  213. return ul;
  214. };
  215. // 下一页
  216. Pagination.prototype.next = function () {
  217. var _this_1 = this;
  218. // 禁用下一页
  219. var disabled = this.options.pageIndex >= this.pageNum ? ['_disabled_c'] : [];
  220. // 手势禁止
  221. if (this.options.pageIndex >= this.pageNum && this.options.disabled)
  222. disabled.push('_disabled');
  223. // 下一页
  224. var element = this.createElement('div', __spreadArrays(['_next', "_next_" + this.options.type], disabled, (this.options.nextText ? ['_next_text'] : ['_iconfont', 'iconGroup-'])));
  225. // 下一页文字
  226. element.innerText = this.options.nextText || '';
  227. // 下一页事件
  228. element.addEventListener('click', function () {
  229. if (_this_1.options.pageIndex < _this_1.pageNum) {
  230. _this_1.handleChangePage(_this_1.options.pageIndex + 1);
  231. }
  232. });
  233. return element;
  234. };
  235. // 尾页
  236. Pagination.prototype.last = function () {
  237. var _this_1 = this;
  238. // 禁用下一页
  239. var disabled = this.options.pageIndex >= this.pageNum ? ['_disabled_c'] : [];
  240. // // 手势禁止
  241. if (this.options.pageIndex >= this.pageNum && this.options.disabled)
  242. disabled.push('_disabled');
  243. var element = this.createElement('div', __spreadArrays(['_last', "_last_" + this.options.type], disabled));
  244. element.innerText = '尾页';
  245. element.addEventListener('click', function () {
  246. if (_this_1.options.pageIndex < _this_1.pageNum) {
  247. _this_1.handleChangePage(_this_1.pageNum);
  248. }
  249. });
  250. return element;
  251. };
  252. // 输入框跳转
  253. Pagination.prototype.jumper = function () {
  254. var _this = this;
  255. // 容器
  256. var jumper = this.createElement('div', '_jumper');
  257. // 总页码
  258. var total = this.createElement('div', '_count');
  259. total.innerText = "\u5171 " + this.pageNum + " \u9875";
  260. jumper.appendChild(total);
  261. var text_1 = this.createElement('span');
  262. text_1.innerText = '前往';
  263. jumper.appendChild(text_1);
  264. var value = 0;
  265. // 输入框
  266. var input = this.createElement('input', ['_jumper_input', this.uniqid]);
  267. input.type = 'number';
  268. input.value = this.options.pageIndex.toString();
  269. input.setAttribute('min', '1');
  270. input.setAttribute('max', this.pageNum.toString());
  271. var handle = ['blur', 'keydown'];
  272. handle.forEach(function (v) {
  273. input.addEventListener(v, function (e) {
  274. if (e.type === 'keydown' && e.keyCode !== 13) {
  275. return;
  276. }
  277. value = ~~this.value;
  278. if (value < 1)
  279. value = 1;
  280. if (value > _this.pageNum)
  281. value = _this.pageNum;
  282. // @ts-ignore
  283. this.value = value;
  284. if (value !== _this.options.pageIndex)
  285. _this.handleChangePage(value);
  286. // 获取焦点
  287. if (e.keyCode == 13) {
  288. setTimeout(function () {
  289. document.querySelector("." + _this.uniqid).focus();
  290. });
  291. }
  292. });
  293. });
  294. jumper.appendChild(input);
  295. var text_2 = this.createElement('span');
  296. text_2.innerText = '页';
  297. jumper.appendChild(text_2);
  298. return jumper;
  299. };
  300. // 选择页码
  301. Pagination.prototype.sizes = function () {
  302. var _this_1 = this;
  303. var _this = this;
  304. var success = false;
  305. var mode = '';
  306. var lis = null;
  307. var active = [];
  308. var element = this.createElement('div', '_sizes');
  309. // 选择框容器
  310. var box = this.createElement('div', '_sizes_select_container');
  311. // 每页条数选择框
  312. var ul = this.createElement('ul', '_sizes_select');
  313. if (this.options.pageSizes instanceof Array) {
  314. // 渲染选择框
  315. this.options.pageSizes.forEach(function (v, key) {
  316. if (!isNaN(v)) {
  317. success = true;
  318. active = _this_1.selectedIndex === key ? ['_sizes_select_active'] : [];
  319. lis = _this_1.createElement('li', __spreadArrays(['_sizes_select_li'], active));
  320. lis.innerText = v + "\u6761/\u9875";
  321. lis.addEventListener('click', function () {
  322. if (_this.selectedIndex !== key) {
  323. _this.selectedIndex = key;
  324. _this.handleChangePage(1);
  325. }
  326. else {
  327. var mode_1 = _this.showSelector ? 'remove' : 'add';
  328. i.classList[mode_1]('_sizes_icon_rotate');
  329. box.classList[mode_1]('_sizes_select_container_show');
  330. _this.showSelector = !_this.showSelector;
  331. }
  332. });
  333. ul.appendChild(lis);
  334. }
  335. });
  336. }
  337. else {
  338. return false;
  339. }
  340. if (!success)
  341. return false;
  342. box.appendChild(ul);
  343. element.appendChild(box);
  344. var text = this.createElement('div', '_sizes_text');
  345. text.innerText = this.options.pageSizes[this.selectedIndex] + "\u6761/\u9875";
  346. // 显示选择框、旋转icon
  347. text.addEventListener('click', function () {
  348. this.classList.add('_sizes_active');
  349. mode = _this.showSelector ? 'remove' : 'add';
  350. _this.showSelector = !_this.showSelector;
  351. i.classList[mode]('_sizes_icon_rotate');
  352. box.classList[mode]('_sizes_select_container_show');
  353. });
  354. var i = this.createElement('i', ['icon_down', '_iconfont', 'iconGroup-1']);
  355. text.appendChild(i);
  356. element.appendChild(text);
  357. return element;
  358. };
  359. // 总页数
  360. Pagination.prototype.total = function () {
  361. var element = this.createElement('div', '_count');
  362. element.innerText = "\u5171 " + this.options.total + " \u6761";
  363. return element;
  364. };
  365. // 页码变化
  366. Pagination.prototype.handleChangePage = function (index) {
  367. this.options.pageIndex = index;
  368. this.showSelector = false;
  369. // 回调
  370. if (typeof this.options.currentChange === 'function') {
  371. if (this.options.pageSizes[this.selectedIndex])
  372. this.options.pageSize = this.options.pageSizes[this.selectedIndex];
  373. this.options.currentChange(index, this.options.pageSize);
  374. }
  375. // 重新渲染
  376. this.render();
  377. };
  378. // 计算区间值
  379. Pagination.prototype.getBetween = function () {
  380. // 最小下标
  381. var min = this.options.pageIndex - Math.floor(this.options.pageCount / 2);
  382. // 最小下标最大值
  383. if (min > this.pageNum - this.options.pageCount) {
  384. min = this.pageNum - this.options.pageCount + 1;
  385. }
  386. // 最小值
  387. if (min <= 1)
  388. min = 1;
  389. // 最大下标
  390. var max = this.options.pageIndex + Math.floor(this.options.pageCount / 2);
  391. // 最大下标最小值
  392. if (max < this.options.pageCount)
  393. max = this.options.pageCount;
  394. // 最大值
  395. if (max > this.pageNum)
  396. max = this.pageNum;
  397. return { min: min, max: max };
  398. };
  399. // 生成区间数组
  400. Pagination.prototype.generateArray = function (start, end) {
  401. var arr = [];
  402. for (var i = start; i <= end; i++) {
  403. arr.push(i);
  404. }
  405. return arr;
  406. };
  407. // 创建元素
  408. Pagination.prototype.createElement = function (tag, classList) {
  409. var dom = document.createElement(tag);
  410. if (classList) {
  411. if (classList instanceof Array) {
  412. classList.forEach(function (v) {
  413. dom.classList.add(v);
  414. });
  415. }
  416. else {
  417. dom.classList.add(classList);
  418. }
  419. }
  420. return dom;
  421. };
  422. // 参数验证
  423. Pagination.prototype.validate = function (options) {
  424. if (!options)
  425. throw new Error('options of null');
  426. if (typeof options !== 'object')
  427. throw new Error('options not an object');
  428. if (!document.querySelector(options.element))
  429. throw new Error('element of null');
  430. if (!options.layout)
  431. throw new Error('layout of null');
  432. ['type', 'pageIndex', 'pageSize', 'pageCount', 'total'].forEach(function (v) {
  433. if (options[v]) {
  434. if (isNaN(options[v]))
  435. throw new Error(v + " not an number");
  436. if (v === 'pageCount' && options[v] % 2 === 0)
  437. throw new Error(v + " not an odd number");
  438. if (v === 'pageCount' && options[v] < 4)
  439. throw new Error(v + " must be greater than four");
  440. }
  441. });
  442. return true;
  443. };
  444. // 参数赋值
  445. Pagination.prototype.setOptions = function (options) {
  446. // 容器
  447. this.element = document.querySelector(options.element);
  448. for (var name_1 in options) {
  449. if (options[name_1] !== void 0) {
  450. this.options[name_1] = isNaN(options[name_1]) ? options[name_1] : +options[name_1];
  451. }
  452. }
  453. };
  454. return Pagination;
  455. }());