src/js/iframe_embed/embedder.js
branchnew-model
changeset 882 61c384dda19e
parent 809 1997dae651c6
child 906 4b6e154ae8de
equal deleted inserted replaced
881:f11b234497f7 882:61c384dda19e
     1 /* This piece of code is directly requested by the page the player is embedded
     1 /* This piece of code is directly requested by the page the player is embedded
     2    on. It creates the iframe the player is embedded in and it reflects changes
     2    on. It creates the iframe the player is embedded in and it reflects changes
     3    to the iframe url in the page url.   
     3    to the iframe url in the page url.   
     4 */
     4 */
     5 
     5 
     6 IriSP = {};
     6 (function(_frameId) {
       
     7     var _frame = document.getElementById(_frameId);
       
     8     
       
     9     window.onhashchange = function() {
       
    10         frame.contentWindow.postMessage({type: "hashchange", hash: hashvalue}, "*");
       
    11     };
     7 
    12 
     8 window.onhashchange = function() {
    13     window.addEventListener('message', function(_e) {
     9   var url = window.location.href;
    14         if (e.data.type === "hashchange") {
    10   var frame = document.getElementById("metadataplayer_embed");
    15             document.location.hash = e.data.hash;
    11 
       
    12   if ( url.split( "#" )[ 1 ] != null ) {
       
    13        hashvalue = url.split("#")[1];
       
    14        frame.contentWindow.postMessage({type: "hashchange", value: hashvalue}, "*");
       
    15   }    
       
    16 };
       
    17 
       
    18 
       
    19 IriSP.handleMessages = function(e) {
       
    20   var history = window.history;
       
    21   
       
    22   if ( !history.pushState ) {
       
    23     return false;
       
    24   }
       
    25 
       
    26   if (e.data.type === "hashchange") {
       
    27     console.log(e.data.value);
       
    28     history.replaceState( {}, "", e.data.value);
       
    29   }
       
    30 };
       
    31 
       
    32 // http://stackoverflow.com/questions/799981/document-ready-equivalent-without-jquery
       
    33 var ready = (function(){    
       
    34 
       
    35     var readyList,
       
    36         DOMContentLoaded,
       
    37         class2type = {};
       
    38         class2type["[object Boolean]"] = "boolean";
       
    39         class2type["[object Number]"] = "number";
       
    40         class2type["[object String]"] = "string";
       
    41         class2type["[object Function]"] = "function";
       
    42         class2type["[object Array]"] = "array";
       
    43         class2type["[object Date]"] = "date";
       
    44         class2type["[object RegExp]"] = "regexp";
       
    45         class2type["[object Object]"] = "object";
       
    46 
       
    47     var ReadyObj = {
       
    48         // Is the DOM ready to be used? Set to true once it occurs.
       
    49         isReady: false,
       
    50         // A counter to track how many items to wait for before
       
    51         // the ready event fires. See #6781
       
    52         readyWait: 1,
       
    53         // Hold (or release) the ready event
       
    54         holdReady: function( hold ) {
       
    55             if ( hold ) {
       
    56                 ReadyObj.readyWait++;
       
    57             } else {
       
    58                 ReadyObj.ready( true );
       
    59             }
       
    60         },
       
    61         // Handle when the DOM is ready
       
    62         ready: function( wait ) {
       
    63             // Either a released hold or an DOMready/load event and not yet ready
       
    64             if ( (wait === true && !--ReadyObj.readyWait) || (wait !== true && !ReadyObj.isReady) ) {
       
    65                 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
       
    66                 if ( !document.body ) {
       
    67                     return setTimeout( ReadyObj.ready, 1 );
       
    68                 }
       
    69 
       
    70                 // Remember that the DOM is ready
       
    71                 ReadyObj.isReady = true;
       
    72                 // If a normal DOM Ready event fired, decrement, and wait if need be
       
    73                 if ( wait !== true && --ReadyObj.readyWait > 0 ) {
       
    74                     return;
       
    75                 }
       
    76                 // If there are functions bound, to execute
       
    77                 readyList.resolveWith( document, [ ReadyObj ] );
       
    78 
       
    79                 // Trigger any bound ready events
       
    80                 //if ( ReadyObj.fn.trigger ) {
       
    81                 //  ReadyObj( document ).trigger( "ready" ).unbind( "ready" );
       
    82                 //}
       
    83             }
       
    84         },
       
    85         bindReady: function() {
       
    86             if ( readyList ) {
       
    87                 return;
       
    88             }
       
    89             readyList = ReadyObj._Deferred();
       
    90 
       
    91             // Catch cases where $(document).ready() is called after the
       
    92             // browser event has already occurred.
       
    93             if ( document.readyState === "complete" ) {
       
    94                 // Handle it asynchronously to allow scripts the opportunity to delay ready
       
    95                 return setTimeout( ReadyObj.ready, 1 );
       
    96             }
       
    97 
       
    98             // Mozilla, Opera and webkit nightlies currently support this event
       
    99             if ( document.addEventListener ) {
       
   100                 // Use the handy event callback
       
   101                 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
       
   102                 // A fallback to window.onload, that will always work
       
   103                 window.addEventListener( "load", ReadyObj.ready, false );
       
   104 
       
   105             // If IE event model is used
       
   106             } else if ( document.attachEvent ) {
       
   107                 // ensure firing before onload,
       
   108                 // maybe late but safe also for iframes
       
   109                 document.attachEvent( "onreadystatechange", DOMContentLoaded );
       
   110 
       
   111                 // A fallback to window.onload, that will always work
       
   112                 window.attachEvent( "onload", ReadyObj.ready );
       
   113 
       
   114                 // If IE and not a frame
       
   115                 // continually check to see if the document is ready
       
   116                 var toplevel = false;
       
   117 
       
   118                 try {
       
   119                     toplevel = window.frameElement == null;
       
   120                 } catch(e) {}
       
   121 
       
   122                 if ( document.documentElement.doScroll && toplevel ) {
       
   123                     doScrollCheck();
       
   124                 }
       
   125             }
       
   126         },
       
   127         _Deferred: function() {
       
   128             var // callbacks list
       
   129                 callbacks = [],
       
   130                 // stored [ context , args ]
       
   131                 fired,
       
   132                 // to avoid firing when already doing so
       
   133                 firing,
       
   134                 // flag to know if the deferred has been cancelled
       
   135                 cancelled,
       
   136                 // the deferred itself
       
   137                 deferred  = {
       
   138 
       
   139                     // done( f1, f2, ...)
       
   140                     done: function() {
       
   141                         if ( !cancelled ) {
       
   142                             var args = arguments,
       
   143                                 i,
       
   144                                 length,
       
   145                                 elem,
       
   146                                 type,
       
   147                                 _fired;
       
   148                             if ( fired ) {
       
   149                                 _fired = fired;
       
   150                                 fired = 0;
       
   151                             }
       
   152                             for ( i = 0, length = args.length; i < length; i++ ) {
       
   153                                 elem = args[ i ];
       
   154                                 type = ReadyObj.type( elem );
       
   155                                 if ( type === "array" ) {
       
   156                                     deferred.done.apply( deferred, elem );
       
   157                                 } else if ( type === "function" ) {
       
   158                                     callbacks.push( elem );
       
   159                                 }
       
   160                             }
       
   161                             if ( _fired ) {
       
   162                                 deferred.resolveWith( _fired[ 0 ], _fired[ 1 ] );
       
   163                             }
       
   164                         }
       
   165                         return this;
       
   166                     },
       
   167 
       
   168                     // resolve with given context and args
       
   169                     resolveWith: function( context, args ) {
       
   170                         if ( !cancelled && !fired && !firing ) {
       
   171                             // make sure args are available (#8421)
       
   172                             args = args || [];
       
   173                             firing = 1;
       
   174                             try {
       
   175                                 while( callbacks[ 0 ] ) {
       
   176                                     callbacks.shift().apply( context, args );//shifts a callback, and applies it to document
       
   177                                 }
       
   178                             }
       
   179                             finally {
       
   180                                 fired = [ context, args ];
       
   181                                 firing = 0;
       
   182                             }
       
   183                         }
       
   184                         return this;
       
   185                     },
       
   186 
       
   187                     // resolve with this as context and given arguments
       
   188                     resolve: function() {
       
   189                         deferred.resolveWith( this, arguments );
       
   190                         return this;
       
   191                     },
       
   192 
       
   193                     // Has this deferred been resolved?
       
   194                     isResolved: function() {
       
   195                         return !!( firing || fired );
       
   196                     },
       
   197 
       
   198                     // Cancel
       
   199                     cancel: function() {
       
   200                         cancelled = 1;
       
   201                         callbacks = [];
       
   202                         return this;
       
   203                     }
       
   204                 };
       
   205 
       
   206             return deferred;
       
   207         },
       
   208         type: function( obj ) {
       
   209             return obj == null ?
       
   210                 String( obj ) :
       
   211                 class2type[ Object.prototype.toString.call(obj) ] || "object";
       
   212         }
    16         }
   213     }
    17     });
   214     // The DOM ready check for Internet Explorer
    18     
   215     function doScrollCheck() {
    19 })("metadataplayer_embed");
   216         if ( ReadyObj.isReady ) {
       
   217             return;
       
   218         }
       
   219 
       
   220         try {
       
   221             // If IE is used, use the trick by Diego Perini
       
   222             // http://javascript.nwbox.com/IEContentLoaded/
       
   223             document.documentElement.doScroll("left");
       
   224         } catch(e) {
       
   225             setTimeout( doScrollCheck, 1 );
       
   226             return;
       
   227         }
       
   228 
       
   229         // and execute any waiting functions
       
   230         ReadyObj.ready();
       
   231     }
       
   232     // Cleanup functions for the document ready method
       
   233     if ( document.addEventListener ) {
       
   234         DOMContentLoaded = function() {
       
   235             document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
       
   236             ReadyObj.ready();
       
   237         };
       
   238 
       
   239     } else if ( document.attachEvent ) {
       
   240         DOMContentLoaded = function() {
       
   241             // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
       
   242             if ( document.readyState === "complete" ) {
       
   243                 document.detachEvent( "onreadystatechange", DOMContentLoaded );
       
   244                 ReadyObj.ready();
       
   245             }
       
   246         };
       
   247     }
       
   248     function ready( fn ) {
       
   249         // Attach the listeners
       
   250         ReadyObj.bindReady();
       
   251 
       
   252         var type = ReadyObj.type( fn );
       
   253 
       
   254         // Add the callback
       
   255         readyList.done( fn );//readyList is result of _Deferred()
       
   256     }
       
   257     return ready;
       
   258 })();
       
   259 
       
   260 ready(function() { window.addEventListener('message', IriSP.handleMessages, false); });