1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-06-30 00:42:53 +00:00
seahub/static/scripts/app/views/group.js

331 lines
12 KiB
JavaScript
Raw Normal View History

2015-01-21 14:22:34 +00:00
define([
'jquery',
'underscore',
'backbone',
2015-01-27 09:32:31 +00:00
'common',
2015-01-21 14:22:34 +00:00
'app/collections/group-repos',
'app/collections/group-owned-repos', // for address book group
2015-04-11 07:14:56 +00:00
'app/views/group-repo',
2015-12-11 12:41:16 +00:00
'app/views/add-group-repo',
'app/views/add-department-repo',
'app/views/repo-details',
2016-01-06 08:28:26 +00:00
'app/views/group-members',
2016-03-08 05:36:58 +00:00
'app/views/group-discussions',
2016-01-06 08:28:26 +00:00
'app/views/group-settings'
], function($, _, Backbone, Common, GroupRepos, GroupOwnedRepos, GroupRepoView,
AddGroupRepoView, AddDepartmentRepoView, RepoDetailsView,
GroupMembersView, GroupDiscussionsView, GroupSettingsView) {
2015-01-21 14:22:34 +00:00
'use strict';
var GroupView = Backbone.View.extend({
2017-12-05 09:53:45 +00:00
el: '.main-panel',
2015-01-21 14:22:34 +00:00
template: _.template($('#group-tmpl').html()),
2017-12-05 09:53:45 +00:00
toolbarTemplate: _.template($('#group-toolbar-tmpl').html()),
toolbar2Template: _.template($('#group-toolbar2-tmpl').html()),
pathTemplate: _.template($('#group-path-tmpl').html()),
theadTemplate: _.template($('#shared-repos-hd-tmpl').html()),
theadMobileTemplate: _.template($('#shared-repos-hd-mobile-tmpl').html()),
2015-05-11 10:39:21 +00:00
2015-01-22 09:15:17 +00:00
events: {
2016-01-06 08:28:26 +00:00
'click #group-settings-icon': 'toggleSettingsPanel',
2015-12-11 12:41:16 +00:00
'click #group-members-icon': 'toggleMembersPanel',
2016-03-08 05:36:58 +00:00
'click #group-discussions-icon': 'toggleDiscussionsPanel',
2017-12-05 09:53:45 +00:00
'click #group-toolbar .repo-create': 'createRepo',
'click #group-repos .by-name': 'sortByName',
'click #group-repos .by-time': 'sortByTime'
2015-01-22 09:15:17 +00:00
},
initialize: function(options) {
2016-01-30 08:23:52 +00:00
this.group = {}; // will be fetched when rendering the top bar
this.repos = new GroupRepos();
this.listenTo(this.repos, 'add', this.addOne);
this.listenTo(this.repos, 'reset', this.reset);
this.ownedRepos = new GroupOwnedRepos();
this.listenTo(this.ownedRepos, 'add', this.addOne);
// for adding the first owned repo when there is no repos in the group.
this.listenTo(this.ownedRepos, 'reset', this.resetOwnedRepos);
2017-12-05 09:53:45 +00:00
this.settingsView = new GroupSettingsView({groupView: this});
this.membersView = new GroupMembersView({groupView: this});
this.discussionsView = new GroupDiscussionsView({groupView: this});
this.repoDetailsView = new RepoDetailsView({'parentView': this});
2015-01-21 14:22:34 +00:00
},
2015-01-27 09:32:31 +00:00
addOne: function(repo, collection, options) {
// for newly created group owned repo, returned by 'POST' request
if (!repo.get('size_formatted') && repo.get('size') == 0
&& !repo.get('mtime_relative')) {
repo.set({
'size_formatted': '0 bytes', // no trans here
'mtime_relative': gettext("Just now")
});
}
var view = new GroupRepoView({
model: repo,
group_id: this.group_id,
show_repo_owner: true,
repoDetailsView: this.repoDetailsView,
is_staff: this.repos.is_staff
});
2015-01-27 09:32:31 +00:00
if (options.prepend) {
2015-01-31 05:17:47 +00:00
this.$tableBody.prepend(view.render().el);
2015-01-27 09:32:31 +00:00
} else {
this.$tableBody.append(view.render().el);
}
2015-01-21 14:22:34 +00:00
},
2017-12-05 09:53:45 +00:00
renderThead: function() {
var tmpl;
if ($(window).width() < 768) {
tmpl = this.theadMobileTemplate;
} else {
tmpl = this.theadTemplate;
}
this.$tableHead.html(tmpl());
2015-05-11 10:39:21 +00:00
},
reset: function(options) {
var repos = options.repos || this.repos;
if (repos.length) {
this.$emptyTip.hide();
2017-12-05 09:53:45 +00:00
this.renderThead();
this.$tableBody.empty();
2016-09-08 08:26:23 +00:00
// sort
Common.updateSortIconByMode({'context': this.$el});
Common.sortLibs({'libs': repos});
2016-09-08 08:26:23 +00:00
repos.each(this.addOne, this);
this.$table.show();
} else {
this.showEmptyTip();
this.$table.hide();
}
},
resetOwnedRepos: function() {
// no repos in the group
if (this.repos.length == 0) {
this.reset({repos: this.ownedRepos});
}
2015-01-21 14:22:34 +00:00
},
showEmptyTip: function() {
if (this.group.is_address_book_group) {
if (this.group.user_is_admin) {
this.$emptyTip.filter('.address-book-group-empty-tips-for-admin').show();
} else {
this.$emptyTip.filter('.address-book-group-empty-tips-for-member').show();
}
} else {
this.$emptyTip.filter('.common-group-empty-tips').show();
}
},
2017-12-05 09:53:45 +00:00
showGroup: function(options) {
2015-11-24 08:55:38 +00:00
var _this = this;
2017-12-05 09:53:45 +00:00
this.$table.hide();
this.$emptyTip.hide();
this.$error.hide();
this.$loadingTip.show();
2015-11-24 08:55:38 +00:00
$.ajax({
url: Common.getUrl({
'name': 'group',
'group_id': this.group_id
2015-11-24 08:55:38 +00:00
}),
cache: false,
dataType: 'json',
2017-12-05 09:53:45 +00:00
success: function(data) {
2016-01-30 08:23:52 +00:00
_this.group = data;
var user_can_add_repo = false;
var user_is_admin = false;
if ($.inArray(app.pageOptions.username, data.admins) != -1) {
user_is_admin = true;
}
_this.group.is_address_book_group = false;
_this.group.user_is_admin = user_is_admin;
if (data.parent_group_id == 0) { // common group
user_can_add_repo = true;
_this.group.repos_for_new = _this.repos; // for creating a new library
} else { // address book group
_this.group.is_address_book_group = true;
if (app.pageOptions.is_pro && user_is_admin) {
user_can_add_repo = true;
_this.group.repos_for_new = _this.ownedRepos; // for creating a new library
}
}
if (user_can_add_repo) {
_this.$toolbar.removeClass('hide'); // show 'New Library' button
} else {
_this.$toolbar.addClass('hide');
}
2017-12-05 09:53:45 +00:00
_this.renderPath({
2018-05-07 11:55:41 +00:00
'name': data.name,
'is_address_book_group': _this.group.is_address_book_group
2017-12-05 09:53:45 +00:00
});
_this.renderToolbar2({
'id': data.id,
'wiki_enabled': data.wiki_enabled
});
// for common member in a group, there is only 'leave group' option in the 'settings' popup
// in an address book group, a common member can't 'leave group'
if (data.parent_group_id != 0 && !user_is_admin) {
$('#group-settings-icon').hide();
}
2017-12-05 09:53:45 +00:00
_this.showRepoList();
2016-03-28 10:12:25 +00:00
if (options) {
if (options.showDiscussions) {
_this.showDiscussions();
}
}
2015-11-24 08:55:38 +00:00
},
error: function(xhr) {
2018-07-31 10:15:44 +00:00
var err_msg = Common.prepareAjaxErrorMsg(xhr);
2017-12-05 09:53:45 +00:00
_this.$toolbar.addClass('hide');
_this.$path.empty();
_this.$toolbar2.empty();
_this.$loadingTip.hide();
_this.$error.html(err_msg).show();
2015-11-24 08:55:38 +00:00
}
});
},
2017-12-05 09:53:45 +00:00
showRepoList: function() {
var _this = this;
2017-12-05 09:53:45 +00:00
this.repos.setGroupID(this.group_id);
this.repos.fetch({
2015-10-08 09:54:59 +00:00
cache: false,
reset: true,
data: {from: 'web'},
2017-12-05 09:53:45 +00:00
success: function(collection, response, opts) {
2016-01-30 08:23:52 +00:00
},
2017-12-05 09:53:45 +00:00
error: function(collection, response, opts) {
2018-07-31 10:15:44 +00:00
var err_msg = Common.prepareCollectionFetchErrorMsg(collection, response, opts);
2017-12-05 09:53:45 +00:00
_this.$error.html(err_msg).show();
},
complete: function() {
_this.$loadingTip.hide();
}
});
2015-01-21 14:22:34 +00:00
},
2017-12-05 09:53:45 +00:00
renderToolbar: function() {
this.$toolbar = $('<div class="cur-view-toolbar hide" id="group-toolbar"></div>')
.html(this.toolbarTemplate());
2017-12-05 09:53:45 +00:00
this.$('.common-toolbar').before(this.$toolbar);
},
renderMainCon: function() {
this.$mainCon = $('<div class="main-panel-main main-panel-main-with-side" id="group"></div>')
.html(this.template());
2017-12-05 09:53:45 +00:00
this.$el.append(this.$mainCon);
this.$path = $('.group-path', this.$mainCon);
this.$toolbar2 = $('.group-toolbar-2', this.$mainCon);
this.$table = $('table', this.$mainCon);
this.$tableHead = $('thead', this.$table);
this.$tableBody = $('tbody', this.$table);
this.$loadingTip = $('.loading-tip', this.$mainCon);
this.$emptyTip = this.$('#group-repos .empty-tips');
2017-12-05 09:53:45 +00:00
this.$error = $('.error', this.$mainCon);
},
renderPath: function(data) {
this.$path.html(this.pathTemplate(data));
},
renderToolbar2: function(data) {
this.$toolbar2.html(this.toolbar2Template(data));
},
// update group name. e.g 'rename'
updateName: function(new_name) {
this.group.name = new_name;
$('.group-name', this.$mainCon).html(Common.HTMLescape(new_name));
2015-01-21 14:22:34 +00:00
},
show: function(group_id, options) {
2017-12-05 09:53:45 +00:00
if (!$('#group').length) {
this.renderToolbar();
this.renderMainCon();
}
// when switch from a group to another group
if (this.repoDetailsView.$el.is(':visible')) {
this.repoDetailsView.hide();
}
2017-12-05 09:53:45 +00:00
this.group_id = group_id;
this.showGroup(options);
},
hide: function() {
2017-12-05 09:53:45 +00:00
this.$toolbar.detach();
this.$mainCon.detach();
2015-01-21 14:22:34 +00:00
},
2015-01-22 09:15:17 +00:00
createRepo: function() {
var repos = this.group.repos_for_new;
repos.setGroupID(this.group_id);
if (this.group.is_address_book_group) {
new AddDepartmentRepoView(repos);
} else {
new AddGroupRepoView(repos);
}
2015-01-29 06:45:26 +00:00
},
sortByName: function() {
2016-09-08 08:26:23 +00:00
Common.toggleSortByNameMode();
Common.updateSortIconByMode({'context': this.$el});
Common.sortLibs({'libs': this.repos});
this.$tableBody.empty();
this.repos.each(this.addOne, this);
this.repos.comparator = null;
return false;
},
sortByTime: function() {
2016-09-08 08:26:23 +00:00
Common.toggleSortByTimeMode();
Common.updateSortIconByMode({'context': this.$el});
Common.sortLibs({'libs': this.repos});
this.$tableBody.empty();
this.repos.each(this.addOne, this);
this.repos.comparator = null;
return false;
2015-04-11 07:14:56 +00:00
},
2016-01-06 08:28:26 +00:00
toggleSettingsPanel: function() {
return this.settingsView.toggle();
2016-01-06 08:28:26 +00:00
},
2015-12-15 03:33:07 +00:00
toggleMembersPanel: function() {
return this.membersView.toggle();
2016-03-08 05:36:58 +00:00
},
showDiscussions: function() {
return this.discussionsView.show();
2016-03-08 05:36:58 +00:00
},
toggleDiscussionsPanel: function() {
return this.discussionsView.toggle();
2015-01-21 14:22:34 +00:00
}
2015-01-22 09:15:17 +00:00
2015-01-21 14:22:34 +00:00
});
return GroupView;
});