Skip to content

Instantly share code, notes, and snippets.

@jrthib
Last active August 29, 2015 14:02
Show Gist options
  • Save jrthib/5357c81eb9a69428a351 to your computer and use it in GitHub Desktop.
Save jrthib/5357c81eb9a69428a351 to your computer and use it in GitHub Desktop.
do ($ = jQuery, window) ->
pluginName = "economicImpactMap"
defaults =
delay: 2000
currentLocation = 0
rotator = null
class Plugin
constructor: (@element, options) ->
@settings = $.extend {}, defaults, options
@_defaults = defaults
@_name = pluginName
@factBox = $(@element).find('.factbox')
@factBoxLocations = $(@factBox).find('li')
@map = $(@element).find('.map')
@mapLocations = $(@map).find('li')
@init()
init: ->
# Move to the first location
@toLocation currentLocation
# Attach events
@events()
# Start rotation
@rotate true
# Clears all of the hover states
clearHovers: ->
@factBoxLocations.removeClass('hovered')
@mapLocations.removeClass('hovered')
# Attaches the enter and leave events to control the map
events: ->
# Reassign this to be used in the closure
that = @
# Apply events to factBoxLocations
$(location).mouseenter( ->
that.rotate false
currentLocation = that.factBoxLocations.index $(@)
that.toLocation currentLocation
).mouseleave( ->
that.rotate true
) for location in that.factBoxLocations
# Apply events to mapLocations
$(location).mouseenter( ->
that.rotate false
currentLocation = that.mapLocations.index $(@)
that.toLocation currentLocation
).mouseleave( ->
that.rotate true
) for location in that.mapLocations
# Kicks off the rotator interval
rotate: (ok) ->
if ok
rotator = setInterval (
$.proxy @nextLocation, this ), @settings.delay
else
clearInterval rotator
# Moves the hovered states to the specified location pair
toLocation: (index) ->
@clearHovers()
$(@mapLocations[index]).toggleClass('hovered')
$(@factBoxLocations[index]).toggleClass('hovered')
# Increments the global location index and moves to it
nextLocation: ->
if currentLocation < @factBoxLocations.length-1
currentLocation++
else
currentLocation = 0
@toLocation currentLocation
# Bootstrap the plugin and check that there isn't
# already an instance running
$.fn[pluginName] = (options) ->
@each ->
unless $.data @, "plugin_#{pluginName}"
$.data @, "plugin_#{pluginName}", new Plugin @, options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment