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('name').join('/').replace(/^\/+/, '/');
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 var isHidden = this.model.isHidden() && !this.view.showHidden();
156 this.$el.toggleClass('hidden', isHidden);
158 if (isHidden && this.model.get('checked'))
162 toggleChecked: function(obj, value) {
163 this.$el.toggleClass('active', value);
164 this.$el.find('input[type="checkbox"]').prop('checked', value);
168 if (e.target.className == 'link') {
169 if (this.model.isDir()) {
171 this.model.trigger('selected:dir', this.model);
181 Filex.Breadcrumb = Backbone.View.extend({
185 'click a': 'selected'
188 initialize: function() {
189 this.listenTo(this.model, 'change:active', this.render);
190 this.listenTo(this.model, 'remove', this.remove);
194 if (this.model.get('active')) {
195 this.$el.text(this.model.get('name'));
198 this.$el.html($('<a/>', {
200 text: this.model.get('name')
203 this.$el.toggleClass('active', this.model.get('active'));
208 selected: function(e) {
210 this.model.trigger('selected', this.model);
214 Filex.FilexView = Backbone.View.extend({
218 'change #show-hidden': 'toggleHidden',
219 'click #select-all': 'selectAll',
220 'click #btn-refresh': 'reloadFiles',
221 'click #btn-favorites': 'toggleFavorites',
222 'click #btn-host': 'editHost'
225 initialize: function(options) {
226 this.$noItems = $('#no-items');
227 this.$error = $('#error');
228 this.$showHidden = $('#show-hidden');
229 this.$selectAll = $('#select-all');
230 this.$favorites = $('#btn-favorites');
231 this.$host = $('#btn-host');
233 this.path = new Filex.Path();
234 this.files = new Filex.FileList();
236 this.listenTo(this.path, 'reset', this.resetPath);
237 this.listenTo(this.path, 'add', this.addPath);
238 this.listenTo(this.path, 'add reset', this.changedPath);
239 this.listenTo(this.path, 'selected', this.selectedPath);
240 this.listenTo(this.files, 'reset', this.resetFiles);
241 this.listenTo(this.files, 'selected:dir', this.selectedDir);
242 this.listenTo(this.files, 'change:checked', this.updateSelectAll);
244 // used in selectize callbacks
246 optionTemplate = _.template('<div><div><%= host %></div><div class="small text-muted"><%= path %></div></div>');
248 this.$('#host-selector').selectize({
249 optgroupField: 'group',
251 searchField: ['host', 'path'],
253 {field: 'host', direction: 'asc'},
254 {field: 'path', direction: 'asc'}
256 options: options.locations,
258 {value: 'sys', label: 'Podstawowe'},
259 {value: 'usr', label: 'Użytkownika'}
261 lockOptgroupOrder: true,
262 create: function(input, callback) {
263 var $form = $('#favorite-form'),
264 parts = input.split('/'),
265 callback_called = false;
268 $form.find('#id_host').val(parts.shift());
271 $form.find('#id_path').val(parts.join('/'));
274 $form.on('submit', function(e) {
276 $btn = $this.find('[type="submit"]');
279 $btn.button('loading');
281 $.post($this.attr('action'), $this.serialize(), function(data) {
283 callback_called = true;
286 $btn.button('reset');
287 }, 'json').fail(function() {
288 console.error(arguments);
289 $btn.button('error');
293 $form.one('hide.bs.modal', function() {
294 if (!callback_called)
302 option: function(item) {
303 return optionTemplate(item);
305 option_create : function(data, escape) {
306 return '<div class="create">Dodaj <em>' + escape(data.input) + '</em>…</div>';
309 onItemAdd: function(value) {
314 $('#host').removeClass('edit');
318 this.hostSelectize = this.$('#host-selector')[0].selectize;
324 this.updateSelectAll();
325 this.updateFavorites();
326 this.$noItems.toggle(!Boolean(this.visibleFiles().length));
330 load: function(location) {
331 var hostRootPath = location.split(/\/(\/|~)(.*)/),
332 pathBits = [new Filex.PathBit({'name': hostRootPath[1]})].concat(
333 _.chain(hostRootPath[2].split('/'))
335 .map(function(name) { return new Filex.PathBit({'name': name}) })
339 this.host = hostRootPath[0];
340 this.path.reset(pathBits);
342 this.$host.text(this.host);
345 reloadFiles: function() {
352 data: {host: this.host, path: this.path.full()},
353 success: function() {
357 error: function(collection, response) {
360 var msg = (response.responseJSON || {}).error || 'Błąd serwera';
362 view.$noItems.hide();
363 view.$error.find('.msg').text(msg);
370 addPath: function(bit) {
371 var view = new Filex.Breadcrumb({model: bit});
372 this.$('.path').append(view.render().el);
375 resetPath: function(models, options) {
376 _.each(options.previousModels, function(model) {
377 model.trigger('remove');
380 this.path.each(this.addPath, this);
383 resetFiles: function(models, options) {
384 _.each(options.previousModels, function(model) {
385 model.trigger('remove');
388 this.files.each(function(file) {
389 var view = new Filex.ListingItem({model: file, view: this});
390 this.$('tbody').append(view.render().el);
394 toggleHidden: function() {
395 this.files.each(function(item) { item.trigger('hidden'); }, this);
399 selectedDir: function(dir) {
400 this.path.add({'name': dir.get('name')});
403 selectedPath: function(bit) {
404 this.path.reset(this.path.slice(0, this.path.indexOf(bit) + 1));
407 showHidden: function() {
408 return this.$showHidden[0].checked;
412 this.$el.addClass('busy');
416 this.$el.removeClass('busy');
419 visibleFiles: function() {
420 return this.showHidden() ? this.files.models : this.files.visible();
423 selectedFiles: function() {
424 return _.filter(this.visibleFiles(), function(item) {
425 return item.get('checked');
429 selectAll: function() {
430 var checked = this.$selectAll[0].checked;
432 _.each(this.visibleFiles(), function(item) {
433 item.set('checked', checked);
437 updateSelectAll: function() {
438 if (this.visibleFiles().length) {
439 this.$selectAll.prop('disabled', false);
440 this.$selectAll.prop('checked', this.selectedFiles().length == this.visibleFiles().length);
443 this.$selectAll.prop('disabled', true);
444 this.$selectAll.prop('checked', false);
448 clearSelection: function() {
449 _.each(this.visibleFiles(), function(item) {
450 item.set('checked', false);
454 toggleFavorites: function() {
455 var $btn = this.$favorites,
456 locations = this.hostSelectize,
457 is_active = $btn.hasClass('active'),
458 url = is_active ? '/filex/fav/delete/' : '/filex/fav/add/',
461 path: this.path.full()
464 $btn.button('loading');
466 $.post(url, data, 'json').done(function (response) {
467 $btn.button('reset');
470 locations.removeOption(data.host + '/' + data.path);
472 locations.addOption(response);
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();