Blame view

bower_components/jquery/src/core/ready.js 2.33 KB
c5169e0e   Renato De Donato   a new hope
1
  define([
74249687   Luigi Serra   Cross browser con...
2
  	"../core",
74249687   Luigi Serra   Cross browser con...
3
4
  	"../core/init",
  	"../deferred"
c5169e0e   Renato De Donato   a new hope
5
  ], function( jQuery ) {
74249687   Luigi Serra   Cross browser con...
6
7
8
9
10
  
  // The deferred used on DOM ready
  var readyList;
  
  jQuery.fn.ready = function( fn ) {
74249687   Luigi Serra   Cross browser con...
11
12
13
14
15
16
  	// Add the callback
  	jQuery.ready.promise().done( fn );
  
  	return this;
  };
  
c5169e0e   Renato De Donato   a new hope
17
  jQuery.extend({
74249687   Luigi Serra   Cross browser con...
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  	// Is the DOM ready to be used? Set to true once it occurs.
  	isReady: false,
  
  	// A counter to track how many items to wait for before
  	// the ready event fires. See #6781
  	readyWait: 1,
  
  	// Hold (or release) the ready event
  	holdReady: function( hold ) {
  		if ( hold ) {
  			jQuery.readyWait++;
  		} else {
  			jQuery.ready( true );
  		}
  	},
  
  	// Handle when the DOM is ready
  	ready: function( wait ) {
  
  		// Abort if there are pending holds or we're already ready
  		if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) {
  			return;
  		}
  
  		// Remember that the DOM is ready
  		jQuery.isReady = true;
  
  		// If a normal DOM Ready event fired, decrement, and wait if need be
  		if ( wait !== true && --jQuery.readyWait > 0 ) {
  			return;
  		}
  
  		// If there are functions bound, to execute
  		readyList.resolveWith( document, [ jQuery ] );
  
  		// Trigger any bound ready events
  		if ( jQuery.fn.triggerHandler ) {
  			jQuery( document ).triggerHandler( "ready" );
  			jQuery( document ).off( "ready" );
  		}
  	}
c5169e0e   Renato De Donato   a new hope
59
  });
74249687   Luigi Serra   Cross browser con...
60
61
62
63
64
  
  /**
   * The ready event handler and self cleanup method
   */
  function completed() {
c5169e0e   Renato De Donato   a new hope
65
66
  	document.removeEventListener( "DOMContentLoaded", completed, false );
  	window.removeEventListener( "load", completed, false );
74249687   Luigi Serra   Cross browser con...
67
68
69
70
71
72
73
74
  	jQuery.ready();
  }
  
  jQuery.ready.promise = function( obj ) {
  	if ( !readyList ) {
  
  		readyList = jQuery.Deferred();
  
c5169e0e   Renato De Donato   a new hope
75
76
77
78
  		// Catch cases where $(document).ready() is called after the browser event has already occurred.
  		// We once tried to use readyState "interactive" here, but it caused issues like the one
  		// discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15
  		if ( document.readyState === "complete" ) {
74249687   Luigi Serra   Cross browser con...
79
  			// Handle it asynchronously to allow scripts the opportunity to delay ready
c5169e0e   Renato De Donato   a new hope
80
  			setTimeout( jQuery.ready );
74249687   Luigi Serra   Cross browser con...
81
82
83
84
  
  		} else {
  
  			// Use the handy event callback
c5169e0e   Renato De Donato   a new hope
85
  			document.addEventListener( "DOMContentLoaded", completed, false );
74249687   Luigi Serra   Cross browser con...
86
87
  
  			// A fallback to window.onload, that will always work
c5169e0e   Renato De Donato   a new hope
88
  			window.addEventListener( "load", completed, false );
74249687   Luigi Serra   Cross browser con...
89
90
91
92
93
94
95
96
  		}
  	}
  	return readyList.promise( obj );
  };
  
  // Kick off the DOM ready check even if the user does not
  jQuery.ready.promise();
  
c5169e0e   Renato De Donato   a new hope
97
  });