1 _kiwi
.view
.MenuBox
= Backbone
.View
.extend({
3 'click .ui_menu_foot .close, a.close_menu': 'dispose'
6 initialize: function(title
) {
9 this.$el
= $('<div class="ui_menu"><div class="items"></div></div>');
11 this._title
= title
|| '';
13 this._display_footer
= true;
14 this._close_on_blur
= true;
21 $items
= that
.$el
.find('.items');
23 $items
.find('*').remove();
26 $title
= $('<div class="ui_menu_title"></div>')
29 this.$el
.prepend($title
);
32 _
.each(this._items
, function(item
) {
33 var $item
= $('<div class="ui_menu_content hover"></div>')
39 if (this._display_footer
)
40 this.$el
.append('<div class="ui_menu_foot"><a class="close" onclick="">Close <i class="icon-remove"></i></a></div>');
45 setTitle: function(new_title
) {
46 this._title
= new_title
;
51 this.$el
.find('.ui_menu_title').text(this._title
);
55 onDocumentClick: function(event
) {
56 var $target
= $(event
.target
);
58 if (!this._close_on_blur
)
61 // If this is not itself AND we don't contain this element, dispose $el
62 if ($target
[0] != this.$el
[0] && this.$el
.has($target
).length
=== 0)
68 _
.each(this._items
, function(item
) {
69 item
.dispose
&& item
.dispose();
70 item
.remove
&& item
.remove();
76 if (this._close_proxy
)
77 $(document
).off('click', this._close_proxy
);
81 addItem: function(item_name
, $item
) {
82 if ($item
.is('a')) $item
.addClass('icon-chevron-right');
83 this._items
[item_name
] = $item
;
87 removeItem: function(item_name
) {
88 delete this._items
[item_name
];
92 showFooter: function(show
) {
93 this._display_footer
= show
;
97 closeOnBlur: function(close_it
) {
98 this._close_on_blur
= close_it
;
104 $controlbox
, menu_height
;
107 this.$el
.appendTo(_kiwi
.app
.view
.$el
);
109 // Ensure the menu doesn't get too tall to overlap the input bar at the bottom
110 $controlbox
= _kiwi
.app
.view
.$el
.find('.controlbox');
111 $items
= this.$el
.find('.items');
112 menu_height
= this.$el
.outerHeight() - $items
.outerHeight();
115 'overflow-y': 'auto',
116 'max-height': $controlbox
.offset().top
- this.$el
.offset().top
- menu_height
119 // We add this document click listener on the next javascript tick.
120 // If the current tick is handling an existing click event (such as the nicklist click handler),
121 // the click event bubbles up and hits the document therefore calling this callback to
122 // remove this menubox before it's even shown.
123 setTimeout(function() {
124 that
._close_proxy = function(event
) {
125 that
.onDocumentClick(event
);
127 $(document
).on('click', that
._close_proxy
);