|
| 1 | +/* |
| 2 | + * Copyright (c) 2014 |
| 3 | + * |
| 4 | + * @author Vincent Petry |
| 5 | + * @copyright 2014 Vincent Petry <pvince81@owncloud.com> |
| 6 | + * |
| 7 | + * This file is licensed under the Affero General Public License version 3 |
| 8 | + * or later. |
| 9 | + * |
| 10 | + * See the COPYING-README file. |
| 11 | + * |
| 12 | + */ |
| 13 | + |
| 14 | +(function() { |
| 15 | + |
| 16 | + var Navigation = function($el) { |
| 17 | + this.initialize($el); |
| 18 | + }; |
| 19 | + |
| 20 | + Navigation.prototype = { |
| 21 | + |
| 22 | + /** |
| 23 | + * Currently selected item in the list |
| 24 | + */ |
| 25 | + _selectedItem: null, |
| 26 | + |
| 27 | + /** |
| 28 | + * Currently selected container |
| 29 | + */ |
| 30 | + $currentContent: null, |
| 31 | + |
| 32 | + /** |
| 33 | + * Initializes the navigation from the given container |
| 34 | + * @param $el element containing the navigation |
| 35 | + */ |
| 36 | + initialize: function($el) { |
| 37 | + this.$el = $el; |
| 38 | + this._selectedItem = null; |
| 39 | + this.$currentContent = null; |
| 40 | + this._setupEvents(); |
| 41 | + }, |
| 42 | + |
| 43 | + /** |
| 44 | + * Setup UI events |
| 45 | + */ |
| 46 | + _setupEvents: function() { |
| 47 | + this.$el.on('click', 'li a', _.bind(this._onClickItem, this)); |
| 48 | + }, |
| 49 | + |
| 50 | + /** |
| 51 | + * Switch the currently selected item, mark it as selected and |
| 52 | + * make the content container visible, if any. |
| 53 | + * @param string itemId id of the navigation item to select |
| 54 | + */ |
| 55 | + setSelectedItem: function(itemId) { |
| 56 | + if (itemId === this._selectedItem) { |
| 57 | + return; |
| 58 | + } |
| 59 | + this._selectedItem = itemId; |
| 60 | + this.$el.find('li').removeClass('selected'); |
| 61 | + if (this.$currentContent) { |
| 62 | + this.$currentContent.addClass('hidden'); |
| 63 | + } |
| 64 | + this.$currentContent = $('#app-content-' + itemId); |
| 65 | + this.$currentContent.removeClass('hidden'); |
| 66 | + this.$el.find('li[data-id=' + itemId + ']').addClass('selected'); |
| 67 | + }, |
| 68 | + |
| 69 | + /** |
| 70 | + * Event handler for when clicking on an item. |
| 71 | + */ |
| 72 | + _onClickItem: function(ev) { |
| 73 | + var $target = $(ev.target); |
| 74 | + var itemId = $target.closest('li').attr('data-id'); |
| 75 | + this.setSelectedItem(itemId); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + OCA.Files.Navigation = Navigation; |
| 80 | + |
| 81 | +})(); |
0 commit comments