Paging.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. o2.widget = o2.widget || {};
  2. o2.require("o2.widget.Common", null, false);
  3. o2.widget.Paging = new Class({
  4. Implements: [Options, Events],
  5. Extends: o2.widget.Common,
  6. options: {
  7. style : "default",
  8. countPerPage: 20,
  9. visiblePages: 10,
  10. currentPage: 1,
  11. itemSize: 0,
  12. pageSize: 0,
  13. hasNextPage: true,
  14. hasPrevPage: true,
  15. hasTruningBar: true,
  16. hasJumper: true,
  17. hiddenWithDisable: false,
  18. hiddenWithNoItem: true,
  19. text: {
  20. prePage: "",
  21. nextPage: "",
  22. firstPage: "",
  23. lastPage: ""
  24. }
  25. },
  26. initialize: function (node, options, css) {
  27. this.setOptions(options || {});
  28. this.container = $(node);
  29. this.path = o2.session.path + "/widget/$Paging/";
  30. this.cssPath = o2.session.path + "/widget/$Paging/" + this.options.style + "/css.wcss";
  31. this._loadCss();
  32. if (css) {
  33. this.css = Object.clone(this.css);
  34. this.css = Object.merge(this.css, css);
  35. }
  36. },
  37. load: function () {
  38. this.fireEvent("queryLoad", this);
  39. this.options.pageSize = Math.ceil(this.options.itemSize / this.options.countPerPage);
  40. if ( this.options.pageSize == 0 && this.options.hiddenWithNoItem) return;
  41. this.container.empty();
  42. this.createNode();
  43. this.fireEvent("postLoad", this);
  44. },
  45. createNode: function() {
  46. var _self = this;
  47. this.node = new Element("div.pagingBar", {styles: this.css.pagingBar}).inject(this.container);
  48. var i, max, min;
  49. var showWithDisable = !this.options.hiddenWithDisable;
  50. var pageSize = this.options.pageSize;
  51. var currentPage = this.options.currentPage;
  52. var visiblePages = this.options.visiblePages;
  53. var halfCount = Math.floor(visiblePages / 2);
  54. if (pageSize <= visiblePages) {
  55. min = 1;
  56. max = pageSize;
  57. } else if (currentPage + halfCount > pageSize) {
  58. min = pageSize - visiblePages;
  59. max = pageSize;
  60. } else if (currentPage - halfCount < 1) {
  61. min = 1;
  62. max = visiblePages;
  63. } else {
  64. min = currentPage - halfCount;
  65. max = currentPage + halfCount;
  66. }
  67. if (min > 1 || !this.options.hiddenWithDisable )this.createFirst();
  68. if (this.options.hasPrevPage && ( currentPage != 1 || showWithDisable ) ){
  69. this.createPrev();
  70. }
  71. if( this.options.hasTruningBar ){
  72. this.pageTurnContainer = new Element("div", {
  73. styles : this.css.pageTurnContainer
  74. }).inject( this.node );
  75. this.pageTurnNodes = [];
  76. for (i = min; i <= max; i++) {
  77. if (currentPage == i) {
  78. this.currentPage = new Element("div.currentPage", {
  79. "styles": this.css.currentPage,
  80. "text" : i
  81. }).inject(this.pageTurnContainer);
  82. } else {
  83. this.pageTurnNodes.push( this.createPageTurnNode(i) );
  84. }
  85. }
  86. }
  87. if (this.options.hasJumper) {
  88. this.createPageJumper();
  89. }
  90. if (this.options.hasNextPage && ( currentPage != pageSize || showWithDisable )){
  91. this.createNext();
  92. }
  93. if( max < pageSize || showWithDisable )this.createLast();
  94. },
  95. createFirst : function(){
  96. var firstPage = this.firstPage = new Element("div.firstPage", {
  97. styles: this.css.firstPage
  98. }).inject(this.node);
  99. if (this.options.text.firstPage) firstPage.set("text", this.options.text.firstPage);
  100. firstPage.addEvents({
  101. "mouseover": function (ev) {
  102. ev.target.setStyles(this.css.firstPage_over)
  103. }.bind(this),
  104. "mouseout": function (ev) {
  105. ev.target.setStyles(this.css.firstPage)
  106. }.bind(this),
  107. "click": function () {
  108. this.gotoPage(1)
  109. }.bind(this)
  110. })
  111. },
  112. createLast : function(){
  113. var lastPage = this.lastPage = new Element("div.lastPage", {
  114. styles: this.css.lastPage
  115. }).inject(this.node);
  116. if (this.options.text.lastPage) lastPage.set("text", this.options.text.lastPage);
  117. lastPage.addEvents({
  118. "mouseover": function (ev) {
  119. ev.target.setStyles(this.css.lastPage_over)
  120. }.bind(this),
  121. "mouseout": function (ev) {
  122. ev.target.setStyles(this.css.lastPage)
  123. }.bind(this),
  124. "click": function () {
  125. this.gotoPage( this.options.pageSize )
  126. }.bind(this)
  127. })
  128. },
  129. createPrev : function(){
  130. var prePage = this.prePage = new Element("div.prePage", {
  131. styles: this.css.prePage
  132. }).inject(this.node);
  133. if (this.options.text.prePage) prePage.set("text", this.options.text.prePage);
  134. prePage.addEvents({
  135. "mouseover": function (ev) {
  136. ev.target.setStyles(this.css.prePage_over)
  137. }.bind(this),
  138. "mouseout": function (ev) {
  139. ev.target.setStyles(this.css.prePage)
  140. }.bind(this),
  141. "click": function () {
  142. this.gotoPage(this.options.currentPage - 1)
  143. }.bind(this)
  144. });
  145. },
  146. createNext : function(){
  147. var nextPage = this.nextPage = new Element("div.nextPage", {
  148. styles: this.css.nextPage
  149. }).inject(this.node);
  150. if (this.options.text.nextPage) nextPage.set("text", this.options.text.nextPage);
  151. nextPage.addEvents({
  152. "mouseover": function (ev) {
  153. ev.target.setStyles(this.css.nextPage_over)
  154. }.bind(this),
  155. "mouseout": function (ev) {
  156. ev.target.setStyles(this.css.nextPage)
  157. }.bind(this),
  158. "click": function () {
  159. this.gotoPage(this.options.currentPage + 1)
  160. }.bind(this)
  161. });
  162. },
  163. createPageTurnNode: function(i){
  164. var pageTurnNode = new Element("div.pageItem", {
  165. "styles": this.css.pageItem,
  166. "text": i
  167. }).inject(this.pageTurnContainer);
  168. pageTurnNode.addEvents({
  169. "mouseover": function (ev) {
  170. ev.target.setStyles(this.css.pageItem_over)
  171. }.bind(this),
  172. "mouseout": function (ev) {
  173. ev.target.setStyles(this.css.pageItem)
  174. }.bind(this),
  175. "click": function () {
  176. this.obj.gotoPage(this.num)
  177. }.bind({obj: this, num: i})
  178. });
  179. return pageTurnNode;
  180. },
  181. createPageJumper : function(){
  182. var _self = this;
  183. var pageJumper = this.pageJumper = new Element("input.pageJumper", {
  184. "styles": this.css.pageJumper,
  185. "title": "输入页码,离开输入框或按回车跳转"
  186. }).inject(this.node);
  187. this.pageJumperText = new Element("div.pageText", {
  188. "styles": this.css.pageJumperText,
  189. "text": "/" + this.options.pageSize
  190. }).inject(this.node);
  191. pageJumper.addEvents({
  192. "focus": function (ev) {
  193. ev.target.setStyles(this.css.pageJumper_over)
  194. }.bind(this),
  195. "blur": function (ev) {
  196. if( this.value )_self.gotoPage(this.value);
  197. ev.target.setStyles(_self.css.pageJumper);
  198. },
  199. "keyup": function (e) {
  200. this.value = this.value.replace(/[^0-9_]/g, '');
  201. },
  202. "keydown": function (e) {
  203. if (e.code == 13 && this.value != "") {
  204. _self.gotoPage(this.value);
  205. e.stopPropagation();
  206. //e.preventDefault();
  207. }
  208. }
  209. });
  210. },
  211. gotoPage: function (num) {
  212. if (num < 1 || num > this.options.pageSize) return;
  213. this.fireEvent("jumpingPage", [num]);
  214. this.options.currentPage = num;
  215. this.load();
  216. },
  217. gotoItem: function (itemNum) {
  218. var pageNum = Math.ceil(itemNum / this.options.countPerPage);
  219. var index = itemNum % this.options.countPerPage;
  220. this.fireEvent("jumpingPage", [pageNum, itemNum, index]);
  221. this.options.currentPage = pageNum;
  222. this.load();
  223. }
  224. });