Commit | Line | Data |
---|---|---|
50ac472f D |
1 | _kiwi.view.MenuBox = Backbone.View.extend({ |
2 | events: { | |
3 | 'click .ui_menu_foot .close': 'dispose' | |
4 | }, | |
5 | ||
6 | initialize: function(title) { | |
7 | var that = this; | |
8 | ||
9 | this.$el = $('<div class="ui_menu"></div>'); | |
10 | ||
11 | this._title = title || ''; | |
12 | this._items = {}; | |
13 | this._display_footer = true; | |
14 | this._close_on_blur = true; | |
15 | ||
16 | this._close_proxy = function(event) { | |
17 | that.onDocumentClick(event); | |
18 | }; | |
19 | $(document).on('click', this._close_proxy); | |
20 | }, | |
21 | ||
22 | ||
23 | render: function() { | |
24 | var that = this; | |
25 | ||
26 | this.$el.find('*').remove(); | |
27 | ||
28 | if (this._title) { | |
29 | $('<div class="ui_menu_title"></div>') | |
30 | .text(this._title) | |
31 | .appendTo(this.$el); | |
32 | } | |
33 | ||
34 | ||
35 | _.each(this._items, function(item) { | |
36 | var $item = $('<div class="ui_menu_content hover"></div>') | |
37 | .append(item); | |
38 | ||
39 | that.$el.append($item); | |
40 | }); | |
41 | ||
42 | if (this._display_footer) | |
43 | this.$el.append('<div class="ui_menu_foot"><a class="close" onclick="">Close <i class="icon-remove"></i></a></div>'); | |
44 | }, | |
45 | ||
46 | ||
47 | onDocumentClick: function(event) { | |
48 | var $target = $(event.target); | |
49 | ||
50 | if (!this._close_on_blur) | |
51 | return; | |
52 | ||
53 | // If this is not itself AND we don't contain this element, dispose $el | |
54 | if ($target[0] != this.$el[0] && this.$el.has($target).length === 0) | |
55 | this.dispose(); | |
56 | }, | |
57 | ||
58 | ||
59 | dispose: function() { | |
60 | _.each(this._items, function(item) { | |
61 | item.dispose && item.dispose(); | |
62 | item.remove && item.remove(); | |
63 | }); | |
64 | ||
65 | this._items = null; | |
66 | this.remove(); | |
67 | ||
68 | $(document).off('click', this._close_proxy); | |
69 | }, | |
70 | ||
71 | ||
72 | addItem: function(item_name, $item) { | |
73 | $item = $($item); | |
74 | if ($item.is('a')) $item.addClass('icon-chevron-right'); | |
75 | this._items[item_name] = $item; | |
76 | }, | |
77 | ||
78 | ||
79 | removeItem: function(item_name) { | |
80 | delete this._items[item_name]; | |
81 | }, | |
82 | ||
83 | ||
84 | showFooter: function(show) { | |
85 | this._display_footer = show; | |
86 | }, | |
87 | ||
88 | ||
89 | closeOnBlur: function(close_it) { | |
90 | this._close_on_blur = close_it; | |
91 | }, | |
92 | ||
93 | ||
94 | show: function() { | |
95 | this.render(); | |
96 | this.$el.appendTo(_kiwi.app.view.$el); | |
97 | } | |
98 | }); |