1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-06-28 16:08:25 +00:00
seahub/static/scripts/sysadmin-app/views/address-book-group-item.js

141 lines
4.9 KiB
JavaScript
Raw Normal View History

2018-01-04 09:06:34 +00:00
define([
'jquery',
'underscore',
'backbone',
'common',
'moment',
'simplemodal',
2018-01-04 09:06:34 +00:00
'app/views/widgets/hl-item-view'
], function($, _, Backbone, Common, Moment, Simplemodal, HLItemView) {
2018-01-04 09:06:34 +00:00
'use strict';
var GroupView = HLItemView.extend({
tagName: 'tr',
template: _.template($('#address-book-group-item-tmpl').html()),
setQuotaFormTemplate: _.template($('#address-book-group-quota-set-form-tmpl').html()),
2018-01-04 09:06:34 +00:00
events: {
'click .group-delete-btn': 'deleteGroup',
'click .quota-edit-icon': 'setQuota'
2018-01-04 09:06:34 +00:00
},
initialize: function() {
HLItemView.prototype.initialize.call(this);
this.listenTo(this.model, "change", this.render);
2018-01-04 09:06:34 +00:00
},
deleteGroup: function() {
var _this = this;
var group_name = this.model.get('name');
2018-05-07 11:55:41 +00:00
var popupTitle = gettext("Delete Department");
2018-01-04 09:06:34 +00:00
var popupContent = gettext("Are you sure you want to delete %s ?").replace('%s', '<span class="op-target ellipsis ellipsis-op-target" title="' + Common.HTMLescape(group_name) + '">' + Common.HTMLescape(group_name) + '</span>');
var yesCallback = function() {
$.ajax({
url: Common.getUrl({
'name':'admin-address-book-group',
'group_id': _this.model.get('id')
}),
type: 'DELETE',
cache: false,
beforeSend: Common.prepareCSRFToken,
dataType: 'json',
success: function() {
_this.$el.remove();
Common.feedback(gettext("Successfully deleted 1 item."), 'success');
},
error: function(xhr, textStatus, errorThrown) {
Common.ajaxErrorHandler(xhr, textStatus, errorThrown);
},
complete: function() {
$.modal.close();
}
});
};
Common.showConfirm(popupTitle, popupContent, yesCallback);
return false;
},
setQuota: function() {
var model = this.model;
var $form = $(this.setQuotaFormTemplate());
$form.modal();
$('#simplemodal-container').css({'width':'auto', 'height':'auto'});
$form.on('submit', function() {
var $error = $('.error', $form);
var $submitBtn = $('[type="submit"]', $form);
var quota = $.trim($('[name="quota"]', $form).val());
var quota_int = parseInt(quota);
if (!quota) {
$error.html(gettext("It is required.")).show();
return false;
}
if (!(quota_int == quota &&
(quota_int > 0 || quota_int == -2))) {
return false;
}
Common.disableButton($submitBtn);
$.ajax({
url: Common.getUrl({
2018-04-25 08:19:15 +00:00
'name':'admin-group',
'group_id': model.get('id')
}),
type: 'PUT',
cache: false,
beforeSend: Common.prepareCSRFToken,
data: {'quota': quota == -2 ? -2 : quota * 1000000},
dataType: 'json',
success: function(data) {
model.set({'quota': data.quota});
$.modal.close();
},
error: function(xhr) {
var err_msg;
if (xhr.responseText) {
err_msg = $.parseJSON(xhr.responseText).error_msg;
} else {
err_msg = gettext("Failed. Please check the network.");
}
$error.html(err_msg).show();
Common.enableButton($submitBtn);
}
});
return false;
});
},
2018-01-04 09:06:34 +00:00
render: function() {
var data = this.model.toJSON(),
created_at = Moment(data['created_at']);
data['time'] = created_at.format('LLLL');
data['time_from_now'] = Common.getRelativeTimeStr(created_at);
2018-04-25 08:19:15 +00:00
switch(data['quota']) {
case -2: // no limit
data['quota_shown'] = '--';
break;
case -1: // not set or error
data['quota_shown'] = 0;
break;
default:
data['quota_shown'] = Common.quotaSizeFormat(data['quota']);
}
2018-01-04 09:06:34 +00:00
this.$el.html(this.template(data));
return this;
}
});
return GroupView;
});