1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-06-30 08:53:49 +00:00
seahub/static/scripts/app/views/myhome-repos.js

155 lines
5.1 KiB
JavaScript
Raw Normal View History

2015-02-01 06:15:00 +00:00
define([
'jquery',
'underscore',
'backbone',
'common',
'app/collections/repos',
'app/views/repo',
'app/views/add-repo',
], function($, _, Backbone, Common, RepoCollection, RepoView, AddRepoView) {
'use strict';
var ReposView = Backbone.View.extend({
el: $('#repo-tabs'),
2015-05-11 10:39:21 +00:00
reposHdTemplate: _.template($('#my-repos-hd-tmpl').html()),
2015-02-01 06:15:00 +00:00
events: {
2015-04-20 08:38:59 +00:00
'click .repo-create': 'createRepo',
'click #my-own-repos .by-name': 'sortByName',
'click #my-own-repos .by-time': 'sortByTime'
2015-02-01 06:15:00 +00:00
},
initialize: function(options) {
this.$tabs = $('#repo-tabs');
this.$table = this.$('#my-own-repos table');
this.$tableHead = $('thead', this.$table);
this.$tableBody = $('tbody', this.$table);
this.$loadingTip = $('.loading-tip', this.$tabs);
2015-03-30 15:21:50 +00:00
this.$emptyTip = $('#my-own-repos .empty-tips');
2015-04-20 08:38:59 +00:00
this.$repoCreateBtn = this.$('.repo-create');
2015-02-01 06:15:00 +00:00
this.repos = new RepoCollection();
this.listenTo(this.repos, 'add', this.addOne);
this.listenTo(this.repos, 'reset', this.reset);
},
addOne: function(repo, collection, options) {
var view = new RepoView({model: repo});
if (options.prepend) {
this.$tableBody.prepend(view.render().el);
} else {
this.$tableBody.append(view.render().el);
}
},
2015-05-11 10:39:21 +00:00
renderReposHd: function() {
this.$tableHead.html(this.reposHdTemplate());
},
2015-02-01 06:15:00 +00:00
reset: function() {
this.$('.error').hide();
this.$loadingTip.hide();
2015-02-01 06:15:00 +00:00
if (this.repos.length) {
this.$emptyTip.hide();
this.renderReposHd();
this.$tableBody.empty();
this.repos.each(this.addOne, this);
2015-02-01 06:15:00 +00:00
this.$table.show();
} else {
this.$table.hide();
this.$emptyTip.show();
2015-05-12 10:54:10 +00:00
}
if (app.pageOptions.guide_enabled) {
$('#guide-for-new').modal({appendTo: '#main', focus:false});
app.pageOptions.guide_enabled = false;
2015-02-01 06:15:00 +00:00
}
},
showMyRepos: function() {
this.$tabs.show();
$('#mylib-tab').parent().addClass('ui-state-active');
2015-02-01 06:15:00 +00:00
this.$table.hide();
var $loadingTip = this.$loadingTip;
$loadingTip.show();
var _this = this;
this.repos.fetch({
reset: true,
success: function (collection, response, opts) {
},
error: function (collection, response, opts) {
$loadingTip.hide();
var $error = _this.$('.error');
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.');
}
$error.html(err_msg).show();
}
});
2015-02-01 06:15:00 +00:00
},
show: function() {
2015-04-20 08:38:59 +00:00
this.$repoCreateBtn.show();
2015-02-01 06:15:00 +00:00
this.showMyRepos();
},
hide: function() {
2015-04-20 08:38:59 +00:00
this.$repoCreateBtn.hide();
2015-02-01 06:15:00 +00:00
this.$el.hide();
2015-02-04 13:57:26 +00:00
this.$table.hide();
2015-03-31 08:22:35 +00:00
this.$emptyTip.hide();
$('#mylib-tab', this.$tabs).parent().removeClass('ui-state-active');
2015-02-01 06:15:00 +00:00
},
createRepo: function() {
new AddRepoView(this.repos);
2015-02-01 06:15:00 +00:00
},
sortByName: function() {
var repos = this.repos;
var el = $('.by-name', this.$table);
repos.comparator = function(a, b) { // a, b: model
var result = Common.compareTwoWord(a.get('name'), b.get('name'));
if (el.hasClass('icon-caret-up')) {
return -result;
} else {
return result;
}
};
repos.sort();
this.$tableBody.empty();
repos.each(this.addOne, this);
el.toggleClass('icon-caret-up icon-caret-down');
2015-05-11 07:31:03 +00:00
repos.comparator = null;
},
sortByTime: function() {
var repos = this.repos;
var el = $('.by-time', this.$table);
repos.comparator = function(a, b) { // a, b: model
if (el.hasClass('icon-caret-down')) {
return a.get('mtime') < b.get('mtime') ? 1 : -1;
} else {
return a.get('mtime') < b.get('mtime') ? -1 : 1;
}
};
repos.sort();
this.$tableBody.empty();
repos.each(this.addOne, this);
el.toggleClass('icon-caret-up icon-caret-down');
2015-05-11 07:31:03 +00:00
repos.comparator = null;
}
2015-02-01 06:15:00 +00:00
});
return ReposView;
});