@@ -54,44 +54,221 @@ define('select2/utils',[], function () {
54
54
return Utils ;
55
55
} ) ;
56
56
57
- define ( 'select2/adapters /select' , [
58
- " ../utils"
57
+ define ( 'select2/data /select' , [
58
+ ' ../utils'
59
59
] , function ( Utils ) {
60
- function SelectAdapter ( element , options ) {
61
- this . element = element ;
60
+ function SelectAdapter ( $ element, options ) {
61
+ this . $ element = $ element;
62
62
}
63
63
64
64
Utils . Extend ( SelectAdapter , Utils . Observable ) ;
65
65
66
+ SelectAdapter . prototype . current = function ( ) {
67
+ var data = [ ] ;
68
+ var self = this ;
69
+
70
+ this . $element . find ( ":selected" ) . each ( function ( ) {
71
+ var $option = $ ( this ) ;
72
+
73
+ var option = self . item ( $option ) ;
74
+
75
+ data . push ( option ) ;
76
+ } ) ;
77
+
78
+ return data ;
79
+ } ;
80
+
81
+ SelectAdapter . prototype . item = function ( $option ) {
82
+ var data = {
83
+ id : $option . val ( ) ,
84
+ text : $option . html ( )
85
+ } ;
86
+
87
+ return data ;
88
+ } ;
89
+
66
90
return SelectAdapter ;
67
91
} ) ;
68
92
93
+ define ( 'select2/results' , [
94
+ './utils'
95
+ ] , function ( Utils ) {
96
+ function Results ( $element , dataAdapter ) {
97
+ this . $element = $element ;
98
+ this . dataAdapter = dataAdapter ;
99
+ }
100
+
101
+ Utils . Extend ( Results , Utils . Observable ) ;
102
+
103
+ Results . prototype . render = function ( ) {
104
+ var $results = $ (
105
+ '<ul class="options"></ul>'
106
+ ) ;
107
+
108
+ return $results ;
109
+ }
110
+
111
+ return Results ;
112
+ } )
113
+ ;
114
+ define ( 'select2/dropdown' , [
115
+ './utils'
116
+ ] , function ( Utils ) {
117
+ function Dropdown ( $element , options ) {
118
+ this . $element = $element ;
119
+ }
120
+
121
+ Utils . Extend ( Dropdown , Utils . Observable ) ;
122
+
123
+ Dropdown . prototype . render = function ( ) {
124
+ var $dropdown = $ (
125
+ '<div class="select2 select2-dropdown">' +
126
+ '<div class="results"></div>' +
127
+ '</div>'
128
+ ) ;
129
+
130
+ return $dropdown ;
131
+ }
132
+
133
+ return Dropdown ;
134
+ } )
135
+ ;
136
+ define ( 'select2/selection' , [
137
+ './utils'
138
+ ] , function ( Utils ) {
139
+ function Selection ( $element , options ) {
140
+ this . $element = $element ;
141
+ this . options = options ;
142
+ }
143
+
144
+ Utils . Extend ( Selection , Utils . Observable ) ;
145
+
146
+ Selection . prototype . render = function ( ) {
147
+ var $selection = $ (
148
+ '<div class="single-select">' +
149
+ '<div class="rendered-selection"></div>' +
150
+ '</div>'
151
+ ) ;
152
+
153
+ this . $selection = $selection ;
154
+
155
+ return $selection ;
156
+ }
157
+
158
+ Selection . prototype . bind = function ( $container ) {
159
+ var self = this ;
160
+
161
+ $container . on ( 'click' , function ( evt ) {
162
+ self . trigger ( "toggle" , {
163
+ originalEvent : evt
164
+ } ) ;
165
+ } ) ;
166
+ }
167
+
168
+ Selection . prototype . clear = function ( ) {
169
+ this . $selection . find ( ".rendered-selection" ) . text ( "" ) ;
170
+ }
171
+
172
+ Selection . prototype . display = function ( data ) {
173
+ return data . text ;
174
+ }
175
+
176
+ Selection . prototype . update = function ( data ) {
177
+ if ( data . length == 0 ) {
178
+ this . clear ( ) ;
179
+ return ;
180
+ }
181
+
182
+ var selection = data [ 0 ] ;
183
+
184
+ var formatted = this . display ( selection ) ;
185
+
186
+ this . $selection . find ( ".rendered-selection" ) . html ( formatted ) ;
187
+ }
188
+
189
+ return Selection ;
190
+ } ) ;
191
+
69
192
define ( 'select2/options' , [
70
- "./adapters/select"
71
- ] , function ( SelectAdapter ) {
193
+ './data/select' ,
194
+ './results' ,
195
+ './dropdown' ,
196
+ './selection'
197
+ ] , function ( SelectData , ResultsList , Dropdown , Selection ) {
72
198
function Options ( options ) {
73
199
this . options = options ;
74
200
75
- this . DataAdapter = SelectAdapter ;
201
+ this . dataAdapter = SelectData ;
202
+ this . resultsAdapter = ResultsList ;
203
+ this . dropdownAdapter = Dropdown ;
204
+ this . selectionAdapter = Selection ;
76
205
}
77
206
78
207
return Options ;
79
208
} )
80
209
;
81
210
define ( 'select2/core' , [
82
- " jquery" ,
83
- " ./options" ,
84
- " ./utils"
211
+ ' jquery' ,
212
+ ' ./options' ,
213
+ ' ./utils'
85
214
] , function ( $ , Options , Utils ) {
86
- var Select2 = function ( element , options ) {
87
- this . element = element ;
215
+ var Select2 = function ( $ element, options ) {
216
+ this . $ element = $ element;
88
217
this . options = new Options ( options ) ;
89
218
90
- this . adapter = new this . options . DataAdapter ( element , options ) ;
219
+ // Set up containers and adapters
220
+
221
+ this . data = new this . options . dataAdapter ( $element , options ) ;
222
+
223
+ var $container = this . render ( ) ;
224
+
225
+ $container . insertAfter ( this . $element ) ;
226
+
227
+ this . selection = new this . options . selectionAdapter ( $element , options ) ;
228
+
229
+ var $selectionContainer = $container . find ( ".selection" ) ;
230
+ var $selection = this . selection . render ( ) ;
231
+
232
+ $selectionContainer . append ( $selection ) ;
233
+
234
+ this . dropdown = new this . options . dropdownAdapter ( $element , options ) ;
235
+
236
+ var $dropdown = this . dropdown . render ( ) ;
237
+
238
+ $dropdown . insertAfter ( $container ) ;
239
+
240
+ this . results = new this . options . resultsAdapter ( $element , options ) ;
241
+
242
+ var $resultsContainer = $dropdown . find ( ".results" ) ;
243
+ var $results = this . results . render ( ) ;
244
+
245
+ $resultsContainer . append ( $results ) ;
246
+
247
+ // Set the initial state
248
+
249
+ var initialData = this . data . current ( ) ;
250
+
251
+ this . selection . update ( initialData ) ;
252
+
253
+ var self = this ;
254
+
255
+ this . $element . on ( "change" , function ( ) {
256
+ self . selection . update ( self . data . current ( ) ) ;
257
+ } )
91
258
} ;
92
259
93
260
Utils . Extend ( Select2 , Utils . Observable ) ;
94
261
262
+ Select2 . prototype . render = function ( ) {
263
+ var $container = $ (
264
+ '<div class="select2 select2-container">' +
265
+ '<div class="selection"></div>' +
266
+ '</div>'
267
+ ) ;
268
+
269
+ return $container ;
270
+ } ;
271
+
95
272
return Select2 ;
96
273
} ) ;
97
274
0 commit comments