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

166 lines
5.0 KiB
JavaScript
Raw Normal View History

2015-01-21 14:22:34 +00:00
define([
'jquery',
'underscore',
'backbone',
'app/collections/group-repos',
'app/collections/dirents',
'app/views/group-repos',
2015-01-22 09:15:17 +00:00
'app/views/add-group-repo',
2015-01-21 14:22:34 +00:00
'app/views/dirents'
2015-01-22 09:15:17 +00:00
], function($, _, Backbone, Repos, DirentCollection, GroupRepoView, AddGroupRepoView, DirentView) {
2015-01-21 14:22:34 +00:00
'use strict';
var GroupView = Backbone.View.extend({
el: '#main',
2015-01-22 09:15:17 +00:00
prepareCsrf: function() { // TODO: move to common
/* alias away the sync method */
Backbone._sync = Backbone.sync;
/* define a new sync method */
Backbone.sync = function(method, model, options) {
/* only need a token for non-get requests */
if (method == 'create' || method == 'update' || method == 'delete') {
// CSRF token value is in an embedded meta tag
// var csrfToken = $("meta[name='csrf_token']").attr('content');
var csrfToken = app.pageOptions.csrfToken;
options.beforeSend = function(xhr){
xhr.setRequestHeader('X-CSRFToken', csrfToken);
};
2015-01-21 14:22:34 +00:00
}
2015-01-22 09:15:17 +00:00
/* proxy the call to the old sync method */
return Backbone._sync(method, model, options);
};
},
events: {
'click #repo-create': 'createRepo',
},
initialize: function() {
this.prepareCsrf();
2015-01-21 14:22:34 +00:00
this.$cont = this.$('#right-panel');
this.$tab = this.$('#tabs div:first-child');
this.$tabCont = this.$('#grp-repos');
this.$tableCont = this.$('#grp-repos table');
2015-01-22 09:15:17 +00:00
this.$createForm = this.$('#repo-create-form');
2015-01-21 14:22:34 +00:00
},
initializeRepos: function() {
this.listenTo(Repos, 'add', this.addOne);
this.listenTo(Repos, 'reset', this.addAll);
this.listenTo(Repos, 'sync', this.render);
2015-01-22 09:15:17 +00:00
// this.listenTo(Repos, 'all', this.render);
2015-01-21 14:22:34 +00:00
},
initializeDirents: function() {
this.listenTo(this.dirents, 'add', this.addOneDirent);
this.listenTo(this.dirents, 'reset', this.addAllDirent);
// this.listenTo(this.dirents, 'sync', this.render);
this.listenTo(this.dirents, 'all', this.renderDirent);
},
addOne: function(repo) {
console.log('add repo: ' + repo);
var view = new GroupRepoView({model: repo});
this.$tableCont.append(view.render().el);
},
addAll: function() {
console.log('add all');
this.resetTable();
Repos.each(this.addOne, this);
},
addOneDirent: function(dirent) {
var view = new DirentView({model: dirent});
this.$tableCont.append(view.render().el);
},
addAllDirent: function() {
this.$tableCont.empty();
this.dirents.each(this.addOneDirent, this);
},
renderDirent: function(eventName) {
console.log('render dirents with event: ' + eventName);
if (this.dirents.length) {
this.$tabCont.show();
}
},
resetTable: function() {
console.log('rest table');
_.each($('#grp-repos table').find('tr'), function(el, idx) {
if (idx != 0) {
$(el).remove(); // remove table content except first row.
}
});
},
hideTable: function() {
this.$tableCont.hide();
},
showTable: function() {
this.$tableCont.show();
},
hideLoading: function() {
this.$cont.find('.loading').hide();
},
showLoading: function() {
this.$cont.find('.loading').show();
},
hideEmptyTips: function() {
this.$cont.find('.empty-tips').hide();
},
showEmptyTips: function() {
this.$cont.find('.empty-tips').show();
},
render: function(eventName) {
this.hideLoading();
if (Repos.length) {
this.hideEmptyTips();
this.showTable();
} else {
this.showEmptyTips();
}
},
showRepoList: function() {
this.initializeRepos();
Repos.fetch({reset: true});
},
showDirentList: function(id, path) {
console.log('show repo page and hide repo list: ' + id + ' ' + path);
var path = path || '/';
this.dirents = new DirentCollection(id, path);
this.initializeDirents();
this.dirents.fetch({reset: true});
// this.dirent_list = new app.DirentListView({id: id, path: path});
// $('#my-own-repos table').children().remove();
// $('#my-own-repos table').append(this.dirent_list.render().el);
2015-01-22 09:15:17 +00:00
},
createRepo: function() {
var view = new AddGroupRepoView();
2015-01-21 14:22:34 +00:00
}
2015-01-22 09:15:17 +00:00
2015-01-21 14:22:34 +00:00
});
return GroupView;
});