Sha256: 037e0d6bd94530c22af0afb30456575ba396ff93fd7cff78805603f7d03ef907

Contents?: true

Size: 1.71 KB

Versions: 24

Compression:

Stored size: 1.71 KB

Contents

u = up.util

class up.FieldObserver

  # Although (depending on the browser) we only need/receive either input or change,
  # we always bind to both events in case another script manually triggers it.
  CHANGE_EVENTS = 'input change'

  constructor: (@$field, options) ->
    @delay = options.delay
    @callback = options.callback

  start: =>
    # Don't use undefined since an unchecked checkbox actually has an undefined value
    @scheduledValue = null
    @processedValue = @readFieldValue()
    @currentTimer = undefined
    @currentCallback = undefined
    @$field.on(CHANGE_EVENTS, @check)

  stop: =>
    @$field.off(CHANGE_EVENTS, @check)
    @cancelTimer()

  cancelTimer: =>
    clearTimeout(@currentTimer)
    @currentTimer = undefined

  scheduleTimer: =>
    @currentTimer = u.setTimer @delay, =>
      @currentTimer = undefined
      @requestCallback()

  isNewValue: (value) =>
    value != @processedValue && (@scheduledValue == null || @scheduledValue != value)

  requestCallback: =>
    if @scheduledValue != null && !@currentTimer && !@currentCallback
      @processedValue = @scheduledValue
      @scheduledValue = null
      @currentCallback = => @callback.call(@$field.get(0), @processedValue, @$field)
      # If the callback returns a promise x, Promise.resolve(x) will wait for it
      callbackDone = Promise.resolve(@currentCallback())
      u.always callbackDone, =>
        # Don't use undefined since an unchecked checkbox actually has an undefined value
        @currentCallback = undefined
        @requestCallback()

  readFieldValue: =>
    u.submittedValue(@$field)

  check: =>
    value = @readFieldValue()
    if @isNewValue(value)
      @scheduledValue = value
      @cancelTimer()
      @scheduleTimer()

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
unpoly-rails-0.57.0 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.56.7 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.56.6 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.56.5 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.56.4 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.56.3 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.56.2 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.56.1 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.56.0 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.55.1 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.55.0 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.54.1 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.54.0 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.53.4 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.53.3 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.53.2 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.53.1 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.53.0 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.52.0 lib/assets/javascripts/unpoly/classes/field_observer.coffee
unpoly-rails-0.51.1 lib/assets/javascripts/unpoly/classes/field_observer.coffee