[483] | 1 | define([ |
---|
| 2 | "dojo/_base/config", |
---|
| 3 | "dojo/_base/declare", |
---|
| 4 | "dojo/_base/lang", |
---|
| 5 | "dojo/dom-class", |
---|
| 6 | "dojo/dom-construct", |
---|
| 7 | "dojo/dom-geometry", |
---|
| 8 | "dojo/dom-style", |
---|
| 9 | "dojo/has", |
---|
| 10 | "dijit/_Contained", |
---|
| 11 | "dijit/_WidgetBase", |
---|
| 12 | "./_css3" |
---|
| 13 | ], function(config, declare, lang, domClass, domConstruct, domGeometry, domStyle, has, Contained, WidgetBase, css3){ |
---|
| 14 | |
---|
| 15 | // module: |
---|
| 16 | // dojox/mobile/ProgressIndicator |
---|
| 17 | |
---|
| 18 | var cls = declare("dojox.mobile.ProgressIndicator", [WidgetBase, Contained], { |
---|
| 19 | // summary: |
---|
| 20 | // A progress indication widget. |
---|
| 21 | // description: |
---|
| 22 | // ProgressIndicator is a round spinning graphical representation |
---|
| 23 | // that indicates the current task is ongoing. |
---|
| 24 | |
---|
| 25 | // interval: Number |
---|
| 26 | // The time interval in milliseconds for updating the spinning |
---|
| 27 | // indicator. |
---|
| 28 | interval: 100, |
---|
| 29 | |
---|
| 30 | // size: [const] Number |
---|
| 31 | // The size of the indicator in pixels. |
---|
| 32 | // Note that changing the value of the property after the widget |
---|
| 33 | // creation has no effect. |
---|
| 34 | size: 40, |
---|
| 35 | |
---|
| 36 | // removeOnStop: Boolean |
---|
| 37 | // If true, this widget is removed from the parent node |
---|
| 38 | // when stop() is called. |
---|
| 39 | removeOnStop: true, |
---|
| 40 | |
---|
| 41 | // startSpinning: Boolean |
---|
| 42 | // If true, calls start() to run the indicator at startup. |
---|
| 43 | startSpinning: false, |
---|
| 44 | |
---|
| 45 | // center: Boolean |
---|
| 46 | // If true, the indicator is displayed as center aligned. |
---|
| 47 | center: true, |
---|
| 48 | |
---|
| 49 | // colors: String[] |
---|
| 50 | // An array of indicator colors. 12 colors have to be given. |
---|
| 51 | // If colors are not specified, CSS styles |
---|
| 52 | // (mblProg0Color - mblProg11Color) are used. |
---|
| 53 | colors: null, |
---|
| 54 | |
---|
| 55 | /* internal properties */ |
---|
| 56 | |
---|
| 57 | // baseClass: String |
---|
| 58 | // The name of the CSS class of this widget. |
---|
| 59 | baseClass: "mblProgressIndicator", |
---|
| 60 | |
---|
| 61 | constructor: function(){ |
---|
| 62 | // summary: |
---|
| 63 | // Creates a new instance of the class. |
---|
| 64 | this.colors = []; |
---|
| 65 | this._bars = []; |
---|
| 66 | }, |
---|
| 67 | |
---|
| 68 | buildRendering: function(){ |
---|
| 69 | this.inherited(arguments); |
---|
| 70 | if(this.center){ |
---|
| 71 | domClass.add(this.domNode, "mblProgressIndicatorCenter"); |
---|
| 72 | } |
---|
| 73 | this.containerNode = domConstruct.create("div", {className:"mblProgContainer"}, this.domNode); |
---|
| 74 | this.spinnerNode = domConstruct.create("div", null, this.containerNode); |
---|
| 75 | for(var i = 0; i < 12; i++){ |
---|
| 76 | var div = domConstruct.create("div", {className:"mblProg mblProg"+i}, this.spinnerNode); |
---|
| 77 | this._bars.push(div); |
---|
| 78 | } |
---|
| 79 | this.scale(this.size); |
---|
| 80 | if(this.startSpinning){ |
---|
| 81 | this.start(); |
---|
| 82 | } |
---|
| 83 | }, |
---|
| 84 | |
---|
| 85 | scale: function(/*Number*/size){ |
---|
| 86 | // summary: |
---|
| 87 | // Changes the size of the indicator. |
---|
| 88 | // size: |
---|
| 89 | // The size of the indicator in pixels. |
---|
| 90 | var scale = size / 40; |
---|
| 91 | domStyle.set(this.containerNode, css3.add({}, { |
---|
| 92 | transform: "scale(" + scale + ")", |
---|
| 93 | transformOrigin: "0 0" |
---|
| 94 | })); |
---|
| 95 | domGeometry.setMarginBox(this.domNode, {w:size, h:size}); |
---|
| 96 | domGeometry.setMarginBox(this.containerNode, {w:size / scale, h:size / scale}); |
---|
| 97 | }, |
---|
| 98 | |
---|
| 99 | start: function(){ |
---|
| 100 | // summary: |
---|
| 101 | // Starts the spinning of the ProgressIndicator. |
---|
| 102 | if(this.imageNode){ |
---|
| 103 | var img = this.imageNode; |
---|
| 104 | var l = Math.round((this.containerNode.offsetWidth - img.offsetWidth) / 2); |
---|
| 105 | var t = Math.round((this.containerNode.offsetHeight - img.offsetHeight) / 2); |
---|
| 106 | img.style.margin = t+"px "+l+"px"; |
---|
| 107 | return; |
---|
| 108 | } |
---|
| 109 | var cntr = 0; |
---|
| 110 | var _this = this; |
---|
| 111 | var n = 12; |
---|
| 112 | this.timer = setInterval(function(){ |
---|
| 113 | cntr--; |
---|
| 114 | cntr = cntr < 0 ? n - 1 : cntr; |
---|
| 115 | var c = _this.colors; |
---|
| 116 | for(var i = 0; i < n; i++){ |
---|
| 117 | var idx = (cntr + i) % n; |
---|
| 118 | if(c[idx]){ |
---|
| 119 | _this._bars[i].style.backgroundColor = c[idx]; |
---|
| 120 | }else{ |
---|
| 121 | domClass.replace(_this._bars[i], |
---|
| 122 | "mblProg" + idx + "Color", |
---|
| 123 | "mblProg" + (idx === n - 1 ? 0 : idx + 1) + "Color"); |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | }, this.interval); |
---|
| 127 | }, |
---|
| 128 | |
---|
| 129 | stop: function(){ |
---|
| 130 | // summary: |
---|
| 131 | // Stops the spinning of the ProgressIndicator. |
---|
| 132 | if(this.timer){ |
---|
| 133 | clearInterval(this.timer); |
---|
| 134 | } |
---|
| 135 | this.timer = null; |
---|
| 136 | if(this.removeOnStop && this.domNode && this.domNode.parentNode){ |
---|
| 137 | this.domNode.parentNode.removeChild(this.domNode); |
---|
| 138 | } |
---|
| 139 | }, |
---|
| 140 | |
---|
| 141 | setImage: function(/*String*/file){ |
---|
| 142 | // summary: |
---|
| 143 | // Sets an indicator icon image file (typically animated GIF). |
---|
| 144 | // If null is specified, restores the default spinner. |
---|
| 145 | if(file){ |
---|
| 146 | this.imageNode = domConstruct.create("img", {src:file}, this.containerNode); |
---|
| 147 | this.spinnerNode.style.display = "none"; |
---|
| 148 | }else{ |
---|
| 149 | if(this.imageNode){ |
---|
| 150 | this.containerNode.removeChild(this.imageNode); |
---|
| 151 | this.imageNode = null; |
---|
| 152 | } |
---|
| 153 | this.spinnerNode.style.display = ""; |
---|
| 154 | } |
---|
| 155 | }, |
---|
| 156 | |
---|
| 157 | destroy: function(){ |
---|
| 158 | this.inherited(arguments); |
---|
| 159 | if(this === cls._instance){ |
---|
| 160 | cls._instance = null; |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | }); |
---|
| 164 | |
---|
| 165 | cls._instance = null; |
---|
| 166 | cls.getInstance = function(props){ |
---|
| 167 | if(!cls._instance){ |
---|
| 168 | cls._instance = new cls(props); |
---|
| 169 | } |
---|
| 170 | return cls._instance; |
---|
| 171 | }; |
---|
| 172 | |
---|
| 173 | return cls; |
---|
| 174 | }); |
---|