2014-05-08 17:00:42 +00: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-23 21:56:10 +00:00
|
|
|
/**
|
|
|
|
* @class OCA.Files.Navigation
|
|
|
|
* @classdesc Navigation control for the files app sidebar.
|
|
|
|
*
|
|
|
|
* @param $el element containing the navigation
|
|
|
|
*/
|
2014-05-08 17:00:42 +00:00
|
|
|
var Navigation = function($el) {
|
|
|
|
this.initialize($el);
|
|
|
|
};
|
|
|
|
|
2014-06-23 21:56:10 +00:00
|
|
|
/**
|
|
|
|
* @memberof OCA.Files
|
|
|
|
*/
|
2014-05-08 17:00:42 +00:00
|
|
|
Navigation.prototype = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently selected item in the list
|
|
|
|
*/
|
2014-05-08 20:06:30 +00:00
|
|
|
_activeItem: null,
|
2014-05-08 17:00:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently selected container
|
|
|
|
*/
|
|
|
|
$currentContent: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the navigation from the given container
|
2014-06-23 21:56:10 +00:00
|
|
|
*
|
|
|
|
* @private
|
2014-05-08 17:00:42 +00:00
|
|
|
* @param $el element containing the navigation
|
|
|
|
*/
|
|
|
|
initialize: function($el) {
|
|
|
|
this.$el = $el;
|
2014-05-08 20:06:30 +00:00
|
|
|
this._activeItem = null;
|
2014-05-08 17:00:42 +00:00
|
|
|
this.$currentContent = null;
|
|
|
|
this._setupEvents();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup UI events
|
|
|
|
*/
|
|
|
|
_setupEvents: function() {
|
|
|
|
this.$el.on('click', 'li a', _.bind(this._onClickItem, this));
|
|
|
|
},
|
|
|
|
|
2014-05-08 20:06:30 +00: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 17:00:42 +00:00
|
|
|
/**
|
|
|
|
* Switch the currently selected item, mark it as selected and
|
|
|
|
* make the content container visible, if any.
|
2014-05-08 20:06:30 +00:00
|
|
|
*
|
2014-05-08 17:00:42 +00:00
|
|
|
* @param string itemId id of the navigation item to select
|
2014-05-08 20:06:30 +00:00
|
|
|
* @param array options "silent" to not trigger event
|
2014-05-08 17:00:42 +00:00
|
|
|
*/
|
2014-05-08 20:06:30 +00:00
|
|
|
setActiveItem: function(itemId, options) {
|
2014-05-12 17:54:20 +00:00
|
|
|
var oldItemId = this._activeItem;
|
2014-05-08 20:06:30 +00:00
|
|
|
if (itemId === this._activeItem) {
|
2014-05-12 17:54:20 +00:00
|
|
|
if (!options || !options.silent) {
|
|
|
|
this.$el.trigger(
|
|
|
|
new $.Event('itemChanged', {itemId: itemId, previousItemId: oldItemId})
|
|
|
|
);
|
|
|
|
}
|
2014-05-08 17:00:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-06-03 07:57:18 +00:00
|
|
|
this.$el.find('li').removeClass('active');
|
2014-05-08 17:00:42 +00:00
|
|
|
if (this.$currentContent) {
|
|
|
|
this.$currentContent.addClass('hidden');
|
2014-05-08 20:06:30 +00:00
|
|
|
this.$currentContent.trigger(jQuery.Event('hide'));
|
2014-05-08 17:00:42 +00:00
|
|
|
}
|
2014-05-12 17:54:20 +00:00
|
|
|
this._activeItem = itemId;
|
2014-06-03 07:57:18 +00:00
|
|
|
this.$el.find('li[data-id=' + itemId + ']').addClass('active');
|
2014-05-08 17:00:42 +00:00
|
|
|
this.$currentContent = $('#app-content-' + itemId);
|
|
|
|
this.$currentContent.removeClass('hidden');
|
2014-05-08 20:06:30 +00:00
|
|
|
if (!options || !options.silent) {
|
|
|
|
this.$currentContent.trigger(jQuery.Event('show'));
|
2014-05-12 17:54:20 +00:00
|
|
|
this.$el.trigger(
|
|
|
|
new $.Event('itemChanged', {itemId: itemId, previousItemId: oldItemId})
|
|
|
|
);
|
2014-05-08 20:06:30 +00:00
|
|
|
}
|
2014-05-08 17:00:42 +00:00
|
|
|
},
|
|
|
|
|
2014-05-12 17:54:20 +00:00
|
|
|
/**
|
|
|
|
* Returns whether a given item exists
|
|
|
|
*/
|
|
|
|
itemExists: function(itemId) {
|
|
|
|
return this.$el.find('li[data-id=' + itemId + ']').length;
|
|
|
|
},
|
|
|
|
|
2014-05-08 17:00:42 +00:00
|
|
|
/**
|
|
|
|
* Event handler for when clicking on an item.
|
|
|
|
*/
|
|
|
|
_onClickItem: function(ev) {
|
|
|
|
var $target = $(ev.target);
|
|
|
|
var itemId = $target.closest('li').attr('data-id');
|
2014-05-08 20:06:30 +00:00
|
|
|
this.setActiveItem(itemId);
|
|
|
|
return false;
|
2014-05-08 17:00:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
OCA.Files.Navigation = Navigation;
|
|
|
|
|
|
|
|
})();
|