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', this.setActive);
99 this.listenTo(this, 'add', this.setActive);
102 setActive: function() {
103 _.each(this.initial(), function(bit) {
104 bit.set('active', false);
107 this.last().set('active', true);
111 return this.pluck('path').join('/') || '/';
116 // ------------------------------------------------------------------------
118 // ------------------------------------------------------------------------
120 Filex.ListingItem = Backbone.View.extend({
127 initialize: function(options) {
128 this.view = options.view;
130 this.listenTo(this.model, 'change', this.render);
131 this.listenTo(this.model, 'remove', this.remove);
132 this.listenTo(this.model, 'hidden', this.toggleHidden);
135 template: function() {
136 var templateSelector = this.model.isDir() ? '#dir-template' : '#file-template';
138 return _.template($(templateSelector).html());
142 var data = this.model.toJSON();
143 data['url_params'] = $.param({
144 host: this.view.host,
145 path: this.view.path.full(),
146 name: this.model.get('name')
148 data['cid'] = this.model.cid;
150 this.$el.html(this.template()(data));
156 toggleHidden: function() {
157 this.$el.toggleClass('hidden', this.model.isHidden() && !this.view.showHidden());
161 if (e.target.className == 'link') {
162 if (this.model.isDir()) {
164 this.model.trigger('selected:dir', this.model);
174 Filex.Breadcrumb = Backbone.View.extend({
178 'click a': 'selected'
181 initialize: function() {
182 this.listenTo(this.model, 'change:active', this.render);
183 this.listenTo(this.model, 'remove', this.remove);
187 if (this.model.get('active')) {
188 this.$el.text(this.model.get('text'));
191 this.$el.html($('<a/>', {
193 text: this.model.get('text')
196 this.$el.toggleClass('active', this.model.get('active'));
201 selected: function(e) {
203 this.model.trigger('selected', this.model);
207 Filex.FilexView = Backbone.View.extend({
211 'change #show-hidden': 'toggleHidden',
212 'click #select-all': 'selectAll',
213 'click #btn-refresh': 'reloadFiles'
216 initialize: function(options) {
217 this.$noItems = $('#no-items');
218 this.$error = $('#error');
219 this.$showHidden = $('#show-hidden');
220 this.$selectAll = $('#select-all');
222 this.host = options.host;
223 this.path = new Filex.Path();
224 this.files = new Filex.FileList();
226 this.listenTo(this.path, 'reset', this.resetPath);
227 this.listenTo(this.path, 'add', this.reloadFiles);
228 this.listenTo(this.path, 'add', this.addPath);
229 this.listenTo(this.path, 'selected', this.selectedPath);
230 this.listenTo(this.files, 'reset', this.resetFiles);
231 this.listenTo(this.files, 'selected:dir', this.selectedDir);
232 this.listenTo(this.files, 'change:checked', this.updateSelectAll);
234 // used in selectize callbacks
236 optionTemplate = _.template('<div><div><%= host %></div><div class="small text-muted"><%= path %></div></div>');
238 this.$('#host-selector').selectize({
241 searchField: ['host', 'path'],
243 {field: 'host', direction: 'asc'},
244 {field: 'path', direction: 'asc'}
247 options: options.hostOptions,
249 option: function(item) {
250 return optionTemplate(item);
253 onDropdownClose: function() {
257 var value = this.getValue();
259 this.addItem(view.host, true);
263 if (value != view.host) {
264 var location = this.options[value];
266 view.host = location.host;
267 view.navigate(location.path);
271 this.hostSelectize = this.$('#host-selector')[0].selectize;
274 this.navigate(this.hostSelectize.options[this.host].path);
278 this.updateSelectAll();
279 this.$noItems.toggle(!Boolean(this.visibleFiles().length));
283 navigate: function(path) {
284 var pathBits = [new Filex.PathBit({'text': '/', 'path': ''})];
286 pathBits = pathBits.concat(_.map(path.replace(/(^\/+|\/+$)/g, '').split('/'), function(name) {
287 return new Filex.PathBit({'text': name, 'path': name});
290 this.path.reset(pathBits);
293 reloadFiles: function() {
300 data: {host: this.host, path: this.path.full()},
301 success: function() {
305 error: function(collection, response) {
308 var msg = (response.responseJSON || {}).msg || 'Błąd serwera';
310 view.$noItems.hide();
311 view.$error.find('.msg').text(msg);
318 addPath: function(bit) {
319 var view = new Filex.Breadcrumb({model: bit});
320 this.$('.path').append(view.render().el);
323 resetPath: function(models, options) {
326 _.each(options.previousModels, function(model) {
327 model.trigger('remove');
330 this.path.each(this.addPath, this);
333 resetFiles: function(models, options) {
334 _.each(options.previousModels, function(model) {
335 model.trigger('remove');
338 this.files.each(function(file) {
339 var view = new Filex.ListingItem({model: file, view: this});
340 this.$('tbody').append(view.render().el);
344 toggleHidden: function() {
345 this.files.each(function(item) { item.trigger('hidden'); }, this);
349 selectedDir: function(dir) {
350 this.path.add({'text': dir.get('name'), 'path': dir.get('name')});
353 selectedPath: function(bit) {
354 var newPath = this.path.slice(0, this.path.indexOf(bit) + 1);
355 this.path.set(newPath);
359 showHidden: function() {
360 return this.$showHidden[0].checked;
364 this.$el.addClass('busy');
368 this.$el.removeClass('busy');
371 visibleFiles: function() {
372 return this.showHidden() ? this.files.models : this.files.visible();
375 selectedFiles: function() {
376 return _.filter(this.visibleFiles(), function(item) {
377 return item.get('checked');
381 selectAll: function() {
382 var checked = this.$selectAll[0].checked;
384 _.each(this.visibleFiles(), function(item) {
385 item.set('checked', checked);
389 updateSelectAll: function() {
390 if (this.visibleFiles().length) {
391 this.$selectAll.prop('disabled', false);
392 this.$selectAll.prop('checked', this.selectedFiles().length == this.visibleFiles().length);
395 this.$selectAll.prop('disabled', true);
396 this.$selectAll.prop('checked', false);
400 clearSelection: function() {
401 _.each(this.visibleFiles(), function(item) {
402 item.set('checked', false);
405 this.updateSelectAll();