Skip to content

Instantly share code, notes, and snippets.

@cha0s
Created March 23, 2013 23:44
Show Gist options
  • Save cha0s/5229806 to your computer and use it in GitHub Desktop.
Save cha0s/5229806 to your computer and use it in GitHub Desktop.
$module.service 'title', [
'$timeout'
($timeout) ->
# Certain things will want to make the window/tab title flash for attention.
# Those things will use this API to do so.
_flashUpWrapper = (text) -> "¯¯¯#{text.toUpperCase()}¯¯¯"
_flashDownWrapper = (text) -> "___#{text}___"
_windowWrapper = angular.identity
flashInProgress = null
@flash = ->
# Don't queue up a million timeouts.
return if flashInProgress?
(flashTimeout = ->
if _windowWrapper is _flashUpWrapper
_windowWrapper = _flashDownWrapper
else
_windowWrapper = _flashUpWrapper
# I would like to flash a bit more often, but it seems like Firefox (and maybe
# others?) artificially limit how fast you can do this, and although I can't
# be sure (because I haven't tested enough), it appears that after an
# extended period of being limited in this way, Firefox crashes. So... we'll
# play it safe, they seem to limit to one update per second, so we'll give an
# extra 100ms padding to be sure! :)
flashInProgress = $timeout(
-> flashTimeout()
1100
)
)()
# Restore the window/tab title to normal again.
@stopFlashing = ->
$timeout.cancel flashInProgress
flashInProgress = null
_windowWrapper = angular.identity
# The wrappers below handle rendering the window title and flash states. The
# wrappers are passed in a single argument, the title text. The wrapper
# function returns another string, which is the text after whatever wrapping
# you'd like to do.
# Get and set the window title wrapper.
@windowWrapper = -> _windowWrapper
@setWindowWrapper = (windowWrapper) -> _windowWrapper = windowWrapper
# Get and set the flash down state wrapper.
@flashDownWrapper = -> _flashDownWrapper
@setFlashDownWrapper = (flashDownWrapper) -> _flashDownWrapper = flashDownWrapper
# Get and set the flash up state wrapper.
@flashUpWrapper = -> _flashUpWrapper
@setFlashUpWrapper = (flashUpWrapper) -> _flashUpWrapper = flashUpWrapper
# Get and set the page title.
_page = ''
@page = -> _page
@setPage = (page, setWindow = true) ->
_page = page
@setWindow [_page, _site].join _separator if setWindow
# Get and set the token that separates the page and window title.
_separator = ' | '
@separator = -> _separator
@setSeparator = (separator) -> _separator = separator
# Get and set the site name.
_site = ''
@site = -> _site
@setSite = (site) -> _site = site
# Get and set the window title.
_window = _site
@window = -> _windowWrapper _window
@setWindow = (window) -> _window = window
return
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment