-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradiobuttons.js-e
179 lines (158 loc) · 5.02 KB
/
radiobuttons.js-e
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
## Copyright (c) 2013, Empirical Modelling Group
## All rights reserved.
##
## See LICENSE.txt
${{
RadioButtons = function (name, values, labels, x, y, width, horizontal, enabled) {
this.name = name;
this.obsName = root.currentObservableName();
this.values = values;
this.labels = labels;
this.x = x;
this.y = y;
this.width = width;
this.horizontal = horizontal;
this.enabled = enabled;
}
RadioButtons.prototype.hash = function () {
return this.name+"$$"+
(Array.isArray(this.values)? this.values.join("$$") : "") +
(Array.isArray(this.labels)? this.labels.join("$$") : "") +
this.x+"$$"+
this.y+"$$"+
this.width+"$$"+
this.horizontal+"$$"+
this.enabled
};
}}$;
/**
* Generates a set of radio buttons
*
* @param [name] Prefix for _value observable. Default is the name of the observable being defined.
* @param values A list of strings providing the options
* @param [labels]
* @param x X-coordinate
* @param y Y-coordinate
* @param [width] Width in pixels
* @param [is_horizontal
* @param [enabled]] Boolean to determine if the radio buttons are enabled
*/
func RadioButtons { ${{
var argsProcessed;
var name;
if (typeof(arguments[0]) == "string") {
name = arguments[0];
argsProcessed = 1;
} else {
name = root.currentObservableName();
argsProcessed = 0;
if (name === undefined) {
eden.error(new Error("RadioButtons: Every group of radio buttons must have a name."), "error");
return undefined;
}
}
var values = arguments[argsProcessed];
argsProcessed++;
var labels;
if (Array.isArray(arguments[argsProcessed])) {
labels = arguments[argsProcessed];
argsProcessed++;
} else {
labels = values;
}
var x = arguments[argsProcessed];
argsProcessed++;
var y = arguments[argsProcessed];
argsProcessed++;
var width;
if (typeof(arguments[argsProcessed]) == "number" || arguments.length - argsProcessed > 2) {
width = arguments[argsProcessed];
argsProcessed++;
}
var horizontal = arguments[argsProcessed];
argsProcessed++;
var enabled = arguments[argsProcessed];
return new RadioButtons(name, values, labels, x, y, width, horizontal, enabled);
}}$; }
${{
RadioButtons.prototype.makeHTML = function() {
var build = "";
var disabled = this.enabled === false? ' disabled="disabled"' : '';
for (var i = 0; i < this.values.length; i++) {
build = build + '<label><input type="radio" name="' + this.name +
'" value="' + this.values[i] + '"' + disabled + '/> ' +
this.labels[i] + '</label><br/>';
}
if (this.horizontal) {
return '<form class="canvashtml-item canvas-horizontal-radio-buttons">' + build + '</form>';
} else {
return '<form class="canvashtml-item">' + build + '</form>';
}
};
RadioButtons.prototype.draw = function (context) {
if(this.elements === undefined) {
var name = this.name;
//Make the HTML
var formJQ = $(this.makeHTML());
var formElement = formJQ.get(0);
var updateValue = function (buttonGroup, value) {
for (var i = 0; i < buttonGroup.length; i++) {
if (buttonGroup[i].value == value) {
buttonGroup[i].checked = true;
break;
} else {
//Possibility of having no radio button selected.
buttonGroup[i].checked = false;
}
}
};
var valueSym = root.lookup(name + "_value");
var initialValue = valueSym.value();
if (initialValue !== undefined) {
updateValue(formElement.elements, initialValue);
}
valueSym.addJSObserver("updateUI", function (symbol, value) {
updateValue(formElement.elements, value);
});
formJQ.change(function(event) {
root.lookup(name + "_value").assign(event.target.value, root.scope, EdenSymbol.hciAgent, true);
})
.on("mousedown", function () {
var mouseFollow = root.lookup("mouseFollow").value();
root.lookup("mouseDownZone").assign(undefined, root.scope, EdenSymbol.hciAgent, mouseFollow);
})
.on("mouseup", function () {
edenUI.plugins.Canvas2D.endClick();
})
.on("mouseenter", function () {
var mouseFollow = root.lookup("mouseFollow").value();
root.lookup("mouseZone").assign(name, root.scope, EdenSymbol.hciAgent, mouseFollow);
});
this.elements = [formElement];
}
};
RadioButtons.prototype.scale = function (scale, zoom, origin) {
var elem = this.elements[0];
var style = elem.style;
style.left = Math.round((this.x + origin.x) * scale) + "px";
style.top = Math.round((this.y + origin.y) * scale) + "px";
if (this.width === undefined) {
style.width = "";
} else {
style.width = Math.round(this.width * scale) + "px";
}
style.fontSize = zoom + "em";
var buttonSize = Math.round(13 * zoom) + "px";
$(elem).find("input").css({width: buttonSize, height: buttonSize});
};
RadioButtons.prototype.toString = function() {
if (this.name == this.obsName) {
return "RadioButtons(" + Eden.edenCodeForValues(this.values, this.labels, this.x,
this.y, this.width, this.horizontal, this.enabled) + ")";
} else {
return "RadioButtons(" + Eden.edenCodeForValues(this.name, this.values, this.labels, this.x,
this.y, this.width, this.horizontal, this.enabled) + ")";
}
};
RadioButtons.prototype.getEdenCode = RadioButtons.prototype.toString;
}}$;