k4slide 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ /**
2
+ * k4slide.js
3
+ */
4
+ goog.provide('k4slide.App');
5
+
6
+ goog.require('k4.slide.Base');
7
+
8
+
9
+ /**
10
+ * @constructor
11
+ * @extends {k4.slide.Base}
12
+ * @param {goog.domDomHelper|null|undefined} opt_domHelper DOM helper
13
+ */
14
+ k4slide.App = function(opt_domHelper) {
15
+ goog.base(this, opt_domHelper);
16
+ }
17
+ goog.inherits(k4slide.App, k4.slide.Base);
18
+ goog.addSingletonGetter(k4slide.App);
19
+
20
+ k4slide.App.getInstance();
@@ -0,0 +1,120 @@
1
+ /**
2
+ * @fileoverview Handle DOMContentLoaded event.
3
+ * @author http://d.hatena.ne.jp/cheesepie
4
+ */
5
+ goog.provide('goog.events.ready');
6
+
7
+ goog.require('goog.events');
8
+
9
+
10
+ /**
11
+ * Handle DOMContentLoaded event.
12
+ * <pre>
13
+ * Example:
14
+ * goog.events.ready(function() {
15
+ * // some action...
16
+ * });
17
+ * goog.events.listen(document, 'ready', function() {
18
+ * // some action...
19
+ * }, false);
20
+ * </pre>
21
+ *
22
+ * @param {function} callback Event listener
23
+ * @return {function.<goog.events.ready>}
24
+ */
25
+ goog.events.ready = function(fn) {
26
+ // Attach the listeners
27
+ goog.events.readyObj.bindReady();
28
+
29
+ // If element is already ready
30
+ if (goog.events.readyObj.isReady) {
31
+ fn.call(document, goog);
32
+ } else {
33
+ goog.events.readyObj.readyList.push(fn);
34
+ }
35
+ return this;
36
+ };
37
+
38
+ /**
39
+ * The object related goog.events.ready.
40
+ */
41
+ goog.events.readyObj = {
42
+ /**
43
+ * @type {boolean}
44
+ */
45
+ isReady: false,
46
+ /**
47
+ * @type {Array}
48
+ */
49
+ readyList: [],
50
+ /**
51
+ * @type {boolean}
52
+ */
53
+ readyBound: false,
54
+
55
+ /**
56
+ * bind DOMContentLoaded event.
57
+ */
58
+ bindReady: function() {
59
+ var ro = goog.events.readyObj;
60
+ if (ro.readyBound) { return; }
61
+ ro.readyBound = true;
62
+
63
+ // Mozilla, Opera and webkit
64
+ if (document.addEventListener) {
65
+ document.addEventListener('DOMContentLoaded', function() {
66
+ document.removeEventListener('DOMContentLoaded', arguments.callee, false);
67
+ ro.ready();
68
+ }, false);
69
+
70
+ // If IE event model is used
71
+ } else if (document.attachEvent) {
72
+ document.attachEvent('onreadystatechange', function() {
73
+ if (document.readyState === "complete") {
74
+ document.detachEvent('onreadystatechange', arguments.callee);
75
+ ro.ready();
76
+ }
77
+ });
78
+
79
+ // If IE and not an iframe
80
+ if (document.documentElement.doScroll && window == window.top) (function() {
81
+ if (ro.isReady) { return; }
82
+
83
+ try {
84
+ // If IE us used, use the trick
85
+ // http://javascript.nwbox.com/IEContentLoaded
86
+ document.documentElement.doScroll("left");
87
+ } catch (error) {
88
+ setTimeout(arguments.callee, 0);
89
+ return;
90
+ }
91
+ // and execute any waiting functions
92
+ ro.ready();
93
+ })();
94
+ }
95
+
96
+ // A failback to window.onload, that will always work
97
+ goog.events.listenOnce(window, 'load', ro.ready, false);
98
+ },
99
+
100
+ /**
101
+ * Call functions in readyList
102
+ */
103
+ ready: function() {
104
+ var ro = goog.events.readyObj;
105
+ if (! ro.isReady) {
106
+ // Remember that the DOM is ready
107
+ ro.isReady = true;
108
+
109
+ if (ro.readyList) {
110
+ // Execute all of them
111
+ goog.array.forEach(ro.readyList, function(fn, idx) {
112
+ fn.call(document);
113
+ });
114
+ // Reset the list of functions
115
+ ro.readyList = null;
116
+ }
117
+ goog.events.fireListeners(document, 'ready', false);
118
+ }
119
+ }
120
+ };
@@ -0,0 +1,31 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Closure Unit Tests - goog.events.ready</title>
5
+ <script src="../../../../closure/goog/base.js"></script>
6
+ <script>
7
+ goog.require('goog.events');
8
+ goog.require('goog.events.ready');
9
+ goog.require('goog.testing.jsunit');
10
+ </script>
11
+ </head>
12
+ <body>
13
+ <script>
14
+ function tearDown() {
15
+ goog.events.removeAll();
16
+ }
17
+
18
+ function testOnDOMContentLoaded() {
19
+ goog.events.ready(function() {
20
+ assertTrue(goog.events.readyObj.readyBound);
21
+ });
22
+ }
23
+
24
+ function testIsReadyBeforeOnLoad() {
25
+ window.onload = function() {
26
+ assertTrue(goog.events.readyObj.isReady);
27
+ };
28
+ }
29
+ </script>
30
+ </body>
31
+ </html>
@@ -0,0 +1,101 @@
1
+ goog.provide('k4.Logger');
2
+ goog.provide('logger');
3
+
4
+
5
+ /**
6
+ * @constructor
7
+ */
8
+ k4.Logger = function() {
9
+ // constructor
10
+ };
11
+ goog.addSingletonGetter(k4.Logger);
12
+
13
+ /**
14
+ * @type {object}
15
+ */
16
+ k4.Logger.LEVELS = {
17
+ DEBUG: 0,
18
+ INFO: 1,
19
+ WARN: 7,
20
+ ERROR: 8,
21
+ CRITICAL: 9
22
+ };
23
+
24
+ /**
25
+ * Logging level
26
+ */
27
+ k4.Logger.LEVEL = k4.Logger.LEVELS.DEBUG;
28
+
29
+ /**
30
+ * @type {function|null|undefined}
31
+ */
32
+ k4.Logger.LOGGING_FUNCTION = null;
33
+
34
+
35
+ /**
36
+ * logging with console.log
37
+ * @param {number} level logging level
38
+ * @param {*} args logging arguments
39
+ */
40
+ k4.Logger.prototype.logging = function(level, args) {
41
+ if (level >= k4.Logger.LEVEL) {
42
+ var fn = (goog.isDefAndNotNull(k4.Logger.LOGGING_FUNCTION) && goog.isFunction(k4.Logger.LOGGING_FUNCTION))
43
+ ? k4.Logger.LOGGING_FUNCTION : ((this.enableConsole()) ? function(a){console.log(a);} : goog.nullFunction);
44
+ fn(args);
45
+ }
46
+ };
47
+
48
+ /**
49
+ * Arguments object convert to Array
50
+ */
51
+ k4.Logger.prototype.argumentsToArray = function(args) {
52
+ return Array.prototype.slice.call(args);
53
+ };
54
+
55
+ /**
56
+ * Check enabled console.log function.
57
+ */
58
+ k4.Logger.prototype.enableConsole = function() {
59
+ return (console && console.log && goog.isFunction(console.log));
60
+ };
61
+
62
+
63
+ /**
64
+ * logging DEBUG
65
+ */
66
+ k4.Logger.prototype.debug = function() {
67
+ this.logging(k4.Logger.LEVELS.DEBUG, this.argumentsToArray(arguments));
68
+ };
69
+
70
+ /**
71
+ * logging INFO
72
+ */
73
+ k4.Logger.prototype.info = function() {
74
+ this.logging(k4.Logger.LEVELS.INFO, this.argumentsToArray(arguments));
75
+ };
76
+
77
+ /**
78
+ * logging WARN
79
+ */
80
+ k4.Logger.prototype.warn = function() {
81
+ this.logging(k4.Logger.LEVELS.WARN, this.argumentsToArray(arguments));
82
+ };
83
+
84
+ /**
85
+ * logging ERROR
86
+ */
87
+ k4.Logger.prototype.error = function() {
88
+ this.logging(k4.Logger.LEVELS.ERROR, this.argumentsToArray(arguments));
89
+ };
90
+
91
+ /**
92
+ * logging CRITICAL
93
+ */
94
+ k4.Logger.prototype.critilcal = function() {
95
+ this.logging(k4.Logger.LEVELS.CRITICAL, this.argumentsToArray(arguments));
96
+ };
97
+
98
+
99
+
100
+ // logger
101
+ logger = k4.Logger.getInstance();
@@ -0,0 +1,89 @@
1
+ /**
2
+ * k4slide.js
3
+ */
4
+ goog.provide('k4.slide.Base');
5
+
6
+ goog.require('goog.dom');
7
+ goog.require('goog.dom.ViewportSizeMonitor');
8
+ goog.require('goog.events');
9
+ goog.require('goog.events.EventType');
10
+ goog.require('goog.events.KeyHandler');
11
+ goog.require('goog.events.KeyCodes');
12
+ goog.require('goog.events.ready');
13
+ goog.require('goog.ui.Component');
14
+ goog.require('logger');
15
+ goog.require('k4.slide.Config');
16
+ goog.require('k4.slide.Controller');
17
+
18
+
19
+ /**
20
+ * @constructor
21
+ * @extends {goog.ui.Component}
22
+ * @param {goog.domDomHelper|null|undefined} opt_domHelper DOM helper
23
+ */
24
+ k4.slide.Base = function(opt_domHelper) {
25
+ goog.base(this, opt_domHelper);
26
+
27
+ /**
28
+ * @type {k4slide.Config}
29
+ */
30
+ this.config_ = new k4.slide.Config();
31
+
32
+ /**
33
+ * @type {k4slide.SlideController}
34
+ */
35
+ this.controller_ = new k4.slide.Controller(this.config_);
36
+
37
+
38
+ // ready
39
+ goog.events.ready(goog.bind(this.loaded_, this));
40
+ }
41
+ goog.inherits(k4.slide.Base, goog.ui.Component);
42
+ goog.addSingletonGetter(k4.slide.Base);
43
+
44
+ /**
45
+ * Initialize Application
46
+ */
47
+ k4.slide.Base.prototype.loaded_ = function(event) {
48
+ this.initEvents_();
49
+ this.controller_.init();
50
+ this.controller_.start();
51
+ }
52
+
53
+ k4.slide.Base.prototype.initEvents_ = function() {
54
+ var keyHandler = new goog.events.KeyHandler(goog.dom.getDocument());
55
+ var codes = goog.events.KeyCodes;
56
+ var controller = this.controller_;
57
+ goog.events.listen(keyHandler, goog.events.KeyHandler.EventType.KEY, function(e) {
58
+ switch (e.keyCode) {
59
+ case codes.ENTER:
60
+ case codes.MAC_ENTER:
61
+ case codes.SPACE:
62
+ e.preventDefault();
63
+ e.stopPropagation();
64
+ controller.next();
65
+ break;
66
+ case codes.BACKSPACE:
67
+ e.preventDefault();
68
+ e.stopPropagation();
69
+ controller.back();
70
+ break;
71
+ default:
72
+ // do nothing
73
+ break;
74
+ }
75
+ }, false, this);
76
+
77
+ goog.events.listen(this.controller_.getBody(), goog.events.EventType.CLICK, function(e) {
78
+ e.preventDefault();
79
+ e.stopPropagation();
80
+ controller.next();
81
+ }, false, this);
82
+
83
+ var vsm = new goog.dom.ViewportSizeMonitor();
84
+ goog.events.listen(vsm, goog.events.EventType.RESIZE, function(e) {
85
+ // e.preventDefault();
86
+ // e.stopPropagation();
87
+ controller.resize();
88
+ }, false, this);
89
+ };
@@ -0,0 +1,75 @@
1
+ goog.provide('k4.slide.Config');
2
+
3
+ goog.require('goog.array');
4
+ goog.require('goog.object');
5
+
6
+
7
+ /**
8
+ * @constructor
9
+ * @param {object} opt_config Configuration
10
+ */
11
+ k4.slide.Config = function(opt_config) {
12
+ /**
13
+ * @type {object}
14
+ */
15
+ this.config_ = k4.slide.Config.DEFAULT_CONFIG;
16
+ }
17
+
18
+ /**
19
+ * default configuration
20
+ * @type {object}
21
+ */
22
+ k4.slide.Config.DEFAULT_CONFIG = {
23
+ 'window': {
24
+ 'margin': 10, // px
25
+ 'null': null
26
+ },
27
+ 'slide': {
28
+ 'pattern': 'div[role="slide"]',
29
+ 'width': 640,
30
+ 'height': 480,
31
+ 'ratio': 1.0,
32
+ 'class': {
33
+ 'current': 'current',
34
+ 'prev': 'prev',
35
+ 'next': 'next',
36
+ 'toPrev': 'to-prev',
37
+ 'toNext': 'to-next',
38
+ 'toCurrent': 'to-current'
39
+ },
40
+ 'null': null
41
+ }
42
+ };
43
+
44
+
45
+ /**
46
+ * @param {string} key Key
47
+ * @return {object|array|null|undefined}
48
+ */
49
+ k4.slide.Config.prototype.get= function(key) {
50
+ var tmp = this.config_;
51
+ var keys = key.split('.');
52
+ goog.array.forEach(keys, function(k, i, arr) {
53
+ tmp = (goog.isDefAndNotNull(tmp) && goog.isObject(tmp)) ? tmp[k] : undefined;
54
+ }, this);
55
+ return tmp;
56
+ };
57
+
58
+
59
+ /**
60
+ * @param {string} key Key
61
+ * @param {*} val The value
62
+ */
63
+ k4.slide.Config.prototype.set= function(key, val) {
64
+ var tmp = this.config_;
65
+ var keys = key.split('.');
66
+ var lastIndex = keys.length - 1;
67
+ goog.array.forEach(keys, function(k, i, arr) {
68
+ tmp[k] = (goog.isDefAndNotNull(tmp[k]) && goog.isObject(tmp[k])) ? tmp[k] : {};
69
+ if (i == lastIndex) {
70
+ tmp[k] = val;
71
+ } else {
72
+ tmp = (goog.isDefAndNotNull(tmp) && goog.isObject(tmp)) ? tmp[k] : undefined;
73
+ }
74
+ }, this);
75
+ };
@@ -0,0 +1,255 @@
1
+ goog.provide('k4.slide.Controller');
2
+
3
+ goog.require('goog.array');
4
+ goog.require('goog.dom');
5
+ goog.require('goog.dom.query');
6
+ goog.require('goog.style');
7
+ goog.require('goog.ui.Component');
8
+ goog.require('goog.History');
9
+ goog.require('k4.slide.Slide');
10
+ goog.require('k4.slide.Config');
11
+
12
+
13
+ /**
14
+ * @constructor
15
+ * @extends {goog.ui.Component}
16
+ */
17
+ k4.slide.Controller = function(config) {
18
+ goog.base(this);
19
+
20
+ /**
21
+ * @type {k4.Config}
22
+ */
23
+ this.config_ = config;
24
+
25
+ /**
26
+ * @type {array.<k4.slide.>}
27
+ */
28
+ this.slides_ = [];
29
+
30
+ /**
31
+ * current slide index
32
+ * @type {number}
33
+ */
34
+ this.current_ = 0;
35
+
36
+ /**
37
+ * @type {goog.history.Html5History}
38
+ */
39
+ this.history_ = null;
40
+ }
41
+ goog.inherits(k4.slide.Controller, goog.ui.Component);
42
+
43
+
44
+
45
+
46
+
47
+ /**
48
+ * initialize
49
+ */
50
+ k4.slide.Controller.prototype.init = function() {
51
+ this.initWindow_();
52
+ this.calculateSlideSizeAndPosition_();
53
+ this.initSlides_();
54
+ };
55
+
56
+
57
+ /**
58
+ * Initialize window
59
+ * TODO: to iPhone and iOS
60
+ */
61
+ k4.slide.Controller.prototype.initWindow_ = function() {
62
+ var viewportSize = goog.dom.getViewportSize();
63
+ var c = this.config_;
64
+ c.set('window.width', viewportSize.width);
65
+ c.set('window.height', viewportSize.height);
66
+ };
67
+
68
+ /**
69
+ * calculate slide width & height
70
+ */
71
+ k4.slide.Controller.prototype.calculateSlideSizeAndPosition_ = function() {
72
+ var c = this.config_;
73
+
74
+
75
+ // win size
76
+ var winWidth = c.get('window.width');
77
+ var winHeight = c.get('window.height');
78
+ var winMargin = c.get('window.margin');
79
+
80
+ // size
81
+ var isHorizonal = true;
82
+ var slideAspect = winHeight / winWidth;
83
+ var slideWidth = c.get('slide.width');
84
+ var slideHeight = c.get('slide.height');
85
+ var slideRatio = c.get('slide.ratio');
86
+ var aspectRatio = slideHeight / slideWidth;
87
+ var toWidth = 0;
88
+ var toHeight = 0;
89
+
90
+ // ratio
91
+ if (aspectRatio > slideAspect) {
92
+ toHeight = Math.floor(winHeight - (winMargin * 2));
93
+ slideRatio = toHeight / slideHeight;
94
+ } else {
95
+ toWidth = Math.floor(winWidth - (winMargin * 2));
96
+ slideRatio = toWidth / slideWidth;
97
+ isHorizonal = false;
98
+ }
99
+ c.set('slide.ratio', slideRatio);
100
+
101
+ // position
102
+ var slideTop = Math.round((winHeight - slideHeight) / 2);
103
+ var slideLeft = Math.round((winWidth - slideWidth) / 2);
104
+ c.set('slide.top', slideTop);
105
+ c.set('slide.left', slideLeft)
106
+
107
+ // set ratio
108
+ this.setSlideScale_();
109
+ };
110
+
111
+ k4.slide.Controller.prototype.setSlideScale_ = function() {
112
+ var c = this.config_;
113
+ var ratio = c.get('slide.ratio');
114
+
115
+ var installStyles = [
116
+ 'div[role="slide"]{',
117
+ '-webkit-transform: scale('+ ratio.toString() +');}',
118
+ '-moz-transform: scale('+ ratio.toString() +');',
119
+ '}'
120
+ ].join("");
121
+ goog.style.installStyles(installStyles);
122
+ };
123
+
124
+ /**
125
+ * Initialize slides
126
+ */
127
+ k4.slide.Controller.prototype.initSlides_ = function() {
128
+ var elements = this.findSlideElements_();
129
+ if (goog.isArrayLike(elements) && elements.length > 0) {
130
+ goog.array.forEach(elements, function(el, i, arr) {
131
+ var item = new k4.slide.Slide(el, this.config_);
132
+ this.slides_.push(item);
133
+ }, this);
134
+ }
135
+ };
136
+
137
+ /**
138
+ * Find slide elements
139
+ */
140
+ k4.slide.Controller.prototype.findSlideElements_ = function() {
141
+ var pattern = this.config_.get('slide.pattern');
142
+ var elements = goog.dom.query(pattern);
143
+ return elements;
144
+ };
145
+
146
+ /**
147
+ * return body;
148
+ */
149
+ k4.slide.Controller.prototype.getBody = function() {
150
+ var body = goog.dom.getDocument().body;
151
+ return body;
152
+ };
153
+
154
+ /**
155
+ * Get current slide object
156
+ * @return {k4.slide.|null|undefined}
157
+ */
158
+ k4.slide.Controller.prototype.getCurrent = function() {
159
+ if (goog.isArrayLike(this.slides_) && this.slides_.length > 0) {
160
+ return this.slides_[this.current_];
161
+ } else {
162
+ return null;
163
+ }
164
+ };
165
+
166
+ /**
167
+ * Get current slide object
168
+ * @return {k4.slide.|null|undefined}
169
+ */
170
+ k4.slide.Controller.prototype.getNext = function() {
171
+ if (goog.isArrayLike(this.slides_) && this.slides_.length > 0) {
172
+ return this.slides_[this.current_ + 1];
173
+ } else {
174
+ return null;
175
+ }
176
+ };
177
+
178
+ /**
179
+ * Get current slide object
180
+ * @return {k4.slide.|null|undefined}
181
+ */
182
+ k4.slide.Controller.prototype.getPrev = function() {
183
+ if (goog.isArrayLike(this.slides_) && this.slides_.length > 0) {
184
+ return this.slides_[this.current_ - 1];
185
+ } else {
186
+ return null;
187
+ }
188
+ };
189
+
190
+ /**
191
+ * Get current slide object
192
+ * @return {k4.slide.|null|undefined}
193
+ */
194
+ k4.slide.Controller.prototype.updateTitle = function() {
195
+ if (this.history_) {
196
+ var c = this.getCurrent();
197
+ if (c) {
198
+ var el = c.getElement();
199
+ if (el) {
200
+ var page = el.getAttribute('page');
201
+ if (page) {
202
+ this.history_.setToken(page);
203
+ }
204
+ }
205
+ }
206
+ }
207
+ };
208
+
209
+
210
+ /**
211
+ * start presentation
212
+ */
213
+ k4.slide.Controller.prototype.start = function() {
214
+ if (goog.isArrayLike(this.slides_) && this.slides_.length > 0) {
215
+ this.slides_[0].applyClasses(this.config_.get('slide.class.current'));
216
+ this.slides_[0].show();
217
+ }
218
+ };
219
+
220
+ /**
221
+ * next
222
+ */
223
+ k4.slide.Controller.prototype.next = function() {
224
+ var currentSlide = this.getCurrent();
225
+ var nextSlide = this.getNext();
226
+ if (nextSlide) {
227
+ currentSlide.toPrev();
228
+ nextSlide.toCurrent();
229
+ this.current_++;
230
+ }
231
+ };
232
+
233
+ /**
234
+ * next
235
+ */
236
+ k4.slide.Controller.prototype.back = function() {
237
+ var currentSlide = this.getCurrent();
238
+ var prevSlide = this.getPrev();
239
+ if (prevSlide) {
240
+ currentSlide.toNext();
241
+ prevSlide.toCurrent();
242
+ this.current_--;
243
+ }
244
+ };
245
+
246
+ /**
247
+ * resize
248
+ */
249
+ k4.slide.Controller.prototype.resize = function() {
250
+ this.initWindow_();
251
+ this.calculateSlideSizeAndPosition_();
252
+ goog.array.forEach(this.slides_, function(s, i, a) {
253
+ s.setPositionByConfig();
254
+ }, this);
255
+ };