zhongrj
2023-06-27 d74b495736f0c04522ec54274b70fed0ded2878d
public/map/controls/page/PageControl.js
New file
@@ -0,0 +1,438 @@
/**
 * 加载专题图widget
 * @author Wenyb
 * @date 2015/6/30
 */
define([
  "dojo",
  "dojo/_base/declare",
  "dijit/_WidgetBase",
  "dijit/_TemplatedMixin",
  "dojo/text!controls/page/template.html",
  "dojo/_base/lang",
  "dojo/query",
  "dojo/dom-construct",
  "dojo/dom-style",
  "dojo/on",
  "dojo/dom-class",
  "dojo/window",
  "dojo/_base/array",
  "dojo/dom-attr",
  "dojo/aspect",
  "dojo/domReady!"
], function (
  dojo,
  declare,
  _WidgetBase,
  _TemplatedMixin,
  template,
  lang,
  query,
  domConstruct,
  domStyle,
  on,
  domClass,
  win,
  array,
  domAttr,
  aspect
  ){
   var Widget = declare([_WidgetBase,_TemplatedMixin],
      {
         templateString: template,
         /**
          * 一共的页数
          */
         totalCount:10,
         /**
          * 一次显示的页码数
          */
         pageCount:5,
         _refNode:null,
         pageSpanlist:[],
         pageSpanEventHandlers:[],
         currentPage:1,
         constructor:function(options,srcNodeRef)
         {
            //this._refNode=refNode;
            var csspath = require.toUrl("controls/page/PageControl.css");
            lang.mixin(this, options);
            dojo.create("link",
               {href:csspath, type:'text/css', rel:'stylesheet'},
               document.getElementsByTagName('head')[0]);
         },
         postCreate:function()
         {
            //this.placeAt(this._refNode);
            this.createPage();
         },
         createPage:function(option)
         {
            if(option !== undefined && option != null)
            {
               this.totalCount = option.totalCount;
               this.pageCount=option.pageCount;
            }
            domConstruct.empty(this.pagelist);
            if(this.totalCount < 1)
            {
               this.switchPreButton(false);
               this.switchNextButton(false);
               return;
            }
            this.pageSpanlist=[];
            this.pageSpanlist.push(this.createPageSpan("1",true));
            var tmpTotal;
            if(this.totalCount<this.pageCount)
            {
               tmpTotal=this.totalCount+1;
            }
            else
            {
               tmpTotal=this.pageCount+1;
            }
            for(var i=2;i<tmpTotal;i++)
            {
               this.pageSpanlist.push(this.createPageSpan(i.toString(),false));
            }
            this.currentPage=1;
            this.switchPreButton(false);
            if(this.totalCount==1)
            {
               this.switchNextButton(false);
            }
            else
            {
               this.switchNextButton(true);
            }
         },
         createPageSpan:function(page,selected)
         {
            var pageDom = domConstruct.create("li",{innerHTML:page},this.pagelist);
            domAttr.set(pageDom,"page",page);
            if(selected)
            {
               domClass.add(pageDom,"pagecontrol-selectpage");
            }
            this.own(on(pageDom,"click",lang.hitch(this,this._onPageClick)));
            //this.pageSpanEventHandlers.push(on(pageDom,"click",lang.hitch(this,this._onPageClick)));
            return pageDom;
         },
         _onPageClick:function(evt)
         {
            if(evt.target.innerHTML == this.currentPage.toString())
            {
               return;
            }
            this.currentPage = parseInt(evt.target.innerHTML);
            this.updateControl(this.currentPage);
         },
         _onNextClick:function(evt)
         {
            if(domAttr.get(this.nextbutton,"enable") == "false")
            {
               return;
            }
            this.currentPage++;
            this.updateControl(this.currentPage);
         },
         _onPreClick:function(evt)
         {
            if(domAttr.get(this.prebutton,"enable") == "false")
            {
               return;
            }
            this.currentPage--;
            this.updateControl(this.currentPage);
         },
         /**
          * 让上一页按钮可用
          */
         switchPreButton:function(flag)
         {
            if(flag)
            {
               domClass.remove(this.prebutton,"pagecontrol-pre-disable");
               domAttr.set(this.prebutton, "enable","true");
            }
            else
            {
               domClass.add(this.prebutton,"pagecontrol-pre-disable");
               domAttr.set(this.prebutton, "enable","false");
            }
         },
         /**
          * 让下一页按钮可用
          */
         switchNextButton:function(flag)
         {
            if(flag)
            {
               domClass.remove(this.nextbutton,"pagecontrol-next-disable");
               domAttr.set(this.nextbutton, "enable","true");
            }
            else
            {
               domClass.add(this.nextbutton,"pagecontrol-next-disable");
               domAttr.set(this.nextbutton, "enable","false");
            }
         },
         updateControl:function(nPage)
         {
            //首先移除所有页码控件
            domConstruct.empty(this.pagelist);
//            for(var i=0;i<this.pageSpanEventHandlers.length;i++)
//            {
//               this.pageSpanEventHandlers[i].remove();
//            }
//            this.pageSpanEventHandlers=[];
            //记录是否点击的是第一个或者最后一页
            var strFlag = "";
            var i=0;
            //var ctl:LinkButton = null;
            this.currentPage = nPage;
            //需要显示上一页
            if(this.currentPage>1)
            {
               this.switchPreButton(true);
            }
            //如果点击的是第一项,那么把点击的按钮移到中间
            if(domAttr.get(this.pageSpanlist[this.pageSpanlist.length-1],"page") == nPage.toString())
            {
               strFlag = "last";
            }
            //如果点击的是最后一项,那么把点击的按钮移到中间
            else if(domAttr.get(this.pageSpanlist[0],"page") == nPage.toString())
            {
               strFlag = "first";
            }
            switch(strFlag)
            {
               case "last":
                  var thisPageCount = -1;
                  if(this.pageCount>this.totalCount)
                     thisPageCount = this.totalCount;
                  else
                     thisPageCount = this.pageCount;
                  var step = parseInt((thisPageCount-1)/2);
                  this.pageSpanlist = [];
                  //当前按钮可以放到中间
                  if(step<(this.totalCount-this.currentPage))
                  {
                     for(i=step;i>=0;i--)
                     {
                        if(i == 0)
                        {
                           this.pageSpanlist.push(this.createPageSpan((this.currentPage - i).toString(),true));
                        }
                        else
                        {
                           this.pageSpanlist.push(this.createPageSpan((this.currentPage - i).toString(),false));
                        }
                     }
                     //如果页面数是偶数,则需要再加1
                     if(this.pageCount%2 == 0)
                     {
                        step++;
                     }
                     for(i=1;i<=step;i++)
                     {
                        this.pageSpanlist.push(this.createPageSpan((this.currentPage + i).toString(),false));
                     }
                  }
                  //当前按钮不可放在中间
                  else
                  {
                     for(i = this.totalCount-thisPageCount+1;i<=this.totalCount;i++)
                     {
                        if(i == this.currentPage)
                        {
                           this.pageSpanlist.push(this.createPageSpan(i.toString(),true));
                        }
                        else
                        {
                           this.pageSpanlist.push(this.createPageSpan(i.toString(),false));
                        }
                     }
                  }
                  break;
               case "first":
                  thisPageCount= -1;
                  if(this.pageCount>this.totalCount)
                     thisPageCount = this.totalCount;
                  else
                     thisPageCount = this.pageCount;
                  step = parseInt((thisPageCount-1)/2);
                  this.pageSpanlist = [];
                  //当前按钮可以放到中间
                  if(step<this.currentPage)
                  {
                     for(i=step;i>=0;i--)
                     {
                        if(i == 0)
                        {
                           this.pageSpanlist.push(this.createPageSpan((this.currentPage - i).toString(),true));
                        }
                        else
                        {
                           this.pageSpanlist.push(this.createPageSpan((this.currentPage - i).toString(),false));
                        }
                     }
                     //如果页面数是偶数,则需要再加1
                     if(thisPageCount%2 == 0)
                     {
                        step++;
                     }
                     for(i=1;i<=step;i++)
                     {
                        this.pageSpanlist.push(this.createPageSpan((this.currentPage + i).toString(),false));
                     }
                  }
                     //当前按钮不可放在中间
                  else
                  {
                     for(i = 1;i<=thisPageCount;i++)
                     {
                        if(i == this.currentPage)
                        {
                           this.pageSpanlist.push(this.createPageSpan(i.toString(),true));
                        }
                        else
                        {
                           this.pageSpanlist.push(this.createPageSpan(i.toString(),false));
                        }
                     }
                  }
                  break;
               default:
                  //array.forEach(this.pageSpanlist,this.updatePageSpan,this);
                  break;
            }
            for(i=0;i< this.pageSpanlist.length;i++)
            {
               var pagevalue = domAttr.get(this.pageSpanlist[i],"page");
               this.pageSpanlist[i].innerHTML=pagevalue;
               domConstruct.place(this.pageSpanlist[i],this.pagelist);
            }
            array.forEach(this.pageSpanlist,this.updatePageSpan,this);
            //需要显示“下一页”
            if(this.currentPage<this.totalCount)
            {
               this.switchNextButton(true)
            }
            else
            {
               this.switchNextButton(false)
            }
            if(this.currentPage == 1)
            {
               this.switchPreButton(false);
            }
            else
            {
               this.switchPreButton(true);
            }
            //触发事件
            this.onpagechange();
//            var e:PageChangeEvent = new PageChangeEvent();
//            e.currentPage = this._currentPage;
//            this.dispatchEvent(e);
         },
         onpagechange:function()
         {
            return this.currentPage;
         },
         updatePageSpan:function(span)
         {
            if(span.innerHTML == this.currentPage.toString())
            {
               domClass.add(span,"pagecontrol-selectpage");
            }
            else
            {
               if(domClass.contains(span,"pagecontrol-selectpage"))
               {
                  domClass.remove(span,"pagecontrol-selectpage");
               }
            }
         },
         _onMouseMove:function(evt)
         {
             var newtop = evt.clientY-this._refNode.offsetTop-this._ystart;
              var newleft = evt.clientX-this._refNode.offsetLeft-this._xstart;
              domStyle.set(this.domNode,"top",newtop.toString()+"px");
              domStyle.set(this.domNode,"left",newleft.toString()+"px");
         },
         _onMouseUp:function(evt)
         {
            if(this._movehandler != null){
               this._movehandler.remove();
               this._uphandler.remove();
               this._movehandler = null;
                domStyle.set(this.controlbar,"cursor","default");
             }
         },
         _setModalAttr:function(modal)
         {
            if(modal !== undefined && modal !== null)
            {
               this.modal = modal;
            }
            if(this.modal === true)
            {
               if(this._modalDiv == null){
                  this._modalDiv = domConstruct.create("div");
                  domClass.add(this._modalDiv,"custom-popupwindow-modal");
               }
               var body = query("body")[0];
               domConstruct.place(this._modalDiv,body,"last");
            }
         },
         _getModalAttr:function()
         {
            return this.modal;
         },
         _onCloseButtonClick:function(evt)
         {
            evt.stopPropagation();
            this.close();
         },
         _onCloseButtonMouseDown:function(evt)
         {
            evt.stopPropagation();
         },
         close:function()
         {
            this._refNode.removeChild(this.domNode);
            if(this.modal)
            {
               var body = query("body")[0];
               body.removeChild(this._modalDiv);
            }
         },
         setContent:function(contentDom)
         {
            domConstruct.empty(this.content);
            domConstruct.place(contentDom,this.content);
         },
         setInnerHTML:function(htmlText)
         {
            domConstruct.empty(this.content);
            this.content.innerHTML = htmlText;
         },
         show:function()
         {
            domStyle.set(this.domNode, "display", "block");
         },
         hide:function()
         {
            domStyle.set(this.domNode, "display", "none");
         }
      });
   return Widget;
});