-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.htm
167 lines (132 loc) · 4.26 KB
/
test.htm
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
<html>
<head>
<script type="text/javascript" src="yui-min.js"></script>
<script type="text/javascript">
YUI().use('widget', 'node', 'event-key', function (Y) {
var Lang = Y.Lang,
Widget = Y.Widget,
Node = Y.Node;
function SmartCombo(config) {
SmartCombo.superclass.constructor.apply(this, arguments);
this.currentFilter = '';
this.data = config.data;
}
SmartCombo.NAME = "smartcombo" ;
SmartCombo.INPUT_CLASS = Y.ClassNameManager.getClassName(SmartCombo.NAME, "searchBox");
SmartCombo.RESULT_CONTAINER_CLASS = Y.ClassNameManager.getClassName(SmartCombo.NAME, "resultBox");
SmartCombo.INPUT_TEMPLATE = '<input type="text" class="' + SmartCombo.INPUT_CLASS + '">';
SmartCombo.RESULT_CONTAINER_TEMPLATE = '<div class="' + SmartCombo.RESULT_CONTAINER_CLASS + '"><ol><li>default</li> </ol> </div>';
Y.extend(SmartCombo, Widget,
{
/*
* initializer is part of the lifecycle introduced by
* the Widget class. It is invoked during construction,
* and can be used to setup instance specific state.
*
*/
initializer: function() {
// Not doing anything special during initialization
},
/*
* destructor is part of the lifecycle introduced by
* the Widget class. It is invoked during destruction,
* and can be used to cleanup instance specific state.
*
*/
destructor : function() {
},
/*
* renderUI is part of the lifecycle introduced by the
* Widget class. Widget's renderer method invokes:
*
* renderUI()
* bindUI()
* syncUI()
*
* renderUI is intended to be used by the Widget subclass
* to create or insert new elements into the DOM.
*
*/
renderUI : function() {
var contentBox = this.get('contentBox'),
input = contentBox.one("." + SmartCombo.INPUT_CLASS),
resultdiv = contentBox.one("." + SmartCombo.RESULT_CONTAINER_TEMPLATE);
if (!input) {
input = Node.create(SmartCombo.INPUT_TEMPLATE);
contentBox.appendChild(input);
}
if (!resultdiv) {
resultdiv = Node.create(SmartCombo.RESULT_CONTAINER_TEMPLATE);
contentBox.appendChild(resultdiv);
}
this.searchBox = input;
this.resultBox = resultdiv;
},
bindUI : function() {
Y.on('click', this._handleClick, this.resultBox, this );
Y.on('keyup', this._handleTyping, this.searchBox, '', this);
},
syncUI : function() {
this._renderItens();
},
_getHtml: function() {
var i = 0, buffer = [];
buffer[buffer.length] = '<ol>';
for (var i = 0, k = this.data.length; i < k; i += 1) {
//TODO: if should be before the iteration for performance
if (this.data[i].label.indexOf(this.currentFilter) > -1 || '' == this.currentFilter) {
buffer[buffer.length] = '<li id=' + i + '>';
if (this.data[i].checked != 0) {
buffer[buffer.length] = '> ';
} else {
buffer[buffer.length] = '_ ';
}
buffer[buffer.length] = this.data[i].label
buffer[buffer.length] = '</li>';
}
}
buffer[buffer.length] = '</ol>';
return buffer.join('');
},
_handleClick: function (o) {
var item;
item = this._findItem(o);
item.checked = !item.checked;
this._renderItens();
},
_renderItens: function() {
Y.one(this.resultBox).set('innerHTML', this._getHtml());
},
_findItem: function (o) {
var i;
//TODO: plese check this. I think there is a better way to bind JSON and DOM
i = o.target.get('id');
return this.data[i];
},
_handleTyping: function(o) {
//TODO: WTF 'this' is comming wrong????
arguments[1].currentFilter = o.target.get('value');
arguments[1]._renderItens();
}
});
var myData = [
{
label: 'item 1',
checked: 1,
value: 1
},
{
label: 'xxx 1',
checked: 1,
value: 2
}
];
var mySmartCombo = new SmartCombo({contextBox: '#coreControl', data: myData });
mySmartCombo.render();
});
</script>
</head>
<body>
<div id="coreControl"></div>
</body>
</html>