/* * bootstrap-filestyle * Doc: http://dev.tudosobreweb.com.br/bootstrap-filestyle/ * Github: https://github.com/markusslima/bootstrap-filestyle * * Copyright (c) 2014 Markus Vinicius da Silva Lima * Version 1.0.6 * Licensed under the MIT license. */ (function ($) { "use strict"; var Filestyle = function (element, options) { this.options = options; this.$elementFilestyle = []; this.$element = $(element); }; Filestyle.prototype = { clear: function () { this.$element.val(''); this.$elementFilestyle.find(':text').val(''); }, destroy: function () { this.$element .removeAttr('style') .removeData('filestyle') .val(''); this.$elementFilestyle.remove(); }, disabled: function (value) { if (value === true) { if (!this.options.disabled) { this.$element .attr('disabled', 'true'); this.$elementFilestyle.find('label').attr('disabled', 'true'); this.options.disabled = true; } } else if (value === false) { if (this.options.disabled) { this.$element .removeAttr('disabled'); this.$elementFilestyle.find('label').removeAttr('disabled'); this.options.disabled = false; } } else { return this.options.disabled; } }, buttonBefore: function (value) { if (value === true) { if (!this.options.buttonBefore) { this.options.buttonBefore = true; if (this.options.input) { this.$elementFilestyle.remove(); this.constructor(); this.pushNameFiles(); } } } else if (value === false) { if (this.options.buttonBefore) { this.options.buttonBefore = false; if (this.options.input) { this.$elementFilestyle.remove(); this.constructor(); this.pushNameFiles(); } } } else { return this.options.buttonBefore; } }, icon: function (value) { if (value === true) { if (!this.options.icon) { this.options.icon = true; this.$elementFilestyle.find('label').prepend(this.htmlIcon()); } } else if (value === false) { if (this.options.icon) { this.options.icon = false; this.$elementFilestyle.find('i').remove(); } } else { return this.options.icon; } }, input: function (value) { if (value === true) { if (!this.options.input) { this.options.input = true; if (this.options.buttonBefore) { this.$elementFilestyle.append(this.htmlInput()); } else { this.$elementFilestyle.prepend(this.htmlInput()); } this.$elementFilestyle.find('.quant-files-filestyle').remove(); var content = '', files = []; if (this.$element[0].files === undefined) { files[0] = {'name': this.$element[0].value}; } else { files = this.$element[0].files; } for (var i = 0; i < files.length; i++) { content += files[i].name.split("\\").pop() + ', '; } if (content !== '') { this.$elementFilestyle.find(':text').val(content.replace(/\, $/g, '')); } } } else if (value === false) { if (this.options.input) { this.options.input = false; this.$elementFilestyle.find(':text').remove(); var files = []; if (this.$element[0].files === undefined) { files[0] = {'name': this.$element[0].value}; } else { files = this.$element[0].files; } if (files.length > 0) { var style; if (this.options.classButton.search(/btn-inverse|btn-primary|btn-danger|btn-warning|btn-success/i) !== -1) { style = 'style="background-color: #fff !important; color: 000;"'; } else { style = 'style="background-color: #000 !important; color: fff;"'; } this.$elementFilestyle.find('label').append(' '+files.length+''); } } } else { return this.options.input; } }, buttonText: function (value) { if (value !== undefined) { this.options.buttonText = value; this.$elementFilestyle.find('label span').html(this.options.buttonText); } else { return this.options.buttonText; } }, classButton: function (value) { if (value !== undefined) { this.options.classButton = value; this.$elementFilestyle.find('label').attr({'class': this.options.classButton}); if (this.options.classButton.search(/btn-inverse|btn-primary|btn-danger|btn-warning|btn-success/i) !== -1) { this.$elementFilestyle.find('label i').addClass('icon-white'); } else { this.$elementFilestyle.find('label i').removeClass('icon-white'); } } else { return this.options.classButton; } }, classIcon: function (value) { if (value !== undefined) { this.options.classIcon = value; if (this.options.classButton.search(/btn-inverse|btn-primary|btn-danger|btn-warning|btn-success/i) !== -1) { this.$elementFilestyle.find('label').find('i').attr({'class': 'icon-white '+this.options.classIcon}); } else { this.$elementFilestyle.find('label').find('i').attr({'class': this.options.classIcon}); } } else { return this.options.classIcon; } }, classInput: function (value) { if (value !== undefined) { this.options.classInput = value; this.$elementFilestyle.find(':text').addClass(this.options.classInput); } else { return this.options.classInput; } }, htmlIcon: function () { if (this.options.icon) { var colorIcon = ''; if (this.options.classButton.search(/btn-inverse|btn-primary|btn-danger|btn-warning|btn-success/i) !== -1) { colorIcon = ' icon-white '; } return ' '; } else { return ''; } }, htmlInput: function () { if (this.options.input) { return ' '; } else { return ''; } }, // puts the name of the input files pushNameFiles: function () { var content = '', files = []; if (this.$element[0].files === undefined) { files[0] = {'name': this.$element.value}; } else { files = this.$element[0].files; } for (var i = 0; i < files.length; i++) { content += files[i].name.split("\\").pop() + ', '; } if (content !== '') { this.$elementFilestyle.find(':text').val(content.replace(/\, $/g, '')); } else { this.$elementFilestyle.find(':text').val(''); } }, constructor: function () { var _self = this, html = '', id = this.$element.attr('id'), files = []; if (id === '' || !id) { id = 'filestyle-'+$('.bootstrap-filestyle').length; this.$element.attr({'id': id}); } if(this.options.buttonBefore) { html = ''+ this.htmlInput(); } else { html = this.htmlInput()+ ''; } this.$elementFilestyle = $('