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() + '/' + this.model.get('name')
146 data['cid'] = this.model.cid;
148 this.$el.html(this.template()(data));
154 toggleHidden: function() {
155 this.$el.toggleClass('hidden', this.model.isHidden() && !this.view.showHidden());
158 toggleChecked: function(obj, value) {
159 this.$el.toggleClass('active', value);
160 this.$el.find('input[type="checkbox"]').prop('checked', value);
164 if (e.target.className == 'link') {
165 if (this.model.isDir()) {
167 this.model.trigger('selected:dir', this.model);
177 Filex.Breadcrumb = Backbone.View.extend({
181 'click a': 'selected'
184 initialize: function() {
185 this.listenTo(this.model, 'change:active', this.render);
186 this.listenTo(this.model, 'remove', this.remove);
190 if (this.model.get('active')) {
191 this.$el.text(this.model.get('text'));
194 this.$el.html($('<a/>', {
196 text: this.model.get('text')
199 this.$el.toggleClass('active', this.model.get('active'));
204 selected: function(e) {
206 this.model.trigger('selected', this.model);
210 Filex.FilexView = Backbone.View.extend({
214 'change #show-hidden': 'toggleHidden',
215 'click #select-all': 'selectAll',
216 'click #btn-refresh': 'reloadFiles',
217 'click #btn-favorites': 'toggleFavorites',
218 'click #btn-host': 'editHost'
221 initialize: function(options) {
222 this.$noItems = $('#no-items');
223 this.$error = $('#error');
224 this.$showHidden = $('#show-hidden');
225 this.$selectAll = $('#select-all');
226 this.$favorites = $('#btn-favorites');
227 this.$host = $('#btn-host');
229 this.path = new Filex.Path();
230 this.files = new Filex.FileList();
232 this.listenTo(this.path, 'reset', this.resetPath);
233 this.listenTo(this.path, 'add', this.addPath);
234 this.listenTo(this.path, 'add reset', this.changedPath);
235 this.listenTo(this.path, 'selected', this.selectedPath);
236 this.listenTo(this.files, 'reset', this.resetFiles);
237 this.listenTo(this.files, 'selected:dir', this.selectedDir);
238 this.listenTo(this.files, 'change:checked', this.updateSelectAll);
240 // used in selectize callbacks
242 optionTemplate = _.template('<div><div><%= host %></div><div class="small text-muted"><%= path %></div></div>');
244 this.$('#host-selector').selectize({
245 optgroupField: 'group',
247 searchField: ['host', 'path'],
249 {field: 'host', direction: 'asc'},
250 {field: 'path', direction: 'asc'}
252 options: options.locations,
254 {value: 'sys', label: 'Podstawowe'},
255 {value: 'usr', label: 'Użytkownika'}
257 lockOptgroupOrder: true,
258 create: function(input, callback) {
259 var $form = $('#favorite-form'),
260 parts = input.split('/', 1),
261 callback_called = false;
263 $form.find('#id_host').val(parts[0]);
265 if (parts.length > 1)
266 $form.find('#id_path').val(parts[1]);
268 $form.on('submit', function(e) {
270 $btn = $this.find('[type="submit"]');
273 $btn.button('loading');
275 $.post($this.attr('action'), $this.serialize(), function(data) {
277 callback_called = true;
280 $btn.button('reset');
281 }, 'json').fail(function() {
282 console.error(arguments);
283 $btn.button('error');
287 $form.one('hide.bs.modal', function() {
288 if (!callback_called)
296 option: function(item) {
297 return optionTemplate(item);
299 option_create : function(data, escape) {
300 return '<div class="create">Dodaj <em>' + escape(data.input) + '</em>…</div>';
303 onItemAdd: function(value) {
308 $('#host').removeClass('edit');
312 this.hostSelectize = this.$('#host-selector')[0].selectize;
318 this.updateSelectAll();
319 this.updateFavorites();
320 this.$noItems.toggle(!Boolean(this.visibleFiles().length));
324 load: function(location) {
325 var path = location.replace(/(^\/+|\/+$)/g, '').split('/'),
327 pathBits = [new Filex.PathBit({'text': '/', 'path': ''})].concat(_.map(path, function(name) {
328 return new Filex.PathBit({'text': name, 'path': name});
332 this.path.reset(pathBits);
334 this.$host.text(this.host);
337 reloadFiles: function() {
344 data: {host: this.host, path: this.path.full()},
345 success: function() {
349 error: function(collection, response) {
352 var msg = (response.responseJSON || {}).error || 'Błąd serwera';
354 view.$noItems.hide();
355 view.$error.find('.msg').text(msg);
362 addPath: function(bit) {
363 var view = new Filex.Breadcrumb({model: bit});
364 this.$('.path').append(view.render().el);
367 resetPath: function(models, options) {
368 _.each(options.previousModels, function(model) {
369 model.trigger('remove');
372 this.path.each(this.addPath, this);
375 resetFiles: function(models, options) {
376 _.each(options.previousModels, function(model) {
377 model.trigger('remove');
380 this.files.each(function(file) {
381 var view = new Filex.ListingItem({model: file, view: this});
382 this.$('tbody').append(view.render().el);
386 toggleHidden: function() {
387 this.files.each(function(item) { item.trigger('hidden'); }, this);
391 selectedDir: function(dir) {
392 this.path.add({'text': dir.get('name'), 'path': dir.get('name')});
395 selectedPath: function(bit) {
396 this.path.reset(this.path.slice(0, this.path.indexOf(bit) + 1));
399 showHidden: function() {
400 return this.$showHidden[0].checked;
404 this.$el.addClass('busy');
408 this.$el.removeClass('busy');
411 visibleFiles: function() {
412 return this.showHidden() ? this.files.models : this.files.visible();
415 selectedFiles: function() {
416 return _.filter(this.visibleFiles(), function(item) {
417 return item.get('checked');
421 selectAll: function() {
422 var checked = this.$selectAll[0].checked;
424 _.each(this.visibleFiles(), function(item) {
425 item.set('checked', checked);
429 updateSelectAll: function() {
430 if (this.visibleFiles().length) {
431 this.$selectAll.prop('disabled', false);
432 this.$selectAll.prop('checked', this.selectedFiles().length == this.visibleFiles().length);
435 this.$selectAll.prop('disabled', true);
436 this.$selectAll.prop('checked', false);
440 clearSelection: function() {
441 _.each(this.visibleFiles(), function(item) {
442 item.set('checked', false);
446 toggleFavorites: function() {
447 var $btn = this.$favorites,
448 locations = this.hostSelectize,
449 is_active = $btn.hasClass('active'),
450 url = is_active ? '/filex/fav/delete/' : '/filex/fav/add/',
453 path: this.path.full()
456 $btn.button('loading');
458 $.post(url, data, 'json').done(function () {
459 $btn.button('reset');
462 locations.removeOption(data.host + data.path);
465 locations.addOption({
469 value: data.host + data.path
473 $btn.button('reset');
474 $btn.button('toggle');
476 console.error(arguments);
480 initialLoad: function() {
482 var opts = this.hostSelectize.options;
484 this.load(opts[Object.keys(opts)[0]].value);
488 changedPath: function () {
490 this.updateFavorites();
493 updateFavorites: function() {
494 var loc = this.host + this.path.full(),
495 favorites = this.hostSelectize.options;
497 if (favorites.hasOwnProperty(loc)) {
498 if (favorites[loc].group == 'sys') {
499 this.$favorites.addClass('disabled').prop('disabled', true);
500 if (this.$favorites.hasClass('active'))
501 this.$favorites.button('toggle');
504 this.$favorites.removeClass('disabled').prop('disabled', false);
505 if (!this.$favorites.hasClass('active'))
506 this.$favorites.button('toggle');
510 this.$favorites.removeClass('disabled').prop('disabled', false);
511 if (this.$favorites.hasClass('active'))
512 this.$favorites.button('toggle');
516 editHost: function() {
517 $('#host').addClass('edit');
518 this.hostSelectize.focus();