pig-media-server 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +29 -0
  6. data/Rakefile +1 -0
  7. data/bin/pig-media-server +4 -0
  8. data/lib/pig-media-server.rb +10 -0
  9. data/lib/pig-media-server/book2.erb +59 -0
  10. data/lib/pig-media-server/cli.rb +21 -0
  11. data/lib/pig-media-server/crawl.rb +23 -0
  12. data/lib/pig-media-server/model/comic.rb +64 -0
  13. data/lib/pig-media-server/model/migrate.rb +26 -0
  14. data/lib/pig-media-server/model/pig.rb +98 -0
  15. data/lib/pig-media-server/script.rb +4 -0
  16. data/lib/pig-media-server/version.rb +3 -0
  17. data/lib/pig-media-server/views/_ft_other.haml +0 -0
  18. data/lib/pig-media-server/views/_ft_read.haml +9 -0
  19. data/lib/pig-media-server/views/_ft_txt.haml +1 -0
  20. data/lib/pig-media-server/views/_ft_video.haml +6 -0
  21. data/lib/pig-media-server/views/_link.haml +14 -0
  22. data/lib/pig-media-server/views/_new_flag.haml +4 -0
  23. data/lib/pig-media-server/views/app.scss +85 -0
  24. data/lib/pig-media-server/views/book.coffee +236 -0
  25. data/lib/pig-media-server/views/book2.erb +59 -0
  26. data/lib/pig-media-server/views/config.coffee +8 -0
  27. data/lib/pig-media-server/views/config.haml +29 -0
  28. data/lib/pig-media-server/views/index.haml +45 -0
  29. data/lib/pig-media-server/views/movie.coffee +159 -0
  30. data/lib/pig-media-server/views/read.haml +34 -0
  31. data/lib/pig-media-server/views/remote.coffee +25 -0
  32. data/lib/pig-media-server/views/remote.haml +14 -0
  33. data/lib/pig-media-server/views/session.coffee +6 -0
  34. data/lib/pig-media-server/views/sessions.haml +11 -0
  35. data/lib/pig-media-server/views/storage.coffee +32 -0
  36. data/lib/pig-media-server/views/swipe.erb +433 -0
  37. data/lib/pig-media-server/views/unread.coffee +14 -0
  38. data/lib/pig-media-server/web.rb +201 -0
  39. data/pig-media-server.gemspec +32 -0
  40. metadata +250 -0
