1 var Filex = Filex || {};
6 // ------------------------------------------------------------------------
8 // ------------------------------------------------------------------------
10 var OfflineModel = Backbone.Model.extend({
11 // prevent syncing with server
17 Filex.File = OfflineModel.extend({
30 isHidden: function() {
31 return this.get('name')[0] == '.';
35 this.set('checked', !this.get('checked'));
39 Filex.Directory = Filex.File.extend({
49 Filex.PathBit = OfflineModel.extend({
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
60 Filex.FileList = Backbone.Collection.extend({
63 model: function(attrs, options) {
64 switch (attrs['type']) {
66 return new Filex.Directory(attrs, options);
68 return new Filex.File(attrs, options);
70 console.error('Unknown model type:', attrs['type']);
74 comparator: function(a, b) {
75 if (a.isFile() == b.isFile())
76 return a.get('name').toLowerCase().localeCompare(b.get('name').toLowerCase());
78 return (a.isFile() && b.isDir()) ? 1 : -1;
82 return this.filter(function (item) {
83 return item.isHidden();
87 visible: function () {
88 return this.filter(function (item) {
89 return !item.isHidden();
94 Filex.Path = Backbone.Collection.extend({
97 initialize: function() {
98 this.listenTo(this, 'reset add', this.setActive);
101 setActive: function() {
102 _.each(this.initial(), function(bit) {
103 bit.set('active', false);
106 this.last().set('active', true);
110 return this.pluck('path').join('/') || '/';
115 // ------------------------------------------------------------------------
117 // ------------------------------------------------------------------------
119 Filex.ListingItem = Backbone.View.extend({
126 initialize: function(options) {
127 this.view = options.view;
129 this.listenTo(this.model, 'change:checked', this.toggleChecked);
130 this.listenTo(this.model, 'remove', this.remove);
131 this.listenTo(this.model, 'hidden', this.toggleHidden);
134 template: function() {
135 var templateSelector = this.model.isDir() ? '#dir-template' : '#file-template';
137 return _.template($(templateSelector).html());
141 var data = this.model.toJSON();
142 data['url_params'] = $.param({
143 host: this.view.host,
144 path: this.view.path.full(),
145 name: this.model.get('name')
147 data['cid'] = this.model.cid;
149 this.$el.html(this.template()(data));
155 toggleHidden: function() {
156 this.$el.toggleClass('hidden', this.model.isHidden() && !this.view.showHidden());
159 toggleChecked: function(obj, value) {
160 this.$el.toggleClass('active', value);
161 this.$el.find('input[type="checkbox"]').prop('checked', value);
165 if (e.target.className == 'link') {
166 if (this.model.isDir()) {
168 this.model.trigger('selected:dir', this.model);
178 Filex.Breadcrumb = Backbone.View.extend({
182 'click a': 'selected'
185 initialize: function() {
186 this.listenTo(this.model, 'change:active', this.render);
187 this.listenTo(this.model, 'remove', this.remove);
191 if (this.model.get('active')) {
192 this.$el.text(this.model.get('text'));
195 this.$el.html($('<a/>', {
197 text: this.model.get('text')
200 this.$el.toggleClass('active', this.model.get('active'));
205 selected: function(e) {
207 this.model.trigger('selected', this.model);
211 Filex.FilexView = Backbone.View.extend({
215 'change #show-hidden': 'toggleHidden',
216 'click #select-all': 'selectAll',
217 'click #btn-refresh': 'reloadFiles',
218 'click #btn-favorites': 'toggleFavorites',
219 'click #btn-host': 'editHost'
222 initialize: function(options) {
223 this.$noItems = $('#no-items');
224 this.$error = $('#error');
225 this.$showHidden = $('#show-hidden');
226 this.$selectAll = $('#select-all');
227 this.$favorites = $('#btn-favorites');
228 this.$host = $('#btn-host');
230 this.path = new Filex.Path();
231 this.files = new Filex.FileList();
233 this.listenTo(this.path, 'reset', this.resetPath);
234 this.listenTo(this.path, 'add', this.addPath);
235 this.listenTo(this.path, 'add reset', this.changedPath);
236 this.listenTo(this.path, 'selected', this.selectedPath);
237 this.listenTo(this.files, 'reset', this.resetFiles);
238 this.listenTo(this.files, 'selected:dir', this.selectedDir);
239 this.listenTo(this.files, 'change:checked', this.updateSelectAll);
241 // used in selectize callbacks
243 optionTemplate = _.template('<div><div><%= host %></div><div class="small text-muted"><%= path %></div></div>');
245 this.$('#host-selector').selectize({
246 optgroupField: 'group',
248 searchField: ['host', 'path'],
250 {field: 'host', direction: 'asc'},
251 {field: 'path', direction: 'asc'}
253 options: options.locations,
255 {value: 'sys', label: 'Podstawowe'},
256 {value: 'usr', label: 'Użytkownika'}
258 lockOptgroupOrder: true,
259 create: function(input, callback) {
260 var $form = $('#favorite-form'),
261 parts = input.split('/', 1),
262 callback_called = false;
264 $form.find('#id_host').val(parts[0]);
266 if (parts.length > 1)
267 $form.find('#id_path').val(parts[1]);
269 $form.on('submit', function(e) {
271 $btn = $this.find('[type="submit"]');
274 $btn.button('loading');
276 $.post($this.attr('action'), $this.serialize(), function(data) {
278 callback_called = true;
281 $btn.button('reset');
282 }, 'json').fail(function() {
283 console.error(arguments);
284 $btn.button('error');
288 $form.one('hide.bs.modal', function() {
289 if (!callback_called)
297 option: function(item) {
298 return optionTemplate(item);
300 option_create : function(data, escape) {
301 return '<div class="create">Dodaj <em>' + escape(data.input) + '</em>…</div>';
304 onItemAdd: function(value) {
309 $('#host').removeClass('edit');
313 this.hostSelectize = this.$('#host-selector')[0].selectize;
319 this.updateSelectAll();
320 this.updateFavorites();
321 this.$noItems.toggle(!Boolean(this.visibleFiles().length));
325 load: function(location) {
326 var path = location.replace(/(^\/+|\/+$)/g, '').split('/'),
328 pathBits = [new Filex.PathBit({'text': '/', 'path': ''})].concat(_.map(path, function(name) {
329 return new Filex.PathBit({'text': name, 'path': name});
333 this.path.reset(pathBits);
335 this.$host.text(this.host);
338 reloadFiles: function() {
345 data: {host: this.host, path: this.path.full()},
346 success: function() {
350 error: function(collection, response) {
353 var msg = (response.responseJSON || {}).msg || 'Błąd serwera';
355 view.$noItems.hide();
356 view.$error.find('.msg').text(msg);
363 addPath: function(bit) {
364 var view = new Filex.Breadcrumb({model: bit});
365 this.$('.path').append(view.render().el);
368 resetPath: function(models, options) {
369 _.each(options.previousModels, function(model) {
370 model.trigger('remove');
373 this.path.each(this.addPath, this);
376 resetFiles: function(models, options) {
377 _.each(options.previousModels, function(model) {
378 model.trigger('remove');
381 this.files.each(function(file) {
382 var view = new Filex.ListingItem({model: file, view: this});
383 this.$('tbody').append(view.render().el);
387 toggleHidden: function() {
388 this.files.each(function(item) { item.trigger('hidden'); }, this);
392 selectedDir: function(dir) {
393 this.path.add({'text': dir.get('name'), 'path': dir.get('name')});
396 selectedPath: function(bit) {
397 this.path.reset(this.path.slice(0, this.path.indexOf(bit) + 1));
400 showHidden: function() {
401 return this.$showHidden[0].checked;
405 this.$el.addClass('busy');
409 this.$el.removeClass('busy');
412 visibleFiles: function() {
413 return this.showHidden() ? this.files.models : this.files.visible();
416 selectedFiles: function() {
417 return _.filter(this.visibleFiles(), function(item) {
418 return item.get('checked');
422 selectAll: function() {
423 var checked = this.$selectAll[0].checked;
425 _.each(this.visibleFiles(), function(item) {
426 item.set('checked', checked);
430 updateSelectAll: function() {
431 if (this.visibleFiles().length) {
432 this.$selectAll.prop('disabled', false);
433 this.$selectAll.prop('checked', this.selectedFiles().length == this.visibleFiles().length);
436 this.$selectAll.prop('disabled', true);
437 this.$selectAll.prop('checked', false);
441 clearSelection: function() {
442 _.each(this.visibleFiles(), function(item) {
443 item.set('checked', false);
447 toggleFavorites: function() {
448 var $btn = this.$favorites,
449 locations = this.hostSelectize,
450 is_active = $btn.hasClass('active'),
451 url = is_active ? '/filex/fav/delete/' : '/filex/fav/add/',
454 path: this.path.full()
457 $btn.button('loading');
459 $.post(url, data, 'json').done(function () {
460 $btn.button('reset');
463 locations.removeOption(data.host + data.path);
466 locations.addOption({
470 value: data.host + data.path
474 $btn.button('reset');
475 $btn.button('toggle');
477 console.error(arguments);
481 initialLoad: function() {
483 var opts = this.hostSelectize.options;
485 this.load(opts[Object.keys(opts)[0]].value);
489 changedPath: function () {
491 this.updateFavorites();
494 updateFavorites: function() {
495 var loc = this.host + this.path.full(),
496 favorites = this.hostSelectize.options;
498 if (favorites.hasOwnProperty(loc)) {
499 if (favorites[loc].group == 'sys') {
500 this.$favorites.addClass('disabled').prop('disabled', true);
501 if (this.$favorites.hasClass('active'))
502 this.$favorites.button('toggle');
505 this.$favorites.removeClass('disabled').prop('disabled', false);
506 if (!this.$favorites.hasClass('active'))
507 this.$favorites.button('toggle');
511 this.$favorites.removeClass('disabled').prop('disabled', false);
512 if (this.$favorites.hasClass('active'))
513 this.$favorites.button('toggle');
517 editHost: function() {
518 $('#host').addClass('edit');
519 this.hostSelectize.focus();