Paging.js 11 KB

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