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', this.render);
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());
160 if (e.target.className == 'link') {
161 if (this.model.isDir()) {
163 this.model.trigger('selected:dir', this.model);
173 Filex.Breadcrumb = Backbone.View.extend({
177 'click a': 'selected'
180 initialize: function() {
181 this.listenTo(this.model, 'change:active', this.render);
182 this.listenTo(this.model, 'remove', this.remove);
186 if (this.model.get('active')) {
187 this.$el.text(this.model.get('text'));
190 this.$el.html($('<a/>', {
192 text: this.model.get('text')
195 this.$el.toggleClass('active', this.model.get('active'));
200 selected: function(e) {
202 this.model.trigger('selected', this.model);
206 Filex.FilexView = Backbone.View.extend({
210 'change #show-hidden': 'toggleHidden',
211 'click #select-all': 'selectAll',
212 'click #btn-refresh': 'reloadFiles',
213 'click #btn-favorites': 'toggleFavorites',
214 'click #btn-host': 'editHost'
217 initialize: function(options) {
218 this.$noItems = $('#no-items');
219 this.$error = $('#error');
220 this.$showHidden = $('#show-hidden');
221 this.$selectAll = $('#select-all');
222 this.$favorites = $('#btn-favorites');
223 this.$host = $('#btn-host');
225 this.path = new Filex.Path();
226 this.files = new Filex.FileList();
228 this.listenTo(this.path, 'reset', this.resetPath);
229 this.listenTo(this.path, 'add', this.addPath);
230 this.listenTo(this.path, 'add reset', this.changedPath);
231 this.listenTo(this.path, 'selected', this.selectedPath);
232 this.listenTo(this.files, 'reset', this.resetFiles);
233 this.listenTo(this.files, 'selected:dir', this.selectedDir);
234 this.listenTo(this.files, 'change:checked', this.updateSelectAll);
236 // used in selectize callbacks
238 optionTemplate = _.template('<div><div><%= host %></div><div class="small text-muted"><%= path %></div></div>');
240 this.$('#host-selector').selectize({
241 optgroupField: 'group',
243 searchField: ['host', 'path'],
245 {field: 'host', direction: 'asc'},
246 {field: 'path', direction: 'asc'}
248 options: options.locations,
250 {value: 'sys', label: 'Podstawowe'},
251 {value: 'usr', label: 'Użytkownika'}
253 lockOptgroupOrder: true,
254 create: function(input, callback) {
255 var $form = $('#favorite-form'),
256 parts = input.split('/', 1),
257 callback_called = false;
259 $form.find('#id_host').val(parts[0]);
261 if (parts.length > 1)
262 $form.find('#id_path').val(parts[1]);
264 $form.on('submit', function(e) {
266 $btn = $this.find('[type="submit"]');
269 $btn.button('loading');
271 $.post($this.attr('action'), $this.serialize(), function(data) {
273 callback_called = true;
276 $btn.button('reset');
277 }, 'json').fail(function() {
278 console.error(arguments);
279 $btn.button('error');
283 $form.one('hide.bs.modal', function() {
284 if (!callback_called)
292 option: function(item) {
293 return optionTemplate(item);
295 option_create : function(data, escape) {
296 return '<div class="create">Dodaj <em>' + escape(data.input) + '</em>…</div>';
299 onItemAdd: function(value) {
304 $('#host').removeClass('edit');
308 this.hostSelectize = this.$('#host-selector')[0].selectize;
314 this.updateSelectAll();
315 this.updateFavorites();
316 this.$noItems.toggle(!Boolean(this.visibleFiles().length));
320 load: function(location) {
321 var path = location.replace(/(^\/+|\/+$)/g, '').split('/'),
323 pathBits = [new Filex.PathBit({'text': '/', 'path': ''})].concat(_.map(path, function(name) {
324 return new Filex.PathBit({'text': name, 'path': name});
328 this.path.reset(pathBits);
330 this.$host.text(this.host);
333 reloadFiles: function() {
340 data: {host: this.host, path: this.path.full()},
341 success: function() {
345 error: function(collection, response) {
348 var msg = (response.responseJSON || {}).msg || 'Błąd serwera';
350 view.$noItems.hide();
351 view.$error.find('.msg').text(msg);
358 addPath: function(bit) {
359 var view = new Filex.Breadcrumb({model: bit});
360 this.$('.path').append(view.render().el);
363 resetPath: function(models, options) {
364 _.each(options.previousModels, function(model) {
365 model.trigger('remove');
368 this.path.each(this.addPath, this);
371 resetFiles: function(models, options) {
372 _.each(options.previousModels, function(model) {
373 model.trigger('remove');
376 this.files.each(function(file) {
377 var view = new Filex.ListingItem({model: file, view: this});
378 this.$('tbody').append(view.render().el);
382 toggleHidden: function() {
383 this.files.each(function(item) { item.trigger('hidden'); }, this);
387 selectedDir: function(dir) {
388 this.path.add({'text': dir.get('name'), 'path': dir.get('name')});
391 selectedPath: function(bit) {
392 this.path.reset(this.path.slice(0, this.path.indexOf(bit) + 1));
395 showHidden: function() {
396 return this.$showHidden[0].checked;
400 this.$el.addClass('busy');
404 this.$el.removeClass('busy');
407 visibleFiles: function() {
408 return this.showHidden() ? this.files.models : this.files.visible();
411 selectedFiles: function() {
412 return _.filter(this.visibleFiles(), function(item) {
413 return item.get('checked');
417 selectAll: function() {
418 var checked = this.$selectAll[0].checked;
420 _.each(this.visibleFiles(), function(item) {
421 item.set('checked', checked);
425 updateSelectAll: function() {
426 if (this.visibleFiles().length) {
427 this.$selectAll.prop('disabled', false);
428 this.$selectAll.prop('checked', this.selectedFiles().length == this.visibleFiles().length);
431 this.$selectAll.prop('disabled', true);
432 this.$selectAll.prop('checked', false);
436 clearSelection: function() {
437 _.each(this.visibleFiles(), function(item) {
438 item.set('checked', false);
442 toggleFavorites: function() {
443 var $btn = this.$favorites,
444 locations = this.hostSelectize,
445 is_active = $btn.hasClass('active'),
446 url = is_active ? '/filex/fav/delete/' : '/filex/fav/add/',
449 path: this.path.full()
452 $btn.button('loading');
454 $.post(url, data, 'json').done(function () {
455 $btn.button('reset');
458 locations.removeOption(data.host + data.path);
461 locations.addOption({
465 value: data.host + data.path
469 $btn.button('reset');
470 $btn.button('toggle');
472 console.error(arguments);
476 initialLoad: function() {
478 var opts = this.hostSelectize.options;
480 this.load(opts[Object.keys(opts)[0]].value);
484 changedPath: function () {
486 this.updateFavorites();
489 updateFavorites: function() {
490 var loc = this.host + this.path.full(),
491 favorites = this.hostSelectize.options;
493 if (favorites.hasOwnProperty(loc)) {
494 if (favorites[loc].group == 'sys') {
495 this.$favorites.addClass('disabled').prop('disabled', true);
496 if (this.$favorites.hasClass('active'))
497 this.$favorites.button('toggle');
500 this.$favorites.removeClass('disabled').prop('disabled', false);
501 if (!this.$favorites.hasClass('active'))
502 this.$favorites.button('toggle');
506 this.$favorites.removeClass('disabled').prop('disabled', false);
507 if (this.$favorites.hasClass('active'))
508 this.$favorites.button('toggle');
512 editHost: function() {
513 $('#host').addClass('edit');
514 this.hostSelectize.focus();