|
| 1 | +import {Injector, bind} from 'di/di'; |
| 2 | +import {Type, FIELD, isBlank, isPresent, BaseException} from 'facade/lang'; |
| 3 | +import {DOM, Element} from 'facade/dom'; |
| 4 | +import {Compiler} from './compiler/compiler'; |
| 5 | +import {ProtoView} from './compiler/view'; |
| 6 | +import {ClosureMap} from 'change_detection/parser/closure_map'; |
| 7 | +import {Parser} from 'change_detection/parser/parser'; |
| 8 | +import {Lexer} from 'change_detection/parser/lexer'; |
| 9 | +import {ChangeDetector} from 'change_detection/change_detector'; |
| 10 | +import {WatchGroup} from 'change_detection/watch_group'; |
| 11 | +import {TemplateLoader} from './compiler/template_loader'; |
| 12 | +import {Reflector} from './compiler/reflector'; |
| 13 | +import {AnnotatedType} from './compiler/annotated_type'; |
| 14 | +import {ListWrapper} from 'facade/collection'; |
| 15 | + |
| 16 | +var _rootInjector: Injector; |
| 17 | + |
| 18 | +// Contains everything that is safe to share between applications. |
| 19 | +var _rootBindings = [Compiler, TemplateLoader, Reflector, Parser, Lexer, ClosureMap]; |
| 20 | + |
| 21 | +export var appViewToken = new Object(); |
| 22 | +export var appWatchGroupToken = new Object(); |
| 23 | +export var appElementToken = new Object(); |
| 24 | +export var appComponentAnnotatedTypeToken = new Object(); |
| 25 | +export var appDocumentToken = new Object(); |
| 26 | + |
| 27 | +// Exported only for tests that need to overwrite default document binding. |
| 28 | +export function documentDependentBindings(appComponentType) { |
| 29 | + return [ |
| 30 | + bind(appComponentAnnotatedTypeToken).toFactory((reflector) => { |
| 31 | + // TODO(rado): inspect annotation here and warn if there are bindings, |
| 32 | + // lightDomServices, and other component annotations that are skipped |
| 33 | + // for bootstrapping components. |
| 34 | + return reflector.annotatedType(appComponentType); |
| 35 | + }, [Reflector]), |
| 36 | + |
| 37 | + bind(appElementToken).toFactory((appComponentAnnotatedType, appDocument) => { |
| 38 | + var selector = appComponentAnnotatedType.annotation.selector; |
| 39 | + var element = DOM.querySelector(appDocument, selector); |
| 40 | + if (isBlank(element)) { |
| 41 | + throw new BaseException(`The app selector "${selector}" did not match any elements`); |
| 42 | + } |
| 43 | + return element; |
| 44 | + }, [appComponentAnnotatedTypeToken, appDocumentToken]), |
| 45 | + |
| 46 | + bind(appViewToken).toAsyncFactory((compiler, injector, appElement, |
| 47 | + appComponentAnnotatedType) => { |
| 48 | + return compiler.compile(appComponentAnnotatedType.type, null).then( |
| 49 | + (protoView) => { |
| 50 | + var appProtoView = ProtoView.createRootProtoView(protoView, |
| 51 | + appElement, appComponentAnnotatedType); |
| 52 | + // The light Dom of the app element is not considered part of |
| 53 | + // the angular application. Thus the context and lightDomInjector are |
| 54 | + // empty. |
| 55 | + return appProtoView.instantiate(new Object(), injector, null, true); |
| 56 | + }); |
| 57 | + }, [Compiler, Injector, appElementToken, appComponentAnnotatedTypeToken]), |
| 58 | + |
| 59 | + bind(appWatchGroupToken).toFactory((rootView) => rootView.watchGroup, |
| 60 | + [appViewToken]), |
| 61 | + bind(ChangeDetector).toFactory((appWatchGroup) => |
| 62 | + new ChangeDetector(appWatchGroup), [appWatchGroupToken]) |
| 63 | + ]; |
| 64 | +} |
| 65 | + |
| 66 | +function _injectorBindings(appComponentType) { |
| 67 | + return ListWrapper.concat([bind(appDocumentToken).toValue(DOM.defaultDoc())], |
| 68 | + documentDependentBindings(appComponentType)); |
| 69 | +} |
| 70 | + |
| 71 | +// Multiple calls to this method are allowed. Each application would only share |
| 72 | +// _rootInjector, which is not user-configurable by design, thus safe to share. |
| 73 | +export function bootstrap(appComponentType: Type, bindings=null) { |
| 74 | + // TODO(rado): prepopulate template cache, so applications with only |
| 75 | + // index.html and main.js are possible. |
| 76 | + if (isBlank(_rootInjector)) _rootInjector = new Injector(_rootBindings); |
| 77 | + var appInjector = _rootInjector.createChild(_injectorBindings( |
| 78 | + appComponentType)); |
| 79 | + if (isPresent(bindings)) appInjector = appInjector.createChild(bindings); |
| 80 | + return appInjector.asyncGet(ChangeDetector).then((cd) => { |
| 81 | + // TODO(rado): replace with zone. |
| 82 | + cd.detectChanges(); |
| 83 | + return appInjector; |
| 84 | + }); |
| 85 | +} |
0 commit comments