aboutsummaryrefslogtreecommitdiff
path: root/point/control/slide.js
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2014-08-10 17:28:37 +0200
committerPacien TRAN-GIRARD2014-08-10 17:28:37 +0200
commite7bf5952d0729b37e677168b6e8fbd1ce58ed1a2 (patch)
tree189988e3e272b806262d1df6b87f1da089ef4af8 /point/control/slide.js
parenta32e898c8d7ad3774f5654e88bb24d5c26482137 (diff)
downloadwhatsthepoint-e7bf5952d0729b37e677168b6e8fbd1ce58ed1a2.tar.gz
First versionHEADmaster
Diffstat (limited to 'point/control/slide.js')
-rw-r--r--point/control/slide.js86
1 files changed, 86 insertions, 0 deletions
diff --git a/point/control/slide.js b/point/control/slide.js
new file mode 100644
index 0000000..c539a60
--- /dev/null
+++ b/point/control/slide.js
@@ -0,0 +1,86 @@
1/*
2 * This file is part of "What's The Point" <https://github.com/Pacien/WhatsThePoint>
3 * Copyright (C) 2014 Pacien TRAN-GIRARD
4 *
5 * "What's The Point" is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * "What's The Point" is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19define(["control/control"], function (control) {
20
21 var slide = {
22
23 init: function () {
24 this.slideCount = document.getElementsByTagName("p-slide").length;
25 this.start();
26 this.bindEvent();
27 },
28
29 start: function () {
30 var slideIndex = location.hash.substr(1);
31 location.hash = 0;
32 location.hash = 1;
33 this.setCurrentSlide(slideIndex);
34 },
35
36 setCurrentSlide: function (slideIndex) {
37 if (isNaN(slideIndex) || slideIndex < 1 || slideIndex > this.slideCount) {
38 return false;
39 }
40 location.hash = slideIndex;
41 return true;
42 },
43
44 getCurrentSlideIndex: function () {
45 var hash = location.hash.substring(1);
46 return isNaN(hash) || hash < 1 || hash > this.slideCount ? 0 : Number(hash);
47 },
48
49 shift: function (delta) {
50 return this.getCurrentSlideIndex() + delta;
51 },
52
53 getIndex: function (target) {
54 switch (target) {
55 case control.GOTO.PREVIOUS_SLIDE:
56 return this.shift(-1);
57
58 case control.GOTO.NEXT_SLIDE:
59 return this.shift(+1);
60
61 case control.GOTO.FIRST_SLIDE:
62 return 1;
63
64 case control.GOTO.LAST_SLIDE:
65 return this.slideCount;
66 }
67 },
68
69 translate: function (target) {
70 control.dispatchEvent(control.EVENT.SLIDE, this.getIndex(target));
71 },
72
73 bindEvent: function () {
74 control.bindEvent(control.EVENT.GOTO, function (gotoEvent) {
75 slide.translate(gotoEvent.detail);
76 });
77
78 control.bindEvent(control.EVENT.SLIDE, function (slideEvent) {
79 slide.setCurrentSlide(slideEvent.detail);
80 });
81 },
82 };
83
84 return slide;
85
86});