]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
merge with annasob/0.4
authorDaniel Brooks <db48x@db48x.net>
Sat, 19 Feb 2011 12:40:15 +0000 (06:40 -0600)
committerDaniel Brooks <db48x@db48x.net>
Sat, 19 Feb 2011 12:40:15 +0000 (06:40 -0600)
1  2 
popcorn.js
test/popcorn.unit.js

diff --cc popcorn.js
index 310a9bacf4708014a8c0d46ff6f042fdcd70bab7,16deb0a709d4c380f284a7c15c60a9d25151758e..786c6b209f026d6ade0f84f411e648e5c4098b27
mode 100644,100644..100755
            previousUpdateTime: 0
          }
        };
 -      
 +
        var isReady = function( that ) {
  
-         if ( that.video.readyState >= 3 ) {
+         if ( that.video.readyState >= 2 ) {
            // adding padding to the front and end of the arrays
            // this is so we do not fall off either end
  
              // Playbar receding
              } else if ( previousTime > currentTime ) {
  
-               while ( tracksByStart[tracks.startIndex] && tracksByStart[tracks.startIndex].start > currentTime ) {
-                 if ( tracksByStart[tracks.startIndex]._running === true ) {
-                   tracksByStart[tracks.startIndex]._running = false;
-                   tracksByStart[tracks.startIndex]._natives.end.call( that, event, tracksByStart[tracks.startIndex] );
+               while ( tracksByStart[ tracks.startIndex ] && tracksByStart[ tracks.startIndex ].start > currentTime ) {
+                 // if plugin does not exist on this instance, remove it
+                 if ( !tracksByStart[ tracks.startIndex ]._natives || !!that[ tracksByStart[ tracks.startIndex ]._natives.type ] ) {
+                   if ( tracksByStart[ tracks.startIndex ]._running === true ) {
+                     tracksByStart[ tracks.startIndex ]._running = false;
+                     tracksByStart[ tracks.startIndex ]._natives.end.call( that, event, tracksByStart[ tracks.startIndex ] );
+                   }
+                   tracks.startIndex--;
+                 } else {
+                   // remove track event
+                   Popcorn.removeTrackEvent( that, tracksByStart[ tracks.startIndex ]._id );
+                   return;
                  }
-                 tracks.startIndex--;
                }
 -              
 +
-               while ( tracksByEnd[tracks.endIndex] && tracksByEnd[tracks.endIndex].end > currentTime ) {
-                 if ( tracksByEnd[tracks.endIndex].start <= currentTime && tracksByEnd[tracks.endIndex]._running === false ) {
-                   tracksByEnd[tracks.endIndex]._running = true;
-                   tracksByEnd[tracks.endIndex]._natives.start.call( that, event, tracksByEnd[tracks.endIndex] );
+               while ( tracksByEnd[ tracks.endIndex ] && tracksByEnd[ tracks.endIndex ].end > currentTime ) {
+                 // if plugin does not exist on this instance, remove it
+                 if ( !tracksByEnd[ tracks.endIndex ]._natives || !!that[ tracksByEnd[ tracks.endIndex ]._natives.type ] ) {
+                   if ( tracksByEnd[ tracks.endIndex ].start <= currentTime && tracksByEnd[ tracks.endIndex ]._running === false ) {
+                     tracksByEnd[ tracks.endIndex ]._running = true;
+                     tracksByEnd[ tracks.endIndex ]._natives.start.call( that, event, tracksByEnd[tracks.endIndex] );
+                   }
+                   tracks.endIndex--;
+                 } else {
+                   // remove track event
+                   Popcorn.removeTrackEvent( that, tracksByEnd[ tracks.endIndex ]._id );
+                   return;
                  }
-                 tracks.endIndex--;
                }
 -            } 
 +            }
              /*
              //  This empty block causes errors with jslint
 -            
 +
              else {
                // When user seeks, currentTime can be equal to previousTime on the
                // timeUpdate event. We are not doing anything with this right now, but we
      roundTime: function () {
        return -~this.video.currentTime;
      },
- 
- 
+     
      exec: function ( time, fn ) {
 -      
 +
        !fn && ( fn = Popcorn.nop );
- 
- 
-       var self = this,
-           guid = Popcorn.guid( "execCallback" ),
+       
+       var self = this, 
+           guid = Popcorn.guid( "execCallback" ), 
            callback = function execCallback( event ) {
  
-             if ( this.currentTime() >= time && !callback.fired ) {
- 
+             if ( Math.round( this.currentTime() ) === time && !callback.fired ) {
+             
                callback.fired = true;
- 
-               this.unlisten("timeupdate", guid );
- 
+               
                fn.call(self, event);
- 
+               
+               //  Enforce a once per second execution
+               setTimeout(function() {
+                 
+                 callback.fired = false;
+                 
+               }, 1000 );
              }
            };
 -      
 +
        callback.fired = false;
        callback.name = guid;
 -      
 +
        this.listen("timeupdate", callback);
 -      
 +
-       return this;
-     },
-     removePlugin: function( name ) {
- 
-       var byStart = this.data.trackEvents.byStart,
-           byEnd = this.data.trackEvents.byEnd,
-           idx, rl, sl;
- 
-       delete Popcorn.p[ name ];
- 
-       // remove plugin reference from registry
-       for ( idx = 0, rl = Popcorn.registry.length; idx < rl; idx++ ) {
-         if ( Popcorn.registry[ idx ].type === name ) {
-           Popcorn.registry.splice( idx, 1 );
-           break; // plugin found, stop checking
-         }
-       }
- 
-       // remove all trackEvents
-       for ( idx = 0, sl = byStart.length; idx < sl; idx++ ) {
- 
-         if ( ( byStart[ idx ] && byStart[ idx ]._natives && byStart[ idx ]._natives.type === name ) &&
-                   ( byEnd[ idx ] && byEnd[ idx ]._natives && byEnd[ idx ]._natives.type === name ) ) {
- 
-           byStart.splice( idx, 1 );
-           byEnd.splice( idx, 1 );
- 
-           // update for loop if something removed, but keep checking
-           idx--; sl--;
-           if ( this.data.trackEvents.startIndex <= idx ) {
-             this.data.trackEvents.startIndex--;
-             this.data.trackEvents.endIndex--;
-           }
-         }
-       }
        return this;
      }
    });
                }
  
              });
 -            
 +
              //fn.call( self, event );
 -          
 -          }, false);          
 +
 +          }, false);
          }
          return this;
 -      }, 
 +      },
        unlisten: function( type, fn ) {
 -        
 +
          if ( this.data.events[type] && this.data.events[type][fn] ) {
 -          
 +
            delete this.data.events[type][ fn ];
 -          
 +
            return this;
          }
 -      
 +
          this.data.events[type] = null;
-         return this;
-       },
-       special: {
-         // handles timeline controllers
-         play: function () {
-           //  renders all of the interally stored track commands
-         }
+         
+         return this;        
        }
      }
    };
  
    };
  
+   // removePlugin( type ) removes all tracks of that from all instances of popcorn
+   // removePlugin( obj, type ) removes all tracks of type from obj, where obj is a single instance of popcorn
+   Popcorn.removePlugin = function( obj, name ) {
+ 
+     // check if we are removing plugin from an instance or from all of Popcorn
+     if ( !name ) {
+ 
+       // all of Popcorn it is
+ 
+       // fix the order
+       name = obj;
+       obj = Popcorn.p;
+ 
+       var registryLength = Popcorn.registry.length,
+           registryIndex;
+ 
+       // remove plugin reference from registry
+       for ( registryIndex = 0; registryIndex < registryLength; registryIndex++ ) {
+         if ( Popcorn.registry[ registryIndex ].type === name ) {
+           Popcorn.registry.splice( registryIndex, 1 );
+ 
+           // delete the plugin
+           delete obj[ name ];
+ 
+           // plugin found and removed, stop checking, we are done
+           return;
+         }
+       }
+ 
+     }
+ 
+     var byStart = obj.data.trackEvents.byStart, 
+         byEnd = obj.data.trackEvents.byEnd, 
+         idx, sl;
+ 
+     // remove all trackEvents
+     for ( idx = 0, sl = byStart.length; idx < sl; idx++ ) {
+ 
+       if ( ( byStart[ idx ] && byStart[ idx ]._natives && byStart[ idx ]._natives.type === name ) && 
+                 ( byEnd[ idx ] && byEnd[ idx ]._natives && byEnd[ idx ]._natives.type === name ) ) {
+           
+         byStart.splice( idx, 1 );
+         byEnd.splice( idx, 1 );
+           
+         // update for loop if something removed, but keep checking
+         idx--; sl--;
+         if ( obj.data.trackEvents.startIndex <= idx ) {
+           obj.data.trackEvents.startIndex--;
+           obj.data.trackEvents.endIndex--;
+         }
+       }
+     }
+   };
+ 
    Popcorn.removeTrackEvent  = function( obj, trackId ) {
 -    var historyLen = obj.data.history.length, 
 -        indexWasAt = 0, 
 -        byStart = [], 
 -        byEnd = [], 
 -        history = []; 
 -    
 -    
 -    Popcorn.forEach( obj.data.trackEvents.byStart, function( o, i, context ) {
 -      
 +
 +    var historyLen = obj.data.history.length,
 +        indexWasAt = 0,
 +        byStart = [],
 +        byEnd = [],
 +        history = [];
 +
 +
 +    Popcorn.forEach( obj.data.trackEvents.byStart, function( o, i, context) {
- 
        // Preserve the original start/end trackEvents
        if ( !o._id ) {
          byStart.push( obj.data.trackEvents.byStart[i] );
      removeTrackEvent: function( id ) {
        Popcorn.removeTrackEvent.call( null, this, id );
        return this;
+     }, 
+     
+     removePlugin: function( name ) {
+       Popcorn.removePlugin.call( null, this, name );
+       return this;
      }
 -  
 +
    });
 -  
 +
    //  Plugin manifests
    Popcorn.manifest = {};
 -  //  Plugins are registered 
 +  //  Plugins are registered
    Popcorn.registry = [];
 -  //  An interface for extending Popcorn 
 +  //  An interface for extending Popcorn
    //  with plugin functionality
 -  Popcorn.plugin = function( name, definition ) {
 +  Popcorn.plugin = function( name, definition, manifest ) {
  
      if ( Popcorn.protect.natives.indexOf( name.toLowerCase() ) >= 0 ) {
        Popcorn.error("'" + name + "' is a protected function name");
      xhr: function()  {
        return new global.XMLHttpRequest();
      }
 -  };   
 -  
 +  };
 +
    Popcorn.xhr = function ( options ) {
  
-     if ( options.dataType && options.dataType.toLowerCase() === "jsonp" ) {
+     if ( options.dataType && 
+           ( options.dataType.toLowerCase() === "jsonp" || 
+               options.dataType.toLowerCase() === "script" ) ) {
  
 -      Popcorn.xhr.getJSONP( 
 +      Popcorn.xhr.getJSONP(
          options.url,
-         options.success
+         options.success, 
+         options.dataType.toLowerCase() === "script"
        );
        return;
      }
          if ( settings.dataType ) {
            data = data[ settings.dataType ];
          }
 -        
 +
  
          settings.success.call( settings.ajax, data );
 -        
 -      } 
 -    }; 
 -    return data;  
 +
 +      }
 +    };
 +    return data;
    };
 -  
 -  
+   
+   Popcorn.xhr.getJSONP = function ( url, success, isScript ) {
+     
+     //  If this is a script request, ensure that we do not call something that has already been loaded
+     if ( isScript ) {
+       
+       var scripts = document.querySelectorAll('script[src="' + url + '"]');
+       
+       //  If there are scripts with this url loaded, early return      
+       if ( scripts.length ) {
+       
+         //  Execute success callback and pass "exists" flag
+         success && success( true );
  
- 
- 
-   Popcorn.xhr.getJSONP = function ( url, success ) {
+         return;
+       }
+     }
 -    
 -  
 +
      var head = document.getElementsByTagName("head")[0] || document.documentElement,
 -      script = document.createElement("script"), 
 -      paramStr = url.split("?")[1], 
 -      fired = false, 
 -      params = [], 
 +      script = document.createElement("script"),
 +      paramStr = url.split("?")[1],
 +      fired = false,
 +      params = [],
        callback;
- 
-     if ( paramStr ) {
+     
+     
+     if ( paramStr && !isScript ) {
        params = paramStr.split("&");
      }
 -    
 +
- 
      callback = params.length ? params[ params.length - 1 ].split("=")[1] : "jsonp";
  
- 
-     if ( !paramStr ) {
+     if ( !paramStr && !isScript ) {
        url += "?callback=" + callback;
      }
 -    
 +
      script.src = url;
  
- 
-     if ( callback ) {
 -    
+     if ( callback && !isScript ) {
        //  define the jsonp success callback globally
        window[ callback ] = function ( data ) {
-         success( data );
+ 
+         success && success( data );
          fired = true;
+ 
        };
      }
  
  
          // cleanup in here
          delete window[ callback ];
+         
          head.removeChild( script );
        }
-     };
 -      
+     };  
  
      head.insertBefore( script, head.firstChild );
+     
+     return;
+   
+   };
+   
+   Popcorn.getJSONP = Popcorn.xhr.getJSONP;
+   
+   Popcorn.getScript = Popcorn.xhr.getScript = function( url, success ) {
  
 -
+     return Popcorn.xhr.getJSONP( url, success, true );
    };
  
  
index e039dff1b1d4d7558721711d60fc9ab80ca60d2e,1652bb03f94bce924d7ab4ff4ffda107f510a177..aaa082f87a5c487b073c70b2cf2b96952ab6b8f1
mode 100755,100644..100755