2016-03-21 13:29:14 +00:00
|
|
|
define([
|
|
|
|
'jquery',
|
|
|
|
'underscore',
|
|
|
|
'backbone',
|
|
|
|
'common',
|
|
|
|
], function($, _, Backbone, Common) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dropdown View.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// There can be only one visible dropdown view
|
|
|
|
$(document).click(function(e) {
|
|
|
|
var view = app.ui.currentDropdown;
|
|
|
|
var target = e.target || event.srcElement;
|
|
|
|
|
|
|
|
if (!view) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-01 09:53:41 +00:00
|
|
|
if (!view.$('.sf-dropdown-menu').is(target)
|
|
|
|
&& !view.$('.sf-dropdown-menu').find('*').is(target))
|
2016-03-21 13:29:14 +00:00
|
|
|
{
|
|
|
|
view.hide();
|
|
|
|
if (app.ui.currentHighlightedItem) {
|
|
|
|
app.ui.currentHighlightedItem.rmHighlight();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2016-04-13 12:16:29 +00:00
|
|
|
$(document).keydown(function(e) {
|
|
|
|
// ESCAPE key pressed
|
|
|
|
if (e.keyCode == 27) {
|
|
|
|
var view = app.ui.currentDropdown;
|
|
|
|
if (view) {
|
|
|
|
view.hide();
|
|
|
|
if (app.ui.currentHighlightedItem) {
|
|
|
|
app.ui.currentHighlightedItem.rmHighlight();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-03-21 13:29:14 +00:00
|
|
|
var DropdownView = Backbone.View.extend({
|
|
|
|
|
2016-03-31 13:05:46 +00:00
|
|
|
defaultOptions: {
|
|
|
|
'left': '0px'
|
|
|
|
},
|
|
|
|
|
2016-03-21 13:29:14 +00:00
|
|
|
initialize: function(options) {
|
2016-04-01 09:53:41 +00:00
|
|
|
this.$el.on('click', '.sf-dropdown-toggle', _.bind(this.toggleDropdown, this));
|
2016-04-14 07:35:03 +00:00
|
|
|
this.$el.on('keydown', _.bind(this.handleKeydown, this));
|
2016-03-31 13:05:46 +00:00
|
|
|
this.options = {};
|
|
|
|
_.extend(this.options, this.defaultOptions, options);
|
2016-03-21 13:29:14 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
hide: function() {
|
|
|
|
app.ui.currentDropdown = null;
|
2016-04-01 09:53:41 +00:00
|
|
|
this.$('.sf-dropdown-menu').addClass('hide');
|
2016-03-21 13:29:14 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
show: function() {
|
2016-04-01 09:53:41 +00:00
|
|
|
var $menu = this.$('.sf-dropdown-menu');
|
2016-03-21 13:29:14 +00:00
|
|
|
app.ui.currentDropdown = this;
|
2016-03-31 13:05:46 +00:00
|
|
|
if (this.options.right) {
|
|
|
|
$menu.css('right', this.options.right);
|
|
|
|
} else {
|
|
|
|
$menu.css('left', this.options.left);
|
|
|
|
}
|
2016-04-01 09:53:41 +00:00
|
|
|
this.$('.sf-dropdown-menu').removeClass('hide');
|
2016-03-21 13:29:14 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
toggleDropdown: function() {
|
|
|
|
if (app.ui.currentDropdown && app.ui.currentDropdown != this) {
|
|
|
|
app.ui.currentDropdown.hide();
|
|
|
|
}
|
|
|
|
|
2016-04-01 09:53:41 +00:00
|
|
|
if (this.$('.sf-dropdown-menu').is(':hidden')) {
|
2016-03-21 13:29:14 +00:00
|
|
|
this.show();
|
|
|
|
} else {
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-04-14 07:35:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
handleKeydown: function(event) {
|
|
|
|
if (!/(38|40)/.test(event.which)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
var items = $.makeArray(this.$el.find('li a'));
|
|
|
|
|
|
|
|
if (!items.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var index = items.indexOf(event.target);
|
|
|
|
if (event.which === 38 && index > 0) { // up
|
|
|
|
index--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.which === 40 && index < items.length - 1) { // down
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
items[index].focus();
|
2016-03-21 13:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return DropdownView;
|
|
|
|
});
|