Skip to content

Commit ccc6bae

Browse files
committed
add option to control how history is updated
* Use update option to only process inbound request from URL (no updates on navigation) * Use history option to control whether history is maintained
1 parent ab0222a commit ccc6bae

File tree

5 files changed

+123
-17
lines changed

5 files changed

+123
-17
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,32 @@ If you'd like to use named hash routes instead, add `data-bespoke-hash` attribut
5050
</article>
5151
```
5252

53+
### Disabling updates
54+
55+
You can prevent the URL from being updated after the initial page load by setting the `update` option to `false`. This switch allows you to respond to incoming changes from the URL, but not continue updating the URL when the slide changes.
56+
57+
```
58+
bespoke.from('article', [
59+
bespoke.plugins.hash({ update: false })
60+
]);
61+
```
62+
63+
Default value: `true`
64+
65+
### History control
66+
67+
You can control whether this browser history is updated using the `history` option. A value of `false` overwrites the last history entry each time the slide is changed (assuming the `update` option is not `false`).
68+
69+
```
70+
bespoke.from('article', [
71+
bespoke.plugins.hash({ history: false })
72+
]);
73+
```
74+
75+
If updates are disabled and the value of the `history` option is false, the hash will be removed from the URL after the deck is loaded.
76+
77+
Default value: `true`
78+
5379
## Package managers
5480

5581
### npm

dist/bespoke-hash.js

100755100644
Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
/*!
22
* bespoke-hash v1.0.2
33
*
4-
* Copyright 2014, Mark Dalgleish
4+
* Copyright 2015, Mark Dalgleish
55
* This content is released under the MIT license
66
* http://mit-license.org/markdalgleish
77
*/
88

99
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var n;"undefined"!=typeof window?n=window:"undefined"!=typeof global?n=global:"undefined"!=typeof self&&(n=self);var o=n;o=o.bespoke||(o.bespoke={}),o=o.plugins||(o.plugins={}),o.hash=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
10-
module.exports = function() {
10+
module.exports = function(opts) {
11+
opts = opts || {};
1112
return function(deck) {
13+
var loc = window.location;
1214
var parseHash = function() {
13-
var hash = window.location.hash.slice(1),
15+
var hash = loc.hash.slice(1),
1416
slideNumberOrName = parseInt(hash, 10);
1517

1618
if (hash) {
@@ -36,10 +38,22 @@ module.exports = function() {
3638
setTimeout(function() {
3739
parseHash();
3840

39-
deck.on('activate', function(e) {
40-
var slideName = e.slide.getAttribute('data-bespoke-hash');
41-
window.location.hash = slideName || e.index + 1;
42-
});
41+
if (opts.update === false) {
42+
if (opts.history === false) {
43+
window.history.replaceState(null, null, loc.pathname + loc.search);
44+
}
45+
}
46+
else {
47+
deck.on('activate', function(e) {
48+
var slideName = e.slide.getAttribute('data-bespoke-hash');
49+
if (opts.history === false) {
50+
window.history.replaceState(null, null, loc.pathname + loc.search + '#' + (slideName || e.index + 1));
51+
}
52+
else {
53+
loc.hash = slideName || e.index + 1;
54+
}
55+
});
56+
}
4357

4458
window.addEventListener('hashchange', parseHash);
4559
}, 0);

dist/bespoke-hash.min.js

100755100644
Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/bespoke-hash.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
module.exports = function() {
1+
module.exports = function(opts) {
2+
opts = opts || {};
23
return function(deck) {
4+
var loc = window.location;
35
var parseHash = function() {
4-
var hash = window.location.hash.slice(1),
6+
var hash = loc.hash.slice(1),
57
slideNumberOrName = parseInt(hash, 10);
68

79
if (hash) {
@@ -27,10 +29,22 @@ module.exports = function() {
2729
setTimeout(function() {
2830
parseHash();
2931

30-
deck.on('activate', function(e) {
31-
var slideName = e.slide.getAttribute('data-bespoke-hash');
32-
window.location.hash = slideName || e.index + 1;
33-
});
32+
if (opts.update === false) {
33+
if (opts.history === false) {
34+
window.history.replaceState(null, null, loc.pathname + loc.search);
35+
}
36+
}
37+
else {
38+
deck.on('activate', function(e) {
39+
var slideName = e.slide.getAttribute('data-bespoke-hash');
40+
if (opts.history === false) {
41+
window.history.replaceState(null, null, loc.pathname + loc.search + '#' + (slideName || e.index + 1));
42+
}
43+
else {
44+
loc.hash = slideName || e.index + 1;
45+
}
46+
});
47+
}
3448

3549
window.addEventListener('hashchange', parseHash);
3650
}, 0);

test/spec/bespoke-hashSpec.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe("bespoke-hash", function() {
1616
slides,
1717
deck;
1818

19-
var createDeck = function() {
19+
var createDeck = function(opts) {
2020
slides = [];
2121

2222
article = document.createElement(PARENT_TAG);
@@ -34,7 +34,7 @@ describe("bespoke-hash", function() {
3434
document.body.appendChild(article);
3535

3636
deck = bespoke.from(PARENT_TAG, [
37-
hash()
37+
hash(opts)
3838
]);
3939

4040
// Wait for next tick
@@ -224,4 +224,56 @@ describe("bespoke-hash", function() {
224224

225225
});
226226

227+
describe("given update mode is deactivated", function() {
228+
beforeEach(function() {
229+
window.location.hash = 2;
230+
});
231+
232+
[true, false].forEach(function(historyOpt) {
233+
describe("when the deck is created with history " + (historyOpt ? 'enabled' : 'disabled'), function() {
234+
beforeEach(function() {
235+
createDeck({ update: false, history: historyOpt });
236+
});
237+
afterEach(destroyDeck);
238+
239+
it("should activate the slide referenced in the hash", function() {
240+
expect(deck.slide()).toBe(1);
241+
expect(window.location.hash).toBe(historyOpt ? '#2' : '');
242+
});
243+
244+
it("should not update the hash when the slide changes", function() {
245+
deck.next();
246+
expect(deck.slide()).toBe(2);
247+
expect(window.location.hash).toBe(historyOpt ? '#2' : '');
248+
});
249+
});
250+
});
251+
});
252+
253+
describe("given history mode is deactivated", function() {
254+
beforeEach(function() {
255+
window.location.hash = 2;
256+
});
257+
258+
describe("when the deck is created", function() {
259+
beforeEach(function() {
260+
createDeck({ history: false });
261+
});
262+
afterEach(destroyDeck);
263+
264+
it("should activate the slide referenced in the hash", function() {
265+
expect(deck.slide()).toBe(1);
266+
expect(window.location.hash).toBe('#2');
267+
});
268+
269+
it("should not add entry to history when the slide changes", function() {
270+
var beforeHistoryLength = window.history.length;
271+
deck.next();
272+
expect(deck.slide()).toBe(2);
273+
expect(window.location.hash).toBe('#3');
274+
expect(window.history.length).toBe(beforeHistoryLength);
275+
});
276+
});
277+
});
278+
227279
});

0 commit comments

Comments
 (0)