PageIndicator.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. define("dojox/mobile/PageIndicator", [
  2. "dojo/_base/connect",
  3. "dojo/_base/declare",
  4. "dojo/_base/window",
  5. "dojo/dom",
  6. "dojo/dom-class",
  7. "dojo/dom-construct",
  8. "dijit/registry", // registry.byNode
  9. "dijit/_Contained",
  10. "dijit/_WidgetBase"
  11. ], function(connect, declare, win, dom, domClass, domConstruct, registry, Contained, WidgetBase){
  12. /*=====
  13. var Contained = dijit._Contained;
  14. var WidgetBase = dijit._WidgetBase;
  15. =====*/
  16. // module:
  17. // dojox/mobile/PageIndicator
  18. // summary:
  19. // A current page indicator.
  20. return declare("dojox.mobile.PageIndicator", [WidgetBase, Contained],{
  21. // summary:
  22. // A current page indicator.
  23. // description:
  24. // PageIndicator displays a series of gray and white dots to
  25. // indicate which page is currently being viewed. It can typically
  26. // be used with dojox.mobile.SwapView. It is also internally used
  27. // in dojox.mobile.Carousel.
  28. // refId: String
  29. // An ID of a DOM node to be searched. Siblings of the reference
  30. // node will be searched for views. If not specified, this.domNode
  31. // will be the reference node.
  32. refId: "",
  33. buildRendering: function(){
  34. this.domNode = this.srcNodeRef || win.doc.createElement("DIV");
  35. this.domNode.className = "mblPageIndicator";
  36. this._tblNode = domConstruct.create("TABLE", {className:"mblPageIndicatorContainer"}, this.domNode);
  37. this._tblNode.insertRow(-1);
  38. this.connect(this.domNode, "onclick", "onClick");
  39. connect.subscribe("/dojox/mobile/viewChanged", this, function(view){
  40. this.reset();
  41. });
  42. },
  43. startup: function(){
  44. var _this = this;
  45. setTimeout(function(){ // to wait until views' visibility is determined
  46. _this.reset();
  47. }, 0);
  48. },
  49. reset: function(){
  50. // summary:
  51. // Updates the indicator.
  52. var r = this._tblNode.rows[0];
  53. var i, c, a = [], dot;
  54. var refNode = (this.refId && dom.byId(this.refId)) || this.domNode;
  55. var children = refNode.parentNode.childNodes;
  56. for(i = 0; i < children.length; i++){
  57. c = children[i];
  58. if(this.isView(c)){
  59. a.push(c);
  60. }
  61. }
  62. if(r.cells.length !== a.length){
  63. domConstruct.empty(r);
  64. for(i = 0; i < a.length; i++){
  65. c = a[i];
  66. dot = domConstruct.create("DIV", {className:"mblPageIndicatorDot"});
  67. r.insertCell(-1).appendChild(dot);
  68. }
  69. }
  70. if(a.length === 0){ return; }
  71. var currentView = registry.byNode(a[0]).getShowingView();
  72. for(i = 0; i < r.cells.length; i++){
  73. dot = r.cells[i].firstChild;
  74. if(a[i] === currentView.domNode){
  75. domClass.add(dot, "mblPageIndicatorDotSelected");
  76. }else{
  77. domClass.remove(dot, "mblPageIndicatorDotSelected");
  78. }
  79. }
  80. },
  81. isView: function(node){
  82. // summary:
  83. // Returns true if the given node is a view.
  84. return (node && node.nodeType === 1 && domClass.contains(node, "mblView"));
  85. },
  86. onClick: function(e){
  87. if(e.target !== this.domNode){ return; }
  88. if(e.layerX < this._tblNode.offsetLeft){
  89. connect.publish("/dojox/mobile/prevPage", [this]);
  90. }else if(e.layerX > this._tblNode.offsetLeft + this._tblNode.offsetWidth){
  91. connect.publish("/dojox/mobile/nextPage", [this]);
  92. }
  93. }
  94. });
  95. });