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() {
|
|
|
|
var TEMPLATE =
|
|
|
|
'<div>' +
|
2015-08-14 13:54:27 +00:00
|
|
|
' <div class="detailFileInfoContainer">' +
|
|
|
|
' </div>' +
|
|
|
|
' <div>' +
|
|
|
|
' {{#if tabHeaders}}' +
|
|
|
|
' <ul class="tabHeaders">' +
|
|
|
|
' {{#each tabHeaders}}' +
|
|
|
|
' <li class="tabHeader" data-tabid="{{tabId}}" data-tabindex="{{tabIndex}}">' +
|
|
|
|
' <a href="#">{{label}}</a>' +
|
|
|
|
' </li>' +
|
|
|
|
' {{/each}}' +
|
|
|
|
' </ul>' +
|
|
|
|
' {{/if}}' +
|
|
|
|
' <div class="tabsContainer">' +
|
|
|
|
' </div>' +
|
|
|
|
' </div>' +
|
|
|
|
' <a class="close icon-close" href="#" alt="{{closeLabel}}"></a>' +
|
2015-07-15 10:06:13 +00:00
|
|
|
'</div>';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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',
|
|
|
|
className: 'detailsView',
|
2015-07-15 10:06:13 +00:00
|
|
|
|
|
|
|
_template: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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',
|
|
|
|
'click .tabHeaders .tabHeader': '_onClickTab'
|
|
|
|
},
|
|
|
|
|
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-16 10:49:34 +00:00
|
|
|
// uncomment to add some dummy tabs for testing
|
2015-08-14 13:54:27 +00:00
|
|
|
//this._addTestTabs();
|
2015-07-15 10:06:13 +00:00
|
|
|
},
|
|
|
|
|
2015-08-12 15:30:20 +00:00
|
|
|
_onClose: function(event) {
|
|
|
|
OC.Apps.hideAppSidebar();
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2015-07-16 10:21:40 +00:00
|
|
|
_addTestTabs: function() {
|
|
|
|
for (var j = 0; j < 2; j++) {
|
2015-08-14 13:54:27 +00:00
|
|
|
var testView = new OCA.Files.DetailTabView({id: 'testtab' + j});
|
2015-07-16 10:21:40 +00:00
|
|
|
testView.index = j;
|
|
|
|
testView.getLabel = function() { return 'Test tab ' + this.index; };
|
|
|
|
testView.render = function() {
|
|
|
|
this.$el.empty();
|
|
|
|
for (var i = 0; i < 100; i++) {
|
|
|
|
this.$el.append('<div>Test tab ' + this.index + ' row ' + i + '</div>');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this._tabViews.push(testView);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-08-14 13:54:27 +00:00
|
|
|
template: function(vars) {
|
|
|
|
if (!this._template) {
|
|
|
|
this._template = Handlebars.compile(TEMPLATE);
|
|
|
|
}
|
|
|
|
return this._template(vars);
|
|
|
|
},
|
|
|
|
|
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-08-14 13:54:27 +00:00
|
|
|
if (this._tabViews.length > 1) {
|
|
|
|
// only render headers if there is more than one available
|
|
|
|
templateVars.tabHeaders = _.map(this._tabViews, function(tabView, i) {
|
|
|
|
return {
|
|
|
|
tabId: tabView.id,
|
|
|
|
tabIndex: i,
|
|
|
|
label: tabView.getLabel()
|
|
|
|
};
|
|
|
|
});
|
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);
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
// 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-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);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
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;
|
|
|
|
})();
|
|
|
|
|