1 // ==========================================================================
  2 // Project:   SproutCore - JavaScript Application Framework
  3 // Copyright: ©2006-2011 Strobe Inc. and contributors.
  4 //            Portions ©2008-2011 Apple Inc. All rights reserved.
  5 // License:   Licensed under MIT license (see license.js)
  6 // ==========================================================================
  7 
  8 
  9 SC.BENCHMARK_LOG_READY = YES;
 10 
 11 sc_require('system/event');
 12 
 13 SC.mixin({
 14   isReady: NO,
 15 
 16   /**
 17     Allows apps to avoid automatically attach the ready handlers if they
 18     want to by setting this flag to YES
 19 
 20     @type Boolean
 21   */
 22   suppressOnReady: SC.suppressOnReady ? YES : NO,
 23 
 24   /**
 25     Allows apps to avoid automatically invoking main() when onReady is called
 26 
 27     @type Boolean
 28   */
 29   suppressMain: SC.suppressMain ? YES : NO,
 30 
 31   /**
 32     Add the passed target and method to the queue of methods to invoke when
 33     the document is ready.  These methods will be called after the document
 34     has loaded and parsed, but before the main() function is called.
 35 
 36     Methods are called in the order they are added.
 37 
 38     If you add a ready handler when the main document is already ready, then
 39     your handler will be called immediately.
 40 
 41     @param target {Object} optional target object
 42     @param method {Function} method name or function to execute
 43     @returns {SC}
 44   */
 45   ready: function (target, method) {
 46     var queue = SC._readyQueue;
 47 
 48     // normalize
 49     if (method === undefined) {
 50       method = target;
 51       target = null;
 52     } else if (SC.typeOf(method) === SC.T_STRING) {
 53       method = target[method];
 54     }
 55 
 56     if (SC.isReady) {
 57       jQuery(document).ready(function () { method.call(target); });
 58     } else {
 59       if (!queue) SC._readyQueue = [];
 60       SC._readyQueue.push(function () { method.call(target); });
 61     }
 62 
 63     return this;
 64   },
 65 
 66   onReady: {
 67     done: function () {
 68       if (SC.isReady) return;
 69 
 70       SC.isReady = true;
 71 
 72       SC.RunLoop.begin();
 73 
 74       SC.Locale.createCurrentLocale();
 75       var loc = SC.Locale.currentLanguage.toLowerCase();
 76       jQuery("body").addClass(loc);
 77 
 78       jQuery("html").attr("lang", loc);
 79 
 80       jQuery("#loading").remove();
 81 
 82       var queue = SC._readyQueue, idx, len;
 83 
 84       if (queue) {
 85         for (idx = 0, len = queue.length; idx < len; idx++) {
 86           queue[idx].call();
 87         }
 88         SC._readyQueue = null;
 89       }
 90 
 91       if (window.main && !SC.suppressMain && (SC.mode === SC.APP_MODE)) { window.main(); }
 92       SC.RunLoop.end();
 93     }
 94   }
 95 
 96 });
 97 
 98 // let apps ignore the regular onReady handling if they need to
 99 if (!SC.suppressOnReady) {
100   $(document).ready(SC.onReady.done);
101 }
102 
103 // default to app mode.  When loading unit tests, this will run in test mode
104 SC.APP_MODE = "APP_MODE";
105 SC.TEST_MODE = "TEST_MODE";
106 SC.mode = SC.APP_MODE;
107