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() {
|
|
|
|
|
|
|
|
var Navigation = function($el) {
|
|
|
|
this.initialize($el);
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
this.$el.find('li').removeClass('selected');
|
|
|
|
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;
|
|
|
|
this.$el.find('li[data-id=' + itemId + ']').addClass('selected');
|
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');
|
2014-05-09 00:06:30 +04:00
|
|
|
this.setActiveItem(itemId);
|
|
|
|
return false;
|
2014-05-08 21:00:42 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
OCA.Files.Navigation = Navigation;
|
|
|
|
|
|
|
|
})();
|