Skip to content

Commit 297a06f

Browse files
committed
Working on rendering everything
1 parent 06e195b commit 297a06f

12 files changed

+960
-72
lines changed

.editorconfig

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[*]
2+
indent_style = space
3+
end_of_line = lf
4+
5+
[*.js]
6+
indent_size = 2

dist/js/select2.amd.full.js

+190-13
Original file line numberDiff line numberDiff line change
@@ -54,44 +54,221 @@ define('select2/utils',[], function () {
5454
return Utils;
5555
});
5656

57-
define('select2/adapters/select',[
58-
"../utils"
57+
define('select2/data/select',[
58+
'../utils'
5959
], function (Utils) {
60-
function SelectAdapter (element, options) {
61-
this.element = element;
60+
function SelectAdapter ($element, options) {
61+
this.$element = $element;
6262
}
6363

6464
Utils.Extend(SelectAdapter, Utils.Observable);
6565

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+
6690
return SelectAdapter;
6791
});
6892

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+
69192
define('select2/options',[
70-
"./adapters/select"
71-
], function (SelectAdapter) {
193+
'./data/select',
194+
'./results',
195+
'./dropdown',
196+
'./selection'
197+
], function (SelectData, ResultsList, Dropdown, Selection) {
72198
function Options (options) {
73199
this.options = options;
74200

75-
this.DataAdapter = SelectAdapter;
201+
this.dataAdapter = SelectData;
202+
this.resultsAdapter = ResultsList;
203+
this.dropdownAdapter = Dropdown;
204+
this.selectionAdapter = Selection;
76205
}
77206

78207
return Options;
79208
})
80209
;
81210
define('select2/core',[
82-
"jquery",
83-
"./options",
84-
"./utils"
211+
'jquery',
212+
'./options',
213+
'./utils'
85214
], function ($, Options, Utils) {
86-
var Select2 = function (element, options) {
87-
this.element = element;
215+
var Select2 = function ($element, options) {
216+
this.$element = $element;
88217
this.options = new Options(options);
89218

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+
})
91258
};
92259

93260
Utils.Extend(Select2, Utils.Observable);
94261

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+
95272
return Select2;
96273
});
97274

0 commit comments

Comments
 (0)