/** * A Slider Control */ /** * Constructor */ function Slider(min, max, defaultValue, step, left, top, width, container, dir) { var that = this; //Build containing DIV var d0 = document.createElement("DIV"); d0.style.left = left; d0.style.top = top; container.appendChild(d0); //Build horizontal line var LINE_TOP = 10; var TOP_COLOR = "#808080", CENTER_COLOR = "#404040", BOTTOM_COLOR = "#D4D0C8"; var t0 = document.createElement("TABLE"); t0.width = width; t0.cellSpacing = "0"; t0.cellPadding = "0"; t0.style.position = "relative"; t0.style.top = "10px"; var tr0 = t0.insertRow(0); tr0.style.height = "1px"; var td00 = tr0.insertCell(0); td00.colSpan = "3"; td00.style.backgroundColor = TOP_COLOR; var tr1 = t0.insertRow(1); tr1.style.height = "1px"; var td10 = tr1.insertCell(0); td10.style.width = "1px"; td10.style.backgroundColor = TOP_COLOR; var td11 = tr1.insertCell(1); td11.style.width = width - 2; td11.style.backgroundColor = CENTER_COLOR; var td12 = tr1.insertCell(2); td12.style.width = "1px"; td12.style.backgroundColor = BOTTOM_COLOR; var tr2 = t0.insertRow(2); tr2.style.height = "1px"; var td20 = tr2.insertCell(0); td20.style.width = "1px"; td20.style.backgroundColor = TOP_COLOR; var td21 = tr2.insertCell(1); td21.colSpan = "2"; td21.style.width = width - 1; td21.style.backgroundColor = BOTTOM_COLOR; d0.appendChild(t0); //Build vertical lines var MARGINX = 5; //d0.appendChild(document.createElement("BR")); var t1 = document.createElement("TABLE"); d0.appendChild(t1); t1.style.position = "relative"; t1.style.top = "20px"; t1.style.width = width + "px"; t1.cellSpacing = "0"; t1.cellPadding = "0"; t1.style.borderWidth = "0px"; var trx1 = t1.insertRow(0); trx1.style.height = "3px"; var trx2 = t1.insertRow(1); trx2.style.height = "1px"; var tdx11 = trx1.insertCell(0); tdx11.rowSpan = "2"; tdx11.style.width = MARGINX + "px"; var tdx12 = trx1.insertCell(1); tdx12.rowSpan = "2"; tdx12.style.backgroundColor = "#000000"; tdx12.style.width = "1px"; var cellid = 2; var cellspacer = (width - 3*MARGINX - ((max - min) / step)) / ((max - min) / step - 1); for (var i = min + step; i < max; i += step) { var tdxi0 = trx1.insertCell(cellid); tdxi0.style.width = cellspacer + "px"; cellid++; var tdxi1 = trx1.insertCell(cellid); cellid++; tdxi1.style.width = "1px"; tdxi1.style.backgroundColor = "#000000"; } var tdx13 = trx1.insertCell(cellid); tdx13.style.width = cellspacer + "px"; var tdx14 = trx1.insertCell(cellid+1); tdx14.rowSpan = "2"; tdx14.style.backgroundColor = "#000000"; tdx14.style.width = "1px"; var tdx15 = trx1.insertCell(cellid+2); tdx15.rowSpan = "2"; tdx15.style.width = MARGINX + "px"; //Build sliding unit var slider = document.createElement("IMG"); slider.src = "primitives/slider.gif"; slider.style.position = "relative"; slider.style.borderWidth = "0px"; slider.style.top = "-7px"; d0.appendChild(slider); var clickX, clickY, dragging; /** * Event handler for mouse down */ document.onmousedown = function(e) { if (!that.enabled) return; if (!e) e = window.event; var target = e.srcElement; if (!target) target = e.target; if (e && target == slider) { clickX = e.mouseX; clickY = e.mouseY; dragging = true; d0.style.cursor = slider.style.cursor = "move"; } } /** * Event handler for mouse move */ document.onmousemove = function(e) { if (!that.enabled || !dragging) return; if (!e) e = window.event; if (e && (e.which || e.button) == 1) { var d0left = getAbsLeft(d0); var d0top = getAbsTop(d0); if (e.clientX >= d0left && e.clientX <= d0left + width && e.clientY >= d0top && e.clientY <= d0top + 25) { var value = Math.round((max - min) * (e.clientX - d0left) / width) + min; that.setValue(value); } return false; } else { document.onmouseup(); } } /** * Event handler for mouse up */ document.onmouseup = function(e) { if (!that.enabled) return; if (!e) e = window.event; if (e && (e.which || e.button) == 1) { var d0left = getAbsLeft(d0); var d0top = getAbsTop(d0); if (e.clientX >= d0left && e.clientX <= d0left + width && e.clientY >= d0top && e.clientY <= d0top + 25) { var value = Math.round((max - min) * (e.clientX - d0left) / width) + min; that.setValue(value); } } dragging = false; d0.style.cursor = slider.style.cursor = ""; } var thisValue = 0; /** * Sets the value of the slider and moves it to the correct position */ this.setValue = function(value) { //if (dir == "rtl") value = max - value; if (value < min || value > max) return false; value = value - min; slider.style.left = (value) * ((width - slider.width) / (max - min)); thisValue = value + min; return true; } /** * Returns the value of this slider */ this.getValue = function() { if (dir == "rtl") return max - thisValue + min; else return thisValue; } this.enabled = true; //Opens a thread that will set default value, hopefully after the slider has been loaded setTimeout(function() { that.setValue(defaultValue); }, 100); setTimeout(function() { that.setValue(defaultValue); }, 1000); }