2015-07-15 13:06:13 +03: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 18:30:20 +03:00
|
|
|
var DetailsView = OC.Backbone.View.extend({
|
|
|
|
id: 'app-sidebar',
|
|
|
|
tabName: 'div',
|
2015-09-01 20:29:55 +03:00
|
|
|
className: 'detailsView scroll-container',
|
2015-07-15 13:06:13 +03: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 18:36:07 +03:00
|
|
|
/**
|
|
|
|
* Id of the currently selected tab
|
|
|
|
*
|
|
|
|
* @type string
|
|
|
|
*/
|
|
|
|
_currentTabId: null,
|
|
|
|
|
2015-08-14 16:54:27 +03:00
|
|
|
/**
|
|
|
|
* Dirty flag, whether the view needs to be rerendered
|
|
|
|
*/
|
|
|
|
_dirty: false,
|
|
|
|
|
2015-08-12 18:30:20 +03:00
|
|
|
events: {
|
|
|
|
'click a.close': '_onClose',
|
2018-06-25 10:29:29 +03:00
|
|
|
'click .tabHeaders .tabHeader': '_onClickTab',
|
|
|
|
'keyup .tabHeaders .tabHeader': '_onKeyboardActivateTab'
|
2015-08-12 18:30:20 +03:00
|
|
|
},
|
|
|
|
|
2015-07-15 13:06:13 +03:00
|
|
|
/**
|
|
|
|
* Initialize the details view
|
|
|
|
*/
|
|
|
|
initialize: function() {
|
|
|
|
this._tabViews = [];
|
|
|
|
this._detailFileInfoViews = [];
|
2015-07-15 18:05:25 +03:00
|
|
|
|
2015-08-14 16:54:27 +03:00
|
|
|
this._dirty = true;
|
2015-07-15 13:06:13 +03:00
|
|
|
},
|
|
|
|
|
2015-08-12 18:30:20 +03:00
|
|
|
_onClose: function(event) {
|
2015-09-22 16:28:48 +03:00
|
|
|
OC.Apps.hideAppSidebar(this.$el);
|
2015-08-12 18:30:20 +03:00
|
|
|
event.preventDefault();
|
2015-07-15 13:06:13 +03:00
|
|
|
},
|
|
|
|
|
2015-07-29 18:36:07 +03:00
|
|
|
_onClickTab: function(e) {
|
|
|
|
var $target = $(e.target);
|
2015-08-14 16:54:27 +03:00
|
|
|
e.preventDefault();
|
2015-07-29 18:36:07 +03:00
|
|
|
if (!$target.hasClass('tabHeader')) {
|
|
|
|
$target = $target.closest('.tabHeader');
|
|
|
|
}
|
2015-08-14 16:54:27 +03:00
|
|
|
var tabId = $target.attr('data-tabid');
|
|
|
|
if (_.isUndefined(tabId)) {
|
2015-07-29 18:36:07 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-14 16:54:27 +03:00
|
|
|
this.selectTab(tabId);
|
2015-07-29 18:36:07 +03:00
|
|
|
},
|
|
|
|
|
2018-06-25 10:29:29 +03:00
|
|
|
_onKeyboardActivateTab: function (event) {
|
|
|
|
if (event.key === " " || event.key === "Enter") {
|
|
|
|
this._onClickTab(event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-08-14 16:54:27 +03:00
|
|
|
template: function(vars) {
|
2018-10-01 19:58:12 +03:00
|
|
|
return OCA.Files.Templates['detailsview'](vars);
|
2015-08-14 16:54:27 +03:00
|
|
|
},
|
|
|
|
|
2015-07-15 13:06:13 +03:00
|
|
|
/**
|
|
|
|
* Renders this details view
|
|
|
|
*/
|
|
|
|
render: function() {
|
2015-08-14 16:54:27 +03:00
|
|
|
var templateVars = {
|
|
|
|
closeLabel: t('files', 'Close')
|
|
|
|
};
|
2015-07-15 13:06:13 +03:00
|
|
|
|
2015-09-28 13:30:12 +03: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 14:23:39 +03:00
|
|
|
|
|
|
|
templateVars.tabHeaders = _.map(this._tabViews, function(tabView, i) {
|
|
|
|
return {
|
|
|
|
tabId: tabView.id,
|
2018-10-18 16:47:18 +03:00
|
|
|
label: tabView.getLabel(),
|
|
|
|
tabIcon: tabView.getIcon()
|
2015-09-25 14:23:39 +03:00
|
|
|
};
|
|
|
|
});
|
2015-07-15 13:06:13 +03:00
|
|
|
|
2015-08-14 16:54:27 +03:00
|
|
|
this.$el.html(this.template(templateVars));
|
2015-08-12 18:30:20 +03:00
|
|
|
|
|
|
|
var $detailsContainer = this.$el.find('.detailFileInfoContainer');
|
2015-07-15 13:06:13 +03:00
|
|
|
|
|
|
|
// render details
|
|
|
|
_.each(this._detailFileInfoViews, function(detailView) {
|
2015-07-20 15:42:10 +03:00
|
|
|
$detailsContainer.append(detailView.get$());
|
2015-07-15 13:06:13 +03:00
|
|
|
});
|
|
|
|
|
2015-08-14 16:54:27 +03:00
|
|
|
if (!this._currentTabId && this._tabViews.length > 0) {
|
|
|
|
this._currentTabId = this._tabViews[0].id;
|
|
|
|
}
|
2015-07-15 17:09:00 +03:00
|
|
|
|
2015-08-14 16:54:27 +03:00
|
|
|
this.selectTab(this._currentTabId);
|
|
|
|
|
2015-09-25 14:23:39 +03:00
|
|
|
this._updateTabVisibilities();
|
|
|
|
|
2015-08-14 16:54:27 +03: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 17:09:00 +03:00
|
|
|
}
|
2015-08-14 16:54:27 +03: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 11:52:33 +03:00
|
|
|
$tabsContainer.attr('class', 'tabsContainer');
|
|
|
|
$tabsContainer.addClass(tabView.getTabsContainerExtraClasses());
|
|
|
|
|
2015-08-14 16:54:27 +03: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 13:06:13 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the file info to be displayed in the view
|
|
|
|
*
|
2015-08-12 18:30:20 +03:00
|
|
|
* @param {OCA.Files.FileInfoModel} fileInfo file info to set
|
2015-07-15 13:06:13 +03:00
|
|
|
*/
|
|
|
|
setFileInfo: function(fileInfo) {
|
2015-08-12 18:30:20 +03:00
|
|
|
this.model = fileInfo;
|
2015-07-15 13:06:13 +03:00
|
|
|
|
2015-08-14 16:54:27 +03:00
|
|
|
if (this._dirty) {
|
|
|
|
this.render();
|
2015-09-25 14:23:39 +03:00
|
|
|
} else {
|
|
|
|
this._updateTabVisibilities();
|
2015-08-14 16:54:27 +03:00
|
|
|
}
|
2015-07-15 18:05:25 +03:00
|
|
|
|
2015-08-14 16:54:27 +03: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 13:06:13 +03:00
|
|
|
tabView.setFileInfo(fileInfo);
|
2015-08-14 16:54:27 +03:00
|
|
|
}
|
|
|
|
|
2015-07-15 13:06:13 +03:00
|
|
|
_.each(this._detailFileInfoViews, function(detailView) {
|
|
|
|
detailView.setFileInfo(fileInfo);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-09-25 14:23:39 +03: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 13:06:13 +03:00
|
|
|
/**
|
|
|
|
* Returns the file info.
|
|
|
|
*
|
2015-08-12 18:30:20 +03:00
|
|
|
* @return {OCA.Files.FileInfoModel} file info
|
2015-07-15 13:06:13 +03:00
|
|
|
*/
|
|
|
|
getFileInfo: function() {
|
2015-08-12 18:30:20 +03:00
|
|
|
return this.model;
|
2015-07-15 13:06:13 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a tab in the tab view
|
|
|
|
*
|
|
|
|
* @param {OCA.Files.DetailTabView} tab view
|
|
|
|
*/
|
|
|
|
addTabView: function(tabView) {
|
|
|
|
this._tabViews.push(tabView);
|
2015-08-14 16:54:27 +03:00
|
|
|
this._dirty = true;
|
2015-07-15 13:06:13 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a detail view for file info.
|
|
|
|
*
|
|
|
|
* @param {OCA.Files.DetailFileInfoView} detail view
|
|
|
|
*/
|
|
|
|
addDetailView: function(detailView) {
|
|
|
|
this._detailFileInfoViews.push(detailView);
|
2015-08-14 16:54:27 +03:00
|
|
|
this._dirty = true;
|
2017-06-09 04:14:23 +03: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 13:06:13 +03:00
|
|
|
}
|
2015-08-12 18:30:20 +03:00
|
|
|
});
|
2015-07-15 13:06:13 +03:00
|
|
|
|
|
|
|
OCA.Files.DetailsView = DetailsView;
|
|
|
|
})();
|