Skip to content
This repository was archived by the owner on Feb 17, 2021. It is now read-only.

Commit 41ced7b

Browse files
author
Timothy Lindvall
committed
Support multiple targets for a step.
1 parent f2581a6 commit 41ced7b

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

js/hopscotch-0.0.5.js

+30-10
Original file line numberDiff line numberDiff line change
@@ -323,34 +323,54 @@
323323
* @private
324324
*/
325325
getStepTarget: function(step) {
326-
var result;
326+
var result,
327+
queriedTarged;
327328

328-
if (!step || !step.target) { return null; }
329-
if (typeof step.target === 'string') {
329+
function runTargetTest(toTest){
330+
alert("Checking for: " + toTest);
330331
// Check if it's querySelector-eligible. Only accepting IDs and classes,
331332
// because that's the only thing that makes sense. Tag name and pseudo-class
332333
// are just silly.
333-
if (/^[#\.]/.test(step.target)) {
334+
if (/^[#\.]/.test(toTest)) {
334335
if (document.querySelector) {
335-
return document.querySelector(step.target);
336+
return document.querySelector(toTest);
336337
}
337338
if (hasJquery) {
338-
result = jQuery(step.target);
339+
result = jQuery(toTest);
339340
return result.length ? result[0] : null;
340341
}
341342
if (Sizzle) {
342-
result = new Sizzle(step.target);
343+
result = new Sizzle(toTest);
343344
return result.length ? result[0] : null;
344345
}
345346
if (step.target[0] === '#' && step.target.indexOf(' ') === -1) {
346-
return document.getElementById(step.target.substring(1));
347+
return document.getElementById(toTest.substring(1));
347348
}
348349
// Can't extract element. Likely IE <=7 and no jQuery/Sizzle.
349350
return null;
350351
}
351352
// Else assume it's a string id.
352-
return document.getElementById(step.target);
353+
return document.getElementById(toTest);
354+
}
355+
356+
if (!step || !step.target) { return null; }
357+
358+
if (typeof step.target === 'string') {
359+
//Just one target to test. Check, cache, and return its results.
360+
return step.target = runTargetTest(step.target);
361+
}
362+
else if (Array.isArray(step.target)) {
363+
//Multiple items to check. Check each and break on first success.
364+
var arrSize = step.target.length,
365+
i;
366+
for (i = 0; i < arrSize; i++){
367+
queriedTarget = runTargetTest(step.target[i]);
368+
if (queriedTarget !== null){ break; }
369+
}
370+
//Cache and return.
371+
return step.target = queriedTarget;
353372
}
373+
//Hey, our result's already been cached. Sweet!
354374
return step.target;
355375
},
356376

@@ -2081,4 +2101,4 @@
20812101

20822102
winHopscotch = new Hopscotch();
20832103
context[namespace] = winHopscotch;
2084-
}(window, 'hopscotch'));
2104+
}(window, 'hopscotch'));

0 commit comments

Comments
 (0)