Paging.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. hasFirstPage : true,
  14. hasLastPage : true,
  15. hasNextPage: true,
  16. hasPrevPage: true,
  17. hasBatchTuring : true,
  18. hasTruningBar: true,
  19. hasJumper: true,
  20. hiddenWithDisable: false,
  21. hiddenWithNoItem: true,
  22. text: {
  23. preBatchTuring : "...",
  24. nextBatchTuring : "...",
  25. prePage: "",
  26. nextPage: "",
  27. firstPage: "",
  28. lastPage: ""
  29. }
  30. },
  31. initialize: function (node, options, css) {
  32. this.setOptions(options || {});
  33. this.container = $(node);
  34. this.path = o2.session.path + "/widget/$Paging/";
  35. this.cssPath = o2.session.path + "/widget/$Paging/" + this.options.style + "/css.wcss";
  36. this._loadCss();
  37. if (css) {
  38. this.css = Object.clone(this.css);
  39. this.css = Object.merge(this.css, css);
  40. }
  41. },
  42. load: function () {
  43. this.fireEvent("queryLoad", this);
  44. this.options.pageSize = Math.ceil(this.options.itemSize / this.options.countPerPage);
  45. if ( this.options.pageSize == 0 && this.options.hiddenWithNoItem) return;
  46. this.container.empty();
  47. this.createNode();
  48. this.fireEvent("postLoad", this);
  49. },
  50. createNode: function() {
  51. var _self = this;
  52. this.node = new Element("div.pagingBar", {styles: this.css.pagingBar}).inject(this.container);
  53. var i, max, min;
  54. var showWithDisable = !this.options.hiddenWithDisable;
  55. var pageSize = this.options.pageSize;
  56. var currentPage = this.options.currentPage;
  57. var visiblePages = this.options.visiblePages;
  58. var halfCount = Math.floor(visiblePages / 2);
  59. if (pageSize <= visiblePages) {
  60. min = 1;
  61. max = pageSize;
  62. } else if (currentPage + halfCount > pageSize) {
  63. min = pageSize - visiblePages;
  64. max = pageSize;
  65. } else if (currentPage - halfCount < 1) {
  66. min = 1;
  67. max = visiblePages;
  68. } else {
  69. min = currentPage - halfCount;
  70. max = currentPage + halfCount;
  71. }
  72. if( this.options.hasFirstPage && (min > 1 || showWithDisable) ){
  73. this.createFirst();
  74. }
  75. if (this.options.hasPrevPage && ( currentPage != 1 || showWithDisable ) ){
  76. this.createPrev();
  77. }
  78. if( this.options.hasTruningBar && this.options.hasBatchTuring && ( min > 1 ) ){ //showWithDisable
  79. this.createPrevBatch( min );
  80. }
  81. if( this.options.hasTruningBar ){
  82. this.pageTurnContainer = new Element("div", {
  83. styles : this.css.pageTurnContainer
  84. }).inject( this.node );
  85. this.pageTurnNodes = [];
  86. for (i = min; i <= max; i++) {
  87. if (currentPage == i) {
  88. this.currentPage = new Element("div.currentPage", {
  89. "styles": this.css.currentPage,
  90. "text" : i
  91. }).inject(this.pageTurnContainer);
  92. } else {
  93. this.pageTurnNodes.push( this.createPageTurnNode(i) );
  94. }
  95. }
  96. }
  97. if( this.options.hasTruningBar && this.options.hasBatchTuring && ( max < pageSize )){ //showWithDisable
  98. this.createNextBatch( max );
  99. }
  100. if (this.options.hasNextPage && ( currentPage != pageSize || showWithDisable )){
  101. this.createNext();
  102. }
  103. if(this.options.hasLastPage && ( max < pageSize || showWithDisable ) ){
  104. this.createLast();
  105. }
  106. if (this.options.hasJumper) {
  107. this.createPageJumper();
  108. }
  109. },
  110. createFirst : function(){
  111. var firstPage = this.firstPage = new Element("div.firstPage", {
  112. styles: this.css.firstPage
  113. }).inject(this.node);
  114. if (this.options.text.firstPage) firstPage.set("text", this.options.text.firstPage);
  115. firstPage.addEvents({
  116. "mouseover": function (ev) {
  117. ev.target.setStyles(this.css.firstPage_over)
  118. }.bind(this),
  119. "mouseout": function (ev) {
  120. ev.target.setStyles(this.css.firstPage)
  121. }.bind(this),
  122. "click": function () {
  123. this.gotoPage(1)
  124. }.bind(this)
  125. })
  126. },
  127. createLast : function(){
  128. var lastPage = this.lastPage = new Element("div.lastPage", {
  129. styles: this.css.lastPage
  130. }).inject(this.node);
  131. if (this.options.text.lastPage) lastPage.set("text", this.options.text.lastPage);
  132. lastPage.addEvents({
  133. "mouseover": function (ev) {
  134. ev.target.setStyles(this.css.lastPage_over)
  135. }.bind(this),
  136. "mouseout": function (ev) {
  137. ev.target.setStyles(this.css.lastPage)
  138. }.bind(this),
  139. "click": function () {
  140. this.gotoPage( this.options.pageSize )
  141. }.bind(this)
  142. })
  143. },
  144. createPrev : function(){
  145. var prePage = this.prePage = new Element("div.prePage", {
  146. styles: this.css.prePage
  147. }).inject(this.node);
  148. if (this.options.text.prePage) prePage.set("text", this.options.text.prePage);
  149. prePage.addEvents({
  150. "mouseover": function (ev) {
  151. ev.target.setStyles(this.css.prePage_over)
  152. }.bind(this),
  153. "mouseout": function (ev) {
  154. ev.target.setStyles(this.css.prePage)
  155. }.bind(this),
  156. "click": function () {
  157. this.gotoPage(this.options.currentPage - 1)
  158. }.bind(this)
  159. });
  160. },
  161. createNext : function(){
  162. var nextPage = this.nextPage = new Element("div.nextPage", {
  163. styles: this.css.nextPage
  164. }).inject(this.node);
  165. if (this.options.text.nextPage) nextPage.set("text", this.options.text.nextPage);
  166. nextPage.addEvents({
  167. "mouseover": function (ev) {
  168. ev.target.setStyles(this.css.nextPage_over)
  169. }.bind(this),
  170. "mouseout": function (ev) {
  171. ev.target.setStyles(this.css.nextPage)
  172. }.bind(this),
  173. "click": function () {
  174. this.gotoPage(this.options.currentPage + 1)
  175. }.bind(this)
  176. });
  177. },
  178. createPageTurnNode: function(i){
  179. var pageTurnNode = new Element("div.pageItem", {
  180. "styles": this.css.pageItem,
  181. "text": i
  182. }).inject(this.pageTurnContainer);
  183. pageTurnNode.addEvents({
  184. "mouseover": function (ev) {
  185. ev.target.setStyles(this.css.pageItem_over)
  186. }.bind(this),
  187. "mouseout": function (ev) {
  188. ev.target.setStyles(this.css.pageItem)
  189. }.bind(this),
  190. "click": function () {
  191. this.obj.gotoPage(this.num)
  192. }.bind({obj: this, num: i})
  193. });
  194. return pageTurnNode;
  195. },
  196. createPrevBatch : function( min ){
  197. this.preBatchPage = new Element("div.prePage", {
  198. styles: this.css.preBatchPage
  199. }).inject(this.node);
  200. if (this.options.text.preBatchTuring ) this.preBatchPage.set("text", this.options.text.preBatchTuring);
  201. this.preBatchPage.addEvents({
  202. "mouseover": function (ev) {
  203. ev.target.setStyles(this.css.preBatchPage_over)
  204. }.bind(this),
  205. "mouseout": function (ev) {
  206. ev.target.setStyles(this.css.preBatchPage )
  207. }.bind(this),
  208. "click": function () {
  209. var page;
  210. if( this.options.visiblePages % 2 == 1 ){
  211. page = min - Math.ceil( this.options.visiblePages / 2 );
  212. }else{
  213. page = min - Math.ceil( this.options.visiblePages / 2 ) - 1;
  214. }
  215. if( page < 1 )page = 1;
  216. this.gotoPage( page );
  217. }.bind(this)
  218. });
  219. },
  220. createNextBatch : function( max ){
  221. this.nextBatchPage = new Element("div.prePage", {
  222. styles: this.css.nextBatchPage
  223. }).inject(this.node);
  224. if (this.options.text.nextBatchTuring ) this.nextBatchPage.set("text", this.options.text.nextBatchTuring);
  225. this.nextBatchPage.addEvents({
  226. "mouseover": function (ev) {
  227. ev.target.setStyles(this.css.nextBatchPage_over);
  228. }.bind(this),
  229. "mouseout": function (ev) {
  230. ev.target.setStyles(this.css.nextBatchPage );
  231. }.bind(this),
  232. "click": function () {
  233. var page;
  234. if( this.options.visiblePages % 2 == 1 ){
  235. page = max + Math.ceil( (this.options.visiblePages) / 2 );
  236. }else{
  237. page = max + Math.ceil( (this.options.visiblePages) / 2 ) + 1;
  238. }
  239. if( page > this.options.pageSize )page = this.options.pageSize;
  240. this.gotoPage( page );
  241. }.bind(this)
  242. });
  243. },
  244. createPageJumper : function(){
  245. var _self = this;
  246. var pageJumper = this.pageJumper = new Element("input.pageJumper", {
  247. "styles": this.css.pageJumper,
  248. "title": "输入页码,离开输入框或按回车跳转"
  249. }).inject(this.node);
  250. this.pageJumperText = new Element("div.pageText", {
  251. "styles": this.css.pageJumperText,
  252. "text": "/" + this.options.pageSize
  253. }).inject(this.node);
  254. pageJumper.addEvents({
  255. "focus": function (ev) {
  256. ev.target.setStyles(this.css.pageJumper_over)
  257. }.bind(this),
  258. "blur": function (ev) {
  259. if( this.value )_self.gotoPage(this.value);
  260. ev.target.setStyles(_self.css.pageJumper);
  261. },
  262. "keyup": function (e) {
  263. this.value = this.value.replace(/[^0-9_]/g, '');
  264. },
  265. "keydown": function (e) {
  266. if (e.code == 13 && this.value != "") {
  267. _self.gotoPage(this.value);
  268. e.stopPropagation();
  269. //e.preventDefault();
  270. }
  271. }
  272. });
  273. },
  274. gotoPage: function (num) {
  275. if( typeOf(num) === "string" )num = num.toInt();
  276. if (num < 1 || num > this.options.pageSize) return;
  277. this.fireEvent("jumpingPage", [num]);
  278. this.options.currentPage = num;
  279. this.load();
  280. },
  281. gotoItem: function (itemNum) {
  282. var pageNum = Math.ceil(itemNum / this.options.countPerPage);
  283. var index = itemNum % this.options.countPerPage;
  284. this.fireEvent("jumpingPage", [pageNum, itemNum, index]);
  285. this.options.currentPage = pageNum;
  286. this.load();
  287. }
  288. });