1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-15 16:04:01 +00:00
seahub/media/scripts/app/views/dir.js

197 lines
7.3 KiB
JavaScript
Raw Normal View History

2015-01-25 10:47:42 +00:00
define([
2015-01-30 11:36:42 +00:00
'jquery',
'underscore',
'backbone',
'common',
'file-tree',
'app/collections/dirents',
'app/views/dirents',
'text!' + app.config._tmplRoot + 'dir-op-bar.html',
'text!' + app.config._tmplRoot + 'path-bar.html',
], function($, _, Backbone, Common, FileTree, DirentCollection, DirentView,
DirOpBarTemplate, PathBarTemplate) {
'use strict';
var DirView = Backbone.View.extend({
el: $('#dir-view'),
path_bar_template: _.template(PathBarTemplate),
dir_op_bar_template: _.template(DirOpBarTemplate),
2015-01-30 11:47:17 +00:00
newDirTemplate: _.template($("#add-new-dir-form-template").html()),
newFileTemplate: _.template($("#add-new-file-form-template").html()),
2015-01-30 11:36:42 +00:00
initialize: function(options) {
this.$dirent_list = this.$('.repo-file-list tbody');
this.$path_bar = this.$('.path');
// For compatible with css, we use .repo-op instead of .dir-op
this.$dir_op_bar = this.$('.repo-op');
this.dir = new DirentCollection();
this.listenTo(this.dir, 'add', this.addOne);
this.listenTo(this.dir, 'reset', this.reset);
// initialize common js behavior
$('th .checkbox-orig').unbind();
},
showDir: function(repo_id, path) {
console.log("showDir " + repo_id + " " + path);
this.repo_id = repo_id;
this.path = path;
this.dir.setPath(repo_id, path);
this.dir.fetch({reset: true});
this.$el.show();
},
hide: function() {
this.$el.hide();
},
addOne: function(dirent) {
var view = new DirentView({model: dirent, dirView: this});
this.$dirent_list.append(view.render().el);
},
reset: function() {
this.$dirent_list.empty();
this.dir.each(this.addOne, this);
this.renderPath();
this.renderDirOpBar();
},
renderPath: function() {
2015-01-30 11:47:17 +00:00
var dir = this.dir,
path = dir.path,
obj = {path: path, repo_name: dir.repo_name};
2015-01-30 11:36:42 +00:00
if (path != '/') {
$.extend(obj, {
path_list: path.substr(1).split('/'),
2015-01-30 11:47:17 +00:00
repo_id: dir.repo_id
2015-01-30 11:36:42 +00:00
});
}
2015-01-30 11:47:17 +00:00
2015-01-30 11:36:42 +00:00
this.$path_bar.html(this.path_bar_template(obj));
},
renderDirOpBar: function() {
2015-01-30 11:47:17 +00:00
var dir = this.dir,
user_perm = dir.user_perm;
2015-01-30 11:36:42 +00:00
this.$dir_op_bar.html($.trim(this.dir_op_bar_template({
user_perm: user_perm,
encrypted: dir.encrypted,
path: dir.path,
2015-01-30 11:47:17 +00:00
repo_id: dir.repo_id
2015-01-30 11:36:42 +00:00
})));
},
// Directory Operations
events: {
'click .path-link': 'visitDir',
'click #upload-file': 'uploadFile',
'click #add-new-dir': 'newDir',
'click #add-new-file': 'newFile',
'click #share-cur-dir': 'share',
'click th.select': 'select',
'click #by-name': 'sortByName',
'click #by-time': 'sortByTime',
'click #del-dirents': 'delete',
'click #mv-dirents': 'mv',
'click #cp-dirents': 'cp'
},
newDir: function() {
2015-01-30 11:47:17 +00:00
var form = $(this.newDirTemplate()),
form_id = form.attr('id'),
dir = this.dir,
dirView = this;
form.modal({appendTo:'#main'});
$('#simplemodal-container').css({'height':'auto'});
form.submit(function() {
var dirent_name = $.trim($('input[name="name"]', form).val()),
post_data = {'dirent_name': dirent_name},
post_url = Common.getUrl({name: "new_dir", repo_id: dir.repo_id})
+ '?parent_dir=' + encodeURIComponent(dir.path);
if (!dirent_name) {
Common.showFormError(form_id, gettext("It is required."));
return false;
};
var after_op_success = function(data) {
$.modal.close();
var new_dirent = dir.add({
'is_dir': true,
'obj_name': data['name'],
'last_modified': new Date().getTime() / 1000,
'last_update': gettext("Just now"),
'p_dpath': data['p_dpath']
}, {silent:true});
var view = new DirentView({model: new_dirent, dirView: dirView});
$('tr:first', dirView.$dirent_list).before(view.render().el); // put the new dir as the first one
};
Common.ajaxPost({
'form': form,
'post_url': post_url,
'post_data': post_data,
'after_op_success': after_op_success,
'form_id': form_id
});
return false;
2015-01-30 11:36:42 +00:00
});
},
2015-01-30 11:47:17 +00:00
newFile: function() {
var form = $(this.newFileTemplate()),
form_id = form.attr('id'),
file_name = form.find('input[name="name"]'),
dir = this.dir,
dirView = this;
form.modal({appendTo:'#main'});
$('#simplemodal-container').css({'height':'auto'});
form.find('.set-file-type').on('click', function() {
file_name.val('.' + $(this).data('filetype'));
Common.setCaretPos(file_name[0], 0);
file_name.focus();
});
form.submit(function() {
var dirent_name = $.trim(file_name.val()),
post_data = {'dirent_name': dirent_name},
post_url = Common.getUrl({name: "new_file", repo_id: dir.repo_id})
+ '?parent_dir=' + encodeURIComponent(dir.path);
if (!dirent_name) {
Common.showFormError(form_id, gettext("It is required."));
return false;
};
var after_op_success = function(data) {
location.href = Common.getUrl({name: "repo_new_file", repo_id: dir.repo_id})
+ '?p=' + encodeURIComponent(dir.path) + encodeURIComponent(data['name']);
};
Common.ajaxPost({
'form': form,
'post_url': post_url,
'post_data': post_data,
'after_op_success': after_op_success,
'form_id': form_id
});
return false;
});
}
2015-01-30 11:36:42 +00:00
});
return DirView;
2015-01-25 10:47:42 +00:00
});