2 var dataset = require('../../utils/dataset-shim');
4 module.exports = (function() {
5 function getContainer(self) {
6 if (self.$childViewContainer) return self.$childViewContainer;
12 keydown: 'selectKeydown',
13 'mouseover li:not(.divider):visible': 'selectMouseOver',
14 'click li': 'selectClicked'
16 selectMouseOver: function(e) {
17 var $c = getContainer(this);
18 var $items = $c.find('li:not(.divider):visible');
20 if (!$items.length) return;
22 var currentActive = $items.filter('.active');
23 var newActive = e.currentTarget;
25 if (currentActive[0] === newActive) return;
27 currentActive.removeClass('active');
28 newActive.classList.add('active');
30 selectKeydown: function(e) {
48 selectPrev: function() {
51 selectNext: function() {
54 simulateClick: function() {
55 var $c = getContainer(this);
57 var first = $c.find('li:not(.divider):visible.active').first();
59 first.trigger('click');
62 $c.find('li:not(.divider):visible.active')
67 selectActive: function() {
70 selectClicked: function(e) {
71 if (!this.collection) return;
72 var selected = e.currentTarget;
73 var cid = dataset.get(selected, 'cid');
74 var model = this.collection.get(cid);
75 this.setSelected(model);
77 this.trigger('selectClicked');
79 if (!this.options.allowClickPropagation) {
84 onAddChild: function(childView) {
85 if (this.selectedModel && childView.model.id && childView.model.id == this.selectedModel.id) {
86 childView.$el.addClass('selected');
89 setSelected: function(model) {
90 if (this.selectedModel) {
91 this.selectedModel.trigger('unselected');
93 var oldSelectedView = this.children.findByModel(this.selectedModel);
94 if (oldSelectedView) {
95 oldSelectedView.$el.removeClass('selected');
99 this.selectedModel = model;
102 model.trigger('selected');
103 var selectedView = this.children.findByModel(model);
104 selectedView.$el.addClass('selected');
106 this.triggerMethod('selected', model);
108 getActive: function() {
109 var $c = getContainer(this);
110 var $active = $c.find('li.active:not(.divider):visible');
112 var active = $active[0];
115 var cid = dataset.get(active, 'cid');
116 var model = this.collection.get(cid);
119 setActive: function(model) {
122 var activeView = this.children.findByModel(model);
123 if (!activeView) return;
125 /* Already active? */
126 if (activeView.$el.hasClass('active')) return;
128 var $c = getContainer(this);
129 $c.find('li.active:not(.divider):visible').removeClass('active');
131 activeView.$el.addClass('active');
133 _moveSelect: function(delta) {
134 var $c = getContainer(this);
136 var $items = $c.find('li:not(.divider):visible');
138 if (!$items.length) return;
140 var currentActive = $items.filter('.active');
141 var index = $items.index(currentActive);
145 index = index + delta;
148 } else if (index >= $items.length) {
149 index = $items.length - 1;
153 if (index != currentActive) {
154 var newActive = $items.eq(index);
155 currentActive.removeClass('active');
156 newActive.addClass('active');
157 newActive.addClass('focus');