-
Notifications
You must be signed in to change notification settings - Fork 268
/
dataTextarea.js
76 lines (61 loc) · 1.67 KB
/
dataTextarea.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
75
76
/**
Template Controllers
@module Templates
*/
/**
The data textarea template.
@class [template] dapp_dataTextarea
@constructor
*/
Template["dapp_dataTextarea"].onCreated(function() {
// default set to true, to show no error
TemplateVar.set("isValid", true);
if (this.data && this.data.value) {
TemplateVar.set("value", this.data.value);
}
});
Template["dapp_dataTextarea"].onRendered(function() {
if (this.data && this.data.value) {
this.$("textarea").trigger("change");
}
});
Template["dapp_dataTextarea"].helpers({
/**
Return the autofocus or disabled attribute.
@method (additionalAttributes)
*/
additionalAttributes: function() {
var attr = {};
if (this.autofocus) attr.autofocus = true;
if (this.disabled) attr.disabled = true;
return attr;
}
});
Template["dapp_dataTextarea"].events({
/**
Set the value while typing
@event input textarea, change textarea
*/
"input textarea, change textarea": function(e, template) {
var value = e.currentTarget.value.replace(/\s+/g, "");
// remove multiline
if (value.indexOf("\n") !== -1) {
value = value.replace("\n", "");
e.currentTarget.value = value;
}
// add 0x
if (value.length > 2 && value.indexOf("0x") === -1) {
value = "0x" + value;
e.currentTarget.value = value;
}
if (/^(0x)?[a-f0-9]*$/i.test(value) || _.isEmpty(value)) {
TemplateVar.set("isValid", true);
if (!_.isEmpty(value))
TemplateVar.set("value", "0x" + value.replace("0x", ""));
else TemplateVar.set("value", undefined);
} else {
TemplateVar.set("isValid", false);
TemplateVar.set("value", undefined);
}
}
});