Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made a handful of changes and a couple of bug fixes #116

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ guiders.createGuider({
The parameters for creating guiders are:

~~~
id: (optional) id string used as a key for the guider. If blank, an automatic id is assigned.
prev: (optional) id the previous guider. If left blank, this will point to the previously created guider.
next: (optional) id the next guider. If left blank, this will point to the next guider that is created.
attachTo: (optional) selector of the html element you want to attach the guider to
autoFocus: (optional) if you want the browser to scroll to the position of the guider, set this to true
buttons: array of button objects
Expand All @@ -59,6 +62,7 @@ closeOnEscape: (optional) if true, the escape key will close the currently open
description: text description that shows up inside the guider
highlight: (optional) selector of the html element you want to highlight (will cause element to be above the overlay)
isHashable: (defaults to true) the guider will be shown auto-shown when a page is loaded with a url hash parameter #guider=guider_name
maxWidth: (optional) maximum width of the guider (can be a string ending with 'px' or a number of pixels)
offset: fine tune the position of the guider, e.g. { left:0, top: -10 }
onClose: (optional) additional function to call if a guider is closed by the x button, close button, or escape key
onHide: (optional) additional function to call when the guider is hidden
Expand Down
File renamed without changes.
68 changes: 40 additions & 28 deletions guiders.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ var guiders = (function($) {
guiders._currentGuiderID = null;
guiders._fixedOrAbsolute = "fixed";
guiders._guiders = {};
guiders._autoNumber = 1;
guiders._lastCreatedGuiderID = null;
guiders._nextButtonTitle = "Next";
guiders._offsetNameMapping = {
Expand Down Expand Up @@ -168,7 +169,7 @@ var guiders = (function($) {
};

guiders._attach = function(myGuider) {
if (typeof myGuider !== 'object') {
if (myGuider === null || typeof myGuider !== 'object') {
return;
}

Expand Down Expand Up @@ -257,18 +258,19 @@ var guiders = (function($) {
return myGuider;
};

guiders._dehighlightElement = function(selector) {
$(selector).removeClass('guiders_highlight');
};

guiders._hideOverlay = function() {
$("#guiders_overlay").fadeOut("fast");
};

guiders._highlightElement = function(selector) {
guiders._removeHighlight();
$(selector).addClass('guiders_highlight');
};

guiders._removeHighlight = function () {
$(".guiders_highlight").removeClass('guiders_highlight');
};

guiders._initializeOverlay = function() {
if ($("#guiders_overlay").length === 0) {
$("<div id='guiders_overlay'></div>").hide().appendTo("body");
Expand Down Expand Up @@ -394,7 +396,7 @@ var guiders = (function($) {

// Extend those settings with passedSettings
myGuider = $.extend({}, guiders._defaultSettings, passedSettings);
myGuider.id = myGuider.id || "guider_random_" + String(Math.floor(Math.random() * 1000));
myGuider.id = myGuider.id || "guider_auto_" + String(guiders._autoNumber++);

var guiderElement = $("#" + myGuider.id);
if (!guiderElement.length) {
Expand Down Expand Up @@ -443,9 +445,17 @@ var guiders = (function($) {
guiders._initializeOverlay();

guiders._guiders[myGuider.id] = myGuider;

if (guiders._lastCreatedGuiderID != null) {
myGuider.prev = guiders._lastCreatedGuiderID;

var prevGuider = guiders.get(guiders._lastCreatedGuiderID);

if(prevGuider && !prevGuider.next) {
prevGuider.next = myGuider.id;
}
}

guiders._lastCreatedGuiderID = myGuider.id;

/**
Expand All @@ -468,11 +478,12 @@ var guiders = (function($) {
};

guiders.getCurrentGuider = function() {
return guiders._guiders[guiders._currentGuiderID] || null;
return guiders.get(guiders._currentGuiderID);
};

guiders.hideAll = function(omitHidingOverlay, next) {
next = next || false;
guiders._removeHighlight();

$(".guider:visible").each(function(index, elem){
var myGuider = guiders.get($(elem).attr('id'));
Expand All @@ -481,21 +492,19 @@ var guiders = (function($) {
}
});
$(".guider").fadeOut("fast");
var currentGuider = guiders._guiders[guiders._currentGuiderID];
if (currentGuider && currentGuider.highlight) {
guiders._dehighlightElement(currentGuider.highlight);
}
if (typeof omitHidingOverlay !== "undefined" && omitHidingOverlay === true) {
// do nothing for now
} else {
guiders._hideOverlay();
}

guiders._currentGuiderID = null; // Set back to initial state
return guiders;
};

guiders.next = function() {
var currentGuider = guiders._guiders[guiders._currentGuiderID];
if (typeof currentGuider === "undefined") {
var currentGuider = guiders.getCurrentGuider();
if (!currentGuider) {
return null;
}
currentGuider.elem.data("locked", true);
Expand All @@ -505,25 +514,24 @@ var guiders = (function($) {
var nextGuider = guiders.get(nextGuiderId);
var omitHidingOverlay = nextGuider.overlay ? true : false;
guiders.hideAll(omitHidingOverlay, true);
if (currentGuider && currentGuider.highlight) {
guiders._dehighlightElement(currentGuider.highlight);
}

if (nextGuider.shouldSkip && nextGuider.shouldSkip()) {
guiders._currentGuiderID = nextGuider.id;
guiders.next();
return guiders.getCurrentGuider();
return guiders.next();
}
else {
// Trigger before show to allow observers to change the
// DOM before the new guider calculates its position
$("body").trigger("guidersNext");
guiders.show(nextGuiderId);
return guiders.getCurrentGuider();
}
}
};

guiders.prev = function () {
var currentGuider = guiders._guiders[guiders._currentGuiderID];
if (typeof currentGuider === "undefined") {
var currentGuider = guiders.getCurrentGuider();
if (!currentGuider) {
// not what we think it is
return null;
}
Expand All @@ -532,7 +540,7 @@ var guiders = (function($) {
return null;
}

var prevGuider = guiders._guiders[currentGuider.prev];
var prevGuider = guiders.get(currentGuider.prev);
prevGuider.elem.data("locked", true);

// Note we use prevGuider.id as "prevGuider" is _already_ looking at the previous guider
Expand All @@ -541,22 +549,24 @@ var guiders = (function($) {
var myGuider = guiders.get(prevGuiderId);
var omitHidingOverlay = myGuider.overlay ? true : false;
guiders.hideAll(omitHidingOverlay, true);
if (prevGuider && prevGuider.highlight) {
guiders._dehighlightElement(prevGuider.highlight);
}

// Trigger before show to allow observers to change the
// DOM before the new guider calculates its position
$("body").trigger("guidersPrev");

guiders.show(prevGuiderId);
return myGuider;
}
};

guiders.reposition = function() {
var currentGuider = guiders._guiders[guiders._currentGuiderID];
var currentGuider = guiders.getCurrentGuider();
guiders._attach(currentGuider);
};

guiders.scrollToCurrent = function() {
var currentGuider = guiders._guiders[guiders._currentGuiderID];
if (typeof currentGuider === "undefined") {
var currentGuider = guiders.getCurrentGuider();
if (!currentGuider) {
return;
}

Expand All @@ -576,11 +586,13 @@ var guiders = (function($) {
}

var myGuider = guiders.get(id);
guiders._removeHighlight();

if (myGuider.overlay) {
guiders._showOverlay(myGuider);
// if guider is attached to an element, make sure it's visible
if (myGuider.highlight && myGuider.attachTo) {
guiders._highlightElement(myGuider.attachTo);
guiders._highlightElement(myGuider.highlight);
}
}

Expand Down