45 | | this.progressBar = this.add({ |
46 | | xtype: 'progress', |
47 | | cls: 'x-deluge-status-progressbar' |
| 45 | this.piecesBar = this.add({ |
| 46 | xtype: 'box', |
| 47 | id: 'piecesbar', |
| 48 | width: '99%', |
| 49 | height: 30, |
| 50 | |
| 51 | style: { |
| 52 | marginRight: '.5%', |
| 53 | marginLeft: '.5%', |
| 54 | marginTop: '7px' |
| 55 | }, |
| 56 | |
| 57 | autoEl: { |
| 58 | tag: 'canvas', |
| 59 | height: 30, |
| 60 | width: '99%' |
| 61 | }, |
| 62 | |
| 63 | colors: { |
| 64 | 0: "#a8a8a9", |
| 65 | 1: "#22dd00", |
| 66 | 2: "#dddd00", |
| 67 | 3: "#3258c1", |
| 68 | 4: "#cccccc" |
| 69 | }, |
| 70 | |
| 71 | pieces: null, |
| 72 | fraction: null, |
| 73 | text: null, |
| 74 | |
| 75 | compare_pieces: function(pieces) { |
| 76 | if (!this.pieces || pieces.length != this.pieces.length) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | for (var i = 0; i < pieces.length; i++) { |
| 81 | if (pieces[i] != this.pieces[i]) { |
| 82 | return false; |
| 83 | } |
| 84 | } |
| 85 | return true; |
| 86 | }, |
| 87 | |
| 88 | roundcorners_clipping: function(ctx) { |
| 89 | this.create_roundcorners_subpath(ctx, 0.0, 0.0, this.getWidth(), this.getHeight()); |
| 90 | ctx.clip(); |
| 91 | }, |
| 92 | |
| 93 | roundcorners_border: function(ctx) { |
| 94 | this.create_roundcorners_subpath(ctx, 0.5, 0.5, this.getWidth()-1, this.getHeight()-1); |
| 95 | ctx.strokeStyle = "rgba(0,0,0,0.9)"; |
| 96 | ctx.stroke(); |
| 97 | }, |
| 98 | |
| 99 | create_roundcorners_subpath: function(ctx, x, y, width, height) { |
| 100 | var aspect = 1.0; |
| 101 | var corner_radius = height/10.0; |
| 102 | var radius = corner_radius/aspect; |
| 103 | var degrees = Math.PI/180.0; |
| 104 | ctx.beginPath(); |
| 105 | ctx.arc(x + width - radius, y + radius, radius, -90 * degrees, 0 * degrees); |
| 106 | ctx.arc(x + width - radius, y + height - radius, radius, 0 * degrees, 90 * degrees); |
| 107 | ctx.arc(x + radius, y + height - radius, radius, 90 * degrees, 180 * degrees); |
| 108 | ctx.arc(x + radius, y + radius, radius, 180 * degrees, 270 * degrees); |
| 109 | ctx.closePath(); |
| 110 | |
| 111 | }, |
| 112 | |
| 113 | draw_progress_overlay: function(ctx, fraction) { |
| 114 | if (fraction == 0) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | var overlay_width = this.getWidth()*fraction; |
| 119 | var gradient = ctx.createLinearGradient(0, this.getHeight()/2, overlay_width, this.getHeight()/2); |
| 120 | gradient.addColorStop(0, "rgba(0,0,0,0.4)"); |
| 121 | |
| 122 | var stop = 20/overlay_width; |
| 123 | gradient.addColorStop(1-stop, "rgba(0,0,0,0.4)"); |
| 124 | |
| 125 | if (fraction != 1.0) { |
| 126 | gradient.addColorStop(1, "rgba(0,0,0,0.0)"); |
| 127 | } |
| 128 | ctx.fillStyle = gradient; |
| 129 | ctx.fillRect(0, 0, overlay_width, this.getHeight()); |
| 130 | }, |
| 131 | |
| 132 | write_text: function(ctx, text) { |
| 133 | ctx.textAlign = "center"; |
| 134 | ctx.font = "bold 13px sans-serif"; |
| 135 | ctx.fillStyle = "rgba(255,255,255,1)"; |
| 136 | ctx.fillText(text, Math.floor(this.getWidth()/2), this.getHeight()/2 + 4); |
| 137 | }, |
| 138 | |
| 139 | draw_pieces: function(ctx, pieces) { |
| 140 | var piece_width = (1/pieces.length) * (this.getWidth() - 2); |
| 141 | var start_pos = 1; |
| 142 | for (var i = 0; i < pieces.length; i++) { |
| 143 | ctx.fillStyle = this.colors[pieces[i]]; |
| 144 | ctx.fillRect(start_pos, 1, piece_width, this.getHeight() - 2); |
| 145 | start_pos += piece_width; |
| 146 | } |
| 147 | }, |
| 148 | |
| 149 | update: function(pieces, fraction, text) { |
| 150 | if (!this.compare_pieces(pieces) && typeof(pieces) != "number" |
| 151 | || this.fraction != fraction || this.text != text) { |
| 152 | |
| 153 | var canvas = document.getElementById("piecesbar"); |
| 154 | var ctx = canvas.getContext("2d"); |
| 155 | canvas.width = this.getWidth(); |
| 156 | |
| 157 | this.roundcorners_clipping(ctx); |
| 158 | |
| 159 | this.pieces = pieces; |
| 160 | this.fraction = fraction; |
| 161 | this.text = text; |
| 162 | |
| 163 | this.draw_pieces(ctx, pieces); |
| 164 | this.draw_progress_overlay(ctx, fraction); |
| 165 | this.write_text(ctx, text); |
| 166 | |
| 167 | this.roundcorners_border(ctx); |
| 168 | } |
| 169 | } |
| 170 | |