Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / new_ui / ui / javascripts / dragfiles.js @ 3fd0db82

History | View | Annotate | Download (7.7 kB)

1
;(function( $, window, document, undefined ){
2

    
3
  // our constructor
4
  var Dragfiles = function( elem, options ){
5
      this.elem = elem;
6
      this.options = options;
7
    };
8

    
9
  // the plugin prototype
10
  Dragfiles.prototype = {    
11
    defaults: {
12
      dictIntroMessage: 'Drag and Drop your files here',
13
      dictremoveFile: 'Remove File',
14
      dictFilesUploading: ' file(s) uploading',
15
      dictLastUpdated: 'Last updated: ',
16
      dictAllFilesUploaded: 'No more files to upload!',
17
    },
18
    
19
    files: [],
20
    
21
    init: function() {
22
      var self = this;
23
      // Introduce defaults that can be extended either 
24
      // globally or using an object literal. 
25
      this.config = $.extend({}, this.defaults, this.options);
26

    
27
      // Sample usage:
28
      // Set the message per instance:
29
      // $('#elem').dragfiles({ dictremoveFile: 'Get it out!'});
30
      // or
31
      // var p = new Dragfiles(document.getElementById('elem'), 
32
      // { dictremoveFile: 'Get it out!'}).init()
33
      // or, set the global default message:
34
      // Dragfiles.defaults.dictremoveFile = 'Get it out!'
35

    
36
      // temporary
37
      folders = new Array();
38
      for(var i=0; i<$(this.elem).find('.storage.entities>.items-list>li').length ; i++) {
39
        folders[i] = [];
40
      }
41
      
42
      $(this.elem).on('dragenter',function(e){
43
        self.dragEnter(e);
44
      });
45

    
46
      $(this.elem).on('dragleave',function(e){
47
        self.dragLeave(e);
48
      });
49

    
50
      $(this.elem).on('dragover',function(e){
51
        self.dragOver(e);
52
      });
53

    
54
      $(this.elem).on('drop',function(e){
55
        self.drop(e);
56
      });
57

    
58
      // folder specific events
59
      $(this.elem).find('.folder').on('dragover', function(e){
60
        $(this).addClass('draghover');
61
      });
62

    
63
      $(this.elem).find('.folder').on('dragleave', function(e){
64
        $(this).removeClass('draghover');
65
      });
66

    
67
      $(this.elem).find('.folder').on('drop',function(e){
68
        console.log($(this).data('path'), 'path');
69
        $(this).removeClass('draghover');
70
        var index = $(this).index();
71
        if($(this).hasClass('updated')) {
72
          if(folders[index].length > 1) {
73
            clearTimeout(folders[index][1]);
74
          }
75
          clearTimeout(folders[index][0]);
76
        }
77
        else if($(this).hasClass('updating')) {
78
          if(folders[index].length > 1) {
79
            clearTimeout(folders[index][1]);
80
          }
81
          clearTimeout(folders[index][0]);
82
        }
83
        self.resetFolder(this);
84
        self.updateFolder(this);
85
      });
86

    
87
      $('input[type=file]').on('change',function (e) {
88
        window.test = e;
89
        self.addFiles( e.originalEvent.target.files );
90
        self.fileSelectHandler(e);
91
      });
92

    
93
      $('.remove a').on('click',function (e) {
94
        e.preventDefault();
95
        alert('k');
96
      });
97

    
98
      return this;
99

    
100
    },
101
    // temporary
102
    updateFolder: function(folder) {
103
      var folderIndex = $(folder).index();
104
      $(folder).find('.img-wrap span').hide();
105
      $(folder).addClass('updating');
106
      folders[folderIndex][0] = setTimeout(function() {
107
        $(folder).removeClass('updating');
108
        $(folder).addClass('updated');
109
        folders[folderIndex][1] = setTimeout(function() {
110
          $(folder).removeClass('updated');
111
          $(folder).find('.img-wrap span').show();
112
        }, 500);
113
      }, 1000);
114
    },
115
    // temporary
116
    resetFolder : function(folder) {
117
      $(folder).find('.img-wrap span').hide();
118
      $(folder).removeClass('updated');
119
      $(folder).removeClass('updating');
120
    },
121

    
122
    dragEnter: function(e){
123
      console.log('Dragenter');
124
    },
125

    
126
    dragLeave: function(e){
127
      console.log('Dragleave');
128
      $(this.elem).find('#drop').removeClass('drag');
129
    },
130

    
131
    dragOver: function(e){
132
      e.stopPropagation();
133
      e.preventDefault();
134
      // console.log('Dragover');
135
      $(this.elem).find('#drop').addClass('drag');
136
    },
137

    
138
    drop: function(e){
139
      e.stopPropagation();
140
      e.preventDefault();
141
      var self = this;
142
      $(this.elem).find('#drop').removeClass('drag');
143
      this.addFiles(e.originalEvent.dataTransfer.files)
144
      this.fileSelectHandler(e);
145
      setTimeout( function(){
146
        self.fileUploaded(self.files[0])
147
        self.removeFile(self.files[0], $('.storage-progress .items-list li').first());
148
      } ,1000);
149

    
150

    
151
    },
152

    
153
    fileSelectHandler: function (e){
154
      $('.storage-progress').slideDown();
155
      this.parseFiles(this.files);
156
    },
157

    
158
    parseFiles: function(files) {
159
      var txt ='';
160
      var self = this;
161
      var list = $('.storage-progress .items-list');
162
      var summary = $('.storage-progress .summary');
163
      list.find('li').remove();
164
      if (this.files.length> 0 ) {
165
        summary.html('<span>'+this.files.length +'</span>'+ this.config.dictFilesUploading);
166
      } else {
167
        summary.html(this.config.dictAllFilesUploaded);
168
      }
169
      summary.find('span').addClass('animated');
170
      setTimeout(function(){
171
        summary.find('span').removeClass('animated');
172
      }, 500)
173
      _.map(files, function(f, index) {
174
        var txt = '';
175
        txt += '<li>';
176
        txt += '<div class="img-wrap">';
177
        txt += '<span class="'+ ui.mimeToExt(f.type) +'"></span>';
178
        txt += '</div>';
179
        txt += '<h4>'+ f.name +'</h4>';
180
        txt += '<div class="size">'+ bytesToSize(f.size) +'</div>';
181
        txt += '<div class="progress-col"><div class="progress">';
182
        txt += '<span class="meter" style="width: 30%">30%</span>';
183
        txt += '</div></div>';
184
        txt += '<div class="remove"><a href="" title="'+this.config.dictremoveFile+'">X <em>Remove</em></a></div>';
185
        txt += '</li>';
186
        el = $(txt);
187
        el.find("a").on('click', _.bind(function(e) {
188
          console.log(e,'e');
189
          console.log(this, 'thisis');
190
          e.preventDefault();
191
          this.removeFile(f, el);
192
        }, this));
193
        list.append(el);
194
      }, this);
195

    
196
      if (this.files.length <= 0){
197
        setTimeout( this.removeArea(), 1000);
198
      }
199
    },
200

    
201
    removeArea: function(){
202
      var el = $('.storage-progress');
203
      el.addClass('ready');
204
      setTimeout( function(){
205
        el.fadeOut('slow', function(){
206
          el.removeClass('ready');
207
        });
208

    
209
      } ,4000);
210
    },
211

    
212
    removeFile: function(file, el) {
213
      var index = this.files.indexOf(file);
214
      var self = this;
215
      if (index > -1) {
216
        this.files.splice(index, 1);
217
      }
218
      if (el) {
219
        el.fadeOut(1000, function(){
220
          self.parseFiles(self.files);
221
        });
222
      }
223
    },
224

    
225
    addFile : function(file) {
226
      this.files.push(files);
227
    },
228

    
229
    addFiles: function(files) {
230
      for (var i = 0, f; f = files[i]; i++) {
231
        this.files.push(f);
232
      }
233
    },
234

    
235
    listFiles : function() {
236
      console.log(this.files);
237
    },
238

    
239
    fileUploaded: function(file) {
240
      var txt = '';
241
      txt += '<li class="with-checkbox">';
242
      txt += '<div class="check"><span class="snf-checkbox-unchecked"></span></div>';
243
      txt += '<div class="img-wrap">';
244
      txt += '<span class='+ ui.mimeToExt(file.type) +'></span>';
245
      txt += '</div>';
246
      txt += '<h4>'+ file.name +'</h4>';
247
      txt += '<div class="size">'+ bytesToSize(file.size) +'</div>';
248
      txt += '<div class="info">'+ this.config.dictLastUpdated + date_ddmmmyytime(file.lastModifiedDate) +'</div>';
249
      txt += '<div class="actions-col"></div>';
250
      txt += '</li>';
251
      el = $(txt);
252
      el.find("a").on('click', _.bind(function(e) {
253
        e.preventDefault();
254
        this.removeFile(f, el);
255
      }, this));
256
      $(this.elem).find('#drop').append(el);
257
    },
258
  }
259

    
260
  Dragfiles.defaults = Dragfiles.prototype.defaults;
261

    
262
  $.fn.dragfiles = function(options) {
263
    return this.each(function() {
264
      new Dragfiles(this, options).init();
265
    });
266
  };
267

    
268
})( jQuery, window , document );
269

    
270

    
271
$(document).ready(function(){
272
  $('.body-wrapper').dragfiles();
273
})