2014-11-25 18:52:42 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import {Injector, bind, OpaqueToken} from 'di/di';
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-11 11:36:05 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import {Type, FIELD, isBlank, isPresent, BaseException, assertionsEnabled, print} from 'facade/lang';
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-07 14:30:04 -08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import {DOM, Element} from 'facade/dom';
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-02 13:21:39 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import {Compiler, CompilerCache} from './compiler/compiler';
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-07 14:30:04 -08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import {ProtoView} from './compiler/view';
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-20 12:07:48 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import {Reflector, reflector} from 'reflection/reflection';
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-07 14:30:04 -08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import {Parser} from 'change_detection/parser/parser';
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import {Lexer} from 'change_detection/parser/lexer';
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import {ChangeDetector} from 'change_detection/change_detector';
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-19 15:52:01 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import {RecordRange} from 'change_detection/record_range';
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-07 14:30:04 -08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import {TemplateLoader} from './compiler/template_loader';
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-20 12:07:48 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import {DirectiveMetadataReader} from './compiler/directive_metadata_reader';
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-07 14:30:04 -08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import {AnnotatedType} from './compiler/annotated_type';
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-11 11:36:05 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import {List, ListWrapper} from 'facade/collection';
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import {PromiseWrapper} from 'facade/async';
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-10 10:48:17 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import {VmTurnZone} from 'core/zone/vm_turn_zone';
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import {LifeCycle} from 'core/life_cycle/life_cycle';
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-07 14:30:04 -08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								var _rootInjector: Injector;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// Contains everything that is safe to share between applications.
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-20 12:07:48 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								var _rootBindings = [
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-02 13:21:39 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  bind(Reflector).toValue(reflector), Compiler, CompilerCache, TemplateLoader, DirectiveMetadataReader, Parser, Lexer
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-20 12:07:48 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								];
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-07 14:30:04 -08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-25 18:52:42 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								export var appViewToken = new OpaqueToken('AppView');
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								export var appRecordRangeToken = new OpaqueToken('AppRecordRange');
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								export var appElementToken = new OpaqueToken('AppElement');
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								export var appComponentAnnotatedTypeToken = new OpaqueToken('AppComponentAnnotatedType');
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								export var appDocumentToken = new OpaqueToken('AppDocument');
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-07 14:30:04 -08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// Exported only for tests that need to overwrite default document binding.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								export function documentDependentBindings(appComponentType) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  return [
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-20 12:07:48 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      bind(appComponentAnnotatedTypeToken).toFactory((reader) => {
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-07 14:30:04 -08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        // TODO(rado): inspect annotation here and warn if there are bindings,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        // lightDomServices, and other component annotations that are skipped
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        // for bootstrapping components.
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-20 12:07:48 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return reader.annotatedType(appComponentType);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      }, [DirectiveMetadataReader]),
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-07 14:30:04 -08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      bind(appElementToken).toFactory((appComponentAnnotatedType, appDocument) => {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        var selector = appComponentAnnotatedType.annotation.selector;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        var element = DOM.querySelector(appDocument, selector);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if (isBlank(element)) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          throw new BaseException(`The app selector "${selector}" did not match any elements`);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return element;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      }, [appComponentAnnotatedTypeToken, appDocumentToken]),
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      bind(appViewToken).toAsyncFactory((compiler, injector, appElement,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            appComponentAnnotatedType) => {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return compiler.compile(appComponentAnnotatedType.type, null).then(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            (protoView) => {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          var appProtoView = ProtoView.createRootProtoView(protoView,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								              appElement, appComponentAnnotatedType);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          // The light Dom of the app element is not considered part of
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          // the angular application. Thus the context and lightDomInjector are
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          // empty.
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-09 10:31:19 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          var view = appProtoView.instantiate(null);
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-01 18:41:55 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          view.hydrate(injector, null, new Object());
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          return view;
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-07 14:30:04 -08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        });
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      }, [Compiler, Injector, appElementToken, appComponentAnnotatedTypeToken]),
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-25 18:52:42 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      bind(appRecordRangeToken).toFactory((rootView) => rootView.recordRange,
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-07 14:30:04 -08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          [appViewToken]),
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-25 18:52:42 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      bind(ChangeDetector).toFactory((appRecordRange) =>
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-04 18:30:54 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          new ChangeDetector(appRecordRange, assertionsEnabled()), [appRecordRangeToken]),
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-08 10:54:43 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      bind(appComponentType).toFactory((rootView) => rootView.elementInjectors[0].getComponent(),
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-10 10:48:17 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          [appViewToken]),
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      bind(LifeCycle).toClass(LifeCycle)
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-07 14:30:04 -08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  ];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								function _injectorBindings(appComponentType) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  return ListWrapper.concat([bind(appDocumentToken).toValue(DOM.defaultDoc())],
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      documentDependentBindings(appComponentType));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-11 11:36:05 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								function _createVmZone(givenReporter:Function){
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  var defaultErrorReporter = (exception, stackTrace) => {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    var longStackTrace = ListWrapper.join(stackTrace, "\n\n-----async gap-----\n");
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    print(`${exception}\n\n${longStackTrace}`);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    throw exception;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  };
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  var reporter = isPresent(givenReporter) ? givenReporter : defaultErrorReporter;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  var zone = new VmTurnZone({enableLongStackTrace: assertionsEnabled()});
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  zone.initCallbacks({onErrorHandler: reporter});
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  return zone;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-07 14:30:04 -08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// Multiple calls to this method are allowed. Each application would only share
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// _rootInjector, which is not user-configurable by design, thus safe to share.
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-11 11:36:05 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								export function bootstrap(appComponentType: Type, bindings=null, givenBootstrapErrorReporter=null) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  var bootstrapProcess = PromiseWrapper.completer();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  var zone = _createVmZone(givenBootstrapErrorReporter);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  zone.run(() => {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    // TODO(rado): prepopulate template cache, so applications with only
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    // index.html and main.js are possible.
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-05 18:30:45 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-10 10:48:17 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if (isBlank(_rootInjector)) _rootInjector = new Injector(_rootBindings);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    var appInjector = _rootInjector.createChild(_injectorBindings(appComponentType));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if (isPresent(bindings)) appInjector = appInjector.createChild(bindings);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-11 11:36:05 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    PromiseWrapper.then(appInjector.asyncGet(LifeCycle),
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      (lc) => {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        lc.registerWith(zone);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        lc.tick(); //the first tick that will bootstrap the app
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        bootstrapProcess.complete(appInjector);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      },
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      (err) => {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        bootstrapProcess.reject(err)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      });
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-07 14:30:04 -08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  });
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-11 11:36:05 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  return bootstrapProcess.promise;
							 | 
						
					
						
							
								
									
										
										
										
											2014-11-07 14:30:04 -08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 |