forked from triffid/WebSkein
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboundvalue.js
74 lines (65 loc) · 1.78 KB
/
boundvalue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// constructor for numeric values bound to input fields
function boundValue(inputElement, defaultValue, min, max, integer) {
var self = this;
this.input = $(inputElement);
this.defaultValue = defaultValue;
this.value = undefined;
this.normalBackground = this.input.style.backgroundColor;
this.lastValidValue = defaultValue;
this.max = max;
this.min = min;
this.integer = integer;
this.input.observe('blur', function(e) {
try {
self.set(this.value);
this.style.backgroundColor = self.normalBackground;
}
catch (e) {
self.normalBackground = this.style.backgroundColor;
this.style.backgroundColor = 'orange';
alert(e);
this.value = self.lastValidValue;
this.focus();
}
});
this.input.observe('change', function(e) {
this.style.backgroundColor = self.normalBackground;
});
this.checkValue = function(newvalue) {
if (!isNaN(newvalue) && isFinite(newvalue)) {
if (!isNaN(self.max) && newvalue > self.max) {
// throw "value too large, " + newvalue + " > " + self.max;
newvalue = self.max;
}
if (!isNaN(self.min) && newvalue < self.min) {
// throw "value too large, " + newvalue + " > " + self.max;
newvalue = self.min;
}
if (integer)
newvalue = parseInt(newvalue);
else
newvalue = parseFloat(newvalue);
return newvalue;
}
else
throw newvalue + " is not numeric!";
}
this.set = function(newvalue) {
newvalue = self.checkValue(newvalue);
self.value = newvalue
self.input.value = newvalue;
self.lastValidValue = newvalue;
};
this.setMin = function(newvalue) {
self.min = self.checkValue(newvalue);
self.set(self.value);
}
this.setMax = function(newvalue) {
self.max = self.checkValue(newvalue);
self.set(self.value);
}
this.toString = function() {
return this.get();
};
this.set(this.defaultValue);
}