1+ /**
2+ * videojs-time-offset
3+ * @version 0.2.1
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+ // in safari with hls, it returns floating number numbers, fix it
147+ if ( Math . ceil ( current ) < 0 ) {
148+ player . pause ( ) ;
149+ player . currentTime ( 0 ) ;
150+ return 0 ;
151+ }
152+ return current ;
153+ } ;
154+
155+ /**
156+ * When user clicks play button after partition finished
157+ * start from beginning of partition
158+ */
159+ player . on ( 'play' , function ( ) {
160+ var remaining = player . remainingTime ( ) ;
161+
162+ if ( remaining <= 0 ) {
163+ player . currentTime ( 0 ) ;
164+ player . play ( ) ;
165+ }
166+ } ) ;
167+
168+ player . on ( 'loadedmetadata' , function ( ) {
169+ var current = player . currentTime ( ) ;
170+ var originalDuration = player . originalDuration ( ) ;
171+
172+ isEndedTriggered = false ;
173+ // if setted end value isn't correct, Fix IT
174+ // it shouldn't be bigger than video length
175+ if ( offsetEnd > originalDuration ) {
176+ computedDuration = originalDuration - offsetStart ;
177+ }
178+
179+ // if setted start value isn't correct, Fix IT
180+ // it shouldn't be bigger than video length
181+ if ( offsetStart > originalDuration ) {
182+ offsetStart = 0 ;
183+ computedDuration = originalDuration ;
184+ }
185+
186+ if ( current < 0 ) {
187+ player . currentTime ( 0 ) ;
188+ }
189+ } ) ;
190+
191+ player . on ( 'timeupdate' , function ( ) {
192+ var remaining = player . remainingTime ( ) ;
193+
194+ if ( remaining <= 0 ) {
195+ player . pause ( ) ;
196+ if ( ! isEndedTriggered ) {
197+ player . trigger ( 'ended' ) ;
198+ isEndedTriggered = true ;
199+ }
200+ }
201+ } ) ;
202+ } ;
203+
204+ /**
205+ * A video.js plugin.
206+ *
207+ * In the plugin function, the value of `this` is a video.js `Player`
208+ * instance. You cannot rely on the player being in a "ready" state here,
209+ * depending on how the plugin is invoked. This may or may not be important
210+ * to you; if not, remove the wait for "ready"!
211+ *
212+ * @function time-offset
213+ * @param {Object } [options={}]
214+ * An object of options left to the plugin author to define.
215+ */
216+ var timeOffset = function timeOffset ( options ) {
217+ var _this = this ;
218+
219+ this . ready ( function ( ) {
220+ onPlayerReady ( _this , _videoJs2 [ 'default' ] . mergeOptions ( defaults , options ) ) ;
221+ } ) ;
222+ } ;
223+
224+ // Register the plugin with video.js.
225+ _videoJs2 [ 'default' ] . plugin ( 'timeOffset' , timeOffset ) ;
226+
227+ // Include the version number.
228+ timeOffset . VERSION = '0.0.1' ;
229+
230+ exports [ 'default' ] = timeOffset ;
231+ module . exports = exports [ 'default' ] ;
232+ } ) . call ( this , typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : { } )
233+ } , { } ] } , { } , [ 1 ] ) ( 1 )
234+ } ) ;
0 commit comments