Skip to content

Commit

Permalink
Move the Tour object to Shepherd.Tour. Improve the event API, fix bug…
Browse files Browse the repository at this point in the history
…s with advanceOn.
  • Loading branch information
Zack Bloom committed Jan 23, 2014
1 parent bf1033f commit f11a045
Show file tree
Hide file tree
Showing 18 changed files with 2,415 additions and 484 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.sass-cache
node_modules/
deps/
bower_components/
.DS_Store
2 changes: 1 addition & 1 deletion .hsdoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ title: "shepherd"
description: "Guide your users through a tour of your app."
source: "shepherd.coffee"
examples: "**/*.md"
assets: "{shepherd.js,deps/tether/*.js,deps/tether/css/*,*.js,*.coffee,css/*.css,docs/css/*.css,docs/js/*,js,docs/welcome/*,examples/*}"
assets: "{shepherd.js,bower_components/tether/*.js,bower_components/tether/css/*,*.js,*.coffee,css/*.css,docs/css/*.css,docs/js/*,js,docs/welcome/*,examples/*}"
50 changes: 0 additions & 50 deletions Gruntfile.coffee

This file was deleted.

3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "shepherd",
"main": "shepherd.js",
"version": "0.2.1",
"version": "0.3.0",
"homepage": "https://github.com/HubSpot/shepherd",
"authors": [
"Zack Bloom <zbloom@hubspot.com>",
"Adam Schwartz <aschwartz@hubspot.com>"
],
"description": "Guide your users through a tour of your app.",
Expand Down
67 changes: 55 additions & 12 deletions shepherd.coffee → coffee/shepherd.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ parseShorthand = (obj, props) ->
out

class Step extends Evented
constructor: (@shepherd, options) ->
constructor: (@tour, options) ->
@setOptions options

@

setOptions: (@options={}) ->
@destroy()

Expand All @@ -53,20 +55,25 @@ class Step extends Evented

@options.buttons ?= [
text: 'Next'
action: @shepherd.next
action: @tour.next
]

getTour: ->
@tour

bindAdvance: ->
# An empty selector matches the step element
{event, selector} = parseShorthand @options.advanceOn, ['event', 'selector']

handler = (e) =>
return unless @isOpen()

if selector?
if matchesSelector(e.target, selector)
@shepherd.advance()
@tour.next()
else
if @el and e.target is @el
@shepherd.advance()
@tour.next()

document.body.addEventListener event, handler
# TODO: this should also bind/unbind on show/hide
Expand Down Expand Up @@ -131,6 +138,9 @@ class Step extends Evented

@trigger 'hide'

isOpen: =>
hasClass @el, 'shepherd-open'

cancel: =>
@hide()

Expand Down Expand Up @@ -229,38 +239,58 @@ class Step extends Evented
if typeof handler is 'string'
page = handler
handler = =>
@shepherd.show page
@tour.show page

el.addEventListener event, handler

@on 'destroy', ->
for event, handler of cfg.events
el.removeEventListener event, handler

class Shepherd extends Evented
class Tour extends Evented
constructor: (@options={}) ->
@steps = @options.steps ? []

# Pass these events onto the global Shepherd object
for event in ['complete', 'cancel', 'hide', 'start', 'show']
@on event, (opts={}) =>
opts.tour = @
Shepherd.trigger event, opts

@

addStep: (name, step) ->
if not step?
step = name
else
step.id = name

step = extend {}, @options.defaults, step
unless step instanceof Step
if typeof name in ['string', 'number']
step.id = name.toString()

step = extend {}, @options.defaults, step

@steps.push new Step(@, step)
step = new Step(@, step)
else
step.tour = @

@steps.push step

step

getById: (id) ->
for step in @steps when step.id is id
return step

getCurrentStep: ->
@currentStep

next: =>
index = @steps.indexOf(@currentStep)

if index is @steps.length - 1
@hide index
@trigger 'complete'
@done()
else
@show(index + 1)

Expand All @@ -273,29 +303,42 @@ class Shepherd extends Evented
@currentStep?.cancel()

@trigger 'cancel'
@done()

hide: =>
@currentStep?.hide()

@trigger 'hide'
@done()

done: ->
Shepherd.activeTour = null

show: (key=0) ->
if @currentStep
@currentStep.hide()

Shepherd.activeTour = @

if typeof key is 'string'
next = @getById key
else
next = @steps[key]

if next
@trigger 'shown', {step: next, previous: @currentStep}
@trigger 'show', {step: next, previous: @currentStep}

@currentStep = next
next.show()

start: ->
@trigger 'start'

@currentStep = null
@next()

window.Shepherd = Shepherd
Shepherd = new Evented

extend Shepherd, {Tour, Step}

window.Shepherd = Shepherd
Loading

0 comments on commit f11a045

Please sign in to comment.