1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-01 17:32:21 +00:00
seahub/static/scripts/app/views/group.js

308 lines
11 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',
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,
2016-03-08 05:36:58 +00:00
AddGroupRepoView, 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()),
ownedReposTheadTemplate: _.template($('#group-owned-repos-hd-tmpl').html()),
2017-12-05 09:53:45 +00:00
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);
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});
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,
parent_group_id: this.group.parent_group_id,
show_repo_owner: this.group.show_repo_owner,
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 {
if (this.group.parent_group_id == 0) {
tmpl = this.theadTemplate;
} else {
tmpl = this.ownedReposTheadTemplate;
}
}
this.$tableHead.html(tmpl());
2015-05-11 10:39:21 +00:00
},
reset: function() {
if (this.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': this.repos});
this.repos.each(this.addOne, this);
this.$table.show();
} else {
this.$emptyTip.show();
this.$table.hide();
}
2015-01-21 14:22:34 +00:00
},
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;
}
if (data.parent_group_id == 0) { // common group
user_can_add_repo = true;
_this.group.show_repo_owner = true; // for repo list
_this.group.repos_for_new = _this.repos; // for creating a new library
} else { // address book group
_this.group.show_repo_owner = false;
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({
'name': data.name
});
_this.renderToolbar2({
'id': data.id,
'wiki_enabled': data.wiki_enabled
});
_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) {
var err_msg;
if (xhr.responseText) {
err_msg = JSON.parse(xhr.responseText).error_msg;
2015-11-24 08:55:38 +00:00
} else {
err_msg = gettext("Please check the network.");
}
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) {
var err_msg;
if (response.responseText) {
if (response['status'] == 401 || response['status'] == 403) {
err_msg = gettext("Permission error");
} else {
err_msg = gettext("Error");
}
} else {
err_msg = gettext('Please check the network.');
}
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();
}
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);
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;
});