root / snf-cyclades-app / synnefo / ui / new_ui / ui / javascripts / dragfiles.js @ 7327e185
History | View | Annotate | Download (4.4 kB)
1 |
function bytesToSize(bytes) { |
---|---|
2 |
var sizes = [ 'n/a', 'bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; |
3 |
var i = +Math.floor(Math.log(bytes) / Math.log(1024)); |
4 |
return (bytes / Math.pow(1024, i)).toFixed( 0 ) + sizes[ isNaN( bytes ) ? 0 : i+1 ]; |
5 |
} |
6 |
|
7 |
;(function( $, window, document, undefined ){ |
8 |
|
9 |
// our constructor
|
10 |
var Dragfiles = function( elem, options ){ |
11 |
this.elem = elem;
|
12 |
this.options = options;
|
13 |
}; |
14 |
|
15 |
// the plugin prototype
|
16 |
Dragfiles.prototype = { |
17 |
defaults: {
|
18 |
dictIntroMessage: 'Drag and Drop your files here', |
19 |
dictRemoveFile: 'Remove File', |
20 |
dictFilesUploading: ' file(s) uploading' |
21 |
}, |
22 |
|
23 |
files: [],
|
24 |
|
25 |
init: function() { |
26 |
|
27 |
var self = this; |
28 |
// Introduce defaults that can be extended either
|
29 |
// globally or using an object literal.
|
30 |
this.config = $.extend({}, this.defaults, this.options); |
31 |
|
32 |
// Sample usage:
|
33 |
// Set the message per instance:
|
34 |
// $('#elem').dragfiles({ dictRemoveFile: 'Get it out!'});
|
35 |
// or
|
36 |
// var p = new Dragfiles(document.getElementById('elem'),
|
37 |
// { dictRemoveFile: 'Get it out!'}).init()
|
38 |
// or, set the global default message:
|
39 |
// Dragfiles.defaults.dictRemoveFile = 'Get it out!'
|
40 |
|
41 |
console.log('this.elem',this.elem); |
42 |
|
43 |
$(this.elem).on('dragenter',function(e){ |
44 |
self.dragenter(e); |
45 |
}); |
46 |
|
47 |
$(this.elem).on('dragleave',function(e){ |
48 |
self.dragleave(e); |
49 |
}); |
50 |
|
51 |
$(this.elem).on('dragover',function(e){ |
52 |
self.dragover(e); |
53 |
}); |
54 |
|
55 |
$(this.elem).on('drop',function(e){ |
56 |
self.drop(e); |
57 |
}); |
58 |
|
59 |
$('input[type=file]').on('change',function (e) { |
60 |
window.test = e; |
61 |
self.addfiles( e.originalEvent.target.files ); |
62 |
self.fileSelectHandler(e); |
63 |
}); |
64 |
|
65 |
$('.remove a').on('click',function (e) { |
66 |
e.preventDefault(); |
67 |
alert('k');
|
68 |
}); |
69 |
|
70 |
return this; |
71 |
|
72 |
}, |
73 |
|
74 |
dragenter: function(e){ |
75 |
console.log('Dragenter');
|
76 |
}, |
77 |
|
78 |
dragleave: function(e){ |
79 |
console.log('Dragleave');
|
80 |
$(this.elem).removeClass('drag'); |
81 |
}, |
82 |
|
83 |
dragover: function(e){ |
84 |
e.stopPropagation(); |
85 |
e.preventDefault(); |
86 |
console.log('Dragover');
|
87 |
$(this.elem).addClass('drag'); |
88 |
}, |
89 |
|
90 |
drop: function(e){ |
91 |
e.stopPropagation(); |
92 |
e.preventDefault(); |
93 |
$(this.elem).removeClass('drag'); |
94 |
this.addfiles(e.originalEvent.dataTransfer.files)
|
95 |
this.fileSelectHandler(e);
|
96 |
}, |
97 |
|
98 |
fileSelectHandler: function (e){ |
99 |
$('.storage-progress .summary').html(this.files.length + this.config.dictFilesUploading); |
100 |
$('.storage-progress').slideDown(); |
101 |
this.parseFiles(this.files); |
102 |
}, |
103 |
|
104 |
parseFiles: function(files) { |
105 |
var txt =''; |
106 |
var self = this; |
107 |
var list = $('.storage-progress .items-list'); |
108 |
list.find('li').remove();
|
109 |
|
110 |
_.map(files, function(f, index) {
|
111 |
var txt = ''; |
112 |
txt += '<li>';
|
113 |
txt += '<div class="img-wrap">';
|
114 |
txt += '<img src="images/icon-txt.png" alt="" />';
|
115 |
txt += '</div>';
|
116 |
txt += '<h4>'+ f.name +'</h4>'; |
117 |
txt += '<div class="size">'+ bytesToSize(f.size) +'</div>'; |
118 |
txt += '<div class="progress-col"><div class="progress">';
|
119 |
txt += '<span class="meter" style="width: 30%">30%</span>';
|
120 |
txt += '</div></div>';
|
121 |
txt += '<div class="remove"><a href="">X <em>Remove</em></a></div>';
|
122 |
txt += '</li>';
|
123 |
el = $(txt);
|
124 |
el.find("a").on('click', _.bind(function(e) { |
125 |
e.preventDefault(); |
126 |
console.log(index); |
127 |
console.log("F", f);
|
128 |
this.removefile(f.name);
|
129 |
}, this));
|
130 |
|
131 |
list.append(el); |
132 |
}, this);
|
133 |
this.listFiles();
|
134 |
}, |
135 |
|
136 |
removefile: function(file) { |
137 |
console.log(file); |
138 |
var index = this.files.indexOf(file); |
139 |
if (index > -1) { |
140 |
this.files.splice(index, 1); |
141 |
} |
142 |
}, |
143 |
|
144 |
addfile : function(file) { |
145 |
this.files.push(files);
|
146 |
}, |
147 |
|
148 |
addfiles: function(files) { |
149 |
for (var i = 0, f; f = files[i]; i++) { |
150 |
this.files.push(f);
|
151 |
} |
152 |
}, |
153 |
|
154 |
listFiles : function() { |
155 |
console.log(this.files);
|
156 |
}, |
157 |
|
158 |
} |
159 |
|
160 |
Dragfiles.defaults = Dragfiles.prototype.defaults; |
161 |
|
162 |
$.fn.dragfiles = function(options) { |
163 |
return this.each(function() { |
164 |
new Dragfiles(this, options).init(); |
165 |
}); |
166 |
}; |
167 |
|
168 |
})( jQuery, window , document ); |
169 |
|
170 |
|
171 |
$(document).ready(function(){ |
172 |
$('#drop').dragfiles(); |
173 |
}) |