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 #host-controls .view': 'hostEdit',
213 'click #select-all': 'selectAll'
216 initialize: function(options) {
217 this.$noItems = $('#no-items');
218 this.$error = $('#error');
219 this.$showHidden = $('#show-hidden');
220 this.$host = $('#host-controls');
221 this.$selectAll = $('#select-all');
223 this.host = options.host;
224 this.path = new Filex.Path();
225 this.files = new Filex.FileList();
227 this.listenTo(this.path, 'reset', this.resetPath);
228 this.listenTo(this.path, 'add', this.reloadFiles);
229 this.listenTo(this.path, 'add', this.addPath);
230 this.listenTo(this.path, 'selected', this.selectedPath);
231 this.listenTo(this.files, 'reset', this.resetFiles);
232 this.listenTo(this.files, 'selected:dir', this.selectedDir);
233 this.listenTo(this.files, 'change:checked', this.updateSelectAll);
235 // used in selectize callbacks
237 optionTemplate = _.template('<div><div><%= host %></div><div class="small text-muted"><%= path %></div></div>');
239 this.$('#host-selector').selectize({
242 searchField: ['host', 'path'],
244 {field: 'host', direction: 'asc'},
245 {field: 'path', direction: 'asc'}
248 options: options.hostOptions,
250 option: function(item) {
251 return optionTemplate(item);
254 onDropdownClose: function() {
258 view.$host.toggleClass('editing');
260 var value = this.getValue();
262 this.addItem(view.host, true);
266 if (value != view.host) {
267 var location = this.options[value];
269 view.host = location.host;
270 view.navigate(location.path);
274 this.hostSelectize = this.$('#host-selector')[0].selectize;
277 this.navigate(this.hostSelectize.options[this.host].path);
281 this.updateSelectAll();
282 this.$host.find('.view').text(this.host);
283 this.$noItems.toggle(!Boolean(this.visibleFiles().length));
287 navigate: function(path) {
288 var pathBits = [new Filex.PathBit({'text': '/', 'path': ''})];
290 pathBits = pathBits.concat(_.map(path.replace(/(^\/+|\/+$)/g, '').split('/'), function(name) {
291 return new Filex.PathBit({'text': name, 'path': name});
294 this.path.reset(pathBits);
297 reloadFiles: function() {
304 data: {host: this.host, path: this.path.full()},
305 success: function() {
309 error: function(collection, response) {
312 var msg = (response.responseJSON || {}).msg || 'Błąd serwera';
314 view.$noItems.hide();
315 view.$error.find('.msg').text(msg);
322 addPath: function(bit) {
323 var view = new Filex.Breadcrumb({model: bit});
324 this.$('.path').append(view.render().el);
327 resetPath: function(models, options) {
330 _.each(options.previousModels, function(model) {
331 model.trigger('remove');
334 this.path.each(this.addPath, this);
337 resetFiles: function(models, options) {
338 _.each(options.previousModels, function(model) {
339 model.trigger('remove');
342 this.files.each(function(file) {
343 var view = new Filex.ListingItem({model: file, view: this});
344 this.$('tbody').append(view.render().el);
348 toggleHidden: function() {
349 this.files.each(function(item) { item.trigger('hidden'); }, this);
353 selectedDir: function(dir) {
354 this.path.add({'text': dir.get('name'), 'path': dir.get('name')});
357 selectedPath: function(bit) {
358 var newPath = this.path.slice(0, this.path.indexOf(bit) + 1);
359 this.path.set(newPath);
363 showHidden: function() {
364 return this.$showHidden[0].checked;
367 hostEdit: function() {
368 this.$host.toggleClass('editing');
369 this.hostSelectize.focus();
373 this.$el.addClass('busy');
377 this.$el.removeClass('busy');
380 visibleFiles: function() {
381 return this.showHidden() ? this.files.models : this.files.visible();
384 selectedFiles: function() {
385 return _.filter(this.visibleFiles(), function(item) {
386 return item.get('checked');
390 selectAll: function() {
391 var checked = this.$selectAll[0].checked;
393 _.each(this.visibleFiles(), function(item) {
394 item.set('checked', checked);
398 updateSelectAll: function() {
399 this.$selectAll.prop('checked', this.selectedFiles().length == this.visibleFiles().length);
402 clearSelection: function() {
403 _.each(this.visibleFiles(), function(item) {
404 item.set('checked', false);
407 this.updateSelectAll();