},
full: function() {
- return this.pluck('path').join('/') || '/';
+ return this.pluck('name').join('/').replace(/^\/+/, '/');
}
});
initialize: function(options) {
this.view = options.view;
- this.listenTo(this.model, 'change', this.render);
+ this.listenTo(this.model, 'change:checked', this.toggleChecked);
this.listenTo(this.model, 'remove', this.remove);
this.listenTo(this.model, 'hidden', this.toggleHidden);
},
var data = this.model.toJSON();
data['url_params'] = $.param({
host: this.view.host,
- path: this.view.path.full(),
- name: this.model.get('name')
+ path: this.view.path.full() + '/' + this.model.get('name')
});
data['cid'] = this.model.cid;
},
toggleHidden: function() {
- this.$el.toggleClass('hidden', this.model.isHidden() && !this.view.showHidden());
+ var isHidden = this.model.isHidden() && !this.view.showHidden();
+ this.$el.toggleClass('hidden', isHidden);
+
+ if (isHidden && this.model.get('checked'))
+ this.model.toggle();
+ },
+
+ toggleChecked: function(obj, value) {
+ this.$el.toggleClass('active', value);
+ this.$el.find('input[type="checkbox"]').prop('checked', value);
},
click: function(e) {
render: function() {
if (this.model.get('active')) {
- this.$el.text(this.model.get('text'));
+ this.$el.text(this.model.get('name'));
}
else {
this.$el.html($('<a/>', {
href: '#',
- text: this.model.get('text')
+ text: this.model.get('name')
}));
}
this.$el.toggleClass('active', this.model.get('active'));
lockOptgroupOrder: true,
create: function(input, callback) {
var $form = $('#favorite-form'),
- parts = input.split('/', 1),
+ parts = input.split('/'),
callback_called = false;
- $form.find('#id_host').val(parts[0]);
+ $form[0].reset();
+ $form.find('.alert-danger').remove();
+ $form.find('#id_host').val(parts.shift());
- if (parts.length > 1)
- $form.find('#id_path').val(parts[1]);
+ if (parts.length)
+ $form.find('#id_path').val(parts.join('/'));
+ $form.off();
$form.on('submit', function(e) {
var $this = $(this),
$btn = $this.find('[type="submit"]');
e.preventDefault();
$btn.button('loading');
+ $this.find('.alert-danger').remove();
$.post($this.attr('action'), $this.serialize(), function(data) {
callback(data);
$this.modal('hide');
$btn.button('reset');
- }, 'json').fail(function() {
+ }, 'json').fail(function(xhr) {
console.error(arguments);
- $btn.button('error');
+ var error = (xhr.responseJSON || {}).error || undefined;
+
+ if (error.__all__) {
+ error = 'Podana lokalizacja jest już zapisana'
+ }
+ else if (xhr.status == 400) {
+ error = 'Niepoprawna nazwa hosta lub ścieżka'
+ }
+
+ if (typeof error === 'string') {
+ $('<div>', {
+ 'class': 'alert alert-danger',
+ html: '<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span> ' + error
+ }).prependTo($this.find('.modal-body'));
+ $btn.button('reset');
+ }
+ else {
+ $btn.button('error');
+ }
});
});
return '<div class="create">Dodaj <em>' + escape(data.input) + '</em>…</div>';
}
},
- onItemRemove: function(value) {
- this.oldValue = value;
- },
onItemAdd: function(value) {
view.load(value);
this.blur();
},
onBlur: function() {
- if (!this.getValue() && this.oldValue)
- this.addItem(this.oldValue);
-
$('#host').removeClass('edit');
this.clear();
}
},
load: function(location) {
- var path = location.replace(/(^\/+|\/+$)/g, '').split('/'),
- host = path.shift(),
- pathBits = [new Filex.PathBit({'text': '/', 'path': ''})].concat(_.map(path, function(name) {
- return new Filex.PathBit({'text': name, 'path': name});
- }));
-
- this.host = host;
+ var hostRootPath = location.split(/\/(\/|~)(.*)/),
+ pathBits = [new Filex.PathBit({'name': hostRootPath[1]})].concat(
+ _.chain(hostRootPath[2].split('/'))
+ .filter(_.identity)
+ .map(function(name) { return new Filex.PathBit({'name': name}) })
+ .value()
+ );
+
+ this.host = hostRootPath[0];
this.path.reset(pathBits);
this.$host.text(this.host);
error: function(collection, response) {
view.files.reset();
- var msg = (response.responseJSON || {}).msg || 'Błąd serwera';
+ var msg = (response.responseJSON || {}).error || 'Błąd serwera';
view.$noItems.hide();
view.$error.find('.msg').text(msg);
},
selectedDir: function(dir) {
- this.path.add({'text': dir.get('name'), 'path': dir.get('name')});
+ this.path.add({'name': dir.get('name')});
},
selectedPath: function(bit) {
$btn.button('loading');
- $.post(url, data, 'json').done(function () {
+ $.post(url, data, 'json').done(function (response) {
$btn.button('reset');
- if (is_active) {
- locations.removeOption(data.host + data.path);
- }
- else {
- locations.addOption({
- group: 'usr',
- host: data.host,
- path: data.path,
- value: data.host + data.path
- });
- }
+ if (is_active)
+ locations.removeOption(data.host + '/' + data.path);
+ else
+ locations.addOption(response);
}).fail(function() {
$btn.button('reset');
$btn.button('toggle');
},
updateFavorites: function() {
- var loc = this.host + this.path.full(),
+ var loc = this.host + '/' + this.path.full(),
favorites = this.hostSelectize.options;
if (favorites.hasOwnProperty(loc)) {