Last active
October 2, 2017 19:59
-
-
Save mattduggan/3d44e5b2543e9f03dacd3c5270504bcb to your computer and use it in GitHub Desktop.
Feature Tours - "Tour Manager" Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
this.FeatureTourManager = function() { | |
var Actions = Yesware.get("FeatureTourActions"); | |
var Store = Yesware.get("FeatureTourStore"); | |
const states = { | |
DISMISSED: "dismissed", | |
FINISHED: "finished", | |
SHOW: "show", | |
SHOW_LATER: "show_later" | |
}; | |
var timeoutId; | |
Store.onChange(function(state) { | |
if (!state.isLoading | |
&& !state.lastError | |
&& state.featureTours.length) { | |
clearTimeout(timeoutId); | |
var current = filterFeatureTours(state.featureTours)[0]; | |
var delay = Math.max(new Date(current.show_after).getTime()-new Date(current.show_after).getTime(), 60000); | |
var view = featureTourViewFactory(current); | |
timeoutId = setTimeout(view.show, delay); | |
} | |
}); | |
Actions.getFeatureTours(); | |
function filterFeatureTours(featureTours) { | |
return featureTours | |
.filter(function(featureTour) { | |
return ( | |
featureTour.status === states.SHOW | |
|| featureTour.status === states.SHOW_LATER | |
); | |
}) | |
.sort(function(a, b) { | |
return (a.show_after < b.show_after); | |
}); | |
} | |
function featureTourViewFactory(featureTour) { | |
var view; | |
switch(featureTour.tour_key) { | |
case "20170920_crm": | |
view = Yesware.get("FeatureTourCRMView"); // let view depend on the store and actions | |
break; | |
default: | |
throw new Error(featureTour.tour_key + " is not a known feature tour"); | |
break; | |
} | |
return view; | |
} | |
}; | |
}).apply(Yesware); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment