1+ /**
2+ * videojs-time-offset
3+ * @version 0.2.0
4+ * @copyright 2016 Can Küçükyılmaz <can@vngrs.com>
5+ * @license MIT
6+ */
7+ ( function ( f ) { if ( typeof exports === "object" && typeof module !== "undefined" ) { module . exports = f ( ) } else if ( typeof define === "function" && define . amd ) { define ( [ ] , f ) } else { var g ; if ( typeof window !== "undefined" ) { g = window } else if ( typeof global !== "undefined" ) { g = global } else if ( typeof self !== "undefined" ) { g = self } else { g = this } g . videojsTimeOffset = f ( ) } } ) ( 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 ) ; var f = new Error ( "Cannot find module '" + o + "'" ) ; throw f . code = "MODULE_NOT_FOUND" , f } var l = n [ o ] = { exports :{ } } ; t [ o ] [ 0 ] . call ( l . exports , function ( e ) { var n = t [ o ] [ 1 ] [ e ] ; return s ( n ?n :e ) } , l , l . 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 ( require , module , exports ) {
8+ ( function ( global ) {
9+ 'use strict' ;
10+
11+ Object . defineProperty ( exports , '__esModule' , {
12+ value : true
13+ } ) ;
14+
15+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { 'default' : obj } ; }
16+
17+ var _videoJs = ( typeof window !== "undefined" ? window [ 'videojs' ] : typeof global !== "undefined" ? global [ 'videojs' ] : null ) ;
18+
19+ var _videoJs2 = _interopRequireDefault ( _videoJs ) ;
20+
21+ // Default options for the plugin.
22+ var defaults = {
23+ start : 0 ,
24+ end : 0 ,
25+ page : 1 ,
26+ perPageInMinutes : 0
27+ } ;
28+
29+ /**
30+ * for fixing buffer status overflow issue
31+ */
32+ var addStyle = function addStyle ( ) {
33+ /**
34+ * Style already included, only include once
35+ */
36+ if ( document . getElementById ( 'vjs-time-offset-style' ) ) {
37+ return false ;
38+ }
39+
40+ var css = '\n .vjs-time-offset .vjs-load-progress {\n overflow: hidden;\n };\n ' ;
41+ var head = document . head || document . getElementsByTagName ( 'head' ) [ 0 ] ;
42+ var style = document . createElement ( 'style' ) ;
43+
44+ style . id = 'vjs-time-offset-style' ;
45+ style . type = 'text/css' ;
46+ if ( style . styleSheet ) {
47+ style . styleSheet . cssText = css ;
48+ } else {
49+ style . appendChild ( document . createTextNode ( css ) ) ;
50+ }
51+
52+ head . appendChild ( style ) ;
53+ } ;
54+
55+ /**
56+ * Function to invoke when the player is ready.
57+ *
58+ * This is a great place for your plugin to initialize itself. When this
59+ * function is called, the player will have its DOM and child components
60+ * in place.
61+ *
62+ * @function onPlayerReady
63+ * @param {Player } player
64+ * @param {Object } [options={}]
65+ */
66+ var onPlayerReady = function onPlayerReady ( player , options ) {
67+ var offsetStart = undefined ;
68+ var offsetEnd = undefined ;
69+ var computedDuration = undefined ;
70+
71+ // trigger ended event only once
72+ var isEndedTriggered = false ;
73+
74+ /**
75+ * calc offsetStart and offsetEnd based on options
76+ * if page params is setted use page values, Otherwise use defaults
77+ * default perPageInMinutes based on minutes, convert to seconds
78+ */
79+ options . perPageInMinutes = options . perPageInMinutes * 60 ;
80+
81+ // page is natural number convert it to integer
82+ options . page = options . page - 1 ;
83+
84+ if ( options . start > 0 ) {
85+ offsetStart = options . start ;
86+ } else {
87+ offsetStart = options . page * options . perPageInMinutes ;
88+ }
89+
90+ if ( options . end > 0 ) {
91+ offsetEnd = options . end ;
92+ } else {
93+ offsetEnd = ( options . page + 1 ) * options . perPageInMinutes ;
94+ }
95+
96+ computedDuration = offsetEnd - offsetStart ;
97+
98+ /**
99+ * For monkey patching take references of original methods
100+ * We will override original methods
101+ */
102+ var __monkey__ = {
103+ currentTime : player . currentTime ,
104+ remainingTime : player . remainingTime ,
105+ duration : player . duration
106+ } ;
107+
108+ player . addClass ( 'vjs-time-offset' ) ;
109+
110+ addStyle ( ) ;
111+
112+ player . remainingTime = function ( ) {
113+ return player . duration ( ) - player . currentTime ( ) ;
114+ } ;
115+
116+ player . duration = function ( ) {
117+ for ( var _len = arguments . length , args = Array ( _len ) , _key = 0 ; _key < _len ; _key ++ ) {
118+ args [ _key ] = arguments [ _key ] ;
119+ }
120+
121+ if ( offsetEnd > 0 ) {
122+ __monkey__ . duration . apply ( player , args ) ;
123+ return computedDuration ;
124+ }
125+
126+ return __monkey__ . duration . apply ( player , args ) - offsetStart ;
127+ } ;
128+
129+ player . originalDuration = function ( ) {
130+ for ( var _len2 = arguments . length , args = Array ( _len2 ) , _key2 = 0 ; _key2 < _len2 ; _key2 ++ ) {
131+ args [ _key2 ] = arguments [ _key2 ] ;
132+ }
133+
134+ return __monkey__ . duration . apply ( player , args ) ;
135+ } ;
136+
137+ player . currentTime = function ( seconds ) {
138+ if ( typeof seconds !== 'undefined' ) {
139+ seconds = seconds + offsetStart ;
140+
141+ return __monkey__ . currentTime . call ( player , seconds ) ;
142+ }
143+
144+ var current = __monkey__ . currentTime . call ( player ) - offsetStart ;
145+
146+ if ( current < 0 ) {
147+ player . currentTime ( 0 ) ;
148+ return 0 ;
149+ }
150+ return current ;
151+ } ;
152+
153+ /**
154+ * When user clicks play button after partition finished
155+ * start from beginning of partition
156+ */
157+ player . on ( 'play' , function ( ) {
158+ var remaining = player . remainingTime ( ) ;
159+
160+ if ( remaining <= 0 ) {
161+ player . currentTime ( 0 ) ;
162+ player . play ( ) ;
163+ }
164+ } ) ;
165+
166+ player . on ( 'loadedmetadata' , function ( ) {
167+ var current = player . currentTime ( ) ;
168+ var originalDuration = player . originalDuration ( ) ;
169+
170+ isEndedTriggered = false ;
171+ // if setted end value isn't correct, Fix IT
172+ // it shouldn't be bigger than video length
173+ if ( offsetEnd > originalDuration ) {
174+ computedDuration = originalDuration - offsetStart ;
175+ }
176+
177+ // if setted start value isn't correct, Fix IT
178+ // it shouldn't be bigger than video length
179+ if ( offsetStart > originalDuration ) {
180+ offsetStart = 0 ;
181+ computedDuration = originalDuration ;
182+ }
183+
184+ if ( current < 0 ) {
185+ player . currentTime ( 0 ) ;
186+ }
187+ } ) ;
188+
189+ player . on ( 'timeupdate' , function ( ) {
190+ var remaining = player . remainingTime ( ) ;
191+
192+ if ( remaining <= 0 ) {
193+ player . pause ( ) ;
194+ if ( ! isEndedTriggered ) {
195+ player . trigger ( 'ended' ) ;
196+ isEndedTriggered = true ;
197+ }
198+ }
199+ } ) ;
200+ } ;
201+
202+ /**
203+ * A video.js plugin.
204+ *
205+ * In the plugin function, the value of `this` is a video.js `Player`
206+ * instance. You cannot rely on the player being in a "ready" state here,
207+ * depending on how the plugin is invoked. This may or may not be important
208+ * to you; if not, remove the wait for "ready"!
209+ *
210+ * @function time-offset
211+ * @param {Object } [options={}]
212+ * An object of options left to the plugin author to define.
213+ */
214+ var timeOffset = function timeOffset ( options ) {
215+ var _this = this ;
216+
217+ this . ready ( function ( ) {
218+ onPlayerReady ( _this , _videoJs2 [ 'default' ] . mergeOptions ( defaults , options ) ) ;
219+ } ) ;
220+ } ;
221+
222+ // Register the plugin with video.js.
223+ _videoJs2 [ 'default' ] . plugin ( 'timeOffset' , timeOffset ) ;
224+
225+ // Include the version number.
226+ timeOffset . VERSION = '0.0.1' ;
227+
228+ exports [ 'default' ] = timeOffset ;
229+ module . exports = exports [ 'default' ] ;
230+ } ) . call ( this , typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : { } )
231+ } , { } ] } , { } , [ 1 ] ) ( 1 )
232+ } ) ;
0 commit comments