1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-06-28 16:08:25 +00:00
seahub/static/scripts/app/views/dirent-grid.js

318 lines
11 KiB
JavaScript
Raw Normal View History

2016-01-31 14:06:40 +00:00
define([
'jquery',
'underscore',
'backbone',
'common',
'file-tree',
'app/views/share',
2016-02-25 08:39:52 +00:00
'app/views/folder-perm',
2016-02-26 06:44:30 +00:00
'app/views/dialogs/dirent-mvcp',
'app/views/dialogs/dirent-rename'
2016-02-25 08:39:52 +00:00
], function($, _, Backbone, Common, FileTree, ShareView, FolderPermView,
2016-02-26 06:44:30 +00:00
DirentMvcpDialog, DirentRenameDialog) {
2016-01-31 14:06:40 +00:00
'use strict';
app = app || {};
app.globalState = app.globalState || {};
var DirentGridView = Backbone.View.extend({
tagName: 'li',
className: 'grid-item',
dirTemplate: _.template($('#grid-view-dir-item-tmpl').html()),
2016-02-22 14:32:52 +00:00
dirOpTemplate: _.template($('#grid-view-dir-op-tmpl').html()),
2016-01-31 14:06:40 +00:00
fileTemplate: _.template($('#grid-view-file-item-tmpl').html()),
2016-02-22 14:32:52 +00:00
fileOpTemplate: _.template($('#grid-view-file-op-tmpl').html()),
2016-02-26 06:44:30 +00:00
renameTemplate: _.template($("#dirent-rename-dialog-template").html()),
2016-01-31 14:06:40 +00:00
initialize: function(options) {
this.dirView = options.dirView;
this.dir = this.dirView.dir;
this.listenTo(this.model, "change", this.render);
this.listenTo(this.model, 'remove', this.remove); // for multi dirents: delete, mv
},
render: function() {
var dir = this.dir;
var dirent_path = this.model.getPath();
2016-01-31 14:06:40 +00:00
var template;
2016-01-31 14:06:40 +00:00
if (this.model.get('is_dir')) {
template = this.dirTemplate;
} else {
template = this.fileTemplate;
}
this.$el.html(template({
dirent: this.model.attributes,
dirent_path: dirent_path,
icon_url: this.model.getIconUrl(192),
2016-02-24 04:11:28 +00:00
url: this.model.getWebUrl(),
2016-01-31 14:06:40 +00:00
category: dir.category,
repo_id: dir.repo_id,
is_repo_owner: dir.is_repo_owner,
2016-02-22 14:32:52 +00:00
is_pro: app.pageOptions.is_pro,
2016-01-31 14:06:40 +00:00
repo_encrypted: dir.encrypted
}));
this.$('.file-locked-icon').attr('title', gettext("locked by {placeholder}").replace('{placeholder}', this.model.get('lock_owner_name')));
this.$el.attr('title', this.model.get('obj_name'));
// for image files
if (this.model.get('is_img')) {
// use specific links such as .img-link, .text-link, in order to make 'open by index' work
this.$('.img-link, .text-link').magnificPopup(this.dirView.magnificPopupOptions);
this.$el.addClass('img-grid-item');
}
2016-01-31 14:06:40 +00:00
return this;
},
events: {
'mouseenter': 'highlight',
'mouseleave': 'rmHighlight',
'click': 'closeMenu',
'click .img-link': 'viewImageWithPopup',
'click .text-link': 'viewImageWithPopup',
'contextmenu': 'showPopupMenu'
2016-01-31 14:06:40 +00:00
},
highlight: function() {
this.$('.img-link').addClass('hl');
this.$('.text-link').addClass('hl');
},
rmHighlight: function() {
this.$('.img-link').removeClass('hl');
this.$('.text-link').removeClass('hl');
},
showPopupMenu: function(e) {
// make sure there is only 1 menu popup
$('.grid-item-op', this.dirView.$dirent_grid).remove();
2016-02-22 14:32:52 +00:00
var dir = this.dir;
var template;
2016-01-31 14:06:40 +00:00
2016-02-22 14:32:52 +00:00
if (this.model.get('is_dir')) {
template = this.dirOpTemplate;
} else {
template = this.fileOpTemplate;
2016-01-31 14:06:40 +00:00
}
2016-02-22 14:32:52 +00:00
this.$('.img-link').addClass('hl');
this.$('.text-link').addClass('hl');
2016-01-31 14:06:40 +00:00
2016-02-24 04:11:28 +00:00
var op = template({
dirent: this.model.attributes,
dirent_path: this.model.getPath(),
download_url: this.model.getDownloadUrl(),
2016-02-24 04:11:28 +00:00
category: dir.category,
repo_id: dir.repo_id,
is_repo_owner: dir.is_repo_owner,
can_generate_share_link: app.pageOptions.can_generate_share_link,
can_generate_upload_link: app.pageOptions.can_generate_upload_link,
2016-02-24 04:11:28 +00:00
is_pro: app.pageOptions.is_pro,
2017-05-18 03:46:43 +00:00
file_audit_enabled: app.pageOptions.file_audit_enabled,
2016-02-24 04:11:28 +00:00
repo_encrypted: dir.encrypted
});
this.$el.append(op);
var el_pos = this.$el.offset();
2016-02-24 04:11:28 +00:00
this.$('.grid-item-op').css({
'position': 'absolute',
'left': e.pageX - el_pos.left,
'top': e.pageY - el_pos.top
2016-02-24 04:11:28 +00:00
});
2016-02-25 05:47:19 +00:00
// Using _.bind(function, object) to make that whenever the function is
// called, the value of this will be the object.
this.$('.download-dir').on('click', _.bind(this.download, this));
this.$('.delete').on('click', _.bind(this.del, this));
this.$('.share').on('click', _.bind(this.share, this));
this.$('.mv').on('click', _.bind(this.mvcp, this));
this.$('.cp').on('click', _.bind(this.mvcp, this));
this.$('.rename').on('click', _.bind(this.rename, this));
this.$('.open-via-client').on('click', _.bind(this.open_via_client, this));
this.$('.lock-file').on('click', _.bind(this.lockFile, this));
this.$('.unlock-file').on('click', _.bind(this.unlockFile, this));
2017-05-18 03:46:43 +00:00
this.$('.view-details').on('click', _.bind(this.viewDetails, this));
2016-03-01 13:24:48 +00:00
this.$('.set-folder-permission').on('click', _.bind(this.setFolderPerm, this));
2016-02-24 09:11:15 +00:00
2016-02-24 04:11:28 +00:00
return false;
2016-02-24 09:11:15 +00:00
},
viewImageWithPopup: function() {
if (this.model.get('is_img')) {
var index = $('.img-grid-item', this.dirView.$dirent_grid).index(this.$el);
$.magnificPopup.open(this.dirView.magnificPopupOptions, index); // open by index
}
},
closeMenu: function() {
2016-02-25 05:47:19 +00:00
this.$('.grid-item-op').remove();
},
2017-01-07 06:18:57 +00:00
download: function() {
this.closeMenu();
2017-01-07 09:55:16 +00:00
var dir = this.dir;
2017-01-07 06:18:57 +00:00
var obj_name = this.model.get('obj_name');
2017-01-07 09:55:16 +00:00
Common.zipDownload(dir.repo_id, dir.path, obj_name);
2017-01-07 06:18:57 +00:00
return false;
},
del: function() {
var _this = this;
if (this.model.get('is_img')) {
var index = $('.img-grid-item', this.dirView.$dirent_grid).index(this.$el);
}
this.closeMenu();
2016-02-25 05:47:19 +00:00
var dirent_name = this.model.get('obj_name');
this.model.deleteFromServer({
2016-02-24 09:11:15 +00:00
success: function(data) {
var msg = gettext("Successfully deleted %(name)s")
2016-07-04 07:31:55 +00:00
.replace('%(name)s', dirent_name);
2016-02-24 09:11:15 +00:00
Common.feedback(msg, 'success');
if (_this.model.get('is_img')) {
_this.dirView.updateMagnificPopupOptions({'op':'delete-item', 'index':index});
}
2016-02-24 09:11:15 +00:00
},
error: function(xhr) {
Common.ajaxErrorHandler(xhr);
}
});
return false;
},
2016-02-22 14:32:52 +00:00
2016-02-25 05:47:19 +00:00
share: function() {
var dir = this.dir,
obj_name = this.model.get('obj_name'),
dirent_path = this.model.getPath();
var options = {
'is_repo_owner': dir.is_repo_owner,
'is_virtual': dir.is_virtual,
'user_perm': this.model.get('perm'),
'repo_id': dir.repo_id,
'repo_encrypted': false,
'is_dir': this.model.get('is_dir') ? true : false,
'dirent_path': dirent_path,
'obj_name': obj_name
};
new ShareView(options);
this.closeMenu();
2016-02-25 05:47:19 +00:00
return false;
2016-02-25 08:39:52 +00:00
},
mvcp: function(e) {
var op_type = $(e.currentTarget).hasClass('mv') ? 'mv' : 'cp';
2016-02-25 08:39:52 +00:00
var options = {
'dir': this.dir,
'dirent': this.model,
'op_type': op_type
};
if (this.model.get('is_img') && op_type == 'mv') {
var index = $('.img-grid-item', this.dirView.$dirent_grid).index(this.$el);
$.extend(options, {
'dirView': this.dirView,
'imgIndex': index
});
}
2016-02-25 08:39:52 +00:00
new DirentMvcpDialog(options);
this.closeMenu();
2016-02-25 08:39:52 +00:00
return false;
2016-02-26 06:44:30 +00:00
},
rename: function() {
this.closeMenu();
2016-02-26 06:44:30 +00:00
var options = {
'dir': this.dir,
'dirent': this.model
};
if (this.model.get('is_img')) {
var index = $('.img-grid-item', this.dirView.$dirent_grid).index(this.$el);
$.extend(options, {
'dirView': this.dirView,
'imgIndex': index
});
}
2016-02-26 06:44:30 +00:00
new DirentRenameDialog(options);
return false;
},
2016-03-01 13:24:48 +00:00
setFolderPerm: function() {
var options = {
'obj_name': this.model.get('obj_name'),
'dir_path': this.dir.path,
'repo_id': this.dir.repo_id
};
new FolderPermView(options);
return false;
},
lockFile: function() {
this.closeMenu();
this.model.lockFile({
success: function() {
},
error: function(xhr) {
Common.ajaxErrorHandler(xhr);
}
});
return false;
},
unlockFile: function() {
this.closeMenu();
this.model.unlockFile({
success: function() {
},
error: function(xhr) {
Common.ajaxErrorHandler(xhr);
}
});
return false;
},
2017-05-18 03:46:43 +00:00
viewDetails: function() {
var file_icon_size = Common.isHiDPI() ? 48 : 24;
var data = {
icon_url: this.model.getIconUrl(file_icon_size),
big_icon_url: this.model.getIconUrl(192),
dirent: this.model.attributes,
thumbnail_url: '',
path: this.dir.repo_name + this.dir.path
};
if (app.pageOptions.enable_thumbnail &&
!this.dir.encrypted &&
(this.model.get('is_img') || this.model.get('is_video'))) {
data.thumbnail_url = Common.getUrl({
'name': 'thumbnail_get',
'repo_id': this.dir.repo_id,
'path': Common.encodePath(Common.pathJoin([this.dir.path, this.model.get('obj_name')])),
'size': 1024
});
}
this.dirView.direntDetailsView.show(data);
this.closeMenu();
return false;
},
open_via_client: function() {
this.closeMenu();
return true;
2016-02-25 05:47:19 +00:00
}
2016-01-31 14:06:40 +00:00
});
return DirentGridView;
});