X-Git-Url: http://mmka.chem.univ.gda.pl/gitweb/?a=blobdiff_plain;f=filex%2Fstatic%2Ffilex%2Ffilex.js;h=e7ad4c4b575c3accb1deba6ac6978e0b31fcacf1;hb=edd1abec6c3948dd007a7a7d75ddcc28e9197e41;hp=68051c3d077c2a2d3eb99aa549aee556509d9e58;hpb=93056bba14871711faa5e2070689657939dd52e2;p=qcg-portal.git diff --git a/filex/static/filex/filex.js b/filex/static/filex/filex.js index 68051c3..e7ad4c4 100644 --- a/filex/static/filex/filex.js +++ b/filex/static/filex/filex.js @@ -95,8 +95,7 @@ $(function(){ model: Filex.PathBit, initialize: function() { - this.listenTo(this, 'reset', this.setActive); - this.listenTo(this, 'add', this.setActive); + this.listenTo(this, 'reset add', this.setActive); }, setActive: function() { @@ -127,7 +126,7 @@ $(function(){ 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); }, @@ -142,8 +141,7 @@ $(function(){ 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; @@ -157,6 +155,11 @@ $(function(){ this.$el.toggleClass('hidden', this.model.isHidden() && !this.view.showHidden()); }, + toggleChecked: function(obj, value) { + this.$el.toggleClass('active', value); + this.$el.find('input[type="checkbox"]').prop('checked', value); + }, + click: function(e) { if (e.target.className == 'link') { if (this.model.isDir()) { @@ -209,25 +212,26 @@ $(function(){ events: { 'change #show-hidden': 'toggleHidden', - 'click #host-controls .view': 'hostEdit', 'click #select-all': 'selectAll', - 'click #btn-refresh': 'reloadFiles' + 'click #btn-refresh': 'reloadFiles', + 'click #btn-favorites': 'toggleFavorites', + 'click #btn-host': 'editHost' }, initialize: function(options) { this.$noItems = $('#no-items'); this.$error = $('#error'); this.$showHidden = $('#show-hidden'); - this.$host = $('#host-controls'); this.$selectAll = $('#select-all'); + this.$favorites = $('#btn-favorites'); + this.$host = $('#btn-host'); - this.host = options.host; this.path = new Filex.Path(); this.files = new Filex.FileList(); this.listenTo(this.path, 'reset', this.resetPath); - this.listenTo(this.path, 'add', this.reloadFiles); this.listenTo(this.path, 'add', this.addPath); + this.listenTo(this.path, 'add reset', this.changedPath); this.listenTo(this.path, 'selected', this.selectedPath); this.listenTo(this.files, 'reset', this.resetFiles); this.listenTo(this.files, 'selected:dir', this.selectedDir); @@ -238,61 +242,96 @@ $(function(){ optionTemplate = _.template('
<%= host %>
<%= path %>
'); this.$('#host-selector').selectize({ - valueField: 'host', + optgroupField: 'group', labelField: 'host', searchField: ['host', 'path'], sortField: [ {field: 'host', direction: 'asc'}, {field: 'path', direction: 'asc'} ], - items: [this.host], - options: options.hostOptions, + options: options.locations, + optgroups: [ + {value: 'sys', label: 'Podstawowe'}, + {value: 'usr', label: 'Użytkownika'} + ], + lockOptgroupOrder: true, + create: function(input, callback) { + var $form = $('#favorite-form'), + parts = input.split('/', 1), + callback_called = false; + + $form.find('#id_host').val(parts[0]); + + if (parts.length > 1) + $form.find('#id_path').val(parts[1]); + + $form.on('submit', function(e) { + var $this = $(this), + $btn = $this.find('[type="submit"]'); + + e.preventDefault(); + $btn.button('loading'); + + $.post($this.attr('action'), $this.serialize(), function(data) { + callback(data); + callback_called = true; + + $this.modal('hide'); + $btn.button('reset'); + }, 'json').fail(function() { + console.error(arguments); + $btn.button('error'); + }); + }); + + $form.one('hide.bs.modal', function() { + if (!callback_called) + callback(); + $form.off(); + }); + + $form.modal(); + }, render: { option: function(item) { return optionTemplate(item); + }, + option_create : function(data, escape) { + return '
Dodaj ' + escape(data.input) + '
'; } }, - onDropdownClose: function() { + onItemAdd: function(value) { + view.load(value); this.blur(); }, onBlur: function() { - view.$host.toggleClass('editing'); - - var value = this.getValue(); - if (!value) { - this.addItem(view.host, true); - return; - } - - if (value != view.host) { - var location = this.options[value]; - - view.host = location.host; - view.navigate(location.path); - } + $('#host').removeClass('edit'); + this.clear(); } }); this.hostSelectize = this.$('#host-selector')[0].selectize; this.render(); - this.navigate(this.hostSelectize.options[this.host].path); }, render: function() { this.updateSelectAll(); - this.$host.find('.view').text(this.host); + this.updateFavorites(); this.$noItems.toggle(!Boolean(this.visibleFiles().length)); this.$error.hide(); }, - navigate: function(path) { - var pathBits = [new Filex.PathBit({'text': '/', 'path': ''})]; - - pathBits = pathBits.concat(_.map(path.replace(/(^\/+|\/+$)/g, '').split('/'), function(name) { - return new Filex.PathBit({'text': name, 'path': name}); - })); + 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; this.path.reset(pathBits); + + this.$host.text(this.host); }, reloadFiles: function() { @@ -310,7 +349,7 @@ $(function(){ 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); @@ -326,8 +365,6 @@ $(function(){ }, resetPath: function(models, options) { - this.reloadFiles(); - _.each(options.previousModels, function(model) { model.trigger('remove'); }); @@ -356,20 +393,13 @@ $(function(){ }, selectedPath: function(bit) { - var newPath = this.path.slice(0, this.path.indexOf(bit) + 1); - this.path.set(newPath); - this.reloadFiles(); + this.path.reset(this.path.slice(0, this.path.indexOf(bit) + 1)); }, showHidden: function() { return this.$showHidden[0].checked; }, - hostEdit: function() { - this.$host.toggleClass('editing'); - this.hostSelectize.focus(); - }, - busy: function() { this.$el.addClass('busy'); }, @@ -397,15 +427,95 @@ $(function(){ }, updateSelectAll: function() { - this.$selectAll.prop('checked', this.selectedFiles().length == this.visibleFiles().length); + if (this.visibleFiles().length) { + this.$selectAll.prop('disabled', false); + this.$selectAll.prop('checked', this.selectedFiles().length == this.visibleFiles().length); + } + else { + this.$selectAll.prop('disabled', true); + this.$selectAll.prop('checked', false); + } }, clearSelection: function() { _.each(this.visibleFiles(), function(item) { item.set('checked', false); }); + }, - this.updateSelectAll(); + toggleFavorites: function() { + var $btn = this.$favorites, + locations = this.hostSelectize, + is_active = $btn.hasClass('active'), + url = is_active ? '/filex/fav/delete/' : '/filex/fav/add/', + data = { + host: this.host, + path: this.path.full() + }; + + $btn.button('loading'); + + $.post(url, data, 'json').done(function () { + $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 + }); + } + }).fail(function() { + $btn.button('reset'); + $btn.button('toggle'); + + console.error(arguments); + }); + }, + + initialLoad: function() { + if (!this.host) { + var opts = this.hostSelectize.options; + + this.load(opts[Object.keys(opts)[0]].value); + } + }, + + changedPath: function () { + this.reloadFiles(); + this.updateFavorites(); + }, + + updateFavorites: function() { + var loc = this.host + this.path.full(), + favorites = this.hostSelectize.options; + + if (favorites.hasOwnProperty(loc)) { + if (favorites[loc].group == 'sys') { + this.$favorites.addClass('disabled').prop('disabled', true); + if (this.$favorites.hasClass('active')) + this.$favorites.button('toggle'); + } + else { + this.$favorites.removeClass('disabled').prop('disabled', false); + if (!this.$favorites.hasClass('active')) + this.$favorites.button('toggle'); + } + } + else { + this.$favorites.removeClass('disabled').prop('disabled', false); + if (this.$favorites.hasClass('active')) + this.$favorites.button('toggle'); + } + }, + + editHost: function() { + $('#host').addClass('edit'); + this.hostSelectize.focus(); } }); });