/** * @author zhixin wen * https://github.com/wenzhixin/bootstrap-show-password * version: 1.0.3 */ !function ($) { 'use strict'; // tools definition // ====================== // it only does '%s', and return '' when arguments are undefined var sprintf = function(str) { var args = arguments, flag = true, i = 1; str = str.replace(/%s/g, function () { var arg = args[i++]; if (typeof arg === 'undefined') { flag = false; return ''; } return arg; }); if (flag) { return str; } return ''; }; // password class definition // ====================== var password = function(element, options) { this.options = options; this.$element = $(element); this.isshown = false; this.init(); }; password.defaults = { placement: 'after', // 'before' or 'after' white: false, // v2 message: 'click here to show/hide password', eyeclass: 'glyphicon', eyeopenclass: 'glyphicon-eye-open', eyecloseclass: 'glyphicon-eye-close' }; password.prototype.init = function() { var placementfuc, inputclass; // v2 class if (this.options.placement === 'before') { placementfuc = 'insertbefore'; inputclass = 'input-prepend'; } else { this.options.placement = 'after'; // default to after placementfuc = 'insertafter'; inputclass = 'input-append'; } // create the text, icon and assign this.$element.wrap(sprintf('
', inputclass)); this.$icon = $([ '', '', '' ].join(''))[placementfuc](this.$element).css('cursor', 'pointer'); this.$icon.off('click').on('click', $.proxy(function () { this.$element.prop('type', this.isshown ? 'password' : 'text'); this.toggle(); }, this)); }; password.prototype.toggle = function(_relatedtarget) { this[!this.isshown ? 'show' : 'hide'](_relatedtarget); }; password.prototype.show = function(_relatedtarget) { var e = $.event('show.bs.password', {relatedtarget: _relatedtarget}); this.$element.trigger(e); this.isshown = true; this.$icon.find('i') .removeclass('icon-eye-open ' + this.options.eyeopenclass) .addclass('icon-eye-close ' + this.options.eyecloseclass); }; password.prototype.hide = function(_relatedtarget) { var e = $.event('hide.bs.password', {relatedtarget: _relatedtarget}); this.$element.trigger(e); this.isshown = false; this.$icon.find('i') .removeclass('icon-eye-close ' + this.options.eyecloseclass) .addclass('icon-eye-open ' + this.options.eyeopenclass); }; password.prototype.val = function (value) { return this.$element.val(); }; // password plugin definition // ======================= var old = $.fn.password; $.fn.password = function() { var option = arguments[0], args = arguments, value, allowedmethods = ['show', 'hide', 'toggle', 'val']; // public function this.each(function() { var $this = $(this), data = $this.data('bs.password'), options = $.extend({}, password.defaults, $this.data(), typeof option === 'object' && option); if (typeof option === 'string') { if ($.inarray(option, allowedmethods) < 0) { throw "unknown method: " + option; } value = data[option](args[1]); } else { if (!data) { data = new password($this, options); $this.data('bs.password', data); } else { data.init(options); } } }); return value ? value : this; }; $.fn.password.constructor = password; // password no conflict // ================= $.fn.password.noconflict = function() { $.fn.password = old; return this; }; $(function () { $('[data-toggle="password"]').password(); }); }(window.jquery);