Skip to content

Commit b4a2660

Browse files
mikolaj.kocotmikolaj.kocot
authored andcommitted
pause until an element becomes visible
1 parent 6466459 commit b4a2660

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

enjoyhint.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
var SHAPE_BACKGROUND_COLOR = _options.backgroundColor || "rgba(0,0,0,0.6)";
2424

2525
var body = "body"; // TODO: Is it possible case when we need to define enjoyhint somewhere else?
26+
var elementAvailableEventName = "enjoyhint-element-available";
2627

2728
var defaults = {
2829
onStart: function() {},
@@ -97,7 +98,7 @@
9798
$body.enjoyhint("hide_skip");
9899
};
99100

100-
var stepAction = function() {
101+
var stepAction = function(unpause) {
101102
if (!(data && data[current_step])) {
102103
$body.enjoyhint("hide");
103104
options.onEnd();
@@ -113,9 +114,63 @@
113114
$enjoyhint.removeClass("enjoyhint-step-" + (current_step + 1));
114115
$enjoyhint.removeClass("enjoyhint-step-" + (current_step + 2));
115116
$enjoyhint.addClass("enjoyhint-step-" + (current_step + 1));
116-
117+
117118
var step_data = data[current_step];
118119

120+
$body.off(elementAvailableEventName);
121+
122+
//loops waiting until specified element becomes visible
123+
var waitUntilAvailable = function (selector, interval) {
124+
if (interval == null)
125+
interval = 150;
126+
127+
var triggerIfAvailable = function () {
128+
if ($(selector).is(":visible")) {
129+
$body.trigger(elementAvailableEventName);
130+
}
131+
else {
132+
setTimeout(triggerIfAvailable, interval)
133+
}
134+
};
135+
136+
setTimeout(triggerIfAvailable, 0);
137+
}
138+
139+
//if pausedUntil was specified, hide current overlay and wait until specified event occurs
140+
if (!unpause && step_data.pausedUntil != null && step_data.pausedUntil.event != null) {
141+
//hide current overlay during waiting time
142+
$body.enjoyhint("hide");
143+
144+
//if 'available' event was chosen wait for the custom event, which is triggered when the element becomes visible
145+
if (step_data.pausedUntil.event === 'available') {
146+
$body.on(elementAvailableEventName, function () {
147+
stepAction(true); //restart the step without pause
148+
$body.off(elementAvailableEventName);
149+
});
150+
151+
//check if element is available every 150ms
152+
waitUntilAvailable(step_data.pausedUntil.selector);
153+
}
154+
else {
155+
//delay the actual action until 'the event' happens on body or selector
156+
if (step_data.pausedUntil.selector == null) {
157+
on(step_data.pausedUntil.event, function () {
158+
stepAction(true); //restart the step without pause
159+
off(step_data.pausedUntil.event);
160+
});
161+
}
162+
else {
163+
$(step_data.pausedUntil.selector).on(step_data.pausedUntil.event, function () {
164+
stepAction(true); //restart the step without pause
165+
$(step_data.pausedUntil.selector).off(step_data.pausedUntil.event)
166+
});
167+
}
168+
}
169+
170+
//the rest of the logic will be executed whenever the step is restarted
171+
return;
172+
}
173+
119174
var scrollSpeed = step_data.scrollAnimationSpeed;
120175

121176
if (

0 commit comments

Comments
 (0)