| 1 | /* |
|---|
| 2 | Javascript progress bar class |
|---|
| 3 | |
|---|
| 4 | To use: create a div you wish to be the progress bar, and |
|---|
| 5 | instantiate a ProgressBar with the div element |
|---|
| 6 | |
|---|
| 7 | To style it you need to define several classes, a container class which |
|---|
| 8 | defines the progress bar container, an indefinite class which has your |
|---|
| 9 | "barber shop" background tile, and an overlay class which defines the look |
|---|
| 10 | of the progress bar. |
|---|
| 11 | |
|---|
| 12 | requires core.js, dom.js |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | ProgressBar = new Class ( Object, { |
|---|
| 16 | init: function (pbar) { |
|---|
| 17 | this.max = 0; |
|---|
| 18 | this.value = 0; |
|---|
| 19 | this.pbar = pbar; // container |
|---|
| 20 | this.containerClassName = ""; |
|---|
| 21 | this.indefiniteClassName = ""; |
|---|
| 22 | this.overlayClassName = ""; |
|---|
| 23 | this.indefinite = 1; |
|---|
| 24 | this.overlay = null; |
|---|
| 25 | this.update(); |
|---|
| 26 | }, |
|---|
| 27 | |
|---|
| 28 | setContainerClassName: function (classname) { |
|---|
| 29 | if (!this.pbar) |
|---|
| 30 | return; |
|---|
| 31 | |
|---|
| 32 | DOM.removeClassName(this.pbar, classname); |
|---|
| 33 | this.containerClassName = classname; |
|---|
| 34 | this.update(); |
|---|
| 35 | }, |
|---|
| 36 | |
|---|
| 37 | setIndefiniteClassName: function (classname) { |
|---|
| 38 | this.indefiniteClassName = classname; |
|---|
| 39 | this.update(); |
|---|
| 40 | }, |
|---|
| 41 | |
|---|
| 42 | setOverlayClassName: function (classname) { |
|---|
| 43 | this.overlayClassName = classname; |
|---|
| 44 | this.update(); |
|---|
| 45 | }, |
|---|
| 46 | |
|---|
| 47 | setWidth: function (w) { |
|---|
| 48 | if (!this.pbar) |
|---|
| 49 | return; |
|---|
| 50 | |
|---|
| 51 | if (w+0 == w) |
|---|
| 52 | DOM.setWidth(this.pbar, w); |
|---|
| 53 | else |
|---|
| 54 | this.pbar.style.width = w; |
|---|
| 55 | }, |
|---|
| 56 | |
|---|
| 57 | setMax: function (max) { |
|---|
| 58 | this.max = max; |
|---|
| 59 | this.update(); |
|---|
| 60 | }, |
|---|
| 61 | |
|---|
| 62 | setValue: function (value) { |
|---|
| 63 | this.value = value; |
|---|
| 64 | this.indefinite = false; |
|---|
| 65 | this.update(); |
|---|
| 66 | }, |
|---|
| 67 | |
|---|
| 68 | setIndefinite: function (indef) { |
|---|
| 69 | this.indefinite = indef; |
|---|
| 70 | this.update(); |
|---|
| 71 | }, |
|---|
| 72 | |
|---|
| 73 | max: function () { |
|---|
| 74 | return this.max; |
|---|
| 75 | }, |
|---|
| 76 | |
|---|
| 77 | value: function () { |
|---|
| 78 | return this.value; |
|---|
| 79 | }, |
|---|
| 80 | |
|---|
| 81 | hide: function () { |
|---|
| 82 | if (!this.pbar) |
|---|
| 83 | return; |
|---|
| 84 | |
|---|
| 85 | this.pbar.style.display = "none"; |
|---|
| 86 | }, |
|---|
| 87 | |
|---|
| 88 | show: function () { |
|---|
| 89 | if (!this.pbar) |
|---|
| 90 | return; |
|---|
| 91 | |
|---|
| 92 | this.pbar.style.display = ""; |
|---|
| 93 | }, |
|---|
| 94 | |
|---|
| 95 | update: function () { |
|---|
| 96 | if (!this.pbar) |
|---|
| 97 | return; |
|---|
| 98 | |
|---|
| 99 | DOM.addClassName(this.pbar, this.containerClassName); |
|---|
| 100 | |
|---|
| 101 | // definite or indefinite bar? |
|---|
| 102 | if (this.indefinite || this.value < 0) { |
|---|
| 103 | // barber shop |
|---|
| 104 | // is there an overlay? if so, kill it |
|---|
| 105 | if (this.overlay) { |
|---|
| 106 | this.overlay.parentNode.removeChild(this.overlay); |
|---|
| 107 | this.overlay = null; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | // set the indefinite class |
|---|
| 111 | DOM.addClassName(this.pbar, this.indefiniteClassName); |
|---|
| 112 | return; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | DOM.removeClassName(this.pbar, this.indefiniteClassName); |
|---|
| 116 | |
|---|
| 117 | var overlay = this.overlay; |
|---|
| 118 | |
|---|
| 119 | // does the progress bar container have the overlay? |
|---|
| 120 | if (!this.overlay) { |
|---|
| 121 | overlay = document.createElement("div"); |
|---|
| 122 | |
|---|
| 123 | if (!overlay) |
|---|
| 124 | return; |
|---|
| 125 | |
|---|
| 126 | this.pbar.appendChild(overlay); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | DOM.addClassName(overlay, this.overlayClassName); |
|---|
| 130 | |
|---|
| 131 | var dim = DOM.getAbsoluteDimensions(this.pbar); |
|---|
| 132 | var pct = this.value/this.max; |
|---|
| 133 | var oldWidth = dim.absoluteRight - dim.absoluteLeft; |
|---|
| 134 | var newWidth = oldWidth * pct; |
|---|
| 135 | DOM.setWidth(overlay, newWidth); |
|---|
| 136 | DOM.setHeight(overlay, dim.absoluteBottom - dim.absoluteTop); |
|---|
| 137 | |
|---|
| 138 | this.overlay = overlay; |
|---|
| 139 | } |
|---|
| 140 | }); |
|---|