2014-05-08 21:00:42 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014
|
|
|
|
*
|
|
|
|
* @author Vincent Petry
|
|
|
|
* @copyright 2014 Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3
|
|
|
|
* or later.
|
|
|
|
*
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
2014-06-24 01:56:10 +04:00
|
|
|
/**
|
|
|
|
* @class OCA.Files.Navigation
|
|
|
|
* @classdesc Navigation control for the files app sidebar.
|
|
|
|
*
|
|
|
|
* @param $el element containing the navigation
|
|
|
|
*/
|
2014-05-08 21:00:42 +04:00
|
|
|
var Navigation = function($el) {
|
|
|
|
this.initialize($el);
|
|
|
|
};
|
|
|
|
|
2014-06-24 01:56:10 +04:00
|
|
|
/**
|
|
|
|
* @memberof OCA.Files
|
|
|
|
*/
|
2014-05-08 21:00:42 +04:00
|
|
|
Navigation.prototype = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently selected item in the list
|
|
|
|
*/
|
2014-05-09 00:06:30 +04:00
|
|
|
_activeItem: null,
|
2014-05-08 21:00:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently selected container
|
|
|
|
*/
|
|
|
|
$currentContent: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the navigation from the given container
|
2014-06-24 01:56:10 +04:00
|
|
|
*
|
|
|
|
* @private
|
2014-05-08 21:00:42 +04:00
|
|
|
* @param $el element containing the navigation
|
|
|
|
*/
|
|
|
|
initialize: function($el) {
|
|
|
|
this.$el = $el;
|
2014-05-09 00:06:30 +04:00
|
|
|
this._activeItem = null;
|
2014-05-08 21:00:42 +04:00
|
|
|
this.$currentContent = null;
|
|
|
|
this._setupEvents();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup UI events
|
|
|
|
*/
|
|
|
|
_setupEvents: function() {
|
|
|
|
this.$el.on('click', 'li a', _.bind(this._onClickItem, this));
|
|
|
|
},
|
|
|
|
|
2014-05-09 00:06:30 +04:00
|
|
|
/**
|
|
|
|
* Returns the container of the currently active app.
|
|
|
|
*
|
|
|
|
* @return app container
|
|
|
|
*/
|
|
|
|
getActiveContainer: function() {
|
|
|
|
return this.$currentContent;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the currently active item
|
|
|
|
*
|
|
|
|
* @return item ID
|
|
|
|
*/
|
|
|
|
getActiveItem: function() {
|
|
|
|
return this._activeItem;
|
|
|
|
},
|
|
|
|
|
2014-05-08 21:00:42 +04:00
|
|
|
/**
|
|
|
|
* Switch the currently selected item, mark it as selected and
|
|
|
|
* make the content container visible, if any.
|
2014-05-09 00:06:30 +04:00
|
|
|
*
|
2014-05-08 21:00:42 +04:00
|
|
|
* @param string itemId id of the navigation item to select
|
2014-05-09 00:06:30 +04:00
|
|
|
* @param array options "silent" to not trigger event
|
2014-05-08 21:00:42 +04:00
|
|
|
*/
|
2014-05-09 00:06:30 +04:00
|
|
|
setActiveItem: function(itemId, options) {
|
2014-05-12 21:54:20 +04:00
|
|
|
var oldItemId = this._activeItem;
|
2014-05-09 00:06:30 +04:00
|
|
|
if (itemId === this._activeItem) {
|
2014-05-12 21:54:20 +04:00
|
|
|
if (!options || !options.silent) {
|
|
|
|
this.$el.trigger(
|
|
|
|
new $.Event('itemChanged', {itemId: itemId, previousItemId: oldItemId})
|
|
|
|
);
|
|
|
|
}
|
2014-05-08 21:00:42 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-06-03 11:57:18 +04:00
|
|
|
this.$el.find('li').removeClass('active');
|
2014-05-08 21:00:42 +04:00
|
|
|
if (this.$currentContent) {
|
|
|
|
this.$currentContent.addClass('hidden');
|
2014-05-09 00:06:30 +04:00
|
|
|
this.$currentContent.trigger(jQuery.Event('hide'));
|
2014-05-08 21:00:42 +04:00
|
|
|
}
|
2014-05-12 21:54:20 +04:00
|
|
|
this._activeItem = itemId;
|
2014-06-03 11:57:18 +04:00
|
|
|
this.$el.find('li[data-id=' + itemId + ']').addClass('active');
|
2014-05-08 21:00:42 +04:00
|
|
|
this.$currentContent = $('#app-content-' + itemId);
|
|
|
|
this.$currentContent.removeClass('hidden');
|
2014-05-09 00:06:30 +04:00
|
|
|
if (!options || !options.silent) {
|
|
|
|
this.$currentContent.trigger(jQuery.Event('show'));
|
2014-05-12 21:54:20 +04:00
|
|
|
this.$el.trigger(
|
|
|
|
new $.Event('itemChanged', {itemId: itemId, previousItemId: oldItemId})
|
|
|
|
);
|
2014-05-09 00:06:30 +04:00
|
|
|
}
|
2014-05-08 21:00:42 +04:00
|
|
|
},
|
|
|
|
|
2014-05-12 21:54:20 +04:00
|
|
|
/**
|
|
|
|
* Returns whether a given item exists
|
|
|
|
*/
|
|
|
|
itemExists: function(itemId) {
|
|
|
|
return this.$el.find('li[data-id=' + itemId + ']').length;
|
|
|
|
},
|
|
|
|
|
2014-05-08 21:00:42 +04:00
|
|
|
/**
|
|
|
|
* Event handler for when clicking on an item.
|
|
|
|
*/
|
|
|
|
_onClickItem: function(ev) {
|
|
|
|
var $target = $(ev.target);
|
|
|
|
var itemId = $target.closest('li').attr('data-id');
|
2017-06-09 19:05:23 +03:00
|
|
|
if (!_.isUndefined(itemId)) {
|
|
|
|
this.setActiveItem(itemId);
|
|
|
|
}
|
2015-05-19 15:47:13 +03:00
|
|
|
ev.preventDefault();
|
2014-05-08 21:00:42 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
OCA.Files.Navigation = Navigation;
|
|
|
|
|
|
|
|
})();
|