Skip to content

Commit ea0f2cb

Browse files
Adding a way to go to sections by id
#71
1 parent 5dd1b9c commit ea0f2cb

File tree

6 files changed

+68
-9
lines changed

6 files changed

+68
-9
lines changed

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ <h2>
357357
</svg>
358358
</a>
359359
</h2>
360-
<p>People share content that makes them feel inspired. WebSlides is a very effective way to engage young audiences, customers, and teams.</p>
360+
<p>People share content that makes them feel inspired. WebSlides is a very effective way to engage young audiences, customers, and teams.</p>
361361
<p>Best,<br> <a href="https://twitter.com/jlantunez">@jlantunez</a>, <a href="https://twitter.com/belelros">@belelros</a>, and <a href="https://twitter.com/luissacristan">@luissacristan</a>.</p>
362362
</div>
363363
<!-- .end .content-right -->

src/js/modules/slide.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Slide {
3737
*/
3838
this.i = i;
3939

40-
this.el.id = `section-${(i + 1)}`;
40+
this.el.id = this.el.id ? this.el.id : `section-${(i + 1)}`;
4141
this.el.classList.add(CLASSES.SLIDE);
4242

4343
// Hide slides by default

src/js/plugins/navigation.js

+26
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ export default class Navigation {
6060
this.el.appendChild(this.counter);
6161

6262
this.ws_.el.appendChild(this.el);
63+
this.slides = Array.prototype.slice.call(
64+
document.querySelectorAll('#webslides section')).map(s => s.id);
65+
6366
this.bindEvents_();
6467
}
6568

@@ -73,6 +76,29 @@ export default class Navigation {
7376
this.next.addEventListener('click', this.onButtonClicked_.bind(this));
7477
this.prev.addEventListener('click', this.onButtonClicked_.bind(this));
7578
this.counter.addEventListener('click', this.onButtonClicked_.bind(this));
79+
document.body.addEventListener('click', this.onBodyClicked_.bind(this));
80+
}
81+
82+
/**
83+
* Whenever the body is clicked, check if the element has [data-slide] attr
84+
* and if so, navigate to it.
85+
* @param {MouseEvent} event Click event
86+
*/
87+
onBodyClicked_(event) {
88+
const matches = document.body.matches || document.body.msMatchesSelector;
89+
let el;
90+
91+
if (matches.call(event.target, '[data-slide]')) {
92+
el = event.target;
93+
} else if (matches.call(event.target, '[data-slide] *')) {
94+
el = event.target.querySelector('[data-slide]');
95+
}
96+
97+
if (el) {
98+
event.preventDefault();
99+
const i = this.slides.indexOf(el.dataset.slide);
100+
this.ws_.goToSlide(i);
101+
}
76102
}
77103

78104
/**

static/css/webslides.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* Name: WebSlides
33
* Version: 1.5.0
4-
* Date: 2017-09-16
4+
* Date: 2018-01-01
55
* Description: Making HTML presentations easy
66
* URL: https://github.com/webslides/webslides#readme
77
* Credits: @jlantunez, @LuisSacristan, @Belelros

static/js/webslides.js

+37-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* Name: WebSlides
33
* Version: 1.5.0
4-
* Date: 2017-09-16
4+
* Date: 2018-01-01
55
* Description: Making HTML presentations easy
66
* URL: https://github.com/webslides/webslides#readme
77
* Credits: @jlantunez, @LuisSacristan, @Belelros
@@ -263,7 +263,8 @@ var DOM = function () {
263263
var eventInfo = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
264264

265265
var event = new __WEBPACK_IMPORTED_MODULE_0__custom_event__["a" /* default */](eventType, {
266-
detail: eventInfo
266+
detail: eventInfo,
267+
bubbles: true
267268
});
268269

269270
target.dispatchEvent(event);
@@ -377,8 +378,9 @@ var CLASSES = {
377378
var Events = {
378379
ENTER: 'dom:enter',
379380
LEAVE: 'dom:leave',
381+
DISABLE: 'slide:disable',
380382
ENABLE: 'slide:enable',
381-
DISABLE: 'slide:disable'
383+
SHOW: 'slide:show'
382384
};
383385

384386
/**
@@ -408,7 +410,7 @@ var Slide = function () {
408410
*/
409411
this.i = i;
410412

411-
this.el.id = 'section-' + (i + 1);
413+
this.el.id = this.el.id ? this.el.id : 'section-' + (i + 1);
412414
this.el.classList.add(CLASSES.SLIDE);
413415

414416
// Hide slides by default
@@ -436,6 +438,7 @@ var Slide = function () {
436438
value: function show() {
437439
__WEBPACK_IMPORTED_MODULE_0__utils_dom__["a" /* default */].show(this.el);
438440
this.el.classList.add(CLASSES.CURRENT);
441+
this.fire_(Events.SHOW);
439442
}
440443

441444
/**
@@ -1906,6 +1909,10 @@ var Navigation = function () {
19061909
this.el.appendChild(this.counter);
19071910

19081911
this.ws_.el.appendChild(this.el);
1912+
this.slides = Array.prototype.slice.call(document.querySelectorAll('#webslides section')).map(function (s) {
1913+
return s.id;
1914+
});
1915+
19091916
this.bindEvents_();
19101917
}
19111918

@@ -1922,6 +1929,32 @@ var Navigation = function () {
19221929
this.next.addEventListener('click', this.onButtonClicked_.bind(this));
19231930
this.prev.addEventListener('click', this.onButtonClicked_.bind(this));
19241931
this.counter.addEventListener('click', this.onButtonClicked_.bind(this));
1932+
document.body.addEventListener('click', this.onBodyClicked_.bind(this));
1933+
}
1934+
1935+
/**
1936+
* Whenever the body is clicked, check if the element has [data-slide] attr
1937+
* and if so, navigate to it.
1938+
* @param {MouseEvent} event Click event
1939+
*/
1940+
1941+
}, {
1942+
key: 'onBodyClicked_',
1943+
value: function onBodyClicked_(event) {
1944+
var matches = document.body.matches || document.body.msMatchesSelector;
1945+
var el = void 0;
1946+
1947+
if (matches.call(event.target, '[data-slide]')) {
1948+
el = event.target;
1949+
} else if (matches.call(event.target, '[data-slide] *')) {
1950+
el = event.target.querySelector('[data-slide]');
1951+
}
1952+
1953+
if (el) {
1954+
event.preventDefault();
1955+
var i = this.slides.indexOf(el.dataset.slide);
1956+
this.ws_.goToSlide(i);
1957+
}
19251958
}
19261959

19271960
/**

static/js/webslides.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)