2015-07-15 10:06:13 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3
|
|
|
|
* or later.
|
|
|
|
*
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
/**
|
|
|
|
* @class OCA.Files.DetailsView
|
|
|
|
* @classdesc
|
|
|
|
*
|
|
|
|
* The details view show details about a selected file.
|
|
|
|
*
|
|
|
|
*/
|
2015-08-12 15:30:20 +00:00
|
|
|
var DetailsView = OC.Backbone.View.extend({
|
|
|
|
id: 'app-sidebar',
|
|
|
|
tabName: 'div',
|
2015-09-01 17:29:55 +00:00
|
|
|
className: 'detailsView scroll-container',
|
2015-07-15 10:06:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of detail tab views
|
|
|
|
*
|
|
|
|
* @type Array<OCA.Files.DetailTabView>
|
|
|
|
*/
|
|
|
|
_tabViews: [],
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of detail file info views
|
|
|
|
*
|
|
|
|
* @type Array<OCA.Files.DetailFileInfoView>
|
|
|
|
*/
|
|
|
|
_detailFileInfoViews: [],
|
|
|
|
|
2015-07-29 15:36:07 +00:00
|
|
|
/**
|
|
|
|
* Id of the currently selected tab
|
|
|
|
*
|
|
|
|
* @type string
|
|
|
|
*/
|
|
|
|
_currentTabId: null,
|
|
|
|
|
2015-08-14 13:54:27 +00:00
|
|
|
/**
|
|
|
|
* Dirty flag, whether the view needs to be rerendered
|
|
|
|
*/
|
|
|
|
_dirty: false,
|
|
|
|
|
2015-08-12 15:30:20 +00:00
|
|
|
events: {
|
|
|
|
'click a.close': '_onClose',
|
2018-06-25 07:29:29 +00:00
|
|
|
'click .tabHeaders .tabHeader': '_onClickTab',
|
|
|
|
'keyup .tabHeaders .tabHeader': '_onKeyboardActivateTab'
|
2015-08-12 15:30:20 +00:00
|
|
|
},
|
|
|
|
|
2015-07-15 10:06:13 +00:00
|
|
|
/**
|
|
|
|
* Initialize the details view
|
|
|
|
*/
|
|
|
|
initialize: function() {
|
|
|
|
this._tabViews = [];
|
|
|
|
this._detailFileInfoViews = [];
|
2015-07-15 15:05:25 +00:00
|
|
|
|
2015-08-14 13:54:27 +00:00
|
|
|
this._dirty = true;
|
2015-07-15 10:06:13 +00:00
|
|
|
},
|
|
|
|
|
2015-08-12 15:30:20 +00:00
|
|
|
_onClose: function(event) {
|
2015-09-22 13:28:48 +00:00
|
|
|
OC.Apps.hideAppSidebar(this.$el);
|
2015-08-12 15:30:20 +00:00
|
|
|
event.preventDefault();
|
2015-07-15 10:06:13 +00:00
|
|
|
},
|
|
|
|
|
2015-07-29 15:36:07 +00:00
|
|
|
_onClickTab: function(e) {
|
|
|
|
var $target = $(e.target);
|
2015-08-14 13:54:27 +00:00
|
|
|
e.preventDefault();
|
2015-07-29 15:36:07 +00:00
|
|
|
if (!$target.hasClass('tabHeader')) {
|
|
|
|
$target = $target.closest('.tabHeader');
|
|
|
|
}
|
2015-08-14 13:54:27 +00:00
|
|
|
var tabId = $target.attr('data-tabid');
|
|
|
|
if (_.isUndefined(tabId)) {
|
2015-07-29 15:36:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-14 13:54:27 +00:00
|
|
|
this.selectTab(tabId);
|
2015-07-29 15:36:07 +00:00
|
|
|
},
|
|
|
|
|
2018-06-25 07:29:29 +00:00
|
|
|
_onKeyboardActivateTab: function (event) {
|
|
|
|
if (event.key === " " || event.key === "Enter") {
|
|
|
|
this._onClickTab(event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-08-14 13:54:27 +00:00
|
|
|
template: function(vars) {
|
2018-10-01 16:58:12 +00:00
|
|
|
return OCA.Files.Templates['detailsview'](vars);
|
2015-08-14 13:54:27 +00:00
|
|
|
},
|
|
|
|
|
2015-07-15 10:06:13 +00:00
|
|
|
/**
|
|
|
|
* Renders this details view
|
|
|
|
*/
|
|
|
|
render: function() {
|
2015-08-14 13:54:27 +00:00
|
|
|
var templateVars = {
|
|
|
|
closeLabel: t('files', 'Close')
|
|
|
|
};
|
2015-07-15 10:06:13 +00:00
|
|
|
|
2015-09-28 10:30:12 +00:00
|
|
|
this._tabViews = this._tabViews.sort(function(tabA, tabB) {
|
|
|
|
var orderA = tabA.order || 0;
|
|
|
|
var orderB = tabB.order || 0;
|
|
|
|
if (orderA === orderB) {
|
|
|
|
return OC.Util.naturalSortCompare(tabA.getLabel(), tabB.getLabel());
|
|
|
|
}
|
|
|
|
return orderA - orderB;
|
|
|
|
});
|
2015-09-25 11:23:39 +00:00
|
|
|
|
|
|
|
templateVars.tabHeaders = _.map(this._tabViews, function(tabView, i) {
|
|
|
|
return {
|
|
|
|
tabId: tabView.id,
|
2018-10-18 13:47:18 +00:00
|
|
|
label: tabView.getLabel(),
|
|
|
|
tabIcon: tabView.getIcon()
|
2015-09-25 11:23:39 +00:00
|
|
|
};
|
|
|
|
});
|
2015-07-15 10:06:13 +00:00
|
|
|
|
2015-08-14 13:54:27 +00:00
|
|
|
this.$el.html(this.template(templateVars));
|
2015-08-12 15:30:20 +00:00
|
|
|
|
|
|
|
var $detailsContainer = this.$el.find('.detailFileInfoContainer');
|
2015-07-15 10:06:13 +00:00
|
|
|
|
|
|
|
// render details
|
|
|
|
_.each(this._detailFileInfoViews, function(detailView) {
|
2015-07-20 12:42:10 +00:00
|
|
|
$detailsContainer.append(detailView.get$());
|
2015-07-15 10:06:13 +00:00
|
|
|
});
|
|
|
|
|
2015-08-14 13:54:27 +00:00
|
|
|
if (!this._currentTabId && this._tabViews.length > 0) {
|
|
|
|
this._currentTabId = this._tabViews[0].id;
|
|
|
|
}
|
2015-07-15 14:09:00 +00:00
|
|
|
|
2015-08-14 13:54:27 +00:00
|
|
|
this.selectTab(this._currentTabId);
|
|
|
|
|
2015-09-25 11:23:39 +00:00
|
|
|
this._updateTabVisibilities();
|
|
|
|
|
2015-08-14 13:54:27 +00:00
|
|
|
this._dirty = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Selects the given tab by id
|
|
|
|
*
|
|
|
|
* @param {string} tabId tab id
|
|
|
|
*/
|
|
|
|
selectTab: function(tabId) {
|
|
|
|
if (!tabId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var tabView = _.find(this._tabViews, function(tab) {
|
|
|
|
return tab.id === tabId;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!tabView) {
|
|
|
|
console.warn('Details view tab with id "' + tabId + '" not found');
|
|
|
|
return;
|
2015-07-15 14:09:00 +00:00
|
|
|
}
|
2015-08-14 13:54:27 +00:00
|
|
|
|
|
|
|
this._currentTabId = tabId;
|
|
|
|
|
|
|
|
var $tabsContainer = this.$el.find('.tabsContainer');
|
|
|
|
var $tabEl = $tabsContainer.find('#' + tabId);
|
|
|
|
|
|
|
|
// hide other tabs
|
|
|
|
$tabsContainer.find('.tab').addClass('hidden');
|
|
|
|
|
2018-10-04 08:52:33 +00:00
|
|
|
$tabsContainer.attr('class', 'tabsContainer');
|
|
|
|
$tabsContainer.addClass(tabView.getTabsContainerExtraClasses());
|
|
|
|
|
2015-08-14 13:54:27 +00:00
|
|
|
// tab already rendered ?
|
|
|
|
if (!$tabEl.length) {
|
|
|
|
// render tab
|
|
|
|
$tabsContainer.append(tabView.$el);
|
|
|
|
$tabEl = tabView.$el;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this should trigger tab rendering
|
|
|
|
tabView.setFileInfo(this.model);
|
|
|
|
|
|
|
|
$tabEl.removeClass('hidden');
|
|
|
|
|
|
|
|
// update tab headers
|
|
|
|
var $tabHeaders = this.$el.find('.tabHeaders li');
|
|
|
|
$tabHeaders.removeClass('selected');
|
|
|
|
$tabHeaders.filterAttr('data-tabid', tabView.id).addClass('selected');
|
2015-07-15 10:06:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the file info to be displayed in the view
|
|
|
|
*
|
2015-08-12 15:30:20 +00:00
|
|
|
* @param {OCA.Files.FileInfoModel} fileInfo file info to set
|
2015-07-15 10:06:13 +00:00
|
|
|
*/
|
|
|
|
setFileInfo: function(fileInfo) {
|
2015-08-12 15:30:20 +00:00
|
|
|
this.model = fileInfo;
|
2015-07-15 10:06:13 +00:00
|
|
|
|
2015-08-14 13:54:27 +00:00
|
|
|
if (this._dirty) {
|
|
|
|
this.render();
|
2015-09-25 11:23:39 +00:00
|
|
|
} else {
|
|
|
|
this._updateTabVisibilities();
|
2015-08-14 13:54:27 +00:00
|
|
|
}
|
2015-07-15 15:05:25 +00:00
|
|
|
|
2015-08-14 13:54:27 +00:00
|
|
|
if (this._currentTabId) {
|
|
|
|
// only update current tab, others will be updated on-demand
|
|
|
|
var tabId = this._currentTabId;
|
|
|
|
var tabView = _.find(this._tabViews, function(tab) {
|
|
|
|
return tab.id === tabId;
|
|
|
|
});
|
2015-07-15 10:06:13 +00:00
|
|
|
tabView.setFileInfo(fileInfo);
|
2015-08-14 13:54:27 +00:00
|
|
|
}
|
|
|
|
|
2015-07-15 10:06:13 +00:00
|
|
|
_.each(this._detailFileInfoViews, function(detailView) {
|
|
|
|
detailView.setFileInfo(fileInfo);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-09-25 11:23:39 +00:00
|
|
|
/**
|
|
|
|
* Update tab headers based on the current model
|
|
|
|
*/
|
|
|
|
_updateTabVisibilities: function() {
|
|
|
|
// update tab header visibilities
|
|
|
|
var self = this;
|
|
|
|
var deselect = false;
|
|
|
|
var countVisible = 0;
|
|
|
|
var $tabHeaders = this.$el.find('.tabHeaders li');
|
|
|
|
_.each(this._tabViews, function(tabView) {
|
|
|
|
var isVisible = tabView.canDisplay(self.model);
|
|
|
|
if (isVisible) {
|
|
|
|
countVisible += 1;
|
|
|
|
}
|
|
|
|
if (!isVisible && self._currentTabId === tabView.id) {
|
|
|
|
deselect = true;
|
|
|
|
}
|
|
|
|
$tabHeaders.filterAttr('data-tabid', tabView.id).toggleClass('hidden', !isVisible);
|
|
|
|
});
|
|
|
|
|
|
|
|
// hide the whole container if there is only one tab
|
|
|
|
this.$el.find('.tabHeaders').toggleClass('hidden', countVisible <= 1);
|
|
|
|
|
|
|
|
if (deselect) {
|
|
|
|
// select the first visible tab instead
|
|
|
|
var visibleTabId = this.$el.find('.tabHeader:not(.hidden):first').attr('data-tabid');
|
|
|
|
this.selectTab(visibleTabId);
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2015-07-15 10:06:13 +00:00
|
|
|
/**
|
|
|
|
* Returns the file info.
|
|
|
|
*
|
2015-08-12 15:30:20 +00:00
|
|
|
* @return {OCA.Files.FileInfoModel} file info
|
2015-07-15 10:06:13 +00:00
|
|
|
*/
|
|
|
|
getFileInfo: function() {
|
2015-08-12 15:30:20 +00:00
|
|
|
return this.model;
|
2015-07-15 10:06:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a tab in the tab view
|
|
|
|
*
|
|
|
|
* @param {OCA.Files.DetailTabView} tab view
|
|
|
|
*/
|
|
|
|
addTabView: function(tabView) {
|
|
|
|
this._tabViews.push(tabView);
|
2015-08-14 13:54:27 +00:00
|
|
|
this._dirty = true;
|
2015-07-15 10:06:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a detail view for file info.
|
|
|
|
*
|
|
|
|
* @param {OCA.Files.DetailFileInfoView} detail view
|
|
|
|
*/
|
|
|
|
addDetailView: function(detailView) {
|
|
|
|
this._detailFileInfoViews.push(detailView);
|
2015-08-14 13:54:27 +00:00
|
|
|
this._dirty = true;
|
2017-06-09 01:14:23 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array with the added DetailFileInfoViews.
|
|
|
|
*
|
|
|
|
* @return Array<OCA.Files.DetailFileInfoView> an array with the added
|
|
|
|
* DetailFileInfoViews.
|
|
|
|
*/
|
|
|
|
getDetailViews: function() {
|
|
|
|
return [].concat(this._detailFileInfoViews);
|
2015-07-15 10:06:13 +00:00
|
|
|
}
|
2015-08-12 15:30:20 +00:00
|
|
|
});
|
2015-07-15 10:06:13 +00:00
|
|
|
|
|
|
|
OCA.Files.DetailsView = DetailsView;
|
|
|
|
})();
|