var global = this; var widgets = global.widgets = new function() { function debug(msg) { if (window.console && console.log) { console.log(msg); } else { if (!window.debugmessages) { window.debugmessages = []; }; window.debugmessages.push(msg); }; }; var add_event_handler = function add_event_handler( el, evtname, handler, context, preventdefault) { /* register an event handler for event evtname on el use handler as the event handler, the handler is called with the TimeWidget instance as 'this' */ var args = []; for (var i=4; i < arguments.length; i++) { args.push(arguments[i]); }; var _wrapped_handler = function _wrapped_handler(e) { var currargs = args.concat(arguments); handler.apply(context, currargs); if (preventdefault) { if (e.preventDefault) { e.preventDefault(); }; if (e.stopPropagation) { e.stopPropagation(); }; e.cancelBubble = true; e.returnValue = false; }; }; if (el.addEventListener) { el.addEventListener(evtname, _wrapped_handler, false); } else if (el.attachEvent) { el.attachEvent('on' + evtname, _wrapped_handler); } else { debug('error attaching event handler - unrecognized browser'); }; return _wrapped_handler; }; var remove_event_handler = function remove_event_handler(el, evtname, handler) { if (el.removeEventListener) { el.removeEventListener(evtname, handler, false); } else if (el.detachEvent) { el.detachEvent('on' + evtname, handler); }; }; var schedule = function schedule(handler, msecs) { var id = this._last_handler_id ? this._last_handler_id + 1 : 0; this._last_handler_id = id; var args = []; for (var i=2; i < arguments.length; i++) { args.push(arguments[i]); }; var self = this; var wrappedhandler = window['_scheduled_' + id] = function() { handler.apply(self, args); }; return window.setTimeout('window._scheduled_' + id + '()', msecs); }; function TimeWidget(widgetinput) { if (arguments.length) { if (typeof widgetinput == 'string') { widgetinput = document.getElementById(widgetinput); }; this._initialize(widgetinput); }; }; this.TimeWidget = TimeWidget; TimeWidget.prototype._initialize = function _initialize(input) { this.orginput = input; var value = input.value; if (!/^\d{1,2}\:\d{2}$/.exec(value)) { debug('unexpected time value ("' + value + '") on widget ' + 'initialization'); value = '0:00'; }; this._submit_handler = add_event_handler( input.form, 'submit', this.deinit, this); this._unload_handler = add_event_handler( window, 'unload', this.deinit, this); this._build_widget(input, value); }; TimeWidget.prototype.deinit = function deinit() { /* deinitialize for form submit and page reload serialize the value, write to the (now hiddden) text input and remove the widget */ remove_event_handler( this.orginput.form, 'submit', this._submit_handler); remove_event_handler( window, 'unload', this._unload_handler); this.widgetdiv.parentNode.removeChild(this.widgetdiv); var value = this.get_value(); this.orginput.value = value; this.orginput.style.display = 'inline'; }; TimeWidget.prototype.get_value = function get_value() { var value = this.timeinput.value; if (!/^\d{1,2}\:\d{2}$/.exec(value)) { debug('incorrect value in form field: ' + value); value = '0:00'; }; return value; }; TimeWidget.prototype._change_value = function _change_value(intensity) { var value = this.get_value(); var hourminute = value.split(':'); var hour = parseInt(hourminute[0]); var minute = parseInt(hourminute[1], 10); var d = new Date(1970, 1, 2, hour, minute); var hourplus = 0; var minuteplus = 0; switch (intensity) { case -2: hourplus = -1; break; case -1: minuteplus = -1; break; case 1: minuteplus = 1; break; case 2: hourplus = 1; break; }; var toadd = (hourplus * 60 * 60000) + (minuteplus * 60000); var d2 = new Date(d.getTime() + toadd); var hours = d2.getHours().toString(); var minutes = d2.getMinutes().toString(); if (minutes.length == 1) { minutes = '0' + minutes; }; var newvalue = hours + ':' + minutes; this.timeinput.value = newvalue; }; TimeWidget.prototype._build_widget = function _build_widget(placeholder, value) { placeholder.style.display = 'none'; var doc = placeholder.ownerDocument; var div = this.widgetdiv = doc.createElement('div'); div.className = 'timewidget'; var timeinput = this.timeinput = doc.createElement('input'); timeinput.value = value; timeinput.type = 'text'; timeinput.size = 5; timeinput.maxlength = 5; //timeinput.disabled = 'disabled'; div.appendChild(timeinput); var self = this; this.updowncontrol = new UpDownControl( div, timeinput, function(x) {self._change_value(x)}); placeholder.parentNode.insertBefore(div, placeholder); }; function FloatWidget(widgetinput, stepsize, precision, disallownegative) { if (arguments.length) { if (typeof widgetinput == 'string') { widgetinput = document.getElementById(widgetinput); }; this._initialize( widgetinput, stepsize, precision, disallownegative); }; }; this.FloatWidget = FloatWidget; FloatWidget.prototype._initialize = function _initialize(input, stepsize, precision, disallownegative) { this.orginput = input; this.stepsize = stepsize || 0.1; this.precision = precision || 1; this.disallownegative = !!disallownegative; var value = input.value; if (!/^[-]?\d+(\.\d+)?$/.exec(value)) { debug('unexpected float value ("' + value + '") on widget ' + 'initialization'); value = 0.0; } else { value = parseFloat(value); }; this._submit_handler = add_event_handler( input.form, 'submit', this.deinit, this); this._unload_handler = add_event_handler( window, 'unload', this.deinit, this); this._build_widget(input, value); }; FloatWidget.prototype.deinit = function deinit() { /* deinitialize for form submit and page reload serialize the value, write to the (now hiddden) text input and remove the widget */ remove_event_handler( this.orginput.form, 'submit', this._submit_handler); remove_event_handler( window, 'unload', this._unload_handler); this.widgetdiv.parentNode.removeChild(this.widgetdiv); var value = this.get_value(); this.orginput.value = value; this.orginput.style.display = 'inline'; }; FloatWidget.prototype.get_value = function get_value() { var value = this.floatinput.value; if (!/^[-]?\d+(\.\d+)?$/.exec(value)) { debug('incorrect value in form field: ' + value); value = 0.0; } else { value = parseFloat(value); }; if (this.disallownegative && value < 0) { value = 0.0; }; return value; }; FloatWidget.prototype._change_value = function _change_value(intensity) { var value = this.get_value(); var toadd; switch (intensity) { case -2: toadd = -1; break; case -1: toadd = -this.stepsize; break; case 1: toadd = this.stepsize; break; case 2: toadd = 1; break; }; var newvalue = value + toadd; if (this.disallownegative && newvalue < 0) { newvalue = 0.0; }; this.floatinput.value = newvalue.toFixed(this.precision); }; FloatWidget.prototype._build_widget = function _build_widget(placeholder, value) { placeholder.style.display = 'none'; var doc = placeholder.ownerDocument; var div = this.widgetdiv = doc.createElement('div'); div.className = 'floatwidget'; var floatinput = this.floatinput = doc.createElement('input'); alert(value); floatinput.value = value; floatinput.type = 'text'; floatinput.size = 5; floatinput.maxlength = 5; //timeinput.disabled = 'disabled'; div.appendChild(floatinput); var self = this; this.updowncontrol = new UpDownControl( div, floatinput, function(x) {self._change_value(x)}); placeholder.parentNode.insertBefore(div, placeholder); }; function UpDownControl() { if (arguments.length) { this._initialize.apply(this, arguments); }; }; this.UpDownControl = UpDownControl; UpDownControl.prototype._initialize = function _initialize(placeholder, input, handler) { this.placeholder = placeholder; this.input = input; this.handler = handler; var doc = placeholder.ownerDocument; var plusicon = doc.createElement('a'); plusicon.className = 'up'; plusicon.appendChild(doc.createTextNode('\xa0')); add_event_handler( plusicon, 'mousedown', this.plus_down, this, true); add_event_handler( plusicon, 'mouseup', this.cancel, this, true); add_event_handler( plusicon, 'mouseout', this.cancel, this, true); placeholder.appendChild(plusicon); var minusicon = doc.createElement('a'); minusicon.className = 'down'; minusicon.appendChild(doc.createTextNode('\xa0')); add_event_handler( minusicon, 'mousedown', this.minus_down, this, true); add_event_handler( minusicon, 'mouseup', this.cancel, this, true); add_event_handler( minusicon, 'mouseout', this.cancel, this, true); placeholder.appendChild(minusicon); }; UpDownControl.prototype.minus_down = function minus_down() { this._start_loop(-1); }; UpDownControl.prototype.plus_down = function plus_down() { this._start_loop(1); }; UpDownControl.prototype._start_loop = function _start_loop(direction) { var i = 0; var self = this; var handler = function() { var intensity = i < 15 ? 1 : 2; self.handler(direction * intensity); var interval = (i < 3) ? 400 : 200; self._scheduled = schedule(handler, interval); i++; }; handler(); }; UpDownControl.prototype.cancel = function cancel() { if (this._scheduled) { window.clearTimeout(this._scheduled); this._scheduled = null; }; }; }();