root = this $ = jQuery $.fn.showPassword = (options = {}) -> @each -> $this = $(this) data = $this.data("showpassword") if (!data) $this.data("showpassword", new ShowPassword(this, options)) if (typeof options is 'string') data[options]() class ShowPassword constructor: (@dom, options = {}) -> defaults = className: "password-toggler pass_input input_big" toggler: "a.password-toggler" title: "Hide
characters" @options = $.extend defaults, options this._setup() _setup: -> @field = $ @dom @fakeField = this._buildField() _parent = @field.parent() @toggler = if _parent.hasClass('field_error') then _parent.parent().find(@options.toggler) else _parent.find(@options.toggler) @original = @toggler.html() @toggler.bind("click", (e) => this.toggle() return false ) @fakeField.bind("change", (e) => @field.val(@fakeField.val()) ) _buildField: -> input = $("").addClass(@options.className) input.val(@field.val()) #.attr("readonly", true) input.insertAfter(@field).hide() return input toggle: -> if @field.is(":visible") this.hide() else this.show() hide: -> @toggler.html(@options.title) @field.hide() @fakeField.val(@field.val()).show() show: -> @toggler.html(@original) @field.val(@fakeField.val()).show() @fakeField.hide()