]>
git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/ex-mode/node_modules/atom-space-pen-views/lib/select-list-view.js
2 var $, SelectListView
, TextEditorView
, View
, fuzzyFilter
, _ref
,
3 __hasProp
= {}.hasOwnProperty
,
4 __extends = function(child
, parent
) { for (var key
in parent
) { if (__hasProp
.call(parent
, key
)) child
[key
] = parent
[key
]; } function ctor() { this.constructor = child
; } ctor
.prototype = parent
.prototype; child
.prototype = new ctor(); child
.__super__
= parent
.prototype; return child
; };
6 _ref
= require('space-pen'), $ = _ref
.$, View
= _ref
.View
;
8 TextEditorView
= require('./text-editor-view');
12 atom
.themes
.requireStylesheet(require
.resolve('../stylesheets/select-list.less'));
14 module
.exports
= SelectListView
= (function(_super
) {
15 __extends(SelectListView
, _super
);
17 function SelectListView() {
18 return SelectListView
.__super__
.constructor.apply(this, arguments
);
21 SelectListView
.content = function() {
23 "class": 'select-list'
26 _this
.subview('filterEditorView', new TextEditorView({
30 "class": 'error-message',
38 "class": 'loading-message',
43 outlet: 'loadingBadge'
47 "class": 'list-group',
54 SelectListView
.prototype.maxItems
= Infinity
;
56 SelectListView
.prototype.scheduleTimeout
= null;
58 SelectListView
.prototype.inputThrottle
= 50;
60 SelectListView
.prototype.cancelling
= false;
67 SelectListView
.prototype.initialize = function() {
68 this.filterEditorView
.getModel().getBuffer().onDidChange((function(_this
) {
70 return _this
.schedulePopulateList();
73 this.filterEditorView
.on('blur', (function(_this
) {
75 if (!_this
.cancelling
) {
76 return _this
.cancel();
80 atom
.commands
.add(this.element
, {
81 'core:move-up': (function(_this
) {
82 return function(event
) {
83 _this
.selectPreviousItemView();
84 return event
.stopPropagation();
87 'core:move-down': (function(_this
) {
88 return function(event
) {
89 _this
.selectNextItemView();
90 return event
.stopPropagation();
93 'core:move-to-top': (function(_this
) {
94 return function(event
) {
95 _this
.selectItemView(_this
.list
.find('li:first'));
96 _this
.list
.scrollToTop();
97 return event
.stopPropagation();
100 'core:move-to-bottom': (function(_this
) {
101 return function(event
) {
102 _this
.selectItemView(_this
.list
.find('li:last'));
103 _this
.list
.scrollToBottom();
104 return event
.stopPropagation();
107 'core:confirm': (function(_this
) {
108 return function(event
) {
109 _this
.confirmSelection();
110 return event
.stopPropagation();
113 'core:cancel': (function(_this
) {
114 return function(event
) {
116 return event
.stopPropagation();
120 this.list
.on('mousedown', (function(_this
) {
121 return function(_arg
) {
123 target
= _arg
.target
;
124 if (target
=== _this
.list
[0]) {
129 this.list
.on('mousedown', 'li', (function(_this
) {
131 _this
.selectItemView($(e
.target
).closest('li'));
136 return this.list
.on('mouseup', 'li', (function(_this
) {
138 if ($(e
.target
).closest('li').hasClass('selected')) {
139 _this
.confirmSelection();
149 Section: Methods that must be overridden
152 SelectListView
.prototype.viewForItem = function(item
) {
153 throw new Error("Subclass must implement a viewForItem(item) method");
156 SelectListView
.prototype.confirmed = function(item
) {
157 throw new Error("Subclass must implement a confirmed(item) method");
162 Section: Managing the list of items
165 SelectListView
.prototype.setItems = function(items
) {
166 this.items
= items
!= null ? items : [];
168 return this.setLoading();
171 SelectListView
.prototype.getSelectedItem = function() {
172 return this.getSelectedItemView().data('select-list-item');
175 SelectListView
.prototype.getFilterKey = function() {};
177 SelectListView
.prototype.getFilterQuery = function() {
178 return this.filterEditorView
.getText();
181 SelectListView
.prototype.setMaxItems = function(maxItems
) {
182 this.maxItems
= maxItems
;
185 SelectListView
.prototype.populateList = function() {
186 var filterQuery
, filteredItems
, i
, item
, itemView
, _i
, _ref1
;
187 if (this.items
== null) {
190 filterQuery
= this.getFilterQuery();
191 if (filterQuery
.length
) {
192 if (fuzzyFilter
== null) {
193 fuzzyFilter
= require('fuzzaldrin').filter
;
195 filteredItems
= fuzzyFilter(this.items
, filterQuery
, {
196 key: this.getFilterKey()
199 filteredItems
= this.items
;
202 if (filteredItems
.length
) {
204 for (i
= _i
= 0, _ref1
= Math
.min(filteredItems
.length
, this.maxItems
); 0 <= _ref1
? _i
< _ref1 : _i
> _ref1
; i
= 0 <= _ref1
? ++_i : --_i
) {
205 item
= filteredItems
[i
];
206 itemView
= $(this.viewForItem(item
));
207 itemView
.data('select-list-item', item
);
208 this.list
.append(itemView
);
210 return this.selectItemView(this.list
.find('li:first'));
212 return this.setError(this.getEmptyMessage(this.items
.length
, filteredItems
.length
));
218 Section: Messages to the user
221 SelectListView
.prototype.setError = function(message
) {
222 if (message
== null) {
225 if (message
.length
=== 0) {
226 return this.error
.text('').hide();
229 return this.error
.text(message
).show();
233 SelectListView
.prototype.setLoading = function(message
) {
234 if (message
== null) {
237 if (message
.length
=== 0) {
238 this.loading
.text("");
239 this.loadingBadge
.text("");
240 return this.loadingArea
.hide();
243 this.loading
.text(message
);
244 return this.loadingArea
.show();
248 SelectListView
.prototype.getEmptyMessage = function(itemCount
, filteredItemCount
) {
249 return 'No matches found';
254 Section: View Actions
257 SelectListView
.prototype.cancel = function() {
258 var filterEditorViewFocused
;
260 this.cancelling
= true;
261 filterEditorViewFocused
= this.filterEditorView
.hasFocus();
262 if (typeof this.cancelled
=== "function") {
265 this.filterEditorView
.setText('');
266 if (filterEditorViewFocused
) {
269 this.cancelling
= false;
270 return clearTimeout(this.scheduleTimeout
);
273 SelectListView
.prototype.focusFilterEditor = function() {
274 return this.filterEditorView
.focus();
277 SelectListView
.prototype.storeFocusedElement = function() {
278 return this.previouslyFocusedElement
= $(document
.activeElement
);
286 SelectListView
.prototype.selectPreviousItemView = function() {
288 view
= this.getSelectedItemView().prev();
290 view
= this.list
.find('li:last');
292 return this.selectItemView(view
);
295 SelectListView
.prototype.selectNextItemView = function() {
297 view
= this.getSelectedItemView().next();
299 view
= this.list
.find('li:first');
301 return this.selectItemView(view
);
304 SelectListView
.prototype.selectItemView = function(view
) {
308 this.list
.find('.selected').removeClass('selected');
309 view
.addClass('selected');
310 return this.scrollToItemView(view
);
313 SelectListView
.prototype.scrollToItemView = function(view
) {
314 var desiredBottom
, desiredTop
, scrollTop
;
315 scrollTop
= this.list
.scrollTop();
316 desiredTop
= view
.position().top
+ scrollTop
;
317 desiredBottom
= desiredTop
+ view
.outerHeight();
318 if (desiredTop
< scrollTop
) {
319 return this.list
.scrollTop(desiredTop
);
320 } else if (desiredBottom
> this.list
.scrollBottom()) {
321 return this.list
.scrollBottom(desiredBottom
);
325 SelectListView
.prototype.restoreFocus = function() {
327 return (_ref1
= this.previouslyFocusedElement
) != null ? _ref1
.focus() : void 0;
330 SelectListView
.prototype.getSelectedItemView = function() {
331 return this.list
.find('li.selected');
334 SelectListView
.prototype.confirmSelection = function() {
336 item
= this.getSelectedItem();
338 return this.confirmed(item
);
340 return this.cancel();
344 SelectListView
.prototype.schedulePopulateList = function() {
345 var populateCallback
;
346 clearTimeout(this.scheduleTimeout
);
347 populateCallback
= (function(_this
) {
349 if (_this
.isOnDom()) {
350 return _this
.populateList();
354 return this.scheduleTimeout
= setTimeout(populateCallback
, this.inputThrottle
);
357 return SelectListView
;