/* * bootstrap-filestyle * doc: http://markusslima.github.io/bootstrap-filestyle/ * github: https://github.com/markusslima/bootstrap-filestyle * * Copyright (c) 2014 Markus Vinicius da Silva Lima * Version 1.2.1 * Licensed under the MIT license. */ (function($) { "use strict"; var nextId = 0; 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(""); this.$elementFilestyle.find(".badge").remove(); }, destroy: function() { this.$element.removeAttr("style").removeData("filestyle"); 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(".icon-span-filestyle").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(".badge").remove(); this.pushNameFiles(); this.$elementFilestyle.find(".group-span-filestyle").addClass("input-group-btn"); } } else if (value === false) { if (this.options.input) { this.options.input = false; this.$elementFilestyle.find(":text").remove(); var files = this.pushNameFiles(); if (files.length > 0 && this.options.badge) { this.$elementFilestyle .find("label") .append(' ' + files.length + ""); } this.$elementFilestyle.find(".group-span-filestyle").removeClass("input-group-btn"); } } else { return this.options.input; } }, size: function(value) { if (value !== undefined) { var btn = this.$elementFilestyle.find("label"), input = this.$elementFilestyle.find("input"); btn.removeClass("btn-lg btn-sm"); input.removeClass("input-lg input-sm"); if (value != "nr") { btn.addClass("btn-" + value); input.addClass("input-" + value); } } else { return this.options.size; } }, placeholder: function(value) { if (value !== undefined) { this.options.placeholder = value; this.$elementFilestyle.find("input").attr("placeholder", value); } else { return this.options.placeholder; } }, buttonText: function(value) { if (value !== undefined) { this.options.buttonText = value; this.$elementFilestyle.find("label .buttonText").html(this.options.buttonText); } else { return this.options.buttonText; } }, buttonName: function(value) { if (value !== undefined) { this.options.buttonName = value; this.$elementFilestyle.find("label").attr({ class: "btn " + this.options.buttonName }); } else { return this.options.buttonName; } }, iconName: function(value) { if (value !== undefined) { this.$elementFilestyle.find(".icon-span-filestyle").attr({ class: "icon-span-filestyle " + this.options.iconName }); } else { return this.options.iconName; } }, htmlIcon: function() { if (this.options.icon) { return ' '; } else { return ""; } }, htmlInput: function() { if (this.options.input) { return ( ' ' ); } else { return ""; } }, // puts the name of the input files // return files pushNameFiles: function() { var content = "", files = []; if (this.$element[0].files === undefined) { files[0] = { name: this.$element[0] && 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 { this.$elementFilestyle.find(":text").val(""); } return files; }, constructor: function() { var _self = this, html = "", id = _self.$element.attr("id"), files = [], btn = "", clear = "", $label; if (id === "" || !id) { id = "filestyle-" + nextId; _self.$element.attr({ id: id }); nextId++; } html = '
" + _self.htmlInput() : _self.htmlInput() + html + clear + btn + ""; _self.$elementFilestyle = $( '