-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjquery.preimage.js
61 lines (52 loc) · 2.08 KB
/
jquery.preimage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(function( $ ){
var settings = {
'scale': 'contain', // cover
'prefix': 'prev_',
'types': ['image/gif', 'image/png', 'image/jpeg'],
'mime': {'jpe': 'image/jpeg', 'jpeg': 'image/jpeg', 'jpg': 'image/jpeg', 'gif': 'image/gif', 'png': 'image/png', 'x-png': 'image/png', 'tif': 'image/tiff', 'tiff': 'image/tiff'}
};
var methods = {
init : function( options ) {
settings = $.extend(settings, options);
return this.each(function(){
$(this).bind('change', methods.change);
$('#'+settings['prefix']+this.id).html('').addClass(settings['prefix']+'container');
});
},
destroy : function( ) {
return this.each(function(){
$(this).unbind('change');
})
},
change : function(event) {
var id = this.id
$('#'+settings['prefix']+id).html('');
if(window.FileReader){
for(i=0; i<this.files.length; i++){
if(!$.inArray(this.files[i].type, settings['types']) == -1){
window.alert("File of not allowed type");
return false
}
}
for(i=0; i<this.files.length; i++){
var reader = new FileReader();
reader.onload = function (e) {
$('<div />').css({'background-image': ('url('+e.target.result+')'), 'background-repeat': 'no-repeat', 'background-size': settings['scale'] }).addClass(settings['prefix']+'thumb').appendTo($('#'+settings['prefix']+id));
};
reader.readAsDataURL(this.files[i]);
}
}else{
//if(window.confirm('Internet Explorer do not support required HTML5 features. \nPleas, download better browser - Firefox, Google Chrome, Opera... \nDo you want to download and install Google Chrome now?')){ window.location("//google.com/chrome"); }
}
}
};
$.fn.preimage = function( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.preimage' );
}
};
})( jQuery );