if(! ('ace' in window) ) window['ace'] = {} jQuery(function() { //at some places we try to use 'tap' event instead of 'click' if jquery mobile plugin is available window['ace'].click_event = $.fn.tap ? "tap" : "click"; }); (function($ , undefined) { var multiplible = 'multiple' in document.createElement('INPUT'); var hasFileList = 'FileList' in window;//file list enabled in modern browsers var hasFileReader = 'FileReader' in window; var Ace_File_Input = function(element , settings) { var self = this; this.settings = $.extend({}, $.fn.ace_file_input.defaults, settings); this.$element = $(element); this.element = element; this.disabled = false; this.can_reset = true; this.$element.on('change.ace_inner_call', function(e , ace_inner_call){ if(ace_inner_call === true) return;//this change event is called from above drop event return handle_on_change.call(self); }); this.$element.wrap('
'); this.apply_settings(); } Ace_File_Input.error = { 'FILE_LOAD_FAILED' : 1, 'IMAGE_LOAD_FAILED' : 2, 'THUMBNAIL_FAILED' : 3 }; Ace_File_Input.prototype.apply_settings = function() { var self = this; var remove_btn = !!this.settings.icon_remove; this.multi = this.$element.attr('multiple') && multiplible; this.well_style = this.settings.style == 'well'; if(this.well_style) this.$element.parent().addClass('ace-file-multiple'); else this.$element.parent().removeClass('ace-file-multiple'); this.$element.parent().find(':not(input[type=file])').remove();//remove all except our input, good for when changing settings this.$element.after(''+(remove_btn ? '' : '')); this.$label = this.$element.next(); this.$label.on('click', function(){//firefox mobile doesn't allow 'tap'! if(!this.disabled && !self.element.disabled && !self.$element.attr('readonly')) self.$element.click(); }) if(remove_btn) this.$label.next('a').on(ace.click_event, function(){ if(! self.can_reset ) return false; var ret = true; if(self.settings.before_remove) ret = self.settings.before_remove.call(self.element); if(!ret) return false; return self.reset_input(); }); if(this.settings.droppable && hasFileList) { enable_drop_functionality.call(this); } } Ace_File_Input.prototype.show_file_list = function($files) { var files = typeof $files === "undefined" ? this.$element.data('ace_input_files') : $files; if(!files || files.length == 0) return; ////////////////////////////////////////////////////////////////// if(this.well_style) { this.$label.find('.file-name').remove(); if(!this.settings.btn_change) this.$label.addClass('hide-placeholder'); } this.$label.attr('data-title', this.settings.btn_change).addClass('selected'); for (var i = 0; i < files.length; i++) { var filename = typeof files[i] === "string" ? files[i] : $.trim( files[i].name ); var index = filename.lastIndexOf("\\") + 1; if(index == 0)index = filename.lastIndexOf("/") + 1; filename = filename.substr(index); var fileIcon = 'icon-file'; if((/\.(jpe?g|png|gif|svg|bmp|tiff?)$/i).test(filename)) { fileIcon = 'icon-picture'; } else if((/\.(mpe?g|flv|mov|avi|swf|mp4|mkv|webm|wmv|3gp)$/i).test(filename)) fileIcon = 'icon-film'; else if((/\.(mp3|ogg|wav|wma|amr|aac)$/i).test(filename)) fileIcon = 'icon-music'; if(!this.well_style) this.$label.find('.file-name').attr({'data-title':filename}).find('[class*="icon-"]').attr('class', fileIcon); else { this.$label.append(''); var type = $.trim(files[i].type); var can_preview = hasFileReader && this.settings.thumbnail && ( (type.length > 0 && type.match('image')) || (type.length == 0 && fileIcon == 'icon-picture') )//the second one is for Android's default browser which gives an empty text for file.type if(can_preview) { var self = this; $.when(preview_image.call(this, files[i])).fail(function(result){ //called on failure to load preview if(self.settings.preview_error) self.settings.preview_error.call(self, filename, result.code); }); } } } return true; } Ace_File_Input.prototype.reset_input = function() { this.$label.attr({'data-title':this.settings.btn_choose, 'class':'file-label'}) .find('.file-name:first').attr({'data-title':this.settings.no_file , 'class':'file-name'}) .find('[class*="icon-"]').attr('class', this.settings.no_icon) .prev('img').remove(); if(!this.settings.no_icon) this.$label.find('[class*="icon-"]').remove(); this.$label.find('.file-name').not(':first').remove(); if(this.$element.data('ace_input_files')) { this.$element.removeData('ace_input_files'); this.$element.removeData('ace_input_method'); } this.reset_input_field(); return false; } Ace_File_Input.prototype.reset_input_field = function() { //http://stackoverflow.com/questions/1043957/clearing-input-type-file-using-jquery/13351234#13351234 this.$element.wrap('