1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-06-22 05:08:33 +00:00
seahub/static/scripts/sysadmin-app/views/repos.js

202 lines
6.6 KiB
JavaScript
Raw Normal View History

2016-05-25 02:45:22 +00:00
define([
'jquery',
'underscore',
'backbone',
'common',
2016-05-27 08:42:40 +00:00
'sysadmin-app/views/repo',
'sysadmin-app/collection/repos'
], function($, _, Backbone, Common, RepoView, RepoCollection) {
2016-05-25 02:45:22 +00:00
'use strict';
2016-05-27 08:42:40 +00:00
var ReposView = Backbone.View.extend({
2016-05-25 02:45:22 +00:00
2016-05-27 08:42:40 +00:00
id: 'libraries',
2016-05-25 02:45:22 +00:00
tabNavTemplate: _.template($("#libraries-tabnav-tmpl").html()),
2016-05-25 02:45:22 +00:00
template: _.template($("#libraries-tmpl").html()),
2017-05-18 02:43:31 +00:00
libraryAddFormtemplate: _.template($("#library-add-form-tmpl").html()),
2016-05-25 02:45:22 +00:00
initialize: function() {
2016-05-27 08:42:40 +00:00
this.repoCollection = new RepoCollection();
this.listenTo(this.repoCollection, 'add', this.addOne);
this.listenTo(this.repoCollection, 'reset', this.reset);
2016-05-25 02:45:22 +00:00
this.render();
},
render: function() {
var $tabnav = $(this.tabNavTemplate({'cur_tab': 'all'}));
this.$el.append($tabnav);
this.$el.append(this.template());
2016-05-25 02:45:22 +00:00
this.$table = this.$('table');
this.$tableBody = $('tbody', this.$table);
this.$loadingTip = this.$('.loading-tip');
this.$emptyTip = this.$('.empty-tips');
this.$jsPrevious = this.$('.js-previous');
this.$jsNext = this.$('.js-next');
},
events: {
2017-05-18 02:43:31 +00:00
'click .js-add-library': 'addLibrary',
2016-05-25 02:45:22 +00:00
'click #paginator .js-next': 'getNextPage',
'click #paginator .js-previous': 'getPreviousPage'
},
2017-05-18 02:43:31 +00:00
addLibrary: function () {
var $form = $(this.libraryAddFormtemplate()),
repos = this.repoCollection,
_this = this;
$form.modal();
$('#simplemodal-container').css({'height':'auto'});
$('[name="library_owner"]', $form).select2($.extend(
Common.contactInputOptionsForSelect2(), {
width: '268px',
containerCss: {'margin-bottom': '5px'},
maximumSelectionSize: 1,
placeholder: gettext("Search user or enter email and press Enter"), // to override 'placeholder' returned by `Common.conta...`
formatSelectionTooBig: gettext("You cannot select any more choices")
2017-05-18 02:43:31 +00:00
}));
$form.on('submit', function() {
2017-05-18 02:43:31 +00:00
var library_name = $.trim($('[name="library_name"]', $form).val());
var library_owner = $.trim($('[name="library_owner"]', $form).val());
2017-05-18 02:43:31 +00:00
var $error = $('.error', $form);
var $submitBtn = $('[type="submit"]', $form);
if (!library_name) {
$error.html(gettext("Name is required.")).show();
return false;
}
$error.hide();
Common.disableButton($submitBtn);
repos.create({'name': library_name, 'owner': library_owner}, {
2017-05-18 02:43:31 +00:00
prepend: true,
wait: true,
success: function() {
if (repos.length == 1) {
repos.reset(repos.models);
}
Common.closeModal();
},
error: function(collection, response, options) {
2018-07-31 10:15:44 +00:00
var error_msg = Common.prepareAjaxErrorMsg(response);
$error.html(error_msg).show();
2017-05-18 02:43:31 +00:00
Common.enableButton($submitBtn);
}
});
return false;
});
return false;
},
2016-05-25 02:45:22 +00:00
initPage: function() {
this.$table.hide();
this.$tableBody.empty();
this.$loadingTip.show();
this.$emptyTip.hide();
this.$jsNext.hide();
this.$jsPrevious.hide();
},
getNextPage: function() {
this.initPage();
2016-05-27 08:42:40 +00:00
var current_page = this.repoCollection.state.current_page;
if (this.repoCollection.state.has_next_page) {
2016-05-27 08:42:40 +00:00
this.repoCollection.getPage(current_page + 1, {
2016-05-25 02:45:22 +00:00
reset: true
});
}
return false;
},
getPreviousPage: function() {
this.initPage();
2016-05-27 08:42:40 +00:00
var current_page = this.repoCollection.state.current_page;
if (current_page > 1) {
2016-05-27 08:42:40 +00:00
this.repoCollection.getPage(current_page - 1, {
2016-05-25 02:45:22 +00:00
reset: true
});
}
return false;
},
hide: function() {
this.$el.detach();
this.attached = false;
},
show: function(option) {
this.option = option;
if (!this.attached) {
this.attached = true;
$("#right-panel").html(this.$el);
}
this.getContent();
2016-05-25 02:45:22 +00:00
},
getContent: function() {
2016-05-25 02:45:22 +00:00
this.initPage();
var _this = this;
2016-05-27 08:42:40 +00:00
this.repoCollection.fetch({
data: {'page': this.option.page},
cache: false,
2016-05-25 02:45:22 +00:00
reset: true,
error: function(collection, response, opts) {
2018-07-31 10:15:44 +00:00
var err_msg = Common.prepareCollectionFetchErrorMsg(collection, response, opts);
2016-05-25 02:45:22 +00:00
Common.feedback(err_msg, 'error');
},
complete:function() {
_this.$loadingTip.hide();
2016-05-25 02:45:22 +00:00
}
});
},
reset: function() {
// update the url
var current_page = this.repoCollection.state.current_page;
app.router.navigate('all-libs/?page=' + current_page);
2016-05-25 02:45:22 +00:00
this.$loadingTip.hide();
if (this.repoCollection.length > 0) {
2016-05-27 08:42:40 +00:00
this.repoCollection.each(this.addOne, this);
2016-05-25 02:45:22 +00:00
this.$table.show();
this.renderPaginator();
} else {
this.$emptyTip.show();
}
},
renderPaginator: function() {
if (this.repoCollection.state.has_next_page) {
2016-05-25 02:45:22 +00:00
this.$jsNext.show();
} else {
this.$jsNext.hide();
}
2016-05-27 08:42:40 +00:00
var current_page = this.repoCollection.state.current_page;
2016-05-25 02:45:22 +00:00
if (current_page > 1) {
this.$jsPrevious.show();
} else {
this.$jsPrevious.hide();
}
},
2017-05-18 02:43:31 +00:00
addOne: function(library, collection, options) {
2016-05-27 08:42:40 +00:00
var view = new RepoView({model: library});
2017-05-18 02:43:31 +00:00
if (options.prepend) {
this.$tableBody.prepend(view.render().el);
} else {
this.$tableBody.append(view.render().el);
}
2016-05-25 02:45:22 +00:00
}
});
2016-05-27 08:42:40 +00:00
return ReposView;
2016-05-25 02:45:22 +00:00
});