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

Enable regExp for step host option #430

Open
wants to merge 8 commits into
base: master
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
35 changes: 8 additions & 27 deletions build/js/bootstrap-tour-standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@
promise = this._makePromise(step.onShow != null ? step.onShow(this, i) : void 0);
showStepHelper = (function(_this) {
return function(e) {
var current_path, path, showPopoverAndOverlay;
var path, showPopoverAndOverlay;
_this.setCurrentStep(i);
path = (function() {
switch ({}.toString.call(step.path)) {
Expand All @@ -877,10 +877,9 @@
return step.path;
}
}).call(_this);
current_path = [document.location.pathname, document.location.search, document.location.hash].join('');
if (_this._isRedirect(step.host, path, current_path)) {
if (_this._isRedirect(step.host, path, document.location)) {
_this._redirect(step, i, path);
if (!_this._isHostDifferent(step.host, document.location.href) && !_this._isJustPathHashDifferent(path, current_path)) {
if (!_this._isHostDifferent(step.host, document.location.href) && !_this._isJustPathHashDifferent(path, document.location.href)) {
return;
}
}
Expand Down Expand Up @@ -1031,12 +1030,15 @@
}
};

Tour.prototype._isRedirect = function(host, path, currentPath) {
Tour.prototype._isRedirect = function(host, path, location) {
var currentHostURL, currentPath;
if (host !== '') {
if (this._isHostDifferent(host, document.location.href)) {
currentHostURL = "" + location.protocol + "//" + location.host;
if (({}.toString.call(host) === '[object RegExp]' && !host.test(currentHostURL)) || ({}.toString.call(host) === '[object String]' && this._isHostDifferent(host, location.href))) {
return true;
}
}
currentPath = [location.pathname, location.search, location.hash].join('');
return (path != null) && path !== '' && (({}.toString.call(path) === '[object RegExp]' && !path.test(currentPath)) || ({}.toString.call(path) === '[object String]' && this._isPathDifferent(path, currentPath)));
};

Expand All @@ -1055,27 +1057,6 @@
return false;
};

Tour.prototype._pathHashDifferent = function(host, path, currentPath) {
var currentPathArr, current_host, diff, pathArr;
if (host !== '') {
current_host = document.location.href.substr(0, document.location.href.lastIndexOf(document.location.pathname));
if (host !== current_host) {
return false;
}
}
diff = false;
if ({}.toString.call(path) === '[object String]') {
pathArr = path.split('#');
currentPathArr = currentPath.split('#');
if (path.indexOf('#') === 0) {
diff = pathArr[1] !== currentPathArr[1];
} else {
diff = pathArr[0].replace(/\?.*$/, '').replace(/\/?$/, '') === currentPathArr[0].replace(/\/?$/, '') && pathArr[1] !== currentPathArr[1];
}
}
return diff;
};

Tour.prototype._redirect = function(step, i, path) {
if ($.isFunction(step.redirect)) {
return step.redirect.call(this, path);
Expand Down
2 changes: 1 addition & 1 deletion build/js/bootstrap-tour-standalone.min.js

Large diffs are not rendered by default.

26 changes: 3 additions & 23 deletions build/js/bootstrap-tour.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,10 @@
};

Tour.prototype._isRedirect = function(host, path, location) {
var currentPath;
var currentHostURL, currentPath;
if (host !== '') {
if (this._isHostDifferent(host, location.href)) {
currentHostURL = "" + location.protocol + "//" + location.host;
if (({}.toString.call(host) === '[object RegExp]' && !host.test(currentHostURL)) || ({}.toString.call(host) === '[object String]' && this._isHostDifferent(host, location.href))) {
return true;
}
}
Expand All @@ -484,27 +485,6 @@
return false;
};

Tour.prototype._pathHashDifferent = function(host, path, currentPath) {
var currentPathArr, current_host, diff, pathArr;
if (host !== '') {
current_host = document.location.href.substr(0, document.location.href.lastIndexOf(document.location.pathname));
if (host !== current_host) {
return false;
}
}
diff = false;
if ({}.toString.call(path) === '[object String]') {
pathArr = path.split('#');
currentPathArr = currentPath.split('#');
if (path.indexOf('#') === 0) {
diff = pathArr[1] !== currentPathArr[1];
} else {
diff = pathArr[0].replace(/\?.*$/, '').replace(/\/?$/, '') === currentPathArr[0].replace(/\/?$/, '') && pathArr[1] !== currentPathArr[1];
}
}
return diff;
};

Tour.prototype._redirect = function(step, i, path) {
if ($.isFunction(step.redirect)) {
return step.redirect.call(this, path);
Expand Down
2 changes: 1 addition & 1 deletion build/js/bootstrap-tour.min.js

Large diffs are not rendered by default.

33 changes: 10 additions & 23 deletions src/coffee/bootstrap-tour.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,12 @@
# Check if step path equals current document path
_isRedirect: (host, path, location) ->
if host isnt ''
return true if @_isHostDifferent(host, location.href)
currentHostURL = "#{location.protocol}//#{location.host}"

return true if (
(({}).toString.call(host) is '[object RegExp]' and not host.test(currentHostURL)) or
(({}).toString.call(host) is '[object String]' and @_isHostDifferent(host, location.href))
)

currentPath = [
location.pathname,
Expand All @@ -395,15 +400,19 @@
(({}).toString.call(path) is '[object String]' and @_isPathDifferent(path, currentPath))
)

# Check if it the step host and current host are differents
_isHostDifferent: (host, currentURL) ->
@_getProtocol(host) isnt @_getProtocol(currentURL) or
@_getHost(host) isnt @_getHost(currentURL)

# Check if step path and current path with queries and hash are differents
_isPathDifferent: (path, currentPath) ->
@_getPath(path) isnt @_getPath(currentPath) or not
@_equal(@_getQuery(path), @_getQuery(currentPath)) or not
@_equal(@_getHash(path), @_getHash(currentPath))

# Check if it is just the hash part of the step path
# and current path that is different
_isJustPathHashDifferent: (path, currentPath) ->
if ({}).toString.call(path) is '[object String]'
return @_getPath(path) is @_getPath(currentPath) and
Expand All @@ -412,28 +421,6 @@

false

# Check if it is just the hash part of the step path
# and current path that is different
_pathHashDifferent: (host, path, currentPath) ->
if host isnt ''
current_host = document.location.href.substr(0, document.location.href.lastIndexOf(document.location.pathname))
return false if host isnt current_host

diff = false
if ({}).toString.call(path) is '[object String]'
pathArr = path.split('#')
currentPathArr = currentPath.split('#')

if path.indexOf('#') is 0
# Compare just hash part when the step path is an anchor
diff = pathArr[1] isnt currentPathArr[1]
else
# Compare both pathname and hash parts
diff = pathArr[0].replace(/\?.*$/, '').replace(/\/?$/, '') is currentPathArr[0].replace(/\/?$/, '') and
pathArr[1] isnt currentPathArr[1]

diff

# Execute the redirect
_redirect: (step, i, path) ->
if $.isFunction step.redirect
Expand Down
70 changes: 67 additions & 3 deletions src/coffee/bootstrap-tour.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -737,24 +737,88 @@ describe 'Bootstrap Tour', ->
@tour.addStep
element: $('<div></div>').appendTo('body')
path: 'test.html'
host: 'http://sub.exemple.com'
host: 'http://sub.example.com'

@tour.addStep
element: $('<div></div>').appendTo('body')
path: 'test.html'
host: /example\.com$/

expect(
@tour._isRedirect(
@tour.getStep(0).host,
@tour._options.basePath + @tour.getStep(0).path,
href: 'http://exemple.com/test.html' , pathname: 'test.html', search: '', hash: ''
{
href: 'http://example.com/test.html',
pathname: 'test.html',
search: '',
hash: '',
host: 'example.com',
protocol: 'http'
}
)
).toBe true

expect(
@tour._isRedirect(
current_host,
@tour._options.basePath + @tour.getStep(0).path,
href: "http://#{current_host}/test.html" , pathname: 'test.html', search: '', hash: ''
{
href: "http://#{current_host}/test.html",
pathname: 'test.html',
search: '',
hash: '',
host: current_host,
protocol: 'http'
}
)
).toBe false

expect(
@tour._isRedirect(
@tour.getStep(1).host,
@tour._options.basePath + @tour.getStep(1).path,
{
href: 'http://sub.example.com/test.html',
pathname: 'test.html',
search: '',
hash: '',
host: 'sub.example.com',
protocol: 'http'
}
)
).toBe false

expect(
@tour._isRedirect(
@tour.getStep(1).host,
@tour._options.basePath + @tour.getStep(1).path,
{
href: 'http://example.com/test.html',
pathname: 'test.html',
search: '',
hash: '',
host: 'example.com',
protocol: 'http'
}
)
).toBe false

expect(
@tour._isRedirect(
@tour.getStep(1).host,
@tour._options.basePath + @tour.getStep(1).path,
{
href: 'http://anotherexample.com/test.html',
pathname: 'test.html',
search: '',
hash: '',
host: 'another.com',
protocol: 'http'
}
)
).toBe true

it 'with `onNext` option should run the callback before showing the next step', ->
tour_test = 0
@tour = new Tour
Expand Down
6 changes: 3 additions & 3 deletions src/docs/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -368,17 +368,17 @@ <h3>Step Options</h3>
<tbody>
<tr>
<td>path</td>
<td>String or RegExp</td>
<td>String|RegExp</td>
<td>Path to the page on which the step should be shown. This
allows you to build tours that span several pages!</td>
<td><code>''</code></td>
</tr>
<tbody>
<tr class="success">
<td>host <span class="label label-success">NEW</span></td>
<td>String</td>
<td>String|RegExp</td>
<td>Host of the page on which the step should be shown. This
allows you to build tours for several sub-domains</td>
allows you to build tours for several domains</td>
<td><code>''</code></td>
</tr>
<tr>
Expand Down
26 changes: 3 additions & 23 deletions src/docs/assets/js/bootstrap-tour.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,10 @@
};

Tour.prototype._isRedirect = function(host, path, location) {
var currentPath;
var currentHostURL, currentPath;
if (host !== '') {
if (this._isHostDifferent(host, location.href)) {
currentHostURL = "" + location.protocol + "//" + location.host;
if (({}.toString.call(host) === '[object RegExp]' && !host.test(currentHostURL)) || ({}.toString.call(host) === '[object String]' && this._isHostDifferent(host, location.href))) {
return true;
}
}
Expand All @@ -484,27 +485,6 @@
return false;
};

Tour.prototype._pathHashDifferent = function(host, path, currentPath) {
var currentPathArr, current_host, diff, pathArr;
if (host !== '') {
current_host = document.location.href.substr(0, document.location.href.lastIndexOf(document.location.pathname));
if (host !== current_host) {
return false;
}
}
diff = false;
if ({}.toString.call(path) === '[object String]') {
pathArr = path.split('#');
currentPathArr = currentPath.split('#');
if (path.indexOf('#') === 0) {
diff = pathArr[1] !== currentPathArr[1];
} else {
diff = pathArr[0].replace(/\?.*$/, '').replace(/\/?$/, '') === currentPathArr[0].replace(/\/?$/, '') && pathArr[1] !== currentPathArr[1];
}
}
return diff;
};

Tour.prototype._redirect = function(step, i, path) {
if ($.isFunction(step.redirect)) {
return step.redirect.call(this, path);
Expand Down
26 changes: 3 additions & 23 deletions test/bootstrap-tour.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,10 @@
};

Tour.prototype._isRedirect = function(host, path, location) {
var currentPath;
var currentHostURL, currentPath;
if (host !== '') {
if (this._isHostDifferent(host, location.href)) {
currentHostURL = "" + location.protocol + "//" + location.host;
if (({}.toString.call(host) === '[object RegExp]' && !host.test(currentHostURL)) || ({}.toString.call(host) === '[object String]' && this._isHostDifferent(host, location.href))) {
return true;
}
}
Expand All @@ -484,27 +485,6 @@
return false;
};

Tour.prototype._pathHashDifferent = function(host, path, currentPath) {
var currentPathArr, current_host, diff, pathArr;
if (host !== '') {
current_host = document.location.href.substr(0, document.location.href.lastIndexOf(document.location.pathname));
if (host !== current_host) {
return false;
}
}
diff = false;
if ({}.toString.call(path) === '[object String]') {
pathArr = path.split('#');
currentPathArr = currentPath.split('#');
if (path.indexOf('#') === 0) {
diff = pathArr[1] !== currentPathArr[1];
} else {
diff = pathArr[0].replace(/\?.*$/, '').replace(/\/?$/, '') === currentPathArr[0].replace(/\/?$/, '') && pathArr[1] !== currentPathArr[1];
}
}
return diff;
};

Tour.prototype._redirect = function(step, i, path) {
if ($.isFunction(step.redirect)) {
return step.redirect.call(this, path);
Expand Down