@@ -0,0 +1,159 @@
1
+ watch = (link)->
2
+ $link = $(link)
3
+ key = $(link).attr 'key'
4
+ url = $("##{key} .main_link").attr('href')
5
+ title = $(link).attr 'title'
6
+ srt = false
7
+ srt = true if $link.attr('srt') == 'true'
8
+ $link.click ->
9
+ $('#area').html('')
10
+ $('#area').css 'min-height', 0
11
+ width = $('body').width()
12
+ $('<div>').attr('class', 'space').appendTo('#area')
13
+ $('<video>').attr(id: 'play', src: url, key: key, controls: 'controles').css('width', '98%').css('max-height', '98%').appendTo('#area')
14
+ $('<br>').appendTo('#area')
15
+ $('<a href="javascript:void(0)">Gyazo</a>').click(-> gyazo()).appendTo('#area')
16
+ $('<span>&nbsp;&nbsp;&nbsp;</span>').appendTo('#area')
17
+ $('<a>close video</a>').attr('href', 'javascript:void(0)').click(->
18
+ $('#area').html('')
19
+ $('#area').css 'min-height', 0
20
+ ).appendTo('#area')
21
+ $('<span>&nbsp;&nbsp;&nbsp;</span>').appendTo('#area')
22
+ $('<a>').attr(href: 'javascript:void(0)').text('No Controls').click(->
23
+ if $('video').attr('controls')
24
+ $('video').removeAttr 'controls'
25
+ $(this).text('Controls')
26
+ else
27
+ $('video').attr 'controls', 'c'
28
+ $(this).text('No Controls')
29
+ ).appendTo('#area')
30
+ if srt
31
+ $('video').append(
32
+ $('<track default>').attr(src: "/webvtt/#{key}", srclang: "en", kind: "subtitles", label: "Subtitles")
33
+ )
34
+ $('<span>&nbsp;&nbsp;&nbsp;</span>').appendTo('#area')
35
+ $('<span>').text('Rate:').appendTo('#area')
36
+ $('<input>').attr('id', 'playback_rate').appendTo('#area').val(1).attr('size',2)
37
+ $('<span>').html('<br> Now Playing : '+title).appendTo('#area')
38
+ $('<div>').attr('class', 'space').appendTo('#area')
39
+ video = document.querySelector '#play'
40
+ video.load()
41
+ video.addEventListener('canplay', (e)->
42
+ e.target.play()
43
+ $('video').removeAttr 'controls'
44
+ $('video').attr 'controls', 'c'
45
+ window.get_recents (recents)->
46
+ recents['movie/'+key] = {time: parseInt((new Date)/1000), type: 'movie'}
47
+ window.save_recents recents
48
+ $('.new_flag').text('')
49
+ unless $('#action').text() == 'remote'
50
+ setTimeout ->
51
+ window.set_new()
52
+ ,2000
53
+ playback_rate_loop()
54
+ )
55
+
56
+ window.watch_movie = watch
57
+
58
+ playback_rate_loop = ->
59
+ setTimeout ->
60
+ v = $('video').get(0)
61
+ if v
62
+ v.playbackRate = parseFloat($('#playback_rate').val())
63
+ playback_rate_loop()
64
+ ,200
65
+
66
+ initialize_movie = ->
67
+ $.each $('a.watch'), -> watch(this)
68
+
69
+ movie_size = (origin_height)->
70
+ setTimeout ->
71
+ if $('#play').length > 0
72
+ space_height = parseInt(($('#area').height() - $('#play').height())/4)
73
+ if origin_height != space_height
74
+ $('#area').css 'min-height', $(window).height()
75
+ $('.space').css 'height', space_height if space_height != origin_height
76
+ origin_height = space_height
77
+ movie_size(origin_height)
78
+ , 20
79
+
80
+ gyazo = ->
81
+ c = document.querySelector '#canvas'
82
+ v = document.querySelector '#play'
83
+ context = c.getContext '2d'
84
+ c.width = v.videoWidth
85
+ c.height = v.videoHeight
86
+ context.drawImage(v, 0, 0)
87
+ url = c.toDataURL()
88
+ $.post('/gyazo', {url: url, point: localStorage.gyazo}).success((data)->
89
+ window.open(data.url, "", "width=500,height=400")
90
+ copyArea = $("<textarea/>")
91
+ copyArea.text(data.url)
92
+ $("body").append(copyArea)
93
+ copyArea.select()
94
+ document.execCommand("copy")
95
+ copyArea.remove()
96
+ )
97
+
98
+ next_loop = ->
99
+ setTimeout ->
100
+ if $('video')[0]
101
+ v = document.querySelector 'video'
102
+ if v.currentTime== v.duration
103
+ key = $(v).attr 'key'
104
+ keys = $.map($('.watch'), (n,i)-> $(n).attr('key'))
105
+ index = keys.indexOf key
106
+ next = keys[index-1]
107
+ if next
108
+ $('.watch[key="'+next+'"]').click()
109
+ else
110
+ next = keys[keys.length-1]
111
+ $('.watch[key="'+next+'"]').click()
112
+ next_loop()
113
+ ,200
114
+
115
+ seek = (count)->
116
+ node = document.querySelector '#play'
117
+ node.currentTime = node.currentTime + count
118
+
119
+ pause = ->
120
+ node = document.querySelector '#play'
121
+ if node.paused
122
+ node.play()
123
+ else
124
+ node.pause()
125
+
126
+ key_func_j = ->
127
+ seek (15) if $('video')[0]
128
+
129
+ key_func_k = ->
130
+ seek (-15) if $('video')[0]
131
+
132
+ key_func_g = ->
133
+ gyazo() if $('video')[0]
134
+
135
+ key_func_p = ->
136
+ pause() if $('video')[0]
137
+
138
+ remote = ->
139
+ $('a.remote').click ->
140
+ save_to_pig remote_key: $(this).attr('key')
141
+
142
+
143
+ $ ->
144
+ initialize_movie() if $('#action').text() == 'list' or $('#action').text() == 'others'
145
+ next_loop()
146
+ movie_size(0)
147
+ remote()
148
+ $(window).keyup (e)->
149
+ switch e.keyCode
150
+ when 74
151
+ key_func_j()
152
+ when 75
153
+ key_func_k()
154
+ when 80
155
+ key_func_p()
156
+ when 71
157
+ key_func_g()
158
+
159
+ # vim: set ft=coffee:
@@ -0,0 +1,34 @@
1
+ #area2
2
+ #page_insert
3
+ .control
4
+ %p
5
+ %span#left="<<"
6
+  
7
+ %span#right=">>"
8
+  
9
+ %span#plus1="+1"
10
+  
11
+ %span#bound
12
+ %p
13
+ %span
14
+ %input{name: :page_jump, id: :page_jump}
15
+ %span /
16
+ %span=@comic.max_page
17
+  
18
+ %span#jump Jump
19
+ %p
20
+ %span#aozora
21
+ %p.title=@comic.title
22
+ %p.h2#to_aozora
23
+ %a{href: "javascript:void(0)"}Aozora mode
24
+ %div.cache
25
+ %span#cache_insert
26
+ .none
27
+ %span#is_left=@comic.left
28
+ %span#max_page=@comic.max_page
29
+ %span#action read
30
+ %span#controlle page
31
+ %span#id=@comic.id
32
+ %span#page_initial=params[:page]
33
+ %span#now
34
+
@@ -0,0 +1,25 @@
1
+ remote_loop = (original)->
2
+ get_from_pig 'remote_key', (key)->
3
+ if original != key
4
+ if key == '' or key == undefined
5
+ $('#area').html ''
6
+ setTimeout ->
7
+ remote_loop(key)
8
+ , 2000
9
+
10
+ else
11
+ $.get('/remote', key: key).success((data)->
12
+ $('#link').html(data)
13
+ watch_movie($('a.watch').get(0))
14
+ $('a.watch').click()
15
+ setTimeout ->
16
+ remote_loop(key)
17
+ , 2000
18
+ )
19
+ else
20
+ setTimeout ->
21
+ remote_loop(key)
22
+ , 2000
23
+ $ ->
24
+ remote_loop('')
25
+ # vim: set ft=coffee:
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ %meta{charset: 'UTF-8'}
3
+ %title Remote - Pig Media Server
4
+ %script{src: 'http://code.jquery.com/jquery.min.js'}
5
+ -%w{movie storage remote unread session}.each do |js|
6
+ %script{src: "/#{js}.js"}
7
+ %meta(content='width=320, initial-scale=1.0, maximum-scale=1.0, user-scalable=no' name='viewport')
8
+ %link{href: '/app.css', rel: 'stylesheet', type: 'text/css'}
9
+ #area=''
10
+ .none
11
+ #link
12
+ #action remote
13
+ %span#user_id=session[:user_id]
14
+ %canvas#canvas
@@ -0,0 +1,6 @@
1
+ $ ->
2
+ user_id = $('#user_id').text()
3
+ localStorage.user_id = user_id if user_id != ''
4
+ if localStorage.user_id != undefined and localStorage.user_id != ''
5
+ if user_id == ''
6
+ $.post('/sessions', user_id: localStorage.user_id).success(-> location.reload())
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ %meta{charset: 'UTF-8'}
3
+ %title Pork
4
+ <link href='/app.css' media='screen' rel='stylesheet' type='text/css' />
5
+ <meta content='width=320, initial-scale=1.0, maximum-scale=1.0, user-scalable=no' name='viewport' />
6
+ #all
7
+ %form{method: 'POST'}
8
+ !=Rack::Csrf.tag(env)
9
+ %b User:
10
+ %input{name: 'user_id'}
11
+ %input{type: :submit, value: :Submit}
@@ -0,0 +1,32 @@
1
+ window.get_recents = (func)->
2
+ if $('#user_id').text() and $('#user_id').text() != ''
3
+ dd = new Date()
4
+ $.get('/hash', stamp: dd.getTime()).success((data)->
5
+ if data['recents']
6
+ func data['recents']
7
+ else
8
+ func {}
9
+ )
10
+ else
11
+ try
12
+ recents = JSON.parse window.localStorage['recents']
13
+ catch error
14
+ recents = {}
15
+ func(recents)
16
+
17
+ window.save_recents = (recents)->
18
+ if $('#user_id').text() and $('#user_id').text() != ''
19
+ $.get('/hash').success((data)->
20
+ data['recents'] = recents
21
+ $.post('/hash', json: JSON.stringify(data))
22
+ )
23
+
24
+ else
25
+ window.localStorage['recents'] = JSON.stringify recents
26
+
27
+ window.save_to_pig = (hash)->
28
+ $.get('/hash').success((data)->
29
+ data = $.extend data, hash
30
+ $.post('/hash', json: JSON.stringify(data)))
31
+ window.get_from_pig = (key, func)-> $.get('/hash').success((data)-> func(data[key]))
32
+
@@ -0,0 +1,433 @@
1
+ /*
2
+ * touchSwipe - jQuery Plugin
3
+ * http://plugins.jquery.com/project/touchSwipe
4
+ * http://labs.skinkers.com/touchSwipe/
5
+ *
6
+ * Copyright (c) 2010 Matt Bryson (www.skinkers.com)
7
+ * Dual licensed under the MIT or GPL Version 2 licenses.
8
+ *
9
+ * $version: 1.2.3
10
+ *
11
+ * Changelog
12
+ * $Date: 2010-12-12 (Wed, 12 Dec 2010) $
13
+ * $version: 1.0.0
14
+ * $version: 1.0.1 - removed multibyte comments
15
+ *
16
+ * $Date: 2011-21-02 (Mon, 21 Feb 2011) $
17
+ * $version: 1.1.0 - added allowPageScroll property to allow swiping and scrolling of page
18
+ * - changed handler signatures so one handler can be used for multiple events
19
+ * $Date: 2011-23-02 (Wed, 23 Feb 2011) $
20
+ * $version: 1.2.0 - added click handler. This is fired if the user simply clicks and does not swipe. The event object and click target are passed to handler.
21
+ * - If you use the http://code.google.com/p/jquery-ui-for-ipad-and-iphone/ plugin, you can also assign jQuery mouse events to children of a touchSwipe object.
22
+ * $version: 1.2.1 - removed console log!
23
+ *
24
+ * $version: 1.2.2 - Fixed bug where scope was not preserved in callback methods.
25
+ *
26
+ * $Date: 2011-28-04 (Thurs, 28 April 2011) $
27
+ * $version: 1.2.4 - Changed licence terms to be MIT or GPL inline with jQuery. Added check for support of touch events to stop non compatible browsers erroring.
28
+ *
29
+ * A jQuery plugin to capture left, right, up and down swipes on touch devices.
30
+ * You can capture 2 finger or 1 finger swipes, set the threshold and define either a catch all handler, or individual direction handlers.
31
+ * Options:
32
+ * swipe Function A catch all handler that is triggered for all swipe directions. Handler is passed 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
33
+ * swipeLeft Function A handler that is triggered for "left" swipes. Handler is passed 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
34
+ * swipeRight Function A handler that is triggered for "right" swipes. Handler is passed 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
35
+ * swipeUp Function A handler that is triggered for "up" swipes. Handler is passed 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
36
+ * swipeDown Function A handler that is triggered for "down" swipes. Handler is passed 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
37
+ * swipeStatus Function A handler triggered for every phase of the swipe. Handler is passed 4 arguments: event : The original event object, phase:The current swipe face, either "start?, "move?, "end? or "cancel?. direction : The swipe direction, either "up?, "down?, "left " or "right?.distance : The distance of the swipe.
38
+ * click Function A handler triggered when a user just clicks on the item, rather than swipes it. If they do not move, click is triggered, if they do move, it is not.
39
+ *
40
+ * fingers int Default 1. The number of fingers to trigger the swipe, 1 or 2.
41
+ * threshold int Default 75. The number of pixels that the user must move their finger by before it is considered a swipe.
42
+ * triggerOnTouchEnd Boolean Default true If true, the swipe events are triggered when the touch end event is received (user releases finger). If false, it will be triggered on reaching the threshold, and then cancel the touch event automatically.
43
+ * allowPageScroll String Default "auto". How the browser handles page scrolls when the user is swiping on a touchSwipe object.
44
+ * "auto" : all undefined swipes will cause the page to scroll in that direction.
45
+ * "none" : the page will not scroll when user swipes.
46
+ * "horizontal" : will force page to scroll on horizontal swipes.
47
+ * "vertical" : will force page to scroll on vertical swipes.
48
+ *
49
+ * This jQuery plugin will only run on devices running Mobile Webkit based browsers (iOS 2.0+, android 2.2+)
50
+ */
51
+ (function($)
52
+ {
53
+
54
+
55
+
56
+ $.fn.swipe = function(options)
57
+ {
58
+ if (!this) return false;
59
+
60
+ // Default thresholds & swipe functions
61
+ var defaults = {
62
+
63
+ fingers : 1, // int - The number of fingers to trigger the swipe, 1 or 2. Default is 1.
64
+ threshold : 75, // int - The number of pixels that the user must move their finger by before it is considered a swipe. Default is 75.
65
+
66
+ swipe : null, // Function - A catch all handler that is triggered for all swipe directions. Accepts 2 arguments, the original event object and the direction of the swipe : "left", "right", "up", "down".
67
+ swipeLeft : null, // Function - A handler that is triggered for "left" swipes. Accepts 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
68
+ swipeRight : null, // Function - A handler that is triggered for "right" swipes. Accepts 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
69
+ swipeUp : null, // Function - A handler that is triggered for "up" swipes. Accepts 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
70
+ swipeDown : null, // Function - A handler that is triggered for "down" swipes. Accepts 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
71
+ swipeStatus : null, // Function - A handler triggered for every phase of the swipe. Handler is passed 4 arguments: event : The original event object, phase:The current swipe face, either "start?, "move?, "end? or "cancel?. direction : The swipe direction, either "up?, "down?, "left " or "right?.distance : The distance of the swipe.
72
+ click : null, // Function - A handler triggered when a user just clicks on the item, rather than swipes it. If they do not move, click is triggered, if they do move, it is not.
73
+
74
+ triggerOnTouchEnd : true, // Boolean, if true, the swipe events are triggered when the touch end event is received (user releases finger). If false, it will be triggered on reaching the threshold, and then cancel the touch event automatically.
75
+ allowPageScroll : "auto" /* How the browser handles page scrolls when the user is swiping on a touchSwipe object.
76
+ "auto" : all undefined swipes will cause the page to scroll in that direction.
77
+ "none" : the page will not scroll when user swipes.
78
+ "horizontal" : will force page to scroll on horizontal swipes.
79
+ "vertical" : will force page to scroll on vertical swipes.
80
+ */
81
+ };
82
+
83
+
84
+ //Constants
85
+ var LEFT = "left";
86
+ var RIGHT = "right";
87
+ var UP = "up";
88
+ var DOWN = "down";
89
+ var NONE = "none";
90
+ var HORIZONTAL = "horizontal";
91
+ var VERTICAL = "vertical";
92
+ var AUTO = "auto";
93
+
94
+ var PHASE_START="start";
95
+ var PHASE_MOVE="move";
96
+ var PHASE_END="end";
97
+ var PHASE_CANCEL="cancel";
98
+
99
+
100
+
101
+ var phase="start";
102
+
103
+ if (options.allowPageScroll==undefined && (options.swipe!=undefined || options.swipeStatus!=undefined))
104
+ options.allowPageScroll=NONE;
105
+
106
+ if (options)
107
+ $.extend(defaults, options);
108
+
109
+
110
+ /**
111
+ * Setup each object to detect swipe gestures
112
+ */
113
+ return this.each(function()
114
+ {
115
+ var $this = $(this);
116
+
117
+ var triggerElementID = null; // this variable is used to identity the triggering element
118
+ var fingerCount = 0; // the current number of fingers being used.
119
+
120
+ //track mouse points / delta
121
+ var start={x:0, y:0};
122
+ var end={x:0, y:0};
123
+ var delta={x:0, y:0};
124
+
125
+
126
+ /**
127
+ * Event handler for a touch start event.
128
+ * Stops the default click event from triggering and stores where we touched
129
+ */
130
+ function touchStart(event)
131
+ {
132
+ phase = PHASE_START;
133
+
134
+ // get the total number of fingers touching the screen
135
+ fingerCount = event.touches.length;
136
+
137
+ //clear vars..
138
+ distance=0;
139
+ direction=null;
140
+
141
+ // check the number of fingers is what we are looking for
142
+ if ( fingerCount == defaults.fingers )
143
+ {
144
+ // get the coordinates of the touch
145
+ start.x = end.x = event.touches[0].pageX;
146
+ start.y = end.y = event.touches[0].pageY;
147
+
148
+ if (defaults.swipeStatus)
149
+ triggerHandler(event, phase);
150
+ }
151
+ else
152
+ {
153
+ //touch with more/less than the fingers we are looking for
154
+ touchCancel(event);
155
+ }
156
+ }
157
+
158
+ /**
159
+ * Event handler for a touch move event.
160
+ * If we change fingers during move, then cancel the event
161
+ */
162
+ function touchMove(event)
163
+ {
164
+ if (phase == PHASE_END || phase == PHASE_CANCEL)
165
+ return;
166
+
167
+ end.x = event.touches[0].pageX;
168
+ end.y = event.touches[0].pageY;
169
+
170
+ direction = caluculateDirection();
171
+ fingerCount = event.touches.length;
172
+
173
+ phase = PHASE_MOVE
174
+
175
+ //Check if we need to prevent default evnet (page scroll) or not
176
+ validateDefaultEvent(event, direction);
177
+
178
+ if ( fingerCount == defaults.fingers )
179
+ {
180
+ distance = caluculateDistance();
181
+
182
+ if (defaults.swipeStatus)
183
+ triggerHandler(event, phase, direction, distance);
184
+
185
+ //If we trigger whilst dragging, not on touch end, then calculate now...
186
+ if (!defaults.triggerOnTouchEnd)
187
+ {
188
+ // if the user swiped more than the minimum length, perform the appropriate action
189
+ if ( distance >= defaults.threshold )
190
+ {
191
+ phase = PHASE_END;
192
+ triggerHandler(event, phase);
193
+ touchCancel(event); // reset the variables
194
+ }
195
+ }
196
+ }
197
+ else
198
+ {
199
+ phase = PHASE_CANCEL;
200
+ triggerHandler(event, phase);
201
+ touchCancel(event);
202
+ }
203
+ }
204
+
205
+ /**
206
+ * Event handler for a touch end event.
207
+ * Calculate the direction and trigger events
208
+ */
209
+ function touchEnd(event)
210
+ {
211
+ event.preventDefault();
212
+
213
+ distance = caluculateDistance();
214
+ direction = caluculateDirection();
215
+
216
+ if (defaults.triggerOnTouchEnd)
217
+ {
218
+ phase = PHASE_END;
219
+ // check to see if more than one finger was used and that there is an ending coordinate
220
+ if ( fingerCount == defaults.fingers && end.x != 0 )
221
+ {
222
+ // if the user swiped more than the minimum length, perform the appropriate action
223
+ if ( distance >= defaults.threshold )
224
+ {
225
+ triggerHandler(event, phase);
226
+ touchCancel(event); // reset the variables
227
+ }
228
+ else
229
+ {
230
+ phase = PHASE_CANCEL;
231
+ triggerHandler(event, phase);
232
+ touchCancel(event);
233
+ }
234
+ }
235
+ else
236
+ {
237
+ phase = PHASE_CANCEL;
238
+ triggerHandler(event, phase);
239
+ touchCancel(event);
240
+ }
241
+ }
242
+ else if (phase == PHASE_MOVE)
243
+ {
244
+ phase = PHASE_CANCEL;
245
+ triggerHandler(event, phase);
246
+ touchCancel(event);
247
+ }
248
+ }
249
+
250
+ /**
251
+ * Event handler for a touch cancel event.
252
+ * Clears current vars
253
+ */
254
+ function touchCancel(event)
255
+ {
256
+ // reset the variables back to default values
257
+ fingerCount = 0;
258
+
259
+ start.x = 0;
260
+ start.y = 0;
261
+ end.x = 0;
262
+ end.y = 0;
263
+ delta.x = 0;
264
+ delta.y = 0;
265
+ }
266
+
267
+
268
+ /**
269
+ * Trigger the relevant event handler
270
+ * The handlers are passed the original event, the element that was swiped, and in the case of the catch all handler, the direction that was swiped, "left", "right", "up", or "down"
271
+ */
272
+ function triggerHandler(event, phase)
273
+ {
274
+ //update status
275
+ if (defaults.swipeStatus)
276
+ defaults.swipeStatus.call($this,event, phase, direction || null, distance || 0);
277
+
278
+
279
+ if (phase == PHASE_CANCEL)
280
+ {
281
+ if (defaults.click && fingerCount==1 && (isNaN(distance) || distance==0))
282
+ defaults.click.call($this,event, event.target);
283
+ }
284
+
285
+ if (phase == PHASE_END)
286
+ {
287
+ //trigger catch all event handler
288
+ if (defaults.swipe)
289
+ {
290
+
291
+ defaults.swipe.call($this,event, direction, distance);
292
+
293
+ }
294
+ //trigger direction specific event handlers
295
+ switch(direction)
296
+ {
297
+ case LEFT :
298
+ if (defaults.swipeLeft)
299
+ defaults.swipeLeft.call($this,event, direction, distance);
300
+ break;
301
+
302
+ case RIGHT :
303
+ if (defaults.swipeRight)
304
+ defaults.swipeRight.call($this,event, direction, distance);
305
+ break;
306
+
307
+ case UP :
308
+ if (defaults.swipeUp)
309
+ defaults.swipeUp.call($this,event, direction, distance);
310
+ break;
311
+
312
+ case DOWN :
313
+ if (defaults.swipeDown)
314
+ defaults.swipeDown.call($this,event, direction, distance);
315
+ break;
316
+ }
317
+ }
318
+ }
319
+
320
+
321
+ /**
322
+ * Checks direction of the swipe and the value allowPageScroll to see if we should allow or prevent the default behaviour from occurring.
323
+ * This will essentially allow page scrolling or not when the user is swiping on a touchSwipe object.
324
+ */
325
+ function validateDefaultEvent(event, direction)
326
+ {
327
+ if( defaults.allowPageScroll==NONE )
328
+ {
329
+ event.preventDefault();
330
+ }
331
+ else
332
+ {
333
+ var auto=defaults.allowPageScroll==AUTO;
334
+
335
+ switch(direction)
336
+ {
337
+ case LEFT :
338
+ if ( (defaults.swipeLeft && auto) || (!auto && defaults.allowPageScroll!=HORIZONTAL))
339
+ event.preventDefault();
340
+ break;
341
+
342
+ case RIGHT :
343
+ if ( (defaults.swipeRight && auto) || (!auto && defaults.allowPageScroll!=HORIZONTAL))
344
+ event.preventDefault();
345
+ break;
346
+
347
+ case UP :
348
+ if ( (defaults.swipeUp && auto) || (!auto && defaults.allowPageScroll!=VERTICAL))
349
+ event.preventDefault();
350
+ break;
351
+
352
+ case DOWN :
353
+ if ( (defaults.swipeDown && auto) || (!auto && defaults.allowPageScroll!=VERTICAL))
354
+ event.preventDefault();
355
+ break;
356
+ }
357
+ }
358
+
359
+ }
360
+
361
+
362
+
363
+ /**
364
+ * Calcualte the length / distance of the swipe
365
+ */
366
+ function caluculateDistance()
367
+ {
368
+ return Math.round(Math.sqrt(Math.pow(end.x - start.x,2) + Math.pow(end.y - start.y,2)));
369
+ }
370
+
371
+ /**
372
+ * Calcualte the angle of the swipe
373
+ */
374
+ function caluculateAngle()
375
+ {
376
+ var X = start.x-end.x;
377
+ var Y = end.y-start.y;
378
+ var r = Math.atan2(Y,X); //radians
379
+ var angle = Math.round(r*180/Math.PI); //degrees
380
+
381
+ //ensure value is positive
382
+ if (angle < 0)
383
+ angle = 360 - Math.abs(angle);
384
+
385
+ return angle;
386
+ }
387
+
388
+ /**
389
+ * Calcualte the direction of the swipe
390
+ * This will also call caluculateAngle to get the latest angle of swipe
391
+ */
392
+ function caluculateDirection()
393
+ {
394
+ var angle = caluculateAngle();
395
+
396
+ if ( (angle <= 45) && (angle >= 0) )
397
+ return LEFT;
398
+
399
+ else if ( (angle <= 360) && (angle >= 315) )
400
+ return LEFT;
401
+
402
+ else if ( (angle >= 135) && (angle <= 225) )
403
+ return RIGHT;
404
+
405
+ else if ( (angle > 45) && (angle < 135) )
406
+ return DOWN;
407
+
408
+ else
409
+ return UP;
410
+ }
411
+
412
+
413
+
414
+ // Add gestures to all swipable areas if supported
415
+ try
416
+ {
417
+ this.addEventListener("touchstart", touchStart, false);
418
+ this.addEventListener("touchmove", touchMove, false);
419
+ this.addEventListener("touchend", touchEnd, false);
420
+ this.addEventListener("touchcancel", touchCancel, false);
421
+ }
422
+ catch(e)
423
+ {
424
+ //touch not supported
425
+ }
426
+
427
+ });
428
+ };
429
+
430
+
431
+
432
+
433
+ })(jQuery);