-
Notifications
You must be signed in to change notification settings - Fork 0
/
textbox.js-e
183 lines (163 loc) · 5.24 KB
/
textbox.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
180
181
182
183
## Copyright (c) 2013, Empirical Modelling Group
## All rights reserved.
##
## See LICENSE.txt
${{
Textbox = function (name, x, y, width, height, placeholder, maxlength, enabled) {
this.name = name;
this.obsName = root.currentObservableName();
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.placeholder = placeholder;
this.maxlength = maxlength;
this.enabled = enabled;
}
Textbox.prototype.hash = function () {
return this.name+"$$"+
this.x+"$$"+
this.y+"$$"+
this.width+"$$"+
this.height+"$$"+
this.placeholder+"$$"+
this.maxlength+"$$"+
this.enabled;
};
}}$;
/**
* Creates a text input box for drawing on the canvas
*
* @param [name] Prefix for _value observable. Default is the name of the observable being defined.
* @param x X-position
* @param y Y-position
* @param [width
* @param [height]]
* @param [placeholder_text] Text to be displayed by default
* @param [max_characters] Maximum number of characters allowed in input box
* @param [enabled] Boolean (true or false) to enable or disable input
*
* #canvas #input #text #textbox
*/
func Textbox { ${{
var argsProcesssed;
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("Textbox: Every text box must have a name."), "error");
return undefined;
}
}
var x = arguments[argsProcessed];
argsProcessed++;
var y = arguments[argsProcessed];
argsProcessed++;
var width, height, placeholder, maxlength, enabled;
var arg = arguments[argsProcessed];
var argType = typeof(arg);
if (arg === undefined || argType == "number") {
width = arguments[argsProcessed];
argsProcessed++;
arg = arguments[argsProcessed];
argType = typeof(arg);
}
if (arg === undefined || argType == "number") {
height = arg;
argsProcessed++;
arg = arguments[argsProcessed];
argType = typeof(arg);
}
if (arg === undefined || argType == "string") {
placeholder = arg;
argsProcessed++;
arg = arguments[argsProcessed];
argType = typeof(arg);
}
if (arg === undefined || argType == "number") {
maxlength = arg;
argsProcessed++;
arg = arguments[argsProcessed];
}
enabled = arg;
return new Textbox(name, x, y, width, height, placeholder, maxlength, enabled);
}}$; }
${{
Textbox.prototype.draw = function(context) {
if(this.elements === undefined) {
var me = this;
var name = this.name;
var disabled = this.enabled === false? 'disabled="disabled"' : '';
var placeholder;
var maxlength = this.maxlength !== undefined? ' maxlength="' + this.maxlength + '"' : '';
var jQuery;
if (this.height === undefined) {
placeholder = ' placeholder="' +
(this.placeholder === undefined? name : this.placeholder) + '"';
jQuery = $('<input type="text" ' + disabled + placeholder + maxlength + ' class="canvashtml-item" />');
} else {
// Placeholder text for textarea tag is buggy in IE11, so don't use it by default.
placeholder = this.placeholder !== undefined && this.placeholder != ""?
' placeholder="' + this.placeholder + '"' :
'';
jQuery = $('<textarea ' + disabled + placeholder + maxlength + ' class="canvashtml-item"></textarea>');
}
var element = jQuery.get(0);
var valueSymbol = root.lookup(name + "_value");
var value = valueSymbol.value();
if (value === undefined) {
valueSymbol.assign("", root.scope, root.lookup("Textbox"));
} else {
element.value = value;
}
valueSymbol.addJSObserver("updateTextbox", function (obs, value) {
me.elements[0].value = value;
});
jQuery
.on("input", 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 = [element];
}
};
Textbox.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.width = Math.round(this.width * scale - 6) + "px";
if (this.height !== undefined) {
var lineHeight = edenUI.plugins.Canvas2D.defaultLineHeight;
var rows = Math.floor(Math.round(this.height * scale - 4) / (lineHeight * zoom)); //See css/eden.css
if (rows == 0) {
rows = 1;
}
style.height = (rows * lineHeight * zoom) + "px";
}
style.fontSize = (edenUI.plugins.Canvas2D.defaultFontSizePx * zoom) + "px";
};
Textbox.prototype.toString = function() {
if (this.name == this.obsName) {
return "Textbox(" + Eden.edenCodeForValues(this.x, this.y, this.width,
this.height, this.placeholder, this.enabled) + ")";
} else {
return "Textbox(" + Eden.edenCodeForValues(this.name, this.x, this.y, this.width,
this.height, this.placeholder, this.enabled) + ")";
}
};
Textbox.prototype.getEdenCode = Textbox.prototype.toString;
}}$;