-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropdownlist.js-e
160 lines (134 loc) · 4.37 KB
/
dropdownlist.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
## Copyright (c) 2013, Empirical Modelling Group
## All rights reserved.
##
## See LICENSE.txt
${{
DropDownList = function (name, values, labels, x, y, enabled) {
this.name = name;
this.obsName = root.currentObservableName();
this.values = values;
this.labels = labels;
this.x = x;
this.y = y;
this.enabled = enabled;
}
DropDownList.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.enabled;
};
}}$;
/**
* Creates a DropDown list, allowing for the selection
* @param [name] Prefix for _value observable. Default is the name of the observable being defined.
* @param values A list of options
* @param [labels] A list of labels to display, if not supplied, the list of values will be used
* @param x X-coordinate
* @param y Y-coordinate
* @param [enabled] Boolean to determine if list selection is enabled
* #canvas #dropdown #input #select
*/
func DropDownList { ${{
var argsProcesed;
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("DropDownList: Every DropDownList 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 enabled = arguments[argsProcessed];
return new DropDownList(name, values, labels, x, y, enabled);
}}$; }
${{
DropDownList.prototype.makeOptionsHTML = function() {
var html = "";
for (var i = 0; i < this.values.length; i++) {
html = html + '\n<option value="' + this.values[i] + '">' + this.labels[i] + '</option>';
}
return html;
}
DropDownList.prototype.draw = function(context) {
var dropDownList;
var me = this;
var name = this.name;
var valueSym = root.lookup(name + '_value');
if (this.elements === undefined) {
var disabled = this.enabled === false? 'disabled="disabled"' : '';
var dropDownListJQ = $('<select ' + disabled + ' class="canvashtml-item"></select>');
dropDownListJQ.html(this.makeOptionsHTML(this.values));
dropDownList = dropDownListJQ.get(0);
var initialValue = valueSym.value();
if (initialValue === undefined) {
valueSym.assign(me.values[0], root.scope, root.lookup("DropDownList"), true);
} else {
dropDownList.value = initialValue;
}
valueSym.addJSObserver("updateDropDownList", function (symbol, value) {
dropDownList.value = value;
});
dropDownListJQ.change(function(event) {
valueSym.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 = [dropDownList];
} else {
//Case when the user has performed direct assignment to one or more JavaScript properties.
dropDownList = this.elements[0];
$(dropDownList).html(this.makeOptionsHTML());
dropDownList.value = valueSym.value();
if (this.enabled === false) {
dropDownList.disabled = true;
} else {
dropDownList.disabled = false;
}
}
};
DropDownList.prototype.scale = function (scale, zoom, origin) {
var style = this.elements[0].style;
style.left = Math.round((this.x + origin.x) * scale) + "px";
style.top = Math.round((this.y + origin.y) * scale) + "px";
style.fontSize = zoom + "em";
};
DropDownList.prototype.toString = function() {
if (this.name == this.obsName) {
return "DropDownList(" + Eden.edenCodeForValues(this.values, this.labels, this.x,
this.y, this.enabled) + ")";
} else {
return "DropDownList(" + Eden.edenCodeForValues(this.name, this.values, this.labels, this.x,
this.y, this.enabled) + ")";
}
};
DropDownList.prototype.getEdenCode = DropDownList.prototype.toString;
}}$;