Changeset 7504

Show
Ignore:
Timestamp:
03/01/07 12:02:26 (21 months ago)
Author:
peller
Message:

ProgressBar?: more "craptastic" functions, more tweaks

Location:
dijit/trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • dijit/trunk/ProgressBar.js

    r7501 r7504  
    3737                //   places="0" width="..." height="..." dataSource="..." 
    3838                //   pollInterval="..."  
    39                 //   numeric="true|false" orientation="vertical"  
     39                //   annotate="true|false" orientation="vertical"  
    4040                //   progress="..." maximum="..."></div> 
    4141         
     
    6262                color: "", 
    6363 
    64                 // numeric: Boolean 
    65                 // if true, the percent label is visible 
    66                 numeric: false, 
    67  
    6864                // orientation: String 
    6965                // whether bar grows along the x-axis (default) or y- axis (vertical) 
    7066                orientation: "", 
    7167 
     68                // annotate: Boolean 
     69                // if true, the percent label is visible 
     70                annotate: false, 
     71 
    7272                // places: Number 
    73                 // number of places to show in percent values; 0 by default 
     73                // number of places to show in values; 0 by default 
    7474                places: 0, 
    75                  
     75 
    7676                // dataSource: String 
    7777                // dataSource uri for server polling 
     
    8888                templatePath: dojo.uri.moduleUri("dijit", "templates/ProgressBar.html"),                 
    8989 
    90                 _percent: 0.0, 
    91                 _animationStopped: true, 
    92  
    9390                // public functions 
    9491                postCreate: function(){ 
     
    9996                                dojo.html.setClass(this.internalProgress, "dojoProgressBarFull"); 
    10097                        } 
    101                         dojo.html.setClass(this.containerNode, "dojoProgressBarEmpty"); 
    10298                        if(this.orientation == "vertical"){ 
    10399                                dojo.html.addClass(this.containerNode, "dojoProgressBarVertical"); 
     
    112108                        this.domNode.style.height = this.height + "px";  
    113109                        this.domNode.style.width = this.width + "px"; 
    114                         this.setMaximum(this.maximum, true); 
    115                         this.setProgress(this.progress, true); 
    116                         this.showLabel(this.numeric); 
    117                         this.render(); 
    118                 }, 
    119                 showLabel: function(/*Boolean*/visible){ 
    120                         // summary: shows or hides the labels 
    121                         var display = visible ? "block" : "none"; 
    122                         this.backLabel.style.display=display; 
    123                         this.frontLabel.style.display=display; 
    124                         this.numeric = visible; 
    125                 }, 
     110                        this.update(); 
     111                }, 
     112 
     113                update: function(/*Object*/attributes){ 
     114                        // summary: update progress information 
     115                        // 
     116                        // attributes: may provide progress and/or maximum properties on this parameter, 
     117                        //      see attribute specs for details. 
     118                        dojo.lang.mixin(this, attributes); 
     119                        var percent; 
     120                        if(String(this.progress).indexOf("%") != -1){ 
     121                                percent = Math.min(parseFloat(this.progress)/100, 1); 
     122                                this.progress = percent * this.maximum; 
     123                        }else{ 
     124                                this.progress = Math.min(this.progress, this.maximum); 
     125                                percent = this.progress / this.maximum; 
     126                        } 
     127 
     128                        if(!this._animationStopped){return;} 
     129 
     130                        var pixels = percent * this[this._dimension]; 
     131                        this.internalProgress.style[this._dimension] = pixels + 'px'; 
     132 
     133                        var display = this.annotate ? "block" : "none"; 
     134                        dojo.lang.forEach(["front", "back"], function(name){ 
     135                                var labelNode = this[name+"Label"]; 
     136                                dojo.dom.textContent(labelNode, this.report(percent)); 
     137 
     138                                labelNode.style.display = display; 
     139 
     140                                var dim = dojo.html.getContentBox(labelNode); 
     141                                var labelLeft = (this.width - dim.width)/2; 
     142                                var labelBottom = (this.height - dim.height)/2; 
     143                                labelNode.style.left = labelLeft + "px"; 
     144                                labelNode.style.bottom = labelBottom + "px"; 
     145                        }, this); 
     146 
     147                        this.onChange(); 
     148                }, 
     149 
     150                report: function(/*float*/percent){ 
     151                        // Generates percentage to overlay; may be overridden by user 
     152                        return dojo.number.format(percent, {type: "percent", places: this.places}); 
     153                }, 
     154 
     155                _animationStopped: true, 
     156 
    126157                _setupAnimation: function(){ 
    127158                        var self = this; 
    128159                        this._animation = dojo.lfx.html.slideTo(this.internalProgress,  
    129160                                {top: 0, left: parseInt(this.width)-parseInt(this.internalProgress.style.width)}, parseInt(this.duration), null,  
    130                                         function(){ 
    131                                                 var _backAnim = dojo.lfx.html.slideTo(self.internalProgress,  
    132                                                 { top: 0, left: 0 }, parseInt(self.duration)); 
    133                                                 dojo.event.connect(_backAnim, "onEnd", function(){ 
    134                                                         if(!self._animationStopped){ 
    135                                                                 self._animation.play(); 
    136                                                         } 
    137                                                         }); 
     161                                function(){ 
     162                                        var backAnim = dojo.lfx.html.slideTo(self.internalProgress,  
     163                                        { top: 0, left: 0 }, parseInt(self.duration)); 
     164                                        dojo.event.connect(backAnim, "onEnd", function(){ 
    138165                                                if(!self._animationStopped){ 
    139                                                         _backAnim.play(); 
     166                                                        self._animation.play(); 
    140167                                                } 
    141                                                 _backAnim = null; // <-- to avoid memory leaks in IE 
     168                                        }); 
     169                                        if(!self._animationStopped){ 
     170                                                backAnim.play(); 
    142171                                        } 
    143                                 ); 
    144                 }, 
    145                 setMaximum: function(/*Number*/maximum, /*Boolean?*/noRender){ 
    146                         // summary: sets the maximum 
    147                         // if noRender is true, only sets the internal max progress value 
    148                         if(!this._animationStopped){ 
    149                                 return; 
    150                         } 
    151                         this.maximum = maximum; 
    152                         this.setProgress(this.progress, true); 
    153                         if(!noRender){ 
    154                                 this.render(); 
    155                         } 
    156                 }, 
    157                 setProgress: function(/*String|Number*/value, /*Boolean?*/noRender){ 
    158                         // summary: sets the progress 
    159                         // if value ends width "%", does a normalization 
    160                         // if noRender is true, only sets the internal value: useful if 
    161                         // there is a setMaximum call 
    162                         if(!this._animationStopped){ 
    163                                 return; 
    164                         } 
    165                         if(String(value).indexOf("%") != -1){ 
    166                                 this._percent = Math.min(parseFloat(value)/100, 1); 
    167                                 this.progress = this._percent * this.maximum; 
    168                         }else{ 
    169                                 this.progress = Math.min(value, this.maximum); 
    170                                 this._percent = value / this.maximum; 
    171                         } 
    172                         if(!noRender){ 
    173                                 this.render(); 
    174                         } 
    175                 }, 
    176                 start: function(){ 
    177                         // summary: starts the server polling 
    178                         var _showFunction = dojo.lang.hitch(this, this._showRemoteProgress); 
    179                         this._timer = setInterval(_showFunction, this.pollInterval); 
    180                 }, 
     172                                        backAnim = null; // <-- to avoid memory leaks in IE 
     173                                }); 
     174                }, 
     175 
    181176                startAnimation: function(){ 
    182177                        // summary: starts the left-right animation, useful when 
    183178                        // the user doesn't know how much time the operation will last 
    184179                        if(this._animationStopped){ 
    185                                 this._backup = {progress: this.progress, numeric: this.numeric}; 
    186                                 this.setProgress("10%"); 
     180                                this._backup = {progress: this.progress, annotate: this.annotate}; 
     181                                this.update({progress: "10%", annotate: false}); 
    187182                                this._animationStopped = false; 
    188183                                this._setupAnimation(); 
    189                                 this.showLabel(false); 
    190184                                this.internalProgress.style.height="105%"; 
    191185                                this._animation.play(); 
     
    200194                                this.internalProgress.style.left = "0px"; 
    201195                                dojo.lang.mixin(this, this._backup); 
    202                                 this.setProgress(this.progress); 
    203                                 this.showLabel(this.numeric); 
    204                                 this._positionLabels(); 
    205                         } 
    206                 }, 
     196                                this.update(); 
     197                        } 
     198                }, 
     199 
     200                start: function(){ 
     201                        // summary: starts the server polling 
     202                        var _showFunction = dojo.lang.hitch(this, this._showRemoteProgress); 
     203                        this._timer = setInterval(_showFunction, this.pollInterval); 
     204                }, 
     205 
    207206                _showRemoteProgress: function(){ 
    208207                        var self = this; 
     
    210209                                clearInterval(this._timer); 
    211210                                this._timer = null; 
    212                                 this.setProgress("100%"); 
     211                                this.update({progress: "100%"}); 
    213212                                return;  
    214213                        } 
     
    221220                                }, 
    222221                                load: function(type, data, evt){ 
    223                                         self.setProgress(self._timer ? data.progress : "100%"); 
     222                                        self.update({progress: self._timer ? data.progress : "100%"}); 
    224223                                } 
    225224                        }; 
     
    227226                }, 
    228227 
    229                 render: function(){ 
    230                         // summary: renders the ProgressBar, based on current values 
    231  
    232                         this._updateLabels(this._percent); 
    233  
    234                         var pixels = this._percent * this[this._dimension]; 
    235                         this.internalProgress.style[this._dimension] = pixels + 'px'; 
    236                         this.onChange(); 
    237  
    238                         this._positionLabels(); 
    239                 }, 
    240  
    241                 _positionLabels: function(){ 
    242                         dojo.lang.forEach(["front", "back"], function(name){ 
    243                                 var labelNode = this[name+"Label"]; 
    244                                 var dim = dojo.html.getContentBox(labelNode); 
    245                                 var labelLeft = (this.width - dim.width)/2; 
    246                                 var labelBottom = (this.height - dim.height)/2; 
    247                                 labelNode.style.left = labelLeft + "px"; 
    248                                 labelNode.style.bottom = labelBottom + "px"; 
    249                         }, this); 
    250                 }, 
    251                 _updateLabels: function(/*float*/percent){ 
    252                         dojo.lang.forEach(["front", "back"], function(name){ 
    253                                 var labelNode = this[name+"Label"]; 
    254                                 dojo.dom.textContent(labelNode, dojo.number.format(percent, {type: "percent", places: this.places})); 
    255                         }, this); 
    256                 }, 
    257228                onChange: function(){ 
    258229                } 
  • dijit/trunk/templates/ProgressBar.html

    r7501 r7504  
    1 <div dojoAttachPoint="containerNode"> 
     1<div dojoAttachPoint="containerNode" class="dojoProgressBarEmpty"> 
    22        <div style="position:absolute;display:none;width:100%;text-align:center" dojoAttachPoint="backLabel" class="dojoProgressBarBackLabel"></div> 
    33        <div style="position:absolute;overflow:hidden;width:100%;height:100%" dojoAttachPoint="internalProgress"> 
  • dijit/trunk/tests/test_ProgressBar.html

    r7501 r7504  
    1414                        dojo.require("dojo.io.*"); 
    1515                        dojo.require("dojo.io.IframeIO"); 
     16                        dojo.require("dojo.string.extras"); 
    1617                        dojo.hostenv.writeIncludes(); 
    1718                </script> 
     
    2021                        dojo.addOnLoad(go); 
    2122                        function go(){ 
    22                                 var theBar = dijit.byId("testBar"); 
     23                                //TODO: it's a little strange that id must be specified again?   
     24                                var theBar = new dijit.ProgressBar({id: "testBar", width: 400, annotate: true, maximum: 256, duration: 2000, 
     25                                        report:function(percent){ 
     26                                                return dojo.string.substitute("${0} out of ${1} max chars", [this.progress, this.maximum]); 
     27                                        } 
     28                                }, dojo.byId("testBar")); 
    2329                                dojo.event.connect(dojo.byId("start"), "onclick", dojo.lang.hitch(theBar,theBar.startAnimation)); 
    2430                                dojo.event.connect(dojo.byId("stop"), "onclick", dojo.lang.hitch(theBar,theBar.stopAnimation)); 
     
    4854                        } 
    4955                        function setParameters(){ 
    50                                 dijit.byId("setTestBar").setMaximum(dojo.byId("maximum").value, true); 
    51                                 dijit.byId("setTestBar").setProgress(dojo.byId("progressValue").value, true); 
    52                                 dijit.byId("setTestBar").render(); 
     56                                dijit.byId("setTestBar").update({maximum: dojo.byId("maximum").value, progress: dojo.byId("progressValue").value}); 
    5357                        } 
    5458                        function keyUpHandler(){ 
    55                                 dijit.byId("testBar").setProgress(dojo.byId("test").value.length); 
    56                                 dijit.byId("testBarInt").setProgress(dojo.byId("test").value.length); 
    57                                 dijit.byId("testBar2").setProgress(dojo.byId("test").value.length); 
    58                                 dijit.byId("smallTestBar").setProgress(dojo.byId("test").value.length); 
     59                                dijit.byId("testBar").update({progress:dojo.byId("test").value.length}); 
     60                                dijit.byId("testBarInt").update({progress:dojo.byId("test").value.length}); 
     61                                dijit.byId("testBar2").update({progress:dojo.byId("test").value.length}); 
     62                                dijit.byId("smallTestBar").update({progress:dojo.byId("test").value.length}); 
    5963                        } 
    6064                </script> 
     
    6973                <input type="button" name="set" id="set" value="set!" /> 
    7074                <br /> 
    71                 <div width="400" numeric="true" 
    72                         maximum="200" id="setTestBar" progress="20" dojoType="dijit.ProgressBar"></div>          
     75                <div width="400" annotate="true" 
     76                        maximum="200" id="setTestBar" progress="20" dojoType="dijit.ProgressBar"></div> 
    7377                <br /> 
    7478                <br /> 
     
    7781                <br /> 
    7882                <br /> 
    79                 <div width="400" numeric="true"   
    80                         maximum="256" id="testBar" duration="2000" dojoType="dijit.ProgressBar"></div>           
     83                <div id="testBar"></div>                 
    8184                <input type="button" name="start" id="start" value="start" /> 
    8285                <input type="button" name="stop" id="stop" value="stop" /> 
     
    8891                <br /> 
    8992                Show decimal place: 
    90                 <div places="1" width="400" numeric="true" orientation="horizontal"  
    91                         maximum="256" id="testBarInt" dojoType="dijit.ProgressBar"></div>                
     93                <div places="1" width="400" annotate="true" orientation="horizontal" 
     94                        maximum="256" id="testBarInt" dojoType="dijit.ProgressBar"></div> 
    9295                <br /> 
    9396                <div style="position:absolute;top:70px;left:530px"> 
    9497                Vertical: 
    95                 <div progress="0" height="420" width="50" numeric="true" orientation="vertical"  
     98                <div progress="0" height="420" width="50" annotate="true" orientation="vertical"  
    9699                                maximum="256" id="testBar2" dojoType="dijit.ProgressBar"></div> 
    97100                </div> 
    98101                <h3>Test 3</h3> 
    99102                No explicit maximum (both 25%) 
    100                 <div width="400" numeric="true" 
     103                <div width="400" annotate="true" 
    101104                        id="implied1" progress="25" dojoType="dijit.ProgressBar"></div>          
    102                 <div width="400" numeric="true" 
     105                <br /> 
     106                <div width="400" annotate="true" 
    103107                        id="implied2" progress="25%" dojoType="dijit.ProgressBar"></div> 
    104108        </body>