123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- o2.widget = o2.widget || {};
- o2.require("o2.widget.Common", null, false);
- o2.widget.Paging = new Class({
- Implements: [Options, Events],
- Extends: o2.widget.Common,
- options: {
- style : "default",
- countPerPage: 20,
- visiblePages: 10,
- currentPage: 1,
- itemSize: 0,
- pageSize: 0,
- hasChangeCount: false,
- hasFirstPage : true,
- hasLastPage : true,
- hasNextPage: true,
- hasPrevPage: true,
- hasBatchTuring : true,
- hasTruningBar: true,
- hasJumper: true,
- hiddenWithDisable: false,
- hiddenWithNoItem: true,
- text: {
- preBatchTuring : "...",
- nextBatchTuring : "...",
- prePage: "",
- nextPage: "",
- firstPage: "",
- lastPage: ""
- },
- hasInfor: false,
- inforPosition: "bottom",
- inforTextStyle: "",
- useMainColor: false
- },
- initialize: function (node, options, css) {
- this.setOptions(options || {});
- this.container = $(node);
- this.path = o2.session.path + "/widget/$Paging/";
- this.cssPath = o2.session.path + "/widget/$Paging/" + this.options.style + "/css.wcss";
- this._loadCss();
- if (css) {
- this.css = Object.clone(this.css);
- this.css = Object.merge(this.css, css);
- }
- },
- load: function () {
- this.fireEvent("queryLoad", this);
- this.options.pageSize = Math.ceil(this.options.itemSize / this.options.countPerPage);
- if ( this.options.pageSize == 0 && this.options.hiddenWithNoItem) {
- this.fireEvent("postLoad", this);
- return;
- }
- this.container.empty();
- this.createNode();
- this.fireEvent("postLoad", this);
- },
- destroy: function(){
- this.container.empty();
- },
- createNode: function() {
- var _self = this;
- this.wraper = new Element("div.pagingBarWraper", {styles: this.css.pagingBarWraper}).inject(this.container);
- this.node = new Element("div.pagingBar", {styles: this.css.pagingBar}).inject(this.wraper);
- var i, max, min;
- var showWithDisable = !this.options.hiddenWithDisable;
- var pageSize = this.options.pageSize;
- var currentPage = this.options.currentPage;
- var visiblePages = this.options.visiblePages;
- var halfCount = Math.floor(visiblePages / 2);
- if (pageSize <= visiblePages) {
- min = 1;
- max = pageSize;
- } else if (currentPage + halfCount > pageSize) {
- min = pageSize - visiblePages;
- max = pageSize;
- } else if (currentPage - halfCount < 1) {
- min = 1;
- max = visiblePages;
- } else {
- min = currentPage - halfCount;
- max = currentPage + halfCount;
- }
- if(this.options.hasInfor && this.options.inforTextStyle && this.options.inforPosition==="top" ){
- this.createInfor()
- }
- if( this.options.hasFirstPage && (min > 1 || showWithDisable) ){
- this.createFirst();
- }
- if (this.options.hasPrevPage && ( currentPage != 1 || showWithDisable ) ){
- this.createPrev();
- }
- if( this.options.hasTruningBar && this.options.hasBatchTuring && ( min > 1 ) ){ //showWithDisable
- this.createPrevBatch( min );
- }
- if( this.options.hasTruningBar ){
- this.pageTurnContainer = new Element("div", {
- styles : this.css.pageTurnContainer
- }).inject( this.node );
- this.pageTurnNodes = [];
- for (i = min; i <= max; i++) {
- if (currentPage == i) {
- this.currentPage = new Element("div.currentPage", {
- "styles": this.css.currentPage,
- "text" : i
- }).inject(this.pageTurnContainer);
- if(this.options.useMainColor)this.currentPage.addClass("mainColor_bg");
- } else {
- this.pageTurnNodes.push( this.createPageTurnNode(i) );
- }
- }
- }
- if( this.options.hasTruningBar && this.options.hasBatchTuring && ( max < pageSize )){ //showWithDisable
- this.createNextBatch( max );
- }
- if (this.options.hasNextPage && ( currentPage != pageSize || showWithDisable )){
- this.createNext();
- }
- if(this.options.hasLastPage && ( max < pageSize || showWithDisable ) ){
- this.createLast();
- }
- if (this.options.hasJumper) {
- this.createPageJumper();
- }
- if( this.options.hasChangeCount ){
- this.createCountChanger();
- }
- if(this.options.hasInfor && this.options.inforTextStyle && this.options.inforPosition==="bottom" ){
- this.createInfor()
- }
- },
- createFirst : function(){
- var firstPage = this.firstPage = new Element("div.firstPage", {
- styles: this.css.firstPage
- }).inject(this.node);
- if (this.options.text.firstPage) firstPage.set("text", this.options.text.firstPage);
- firstPage.addEvents({
- "mouseover": function (ev) {
- ev.target.setStyles(this.css.firstPage_over);
- if( this.options.useMainColor )ev.target.addClass("mainColor_bg");
- }.bind(this),
- "mouseout": function (ev) {
- ev.target.setStyles(this.css.firstPage);
- if( this.options.useMainColor )ev.target.removeClass("mainColor_bg");
- }.bind(this),
- "click": function () {
- this.gotoPage(1)
- }.bind(this)
- })
- },
- createLast : function(){
- var lastPage = this.lastPage = new Element("div.lastPage", {
- styles: this.css.lastPage
- }).inject(this.node);
- if (this.options.text.lastPage) lastPage.set("text", this.options.text.lastPage);
- lastPage.addEvents({
- "mouseover": function (ev) {
- ev.target.setStyles(this.css.lastPage_over);
- if( this.options.useMainColor )ev.target.addClass("mainColor_bg");
- }.bind(this),
- "mouseout": function (ev) {
- ev.target.setStyles(this.css.lastPage);
- if( this.options.useMainColor )ev.target.removeClass("mainColor_bg");
- }.bind(this),
- "click": function () {
- this.gotoPage( this.options.pageSize )
- }.bind(this)
- })
- },
- createPrev : function(){
- var prePage = this.prePage = new Element("div.prePage", {
- styles: this.css.prePage
- }).inject(this.node);
- if (this.options.text.prePage) prePage.set("text", this.options.text.prePage);
- prePage.addEvents({
- "mouseover": function (ev) {
- ev.target.setStyles(this.css.prePage_over);
- if( this.options.useMainColor )ev.target.addClass("mainColor_bg_opacity");
- }.bind(this),
- "mouseout": function (ev) {
- ev.target.setStyles(this.css.prePage);
- if( this.options.useMainColor )ev.target.removeClass("mainColor_bg_opacity");
- }.bind(this),
- "click": function () {
- this.gotoPage(this.options.currentPage - 1)
- }.bind(this)
- });
- },
- createNext : function(){
- var nextPage = this.nextPage = new Element("div.nextPage", {
- styles: this.css.nextPage
- }).inject(this.node);
- if (this.options.text.nextPage) nextPage.set("text", this.options.text.nextPage);
- nextPage.addEvents({
- "mouseover": function (ev) {
- ev.target.setStyles(this.css.nextPage_over);
- if( this.options.useMainColor )ev.target.addClass("mainColor_bg_opacity");
- }.bind(this),
- "mouseout": function (ev) {
- ev.target.setStyles(this.css.nextPage);
- if( this.options.useMainColor )ev.target.removeClass("mainColor_bg_opacity");
- }.bind(this),
- "click": function () {
- this.gotoPage(this.options.currentPage + 1)
- }.bind(this)
- });
- },
- createPageTurnNode: function(i){
- var pageTurnNode = new Element("div.pageItem", {
- "styles": this.css.pageItem,
- "text": i
- }).inject(this.pageTurnContainer);
- pageTurnNode.addEvents({
- "mouseover": function (ev) {
- ev.target.setStyles(this.css.pageItem_over);
- if( this.options.useMainColor )ev.target.addClass("mainColor_bg_opacity");
- }.bind(this),
- "mouseout": function (ev) {
- ev.target.setStyles(this.css.pageItem);
- if( this.options.useMainColor )ev.target.removeClass("mainColor_bg_opacity");
- }.bind(this),
- "click": function () {
- this.obj.gotoPage(this.num)
- }.bind({obj: this, num: i})
- });
- return pageTurnNode;
- },
- createPrevBatch : function( min ){
- this.preBatchPage = new Element("div.prePage", {
- styles: this.css.preBatchPage
- }).inject(this.node);
- if (this.options.text.preBatchTuring ) this.preBatchPage.set("text", this.options.text.preBatchTuring);
- this.preBatchPage.addEvents({
- "mouseover": function (ev) {
- ev.target.setStyles(this.css.preBatchPage_over);
- if( this.options.useMainColor )ev.target.addClass("mainColor_bg_opacity");
- }.bind(this),
- "mouseout": function (ev) {
- ev.target.setStyles(this.css.preBatchPage );
- if( this.options.useMainColor )ev.target.removeClass("mainColor_bg_opacity");
- }.bind(this),
- "click": function () {
- var page;
- if( this.options.visiblePages % 2 == 1 ){
- page = min - Math.ceil( this.options.visiblePages / 2 );
- }else{
- page = min - Math.ceil( this.options.visiblePages / 2 ) - 1;
- }
- if( page < 1 )page = 1;
- this.gotoPage( page );
- }.bind(this)
- });
- },
- createNextBatch : function( max ){
- this.nextBatchPage = new Element("div.prePage", {
- styles: this.css.nextBatchPage
- }).inject(this.node);
- if (this.options.text.nextBatchTuring ) this.nextBatchPage.set("text", this.options.text.nextBatchTuring);
- this.nextBatchPage.addEvents({
- "mouseover": function (ev) {
- ev.target.setStyles(this.css.nextBatchPage_over);
- if( this.options.useMainColor )ev.target.addClass("mainColor_bg_opacity");
- }.bind(this),
- "mouseout": function (ev) {
- ev.target.setStyles(this.css.nextBatchPage );
- if( this.options.useMainColor )ev.target.removeClass("mainColor_bg_opacity");
- }.bind(this),
- "click": function () {
- var page;
- if( this.options.visiblePages % 2 == 1 ){
- page = max + Math.ceil( (this.options.visiblePages) / 2 );
- }else{
- page = max + Math.ceil( (this.options.visiblePages) / 2 ) + 1;
- }
- if( page > this.options.pageSize )page = this.options.pageSize;
- this.gotoPage( page );
- }.bind(this)
- });
- },
- createCountChanger: function(){
- var _self = this;
- var countChanger = this.countChanger = new Element("input.countChanger", {
- "styles": this.css.pageJumper,
- "title": o2.LP.widget.countChangerTitle || "每页条数",
- }).inject(this.node);
- this.countChangerText = new Element("div.countChangerText", {
- "styles": this.css.pageJumperText,
- "text": o2.LP.widget.countChangerText || "条/页",
- "value": this.options.countPerPage
- }).inject(this.node);
- countChanger.addEvents({
- "focus": function (ev) {
- ev.target.setStyles(this.css.pageJumper_over);
- if( _self.options.useMainColor )ev.target.addClass("mainColor_border");
- }.bind(this),
- "blur": function (ev) {
- var value = this.value;
- _self.changeCountPerPage(value);
- ev.target.setStyles(_self.css.pageJumper);
- if( _self.options.useMainColor )ev.target.removeClass("mainColor_border");
- },
- "keyup": function (e) {
- this.value = this.value.replace(/[^0-9_]/g, '');
- },
- "keydown": function (e) {
- if (e.code == 13 && this.value != "") {
- var value = this.value;
- _self.changeCountPerPage(value);
- e.stopPropagation();
- //e.preventDefault();
- }
- }
- });
- },
- createPageJumper : function(){
- var _self = this;
- var pageJumper = this.pageJumper = new Element("input.pageJumper", {
- "styles": this.css.pageJumper,
- "title": o2.LP.widget.pageJumperTitle,
- }).inject(this.node);
- this.pageJumperText = new Element("div.pageText", {
- "styles": this.css.pageJumperText,
- "text": "/" + this.options.pageSize
- }).inject(this.node);
- pageJumper.addEvents({
- "focus": function (ev) {
- ev.target.setStyles(this.css.pageJumper_over);
- if( _self.options.useMainColor )ev.target.addClass("mainColor_border");
- }.bind(this),
- "blur": function (ev) {
- var value = this.value;
- _self.pageJumper.set("value","");
- if( value )_self.gotoPage(value);
- ev.target.setStyles(_self.css.pageJumper);
- if( _self.options.useMainColor )ev.target.removeClass("mainColor_border");
- },
- "keyup": function (e) {
- this.value = this.value.replace(/[^0-9_]/g, '');
- },
- "keydown": function (e) {
- if (e.code == 13 && this.value != "") {
- var value = this.value;
- _self.pageJumper.set("value","");
- _self.gotoPage(value);
- e.stopPropagation();
- //e.preventDefault();
- }
- }
- });
- },
- changeCountPerPage: function (countPerPage){
- var num = countPerPage;
- if( typeOf(num) === "string" )num = num.toInt();
- this.options.currentPage = 1;
- this.options.countPerPage = num;
- this.load();
- },
- gotoPage: function (num) {
- if( typeOf(num) === "string" )num = num.toInt();
- if (num < 1 || num > this.options.pageSize) return;
- this.fireEvent("jumpingPage", [num]);
- this.options.currentPage = num;
- this.load();
- },
- gotoItem: function (itemNum) {
- var pageNum = Math.ceil(itemNum / this.options.countPerPage);
- var index = itemNum % this.options.countPerPage;
- this.fireEvent("jumpingPage", [pageNum, itemNum, index]);
- this.options.currentPage = pageNum;
- this.load();
- },
- createInfor: function () {
- var html = this.options.inforTextStyle.replace(/\{page\}/g, ""+this.options.pageSize )
- .replace(/\{count\}/g, ""+this.options.itemSize )
- .replace(/\{countPerPage\}/g, ""+this.options.countPerPage )
- .replace(/\{currentPage\}/g, ""+this.options.currentPage );
- this.inforNode = new Element("div.inforNode", {
- styles: this.css.inforNode,
- html:html
- }).inject(this.node);
- }
- });
|