Skip to content

Instantly share code, notes, and snippets.

@wolfflow
Last active December 12, 2015 07:48
Show Gist options
  • Save wolfflow/4739277 to your computer and use it in GitHub Desktop.
Save wolfflow/4739277 to your computer and use it in GitHub Desktop.
CoffeeScript version done with js2coffee and some manual formatting ^ ^ )
nonEmpty = (x) -> x and x.length > 0
isChrome = navigator.userAgent.toLowerCase().indexOf("chrome") > -1
Bacon.UI = {}
Bacon.UI.textFieldValue = (textfield, initValue) ->
getValue = ->
textfield.val()
autofillPoller = ->
if textfield.attr("type") is "password"
Bacon.interval 100
else if isChrome
Bacon.interval(100).take(20).map(getValue).filter(nonEmpty).take 1
else
Bacon.never()
textfield.val initValue if initValue isnt null
textfield.asEventStream("keyup input").merge(textfield.asEventStream("cut paste").delay(1)).merge(autofillPoller()).map(getValue).toProperty(getValue()).skipDuplicates()
Bacon.UI.optionValue = (option) ->
getValue = ->
option.val()
option.asEventStream("change").map(getValue).toProperty getValue()
Bacon.UI.checkBoxGroupValue = (checkboxes, initValue) ->
selectedValues = ->
checkboxes.filter(":checked").map((i, elem) ->
$(elem).val()
).toArray()
if initValue
checkboxes.each (i, elem) ->
$(elem).attr "checked", initValue.indexOf($(elem).val()) >= 0
checkboxes.asEventStream("click").map(selectedValues).toProperty selectedValues()
Bacon.UI.ajax = (params) ->
Bacon.fromPromise $.ajax(params)
Bacon.Observable::awaiting = (response) ->
@map(true).merge(response.map(false)).toProperty(false).skipDuplicates()
Bacon.EventStream::ajax = ->
this["switch"] Bacon.UI.ajax
Bacon.UI.radioGroupValue = (radioButtons, init) ->
if init?
radioButtons.each (i, elem) ->
$(elem).attr 'checked', true if elem.value is init
else init = radioButtons.filter(':checked').first().val()
radioButtons.asEventStream('change').map((e) -> e.target.value).toProperty init
Bacon.UI.checkBoxValue = (checkbox, initValue) ->
isChecked = ->
!!checkbox.attr("checked")
checkbox.attr "checked", initValue if initValue isnt null
checkbox.asEventStream("change").map(isChecked).toProperty(isChecked()).skipDuplicates()
Bacon.UI.hash = (defaultValue) ->
defaultValue = "" if defaultValue is undefined
getHash = ->
(if !!document.location.hash then document.location.hash else defaultValue)
$(window).asEventStream("hashchange").map(getHash).toProperty(getHash()).skipDuplicates()
@dubiousdavid
Copy link

Update to radioGroupValue. Fixes an issue I found with the previous version.

Bacon.UI.radioGroupValue = (radioButtons, init) ->
  if init?
    radioButtons.each (i, elem) ->
      $(elem).attr 'checked', true if elem.value is init
  else init = radioButtons.filter(':checked').first().val()

  radioButtons.asEventStream('change').map((e) -> e.target.value).toProperty init

@wolfflow
Copy link
Author

wolfflow commented Feb 8, 2013

Done )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment