-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathselectInputBox.js
119 lines (113 loc) · 2.91 KB
/
selectInputBox.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
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
;(function() {
function SelectInputBox(opts){
var that = this;
var data = opts.data;
var matchedData = _.first(data, SelectInputBox.SELECT_ITEM_MAX_IN_VIEW);
var dataList = Ti.UI.createView({
width: Ti.UI.FILL,
height: Ti.UI.SIZE,
layout: "vertical"
});
var textField = Ti.UI.createTextField({
width : Ti.UI.FILL,
height : 29,
paddingLeft: "5px",
textAlign : 'left',
font : {
fontSize : "13dp"
},
color : "#333",
verticalAlign : Ti.UI.TEXT_VERTICAL_ALIGNMENT_CENTER
});
var textFieldSeparator = Ti.UI.createView({
height : 1,
left : 1,
right : 1,
borderWidth : 1,
borderColor : '#ddd'
});
var textFieldView = Ti.UI.createView({
width : Ti.UI.FILL,
height : 30,
layout: "vertical"
});
var selectInputBox = Ti.UI.createView({
top: 0,
width: Ti.UI.FILL,
height: Ti.UI.SIZE,
layout: "vertical"
});
// Bind requested event
textField.addEventListener("focus", function(){
that.renderDataList(matchedData);
selectInputBox.add(dataList);
});
textField.addEventListener("blur", function(){
selectInputBox.remove(dataList);
});
textField.addEventListener("change", function(e){
matchedData = [];
if(e.value.length !== 0){
var regex = new RegExp('^' + e.value);
for(var i = 0, l = data.length; i < l; i++){
if(regex.test(data[i])){
matchedData.push(data[i]);
}
if(matchedData.length >= SelectInputBox.SELECT_ITEM_MAX_IN_VIEW) break;
}
}
else{
console.log("here!!")
matchedData = _.first(data, SelectInputBox.SELECT_ITEM_MAX_IN_VIEW);
}
that.renderDataList(matchedData);
});
textFieldView.add(textField);
textFieldView.add(textFieldSeparator);
selectInputBox.add(textFieldView);
this.data = data;
this.dataList = dataList;
this.textField = textField;
this.selectInputBox = selectInputBox;
return this;
}
SelectInputBox.SELECT_ITEM_HEIGHT = '44dp';
SelectInputBox.SELECT_ITEM_MAX_IN_VIEW = 8;
SelectInputBox.prototype.getView = function(){
return this.selectInputBox;
};
SelectInputBox.prototype.getValue = function(){
this.textField.getValue();
};
SelectInputBox.prototype.blur = function(){
this.textField.blur();
};
SelectInputBox.prototype.renderDataList = function(data){
var that = this;
that.dataList.removeAllChildren();
_.each(data, function(content){
var label = Ti.UI.createLabel({
left: "5px",
textAlign : 'left',
font : {
fontSize : "13dp"
},
color : "#333",
text: content
});
var view = Ti.UI.createView({
width: Ti.UI.FILL,
height: SelectInputBox.SELECT_ITEM_HEIGHT,
borderWidth: "1px",
borderColor: "#ccc"
});
view.addEventListener("singletap", function(){
that.textField.setValue(content);
that.textField.blur();
});
view.add(label);
that.dataList.add(view);
});
};
module.exports = SelectInputBox;
})();