Sha256: 4da49a07d196638854df339364d85e1c44cef0ef0e876cb3584d51a40e8b12f0

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

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<br>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 = $("<input></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()

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
translation_cms-0.1.5 app/assets/javascripts/app/vendor/show_password.js.coffee