2016-02-26 06:44:30 +00:00
|
|
|
define([
|
|
|
|
'jquery',
|
|
|
|
'underscore',
|
|
|
|
'backbone',
|
|
|
|
'common',
|
|
|
|
], function($, _, Backbone, Common) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var DirentRenameDialog = Backbone.View.extend({
|
|
|
|
|
|
|
|
template: _.template($("#dirent-rename-dialog-template").html()),
|
|
|
|
|
|
|
|
initialize: function(options) {
|
|
|
|
this.dirent = options.dirent;
|
|
|
|
this.dir = options.dir;
|
|
|
|
|
2017-02-25 09:36:07 +00:00
|
|
|
this.dirView = options.dirView;
|
|
|
|
this.imgIndex = options.imgIndex;
|
|
|
|
|
2016-02-26 06:44:30 +00:00
|
|
|
this.render();
|
2017-12-05 09:53:45 +00:00
|
|
|
this.$el.modal({focus: false});
|
2016-02-26 06:44:30 +00:00
|
|
|
$('#simplemodal-container').css({'width':'auto', 'height':'auto'});
|
|
|
|
|
2016-08-12 11:10:49 +00:00
|
|
|
var $input = this.$('[name="newname"]');
|
|
|
|
var dot_index = this.dirent.get('obj_name').lastIndexOf('.');
|
|
|
|
if (!this.dirent.get('is_dir') && dot_index != -1) {
|
2018-02-05 08:47:56 +00:00
|
|
|
$input.trigger('focus');
|
2016-08-12 11:10:49 +00:00
|
|
|
$input[0].setSelectionRange(0, dot_index);
|
|
|
|
} else {
|
2018-02-05 08:47:56 +00:00
|
|
|
$input.trigger('select');
|
2016-08-12 11:10:49 +00:00
|
|
|
}
|
|
|
|
|
2016-02-26 06:44:30 +00:00
|
|
|
this.$error = this.$('.error');
|
|
|
|
this.$form = this.$('form');
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
var title = this.dirent.get('is_dir') ?
|
|
|
|
gettext("Rename Folder") : gettext("Rename File");
|
|
|
|
|
|
|
|
this.$el.html(this.template({
|
|
|
|
form_title: title,
|
|
|
|
dirent_name: this.dirent.get('obj_name')
|
|
|
|
}));
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
events: {
|
|
|
|
'submit form': 'formSubmit'
|
|
|
|
},
|
|
|
|
|
|
|
|
formSubmit: function() {
|
|
|
|
var _this = this;
|
|
|
|
var new_name = $.trim(this.$('[name="newname"]').val());
|
2017-04-14 06:03:25 +00:00
|
|
|
var err_msg;
|
2016-02-26 06:44:30 +00:00
|
|
|
|
|
|
|
if (!new_name) {
|
2017-04-14 06:03:25 +00:00
|
|
|
err_msg = gettext("It is required.");
|
|
|
|
this.$error.html(err_msg).removeClass('hide');
|
2016-02-26 06:44:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-04-14 06:03:25 +00:00
|
|
|
|
|
|
|
if (new_name.indexOf('/') != -1) {
|
|
|
|
err_msg = gettext("Name should not include '/'.");
|
|
|
|
this.$error.html(err_msg).removeClass('hide');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-26 06:44:30 +00:00
|
|
|
if (new_name == this.dirent.get('obj_name')) {
|
|
|
|
$.modal.close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var $submit_btn = this.$('[type="submit"]');
|
|
|
|
Common.disableButton($submit_btn);
|
|
|
|
|
|
|
|
this.dirent.rename({
|
|
|
|
newname: new_name,
|
|
|
|
success: function() {
|
|
|
|
$.modal.close();
|
2017-02-25 09:36:07 +00:00
|
|
|
|
|
|
|
if (_this.dirent.get('is_img')) {
|
|
|
|
_this.dirView.updateMagnificPopupOptions({
|
|
|
|
'op': 'update-item',
|
|
|
|
'index': _this.imgIndex,
|
|
|
|
'model': _this.dirent
|
|
|
|
});
|
|
|
|
}
|
2016-02-26 06:44:30 +00:00
|
|
|
},
|
|
|
|
error: function(xhr, textStatus, errorThrown) {
|
2018-07-31 10:15:44 +00:00
|
|
|
var error_msg = Common.prepareAjaxErrorMsg(xhr);
|
|
|
|
_this.$error.html(error_msg).removeClass('hide');
|
2016-02-26 06:44:30 +00:00
|
|
|
Common.enableButton($submit_btn);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return DirentRenameDialog;
|
|
|
|
});
|