]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
Use popcorn.extend to inject class methods.
authorBrad Chen <brad5620732@hotmail.com>
Fri, 11 Mar 2011 07:19:59 +0000 (02:19 -0500)
committerBrad Chen <brad5620732@hotmail.com>
Fri, 11 Mar 2011 07:19:59 +0000 (02:19 -0500)
players/youtube/popcorn.youtube.js

index 67e7d28655008bdc9df028f426b1f6f518de8446..5c60835f5f22932dd9a3f3aaac954e601cbee0d9 100644 (file)
     this.registerInternalEventHandlers();\r
   }; // end Popcorn.youtube.init\r
 \r
-  // For internal use only.\r
-  Popcorn.youtube.init.p.registerYoutubeEventHandlers = function() {\r
-    var youcorn = this,\r
-        stateChangeHandler = 'Popcorn.youtube.stateChangeEventHandler',\r
-        errorHandler = 'Popcorn.youtube.errorEventHandler';\r
-    this.video.addEventListener( 'onStateChange', stateChangeHandler );\r
-    this.video.addEventListener( 'onError', errorHandler );\r
+  Popcorn.extend( Popcorn.youtube.init.prototype, {\r
+\r
+    // For internal use only.\r
+    // Register handlers to YouTube events.\r
+    registerYoutubeEventHandlers: function() {\r
+      var youcorn = this,\r
+          stateChangeHandler = 'Popcorn.youtube.stateChangeEventHandler',\r
+          errorHandler = 'Popcorn.youtube.errorEventHandler';\r
+      this.video.addEventListener( 'onStateChange', stateChangeHandler );\r
+      this.video.addEventListener( 'onError', errorHandler );\r
+\r
+      /**\r
+       * Since Flash can only call named functions, they are declared\r
+       * separately here.\r
+       */\r
+      Popcorn.youtube.stateChangeEventHandler = function( state ) {\r
+        if ( debug ) {\r
+          console.log( 'Youtube state updated: ' + state );\r
+        }\r
+        if ( state == YOUTUBE_STATE_UNSTARTED ) {\r
+          youcorn.readyState = READY_STATE_HAVE_METADATA;\r
+          youcorn.raiseEvent('loadedmetadata');\r
+        } else if ( state == YOUTUBE_STATE_ENDED ) {\r
+          youcorn.raiseEvent('ended');\r
+        } else if ( state == YOUTUBE_STATE_PLAYING ) {\r
+          // Being able to play means current data is loaded.\r
+          if ( !this.loadedData ) {\r
+            this.loadedData = true;\r
+            youcorn.raiseEvent( 'loadeddata' );\r
+          }\r
+\r
+          youcorn.readyState = READY_STATE_HAVE_CURRENT_DATA;\r
+          youcorn.raiseEvent( 'playing' );\r
+        } else if ( state == YOUTUBE_STATE_PAUSED ) {\r
+          youcorn.raiseEvent( 'pause' );\r
+        } else if ( state == YOUTUBE_STATE_BUFFERING ) {\r
+          youcorn.raiseEvent( 'waiting' );\r
+        } else if ( state == YOUTUBE_STATE_CUED ) {\r
+          // not handled\r
+        }\r
+      };\r
 \r
-    /**\r
-     * Since Flash can only call named functions, they are declared\r
-     * separately here.\r
-     */\r
-    Popcorn.youtube.stateChangeEventHandler = function( state ) {\r
-      if ( debug ) {\r
-        console.log( 'Youtube state updated: ' + state );\r
-      }\r
-      if ( state == YOUTUBE_STATE_UNSTARTED ) {\r
-        youcorn.readyState = READY_STATE_HAVE_METADATA;\r
-        youcorn.raiseEvent('loadedmetadata');\r
-      } else if ( state == YOUTUBE_STATE_ENDED ) {\r
-        youcorn.raiseEvent('ended');\r
-      } else if ( state == YOUTUBE_STATE_PLAYING ) {\r
-        // Being able to play means current data is loaded.\r
-        if ( !this.loadedData ) {\r
-          this.loadedData = true;\r
-          youcorn.raiseEvent( 'loadeddata' );\r
+      Popcorn.youtube.errorEventHandler = function( state ) {\r
+        if ( debug ) {\r
+          console.log( 'Youtube error encountered: ' + error );\r
         }\r
+        youcorn.raiseEvent( 'error' );\r
+      };\r
+    },\r
+\r
+    // For internal use only.\r
+    // Start current time and loading progress syncing intervals.\r
+    registerInternalEventHandlers: function() {\r
+      this.addEventListener( 'playing', function() {\r
+        startTimeUpdater( this );\r
+      });\r
+      this.addEventListener( 'loadedmetadata', function() {\r
+        startProgressUpdater( this );\r
+      });\r
+    },\r
+\r
+    play: function() {\r
+      this.raiseEvent( 'play' );\r
+      this.video.playVideo();\r
+    },\r
+\r
+    pause: function() {\r
+      this.video.pauseVideo();\r
+      // pause event is raised by Youtube.\r
+    },\r
 \r
-        youcorn.readyState = READY_STATE_HAVE_CURRENT_DATA;\r
-        youcorn.raiseEvent( 'playing' );\r
-      } else if ( state == YOUTUBE_STATE_PAUSED ) {\r
-        youcorn.raiseEvent( 'pause' );\r
-      } else if ( state == YOUTUBE_STATE_BUFFERING ) {\r
-        youcorn.raiseEvent( 'waiting' );\r
-      } else if ( state == YOUTUBE_STATE_CUED ) {\r
-        // not handled\r
-      }\r
-    };\r
+    load: function() {\r
+      this.video.playVideo();\r
+      this.video.pauseVideo();\r
+    },\r
 \r
-    Popcorn.youtube.errorEventHandler = function( state ) {\r
-      if ( debug ) {\r
-        console.log( 'Youtube error encountered: ' + error );\r
+    currentTime: function( time ) {\r
+      if ( time === undef ) {\r
+        return this.video.getCurrentTime();\r
       }\r
-      youcorn.raiseEvent( 'error' );\r
-    };\r
-  };\r
-\r
-  Popcorn.youtube.init.p = Popcorn.youtube.init.prototype;\r
-\r
-  // For internal use only.\r
-  Popcorn.youtube.init.p.registerInternalEventHandlers = function() {\r
-    this.addEventListener( 'playing', function() {\r
-      startTimeUpdater( this );\r
-    });\r
-    this.addEventListener( 'loadedmetadata', function() {\r
-      startProgressUpdater( this );\r
-    });\r
-  };\r
+      var playing = this.video.getPlayerState() == YOUTUBE_STATE_PLAYING;\r
+      this.video.seekTo( time, true );\r
 \r
-  Popcorn.youtube.init.p.play = function() {\r
-    this.raiseEvent( 'play' );\r
-    this.video.playVideo();\r
-  };\r
+      // Prevent Youtube's behaviour to start playing video after seeking.\r
+      if ( !playing ) {\r
+        this.video.pauseVideo();\r
+      }\r
 \r
-  Popcorn.youtube.init.p.pause = function() {\r
-    this.video.pauseVideo();\r
-    // pause event is raised by Youtube.\r
-  };\r
+      // Data need to be loaded again.\r
+      if ( !this.fullyLoaded ) {\r
+        this.loadedData = false;\r
+      }\r
 \r
-  Popcorn.youtube.init.p.load = function() {\r
-    this.video.playVideo();\r
-    this.video.pauseVideo();\r
-  };\r
+      // Raise event.\r
+      this.raiseEvent( 'seeked' );\r
+    },\r
 \r
-  Popcorn.youtube.init.p.currentTime = function( time ) {\r
-    if ( time === undef ) {\r
-      return this.video.getCurrentTime();\r
-    }\r
-    var playing = this.video.getPlayerState() == YOUTUBE_STATE_PLAYING;\r
-    this.video.seekTo( time, true );\r
+    duration: function() {\r
+      return this.video.getDuration();\r
+    },\r
 \r
-    // Prevent Youtube's behaviour to start playing video after seeking.\r
-    if ( !playing ) {\r
-      this.video.pauseVideo();\r
-    }\r
+    mute: function() {\r
+      if ( this.video.getVolume() != 0 ) {\r
+        this.video.mute();\r
+        this.raiseEvent( 'volumechange' );\r
+      }\r
+    },\r
 \r
-    // Data need to be loaded again.\r
-    if ( !this.fullyLoaded ) {\r
-      this.loadedData = false;\r
-    }\r
+    volume: function( vol ) {\r
+      if ( vol == undef ) {\r
+        return this.video.getVolume() / 100;\r
+      }\r
+      if ( vol != this.volume() ) {\r
+        this.video.setVolume( vol * 100 );\r
+        this.raiseEvent( 'volumechange' );\r
+      }\r
+    },\r
 \r
-    // Raise event.\r
-    this.raiseEvent( 'seeked' );\r
-  };\r
+    addEventListener: function( eventName, func ) {\r
+      if ( !this.eventListeners[eventName] ) {\r
+        this.eventListeners[eventName] = [];\r
+      }\r
+      this.eventListeners[eventName].push( func );\r
+    },\r
 \r
-  Popcorn.youtube.init.p.duration = function() {\r
-    return this.video.getDuration();\r
-  };\r
+    /**\r
+     * Notify event listeners about an event.\r
+     */\r
+    raiseEvent: function( name ) {\r
+      if ( debug ) {\r
+        console.log( 'Raising event: ' + name );\r
+      }\r
+      if ( !this.eventListeners[name] ) {\r
+        return;\r
+      }\r
+      for ( var i in this.eventListeners[name] ) {\r
+        // @TODO should we mimic the event object sent by HTMLMediaElement?\r
+        this.eventListeners[name][i].call( this, null );\r
+      }\r
+    },\r
 \r
-  Popcorn.youtube.init.p.mute = function() {\r
-    if ( this.video.getVolume() != 0 ) {\r
-      this.video.mute();\r
-      this.raiseEvent( 'volumechange' );\r
-    }\r
-  };\r
+    /* Unsupported methods. */\r
 \r
-  Popcorn.youtube.init.p.volume = function( vol ) {\r
-    if ( vol == undef ) {\r
-      return this.video.getVolume() / 100;\r
-    }\r
-    if ( vol != this.volume() ) {\r
-      this.video.setVolume( vol * 100 );\r
-      this.raiseEvent( 'volumechange' );\r
-    }\r
-  };\r
+    defaultPlaybackRate: function( arg ) {\r
+    },\r
 \r
-  Popcorn.youtube.init.p.addEventListener = function( eventName, func ) {\r
-    if ( !this.eventListeners[eventName] ) {\r
-      this.eventListeners[eventName] = [];\r
+    playbackRate: function( arg ) {\r
     }\r
-    this.eventListeners[eventName].push( func );\r
-  };\r
 \r
-  /**\r
-   * Notify event listeners about an event.\r
-   */\r
-  Popcorn.youtube.init.p.raiseEvent = function( name ) {\r
-    if ( debug ) {\r
-      console.log( 'Raising event: ' + name );\r
-    }\r
-    if ( !this.eventListeners[name] ) {\r
-      return;\r
-    }\r
-    for ( var i in this.eventListeners[name] ) {\r
-      // @TODO should we mimic the event object sent by HTMLMediaElement?\r
-      this.eventListeners[name][i].call( this, null );\r
-    }\r
-  }\r
+  }); // end Popcorn.extend\r
 \r
   function startTimeUpdater( youcorn ) {\r
     youcorn.timeUpdater = setInterval(function() {\r
     }, progressInterval);\r
   }\r
 \r
-  /* Unsupported properties, methods and events. */\r
-\r
-  Popcorn.youtube.init.p.defaultPlaybackRate = function( arg ) {\r
-  };\r
-\r
-  Popcorn.youtube.init.p.playbackRate = function( arg ) {\r
-  };\r
+  /* Unsupported properties and events. */\r
 \r
   /**\r
    * Unsupported events are:\r