|
| 1 | +Framework.Components.registerComponent("range", { |
| 2 | + |
| 3 | + getValue: function() { |
| 4 | + return this.values; |
| 5 | + }, |
| 6 | + |
| 7 | + setValue: function(val) { |
| 8 | + this.values = val; |
| 9 | + this.updateValues(); |
| 10 | + }, |
| 11 | + |
| 12 | + updateValues: function() { |
| 13 | + var el = this.getElement(); |
| 14 | + var width = el.measure("width")-(this.knobSize*2); |
| 15 | + var minPerc = ((this.values[0] - this.min) / this.max); |
| 16 | + var maxPerc = ((this.values[1] - this.min) / this.max); |
| 17 | + this.minKnob.setStyle({ |
| 18 | + "left": (minPerc*width) + "px" |
| 19 | + }); |
| 20 | + this.maxKnob.setStyle({ |
| 21 | + "left": (this.knobSize+(maxPerc*width)) + "px" |
| 22 | + }); |
| 23 | + var left = minPerc*width; |
| 24 | + var right = (maxPerc*width)+this.knobSize; |
| 25 | + this.bar.setStyle({ |
| 26 | + "left": left + "px", |
| 27 | + "width": (right-left) + "px" |
| 28 | + }); |
| 29 | + |
| 30 | + Event.simulate(el, "change"); |
| 31 | + }, |
| 32 | + |
| 33 | + hook: function(el) { |
| 34 | + el.getValue = this.getValue.bind(this); |
| 35 | + el.setValue = this.setValue.bind(this); |
| 36 | + }, |
| 37 | + |
| 38 | + setup: function(el) { |
| 39 | + this.backTrack = $C(el, "track"); |
| 40 | + this.back = $C(this.backTrack, "back"); |
| 41 | + |
| 42 | + this.barTrack = $C(el, "track"); |
| 43 | + this.bar = $C(this.barTrack, "bar"); |
| 44 | + |
| 45 | + this.minKnob = $C(el, "knob.min"); |
| 46 | + this.maxKnob = $C(el, "knob.max"); |
| 47 | + |
| 48 | + this.min = el.readAttribute("value") || 0; |
| 49 | + this.max = el.readAttribute("value") || 100; |
| 50 | + var valMod = this.max-this.min; |
| 51 | + |
| 52 | + this.values = el.readAttribute("value"); |
| 53 | + if(this.values) |
| 54 | + this.values = this.values.split(/[,\s]+/g); |
| 55 | + else |
| 56 | + this.values = [this.min,this.max]; |
| 57 | + |
| 58 | + el.addClassName("setup"); |
| 59 | + setTimeout((function() { |
| 60 | + this.updateValues(); |
| 61 | + this.knobSize = Element.measure(this.minKnob, "width"); |
| 62 | + this.makeDraggable(this.minKnob); |
| 63 | + this.makeDraggable(this.maxKnob); |
| 64 | + |
| 65 | + Event.on(this.minKnob, "drag:move", (function(e) { |
| 66 | + var pos = e.memo; |
| 67 | + //console.log(pos); |
| 68 | + |
| 69 | + var width = el.measure("width"); |
| 70 | + pos.left = Math.max(0, pos.left); |
| 71 | + pos.left = Math.min(width-(this.knobSize*2), pos.left); |
| 72 | + this.minKnob.setStyle({"left": pos.left + "px"}); |
| 73 | + width -= this.knobSize*2; |
| 74 | + |
| 75 | + var left = pos.left; |
| 76 | + var right = (this.knobSize+(((this.values[1] - this.min) / this.max)*width)); |
| 77 | + this.bar.setStyle({ |
| 78 | + "left": left + "px", |
| 79 | + "width": Math.max(0, right-left) + "px" |
| 80 | + }); |
| 81 | + |
| 82 | + pos.left += this.knobSize; |
| 83 | + this.maxKnob.setStyle({"left": Math.max(right, pos.left) + "px"}); |
| 84 | + e.stop(); |
| 85 | + }).bind(this)); |
| 86 | + Event.on(this.maxKnob, "drag:move", (function(e) { |
| 87 | + var pos = e.memo; |
| 88 | + //console.log(pos); |
| 89 | + |
| 90 | + var width = el.measure("width"); |
| 91 | + pos.left = Math.max(this.knobSize, pos.left); |
| 92 | + pos.left = Math.min(width-this.knobSize, pos.left); |
| 93 | + this.maxKnob.setStyle({"left": pos.left + "px"}); |
| 94 | + width -= this.knobSize*2; |
| 95 | + |
| 96 | + var left = (((this.values[0] - this.min) / this.max)*width); |
| 97 | + var right = pos.left; |
| 98 | + this.bar.setStyle({ |
| 99 | + "left": left + "px", |
| 100 | + "width": Math.max(0, right-left) + "px" |
| 101 | + }); |
| 102 | + |
| 103 | + pos.left -= this.knobSize; |
| 104 | + this.minKnob.setStyle({"left": Math.min(left, pos.left) + "px"}); |
| 105 | + e.stop(); |
| 106 | + }).bind(this)); |
| 107 | + var boundUpdateValues = this.updateValues.bind(this); |
| 108 | + Event.on(this.minKnob, "drag:stop", (function(e) { |
| 109 | + var val = this.minKnob.measure("left"); |
| 110 | + val /= el.measure("width")-(this.knobSize*2); |
| 111 | + val *= valMod; |
| 112 | + val += this.min; |
| 113 | + |
| 114 | + this.values[0] = val; |
| 115 | + this.values[1] = Math.max(val, this.values[1]); |
| 116 | + boundUpdateValues(); |
| 117 | + }).bind(this)); |
| 118 | + Event.on(this.maxKnob, "drag:stop", (function(e) { |
| 119 | + var val = this.maxKnob.measure("left"); |
| 120 | + |
| 121 | + val -= this.knobSize; |
| 122 | + val /= el.measure("width")-(this.knobSize*2); |
| 123 | + val *= valMod; |
| 124 | + val += this.min; |
| 125 | + |
| 126 | + this.values[1] = val; |
| 127 | + this.values[0] = Math.min(this.values[0], val); |
| 128 | + boundUpdateValues(); |
| 129 | + }).bind(this)); |
| 130 | + boundUpdateValues(); |
| 131 | + }).bind(this), 0); |
| 132 | + }, |
| 133 | + |
| 134 | + destroy: function(el) { |
| 135 | + this.bar.remove(); |
| 136 | + this.maxKnob.remove(); |
| 137 | + this.minKnob.remove(); |
| 138 | + |
| 139 | + el.removeClassName("setup"); |
| 140 | + } |
| 141 | + |
| 142 | +}); |
0 commit comments