Paging.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. hasChangeCount: false,
  14. hasFirstPage : true,
  15. hasLastPage : true,
  16. hasNextPage: true,
  17. hasPrevPage: true,
  18. hasBatchTuring : true,
  19. hasTruningBar: true,
  20. hasJumper: true,
  21. hiddenWithDisable: false,
  22. hiddenWithNoItem: true,
  23. text: {
  24. preBatchTuring : "...",
  25. nextBatchTuring : "...",
  26. prePage: "",
  27. nextPage: "",
  28. firstPage: "",
  29. lastPage: ""
  30. },
  31. hasInfor: false,
  32. inforPosition: "bottom",
  33. inforTextStyle: "",
  34. useMainColor: false
  35. },
  36. initialize: function (node, options, css) {
  37. this.setOptions(options || {});
  38. this.container = $(node);
  39. this.path = o2.session.path + "/widget/$Paging/";
  40. this.cssPath = o2.session.path + "/widget/$Paging/" + this.options.style + "/css.wcss";
  41. this._loadCss();
  42. if (css) {
  43. this.css = Object.clone(this.css);
  44. this.css = Object.merge(this.css, css);
  45. }
  46. },
  47. load: function () {
  48. this.fireEvent("queryLoad", this);
  49. this.options.pageSize = Math.ceil(this.options.itemSize / this.options.countPerPage);
  50. if ( this.options.pageSize == 0 && this.options.hiddenWithNoItem) {
  51. this.fireEvent("postLoad", this);
  52. return;
  53. }
  54. this.container.empty();
  55. this.createNode();
  56. this.fireEvent("postLoad", this);
  57. },
  58. destroy: function(){
  59. this.container.empty();
  60. },
  61. createNode: function() {
  62. var _self = this;
  63. this.wraper = new Element("div.pagingBarWraper", {styles: this.css.pagingBarWraper}).inject(this.container);
  64. this.node = new Element("div.pagingBar", {styles: this.css.pagingBar}).inject(this.wraper);
  65. var i, max, min;
  66. var showWithDisable = !this.options.hiddenWithDisable;
  67. var pageSize = this.options.pageSize;
  68. var currentPage = this.options.currentPage;
  69. var visiblePages = this.options.visiblePages;
  70. var halfCount = Math.floor(visiblePages / 2);
  71. if (pageSize <= visiblePages) {
  72. min = 1;
  73. max = pageSize;
  74. } else if (currentPage + halfCount > pageSize) {
  75. min = pageSize - visiblePages;
  76. max = pageSize;
  77. } else if (currentPage - halfCount < 1) {
  78. min = 1;
  79. max = visiblePages;
  80. } else {
  81. min = currentPage - halfCount;
  82. max = currentPage + halfCount;
  83. }
  84. if(this.options.hasInfor && this.options.inforTextStyle && this.options.inforPosition==="top" ){
  85. this.createInfor()
  86. }
  87. if( this.options.hasFirstPage && (min > 1 || showWithDisable) ){
  88. this.createFirst();
  89. }
  90. if (this.options.hasPrevPage && ( currentPage != 1 || showWithDisable ) ){
  91. this.createPrev();
  92. }
  93. if( this.options.hasTruningBar && this.options.hasBatchTuring && ( min > 1 ) ){ //showWithDisable
  94. this.createPrevBatch( min );
  95. }
  96. if( this.options.hasTruningBar ){
  97. this.pageTurnContainer = new Element("div", {
  98. styles : this.css.pageTurnContainer
  99. }).inject( this.node );
  100. this.pageTurnNodes = [];
  101. for (i = min; i <= max; i++) {
  102. if (currentPage == i) {
  103. this.currentPage = new Element("div.currentPage", {
  104. "styles": this.css.currentPage,
  105. "text" : i
  106. }).inject(this.pageTurnContainer);
  107. if(this.options.useMainColor)this.currentPage.addClass("mainColor_bg");
  108. } else {
  109. this.pageTurnNodes.push( this.createPageTurnNode(i) );
  110. }
  111. }
  112. }
  113. if( this.options.hasTruningBar && this.options.hasBatchTuring && ( max < pageSize )){ //showWithDisable
  114. this.createNextBatch( max );
  115. }
  116. if (this.options.hasNextPage && ( currentPage != pageSize || showWithDisable )){
  117. this.createNext();
  118. }
  119. if(this.options.hasLastPage && ( max < pageSize || showWithDisable ) ){
  120. this.createLast();
  121. }
  122. if (this.options.hasJumper) {
  123. this.createPageJumper();
  124. }
  125. if( this.options.hasChangeCount ){
  126. this.createCountChanger();
  127. }
  128. if(this.options.hasInfor && this.options.inforTextStyle && this.options.inforPosition==="bottom" ){
  129. this.createInfor()
  130. }
  131. },
  132. createFirst : function(){
  133. var firstPage = this.firstPage = new Element("div.firstPage", {
  134. styles: this.css.firstPage
  135. }).inject(this.node);
  136. if (this.options.text.firstPage) firstPage.set("text", this.options.text.firstPage);
  137. firstPage.addEvents({
  138. "mouseover": function (ev) {
  139. ev.target.setStyles(this.css.firstPage_over);
  140. if( this.options.useMainColor )ev.target.addClass("mainColor_bg");
  141. }.bind(this),
  142. "mouseout": function (ev) {
  143. ev.target.setStyles(this.css.firstPage);
  144. if( this.options.useMainColor )ev.target.removeClass("mainColor_bg");
  145. }.bind(this),
  146. "click": function () {
  147. this.gotoPage(1)
  148. }.bind(this)
  149. })
  150. },
  151. createLast : function(){
  152. var lastPage = this.lastPage = new Element("div.lastPage", {
  153. styles: this.css.lastPage
  154. }).inject(this.node);
  155. if (this.options.text.lastPage) lastPage.set("text", this.options.text.lastPage);
  156. lastPage.addEvents({
  157. "mouseover": function (ev) {
  158. ev.target.setStyles(this.css.lastPage_over);
  159. if( this.options.useMainColor )ev.target.addClass("mainColor_bg");
  160. }.bind(this),
  161. "mouseout": function (ev) {
  162. ev.target.setStyles(this.css.lastPage);
  163. if( this.options.useMainColor )ev.target.removeClass("mainColor_bg");
  164. }.bind(this),
  165. "click": function () {
  166. this.gotoPage( this.options.pageSize )
  167. }.bind(this)
  168. })
  169. },
  170. createPrev : function(){
  171. var prePage = this.prePage = new Element("div.prePage", {
  172. styles: this.css.prePage
  173. }).inject(this.node);
  174. if (this.options.text.prePage) prePage.set("text", this.options.text.prePage);
  175. prePage.addEvents({
  176. "mouseover": function (ev) {
  177. ev.target.setStyles(this.css.prePage_over);
  178. if( this.options.useMainColor )ev.target.addClass("mainColor_bg_opacity");
  179. }.bind(this),
  180. "mouseout": function (ev) {
  181. ev.target.setStyles(this.css.prePage);
  182. if( this.options.useMainColor )ev.target.removeClass("mainColor_bg_opacity");
  183. }.bind(this),
  184. "click": function () {
  185. this.gotoPage(this.options.currentPage - 1)
  186. }.bind(this)
  187. });
  188. },
  189. createNext : function(){
  190. var nextPage = this.nextPage = new Element("div.nextPage", {
  191. styles: this.css.nextPage
  192. }).inject(this.node);
  193. if (this.options.text.nextPage) nextPage.set("text", this.options.text.nextPage);
  194. nextPage.addEvents({
  195. "mouseover": function (ev) {
  196. ev.target.setStyles(this.css.nextPage_over);
  197. if( this.options.useMainColor )ev.target.addClass("mainColor_bg_opacity");
  198. }.bind(this),
  199. "mouseout": function (ev) {
  200. ev.target.setStyles(this.css.nextPage);
  201. if( this.options.useMainColor )ev.target.removeClass("mainColor_bg_opacity");
  202. }.bind(this),
  203. "click": function () {
  204. this.gotoPage(this.options.currentPage + 1)
  205. }.bind(this)
  206. });
  207. },
  208. createPageTurnNode: function(i){
  209. var pageTurnNode = new Element("div.pageItem", {
  210. "styles": this.css.pageItem,
  211. "text": i
  212. }).inject(this.pageTurnContainer);
  213. pageTurnNode.addEvents({
  214. "mouseover": function (ev) {
  215. ev.target.setStyles(this.css.pageItem_over);
  216. if( this.options.useMainColor )ev.target.addClass("mainColor_bg_opacity");
  217. }.bind(this),
  218. "mouseout": function (ev) {
  219. ev.target.setStyles(this.css.pageItem);
  220. if( this.options.useMainColor )ev.target.removeClass("mainColor_bg_opacity");
  221. }.bind(this),
  222. "click": function () {
  223. this.obj.gotoPage(this.num)
  224. }.bind({obj: this, num: i})
  225. });
  226. return pageTurnNode;
  227. },
  228. createPrevBatch : function( min ){
  229. this.preBatchPage = new Element("div.prePage", {
  230. styles: this.css.preBatchPage
  231. }).inject(this.node);
  232. if (this.options.text.preBatchTuring ) this.preBatchPage.set("text", this.options.text.preBatchTuring);
  233. this.preBatchPage.addEvents({
  234. "mouseover": function (ev) {
  235. ev.target.setStyles(this.css.preBatchPage_over);
  236. if( this.options.useMainColor )ev.target.addClass("mainColor_bg_opacity");
  237. }.bind(this),
  238. "mouseout": function (ev) {
  239. ev.target.setStyles(this.css.preBatchPage );
  240. if( this.options.useMainColor )ev.target.removeClass("mainColor_bg_opacity");
  241. }.bind(this),
  242. "click": function () {
  243. var page;
  244. if( this.options.visiblePages % 2 == 1 ){
  245. page = min - Math.ceil( this.options.visiblePages / 2 );
  246. }else{
  247. page = min - Math.ceil( this.options.visiblePages / 2 ) - 1;
  248. }
  249. if( page < 1 )page = 1;
  250. this.gotoPage( page );
  251. }.bind(this)
  252. });
  253. },
  254. createNextBatch : function( max ){
  255. this.nextBatchPage = new Element("div.prePage", {
  256. styles: this.css.nextBatchPage
  257. }).inject(this.node);
  258. if (this.options.text.nextBatchTuring ) this.nextBatchPage.set("text", this.options.text.nextBatchTuring);
  259. this.nextBatchPage.addEvents({
  260. "mouseover": function (ev) {
  261. ev.target.setStyles(this.css.nextBatchPage_over);
  262. if( this.options.useMainColor )ev.target.addClass("mainColor_bg_opacity");
  263. }.bind(this),
  264. "mouseout": function (ev) {
  265. ev.target.setStyles(this.css.nextBatchPage );
  266. if( this.options.useMainColor )ev.target.removeClass("mainColor_bg_opacity");
  267. }.bind(this),
  268. "click": function () {
  269. var page;
  270. if( this.options.visiblePages % 2 == 1 ){
  271. page = max + Math.ceil( (this.options.visiblePages) / 2 );
  272. }else{
  273. page = max + Math.ceil( (this.options.visiblePages) / 2 ) + 1;
  274. }
  275. if( page > this.options.pageSize )page = this.options.pageSize;
  276. this.gotoPage( page );
  277. }.bind(this)
  278. });
  279. },
  280. createCountChanger: function(){
  281. var _self = this;
  282. var countChanger = this.countChanger = new Element("input.countChanger", {
  283. "styles": this.css.pageJumper,
  284. "title": o2.LP.widget.countChangerTitle || "每页条数",
  285. }).inject(this.node);
  286. this.countChangerText = new Element("div.countChangerText", {
  287. "styles": this.css.pageJumperText,
  288. "text": o2.LP.widget.countChangerText || "条/页",
  289. "value": this.options.countPerPage
  290. }).inject(this.node);
  291. countChanger.addEvents({
  292. "focus": function (ev) {
  293. ev.target.setStyles(this.css.pageJumper_over);
  294. if( _self.options.useMainColor )ev.target.addClass("mainColor_border");
  295. }.bind(this),
  296. "blur": function (ev) {
  297. var value = this.value;
  298. _self.changeCountPerPage(value);
  299. ev.target.setStyles(_self.css.pageJumper);
  300. if( _self.options.useMainColor )ev.target.removeClass("mainColor_border");
  301. },
  302. "keyup": function (e) {
  303. this.value = this.value.replace(/[^0-9_]/g, '');
  304. },
  305. "keydown": function (e) {
  306. if (e.code == 13 && this.value != "") {
  307. var value = this.value;
  308. _self.changeCountPerPage(value);
  309. e.stopPropagation();
  310. //e.preventDefault();
  311. }
  312. }
  313. });
  314. },
  315. createPageJumper : function(){
  316. var _self = this;
  317. var pageJumper = this.pageJumper = new Element("input.pageJumper", {
  318. "styles": this.css.pageJumper,
  319. "title": o2.LP.widget.pageJumperTitle,
  320. }).inject(this.node);
  321. this.pageJumperText = new Element("div.pageText", {
  322. "styles": this.css.pageJumperText,
  323. "text": "/" + this.options.pageSize
  324. }).inject(this.node);
  325. pageJumper.addEvents({
  326. "focus": function (ev) {
  327. ev.target.setStyles(this.css.pageJumper_over);
  328. if( _self.options.useMainColor )ev.target.addClass("mainColor_border");
  329. }.bind(this),
  330. "blur": function (ev) {
  331. var value = this.value;
  332. _self.pageJumper.set("value","");
  333. if( value )_self.gotoPage(value);
  334. ev.target.setStyles(_self.css.pageJumper);
  335. if( _self.options.useMainColor )ev.target.removeClass("mainColor_border");
  336. },
  337. "keyup": function (e) {
  338. this.value = this.value.replace(/[^0-9_]/g, '');
  339. },
  340. "keydown": function (e) {
  341. if (e.code == 13 && this.value != "") {
  342. var value = this.value;
  343. _self.pageJumper.set("value","");
  344. _self.gotoPage(value);
  345. e.stopPropagation();
  346. //e.preventDefault();
  347. }
  348. }
  349. });
  350. },
  351. changeCountPerPage: function (countPerPage){
  352. var num = countPerPage;
  353. if( typeOf(num) === "string" )num = num.toInt();
  354. this.options.currentPage = 1;
  355. this.options.countPerPage = num;
  356. this.load();
  357. },
  358. gotoPage: function (num) {
  359. if( typeOf(num) === "string" )num = num.toInt();
  360. if (num < 1 || num > this.options.pageSize) return;
  361. this.fireEvent("jumpingPage", [num]);
  362. this.options.currentPage = num;
  363. this.load();
  364. },
  365. gotoItem: function (itemNum) {
  366. var pageNum = Math.ceil(itemNum / this.options.countPerPage);
  367. var index = itemNum % this.options.countPerPage;
  368. this.fireEvent("jumpingPage", [pageNum, itemNum, index]);
  369. this.options.currentPage = pageNum;
  370. this.load();
  371. },
  372. createInfor: function () {
  373. var html = this.options.inforTextStyle.replace(/\{page\}/g, ""+this.options.pageSize )
  374. .replace(/\{count\}/g, ""+this.options.itemSize )
  375. .replace(/\{countPerPage\}/g, ""+this.options.countPerPage )
  376. .replace(/\{currentPage\}/g, ""+this.options.currentPage );
  377. this.inforNode = new Element("div.inforNode", {
  378. styles: this.css.inforNode,
  379. html:html
  380. }).inject(this.node);
  381. }
  382. });