1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-01 01:12:41 +00:00
seahub/static/scripts/app/views/groups.js

164 lines
5.4 KiB
JavaScript
Raw Normal View History

2015-11-30 07:02:03 +00:00
define([
'jquery',
'underscore',
'backbone',
'common',
'app/collections/groups',
'app/views/repo-details',
2015-11-30 07:02:03 +00:00
'app/views/group-item'
], function($, _, Backbone, Common, Groups, RepoDetailsView,
GroupItemView) {
2015-11-30 07:02:03 +00:00
'use strict';
var GroupsView = Backbone.View.extend({
2017-12-05 09:53:45 +00:00
el: '.main-panel',
template: _.template($('#groups-tmpl').html()),
2017-12-05 09:53:45 +00:00
toolbarTemplate: _.template($('#groups-toolbar-tmpl').html()),
addGroupTemplate: _.template($('#add-group-form-tmpl').html()),
2015-11-30 07:02:03 +00:00
initialize: function(options) {
this.groups = new Groups();
this.listenTo(this.groups, 'add', this.addOne);
this.listenTo(this.groups, 'reset', this.reset);
this.repoDetailsView = new RepoDetailsView({'parentView': this});
2015-11-30 07:02:03 +00:00
},
events: {
'click #add-group': 'addGroup'
},
addOne: function(group, collection, options) {
var view = new GroupItemView({
model: group,
repoDetailsView: this.repoDetailsView
2015-11-30 07:02:03 +00:00
});
if (options.prepend) {
this.$groupList.prepend(view.render().el);
} else {
this.$groupList.append(view.render().el);
}
},
reset: function() {
this.$error.hide();
this.$loadingTip.hide();
if (this.groups.length) {
this.$emptyTip.hide();
this.$groupList.empty();
this.groups.each(this.addOne, this);
this.$groupList.show();
} else {
this.$emptyTip.show();
this.$groupList.hide();
}
},
2017-12-05 09:53:45 +00:00
renderToolbar: function() {
this.$toolbar = $('<div class="cur-view-toolbar" id="groups-toolbar"></div>').html(this.toolbarTemplate());
this.$('.common-toolbar').before(this.$toolbar);
},
renderMainCon: function() {
this.$mainCon = $('<div class="main-panel-main main-panel-main-with-side" id="groups"></div>').html(this.template());
2017-12-05 09:53:45 +00:00
this.$el.append(this.$mainCon);
this.$loadingTip = this.$('.loading-tip');
this.$groupList = this.$('#group-list');
this.$emptyTip = this.$('.empty-tips');
this.$error = this.$('.error');
},
showGroups: function() {
2015-11-30 07:02:03 +00:00
this.$groupList.hide();
this.$emptyTip.hide();
this.$loadingTip.show();
var _this = this;
this.groups.fetch({
cache: false,
2015-12-02 03:25:11 +00:00
data: {'with_repos': 1}, // list repos of every group
2015-11-30 07:02:03 +00:00
reset: true,
success: function (collection, response, opts) {
},
2015-11-30 07:02:03 +00:00
error: function (collection, response, opts) {
_this.$loadingTip.hide();
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.');
}
_this.$error.html(err_msg).show();
}
});
},
show: function() {
2017-12-05 09:53:45 +00:00
this.renderToolbar();
this.renderMainCon();
this.showGroups();
2015-11-30 07:02:03 +00:00
},
hide: function() {
2017-12-05 09:53:45 +00:00
this.$toolbar.detach();
this.$mainCon.detach();
2015-11-30 07:02:03 +00:00
},
addGroup: function () {
2017-12-05 09:53:45 +00:00
var $form = $(this.addGroupTemplate());
2015-11-30 07:02:03 +00:00
$form.modal();
$('#simplemodal-container').css({'height':'auto'});
var groups = this.groups;
var _this = this;
$form.on('submit', function() {
2015-11-30 07:02:03 +00:00
var group_name = $.trim($('[name="group_name"]', $form).val());
2016-07-20 06:20:19 +00:00
var $error = $('.error', $form);
var $submitBtn = $('[type="submit"]', $form);
2015-11-30 07:02:03 +00:00
if (!group_name) {
2016-07-20 06:20:19 +00:00
$error.html(gettext("It is required.")).show();
2015-11-30 07:02:03 +00:00
return false;
}
2016-07-20 06:20:19 +00:00
$error.hide();
Common.disableButton($submitBtn);
groups.create({'name': group_name, 'repos':[]}, {
2015-11-30 07:02:03 +00:00
wait: true,
validate: true,
prepend: true, // show newly created group at the top
2015-11-30 07:02:03 +00:00
success: function() {
if (groups.length == 1) {
_this.reset();
}
app.ui.sideNavView.updateGroups();
2016-07-20 06:20:19 +00:00
Common.closeModal();
},
error: function(collection, response, options) {
var err_msg;
if (response.responseText) {
err_msg = response.responseJSON.error_msg;
} else {
err_msg = gettext('Please check the network.');
}
2016-07-20 06:20:19 +00:00
$error.html(err_msg).show();
Common.enableButton($submitBtn);
}
});
2015-11-30 07:02:03 +00:00
return false;
});
}
});
return GroupsView;
});