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'
|
2016-06-07 09:32:01 +00:00
|
|
|
], 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
|
|
|
|
2016-06-07 09:32:01 +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() {
|
2016-06-07 09:32:01 +00:00
|
|
|
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(), {
|
2018-08-03 08:08:29 +00:00
|
|
|
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
|
|
|
}));
|
|
|
|
|
2018-02-05 08:47:56 +00:00
|
|
|
$form.on('submit', function() {
|
2017-05-18 02:43:31 +00:00
|
|
|
var library_name = $.trim($('[name="library_name"]', $form).val());
|
2018-08-03 08:08:29 +00:00
|
|
|
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);
|
|
|
|
|
2018-08-03 08:08:29 +00:00
|
|
|
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;
|
2016-06-07 09:32:01 +00:00
|
|
|
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;
|
2016-06-07 09:32:01 +00:00
|
|
|
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);
|
|
|
|
}
|
2016-06-07 09:32:01 +00:00
|
|
|
this.getContent();
|
2016-05-25 02:45:22 +00:00
|
|
|
},
|
|
|
|
|
2016-06-07 09:32:01 +00:00
|
|
|
getContent: function() {
|
2016-05-25 02:45:22 +00:00
|
|
|
this.initPage();
|
2016-06-07 09:32:01 +00:00
|
|
|
var _this = this;
|
2016-05-27 08:42:40 +00:00
|
|
|
this.repoCollection.fetch({
|
2016-06-07 09:32:01 +00:00
|
|
|
data: {'page': this.option.page},
|
|
|
|
cache: false,
|
2016-05-25 02:45:22 +00:00
|
|
|
reset: true,
|
2016-06-07 09:32:01 +00:00
|
|
|
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');
|
2016-06-07 09:32:01 +00:00
|
|
|
},
|
|
|
|
complete:function() {
|
|
|
|
_this.$loadingTip.hide();
|
2016-05-25 02:45:22 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
reset: function() {
|
2016-06-07 09:32:01 +00:00
|
|
|
// 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();
|
2016-06-07 09:32:01 +00:00
|
|
|
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() {
|
2016-06-07 09:32:01 +00:00
|
|
|
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
|
|
|
|
|
|
|
});
|