2015-02-05 13:08:05 -08:00
|
|
|
import {Injector, bind, OpaqueToken} from 'angular2/di';
|
|
|
|
import {Type, FIELD, isBlank, isPresent, BaseException, assertionsEnabled, print} from 'angular2/src/facade/lang';
|
2015-02-27 14:50:06 -08:00
|
|
|
import {BrowserDomAdapter} from 'angular2/src/dom/browser_adapter';
|
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
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';
|
2015-02-05 13:08:05 -08:00
|
|
|
import {Reflector, reflector} from 'angular2/src/reflection/reflection';
|
|
|
|
import {Parser, Lexer, ChangeDetection, dynamicChangeDetection, jitChangeDetection} from 'angular2/change_detection';
|
2015-02-16 14:35:27 +01:00
|
|
|
import {ExceptionHandler} from './exception_handler';
|
2014-11-07 14:30:04 -08:00
|
|
|
import {TemplateLoader} from './compiler/template_loader';
|
2015-02-12 14:44:59 +01:00
|
|
|
import {TemplateResolver} from './compiler/template_resolver';
|
2014-11-20 12:07:48 -08:00
|
|
|
import {DirectiveMetadataReader} from './compiler/directive_metadata_reader';
|
2015-02-05 13:08:05 -08:00
|
|
|
import {List, ListWrapper} from 'angular2/src/facade/collection';
|
2015-02-11 21:04:52 -08:00
|
|
|
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
|
2015-02-05 13:08:05 -08:00
|
|
|
import {VmTurnZone} from 'angular2/src/core/zone/vm_turn_zone';
|
|
|
|
import {LifeCycle} from 'angular2/src/core/life_cycle/life_cycle';
|
2015-03-10 12:30:50 -07:00
|
|
|
import {ShadowDomStrategy, NativeShadowDomStrategy, EmulatedUnscopedShadowDomStrategy} from 'angular2/src/core/compiler/shadow_dom_strategy';
|
2015-01-30 09:43:21 +01:00
|
|
|
import {XHR} from 'angular2/src/core/compiler/xhr/xhr';
|
|
|
|
import {XHRImpl} from 'angular2/src/core/compiler/xhr/xhr_impl';
|
2015-02-19 19:10:16 -08:00
|
|
|
import {EventManager, DomEventsPlugin} from 'angular2/src/core/events/event_manager';
|
2015-02-09 15:11:31 +01:00
|
|
|
import {HammerGesturesPlugin} from 'angular2/src/core/events/hammer_gestures';
|
2015-02-11 21:04:52 -08:00
|
|
|
import {Binding} from 'angular2/src/di/binding';
|
2015-02-24 16:05:45 +01:00
|
|
|
import {ComponentUrlMapper} from 'angular2/src/core/compiler/component_url_mapper';
|
|
|
|
import {UrlResolver} from 'angular2/src/core/compiler/url_resolver';
|
|
|
|
import {StyleUrlResolver} from 'angular2/src/core/compiler/style_url_resolver';
|
|
|
|
import {StyleInliner} from 'angular2/src/core/compiler/style_inliner';
|
2015-03-02 15:02:48 +01:00
|
|
|
import {CssProcessor} from 'angular2/src/core/compiler/css_processor';
|
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 = [
|
2015-02-16 14:35:27 +01:00
|
|
|
bind(Reflector).toValue(reflector)
|
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');
|
2015-01-14 13:51:16 -08:00
|
|
|
export var appChangeDetectorToken = new OpaqueToken('AppChangeDetector');
|
2014-11-25 18:52:42 -08:00
|
|
|
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
|
|
|
|
2015-02-11 21:04:52 -08:00
|
|
|
function _injectorBindings(appComponentType): List<Binding> {
|
2014-11-07 14:30:04 -08:00
|
|
|
return [
|
2015-01-08 09:11:33 -08:00
|
|
|
bind(appDocumentToken).toValue(DOM.defaultDoc()),
|
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-12-23 10:45:20 -08:00
|
|
|
return reader.read(appComponentType);
|
2014-11-20 12:07:48 -08:00
|
|
|
}, [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]),
|
|
|
|
|
2015-01-21 12:05:52 -08:00
|
|
|
bind(appViewToken).toAsyncFactory((changeDetection, compiler, injector, appElement,
|
2015-03-06 18:12:48 +01:00
|
|
|
appComponentAnnotatedType, strategy, eventManager, reflector) => {
|
2015-02-12 14:44:59 +01:00
|
|
|
return compiler.compile(appComponentAnnotatedType.type).then(
|
2014-11-07 14:30:04 -08:00
|
|
|
(protoView) => {
|
2015-01-30 09:43:21 +01:00
|
|
|
var appProtoView = ProtoView.createRootProtoView(protoView, appElement,
|
|
|
|
appComponentAnnotatedType, changeDetection.createProtoChangeDetector('root'),
|
|
|
|
strategy);
|
2014-11-07 14:30:04 -08:00
|
|
|
// The light Dom of the app element is not considered part of
|
|
|
|
// the angular application. Thus the context and lightDomInjector are
|
|
|
|
// empty.
|
2015-03-06 18:12:48 +01:00
|
|
|
var view = appProtoView.instantiate(null, eventManager, reflector);
|
2015-01-21 12:05:52 -08:00
|
|
|
view.hydrate(injector, null, new Object());
|
2014-12-01 18:41:55 -08:00
|
|
|
return view;
|
2014-11-07 14:30:04 -08:00
|
|
|
});
|
2015-01-30 09:43:21 +01:00
|
|
|
}, [ChangeDetection, Compiler, Injector, appElementToken, appComponentAnnotatedTypeToken,
|
2015-03-06 18:12:48 +01:00
|
|
|
ShadowDomStrategy, EventManager, Reflector]),
|
2014-11-07 14:30:04 -08:00
|
|
|
|
2015-01-14 13:51:16 -08:00
|
|
|
bind(appChangeDetectorToken).toFactory((rootView) => rootView.changeDetector,
|
2014-11-07 14:30:04 -08:00
|
|
|
[appViewToken]),
|
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]),
|
2015-02-16 14:55:00 +01:00
|
|
|
bind(LifeCycle).toFactory((exceptionHandler) => new LifeCycle(exceptionHandler, null, assertionsEnabled()),[ExceptionHandler]),
|
2015-02-09 15:11:31 +01:00
|
|
|
bind(EventManager).toFactory((zone) => {
|
2015-02-19 19:10:16 -08:00
|
|
|
var plugins = [new HammerGesturesPlugin(), new DomEventsPlugin()];
|
2015-02-09 15:11:31 +01:00
|
|
|
return new EventManager(plugins, zone);
|
|
|
|
}, [VmTurnZone]),
|
2015-03-10 12:30:50 -07:00
|
|
|
bind(ShadowDomStrategy).toFactory(
|
|
|
|
(styleUrlResolver, doc) => new EmulatedUnscopedShadowDomStrategy(styleUrlResolver, doc.head),
|
|
|
|
[StyleUrlResolver, appDocumentToken]),
|
2015-02-16 10:21:54 +01:00
|
|
|
Compiler,
|
|
|
|
CompilerCache,
|
|
|
|
TemplateResolver,
|
|
|
|
bind(ChangeDetection).toValue(dynamicChangeDetection),
|
|
|
|
TemplateLoader,
|
|
|
|
DirectiveMetadataReader,
|
|
|
|
Parser,
|
|
|
|
Lexer,
|
2015-02-16 14:35:27 +01:00
|
|
|
ExceptionHandler,
|
2015-02-16 10:21:54 +01:00
|
|
|
bind(XHR).toValue(new XHRImpl()),
|
2015-02-24 16:05:45 +01:00
|
|
|
ComponentUrlMapper,
|
|
|
|
UrlResolver,
|
|
|
|
StyleUrlResolver,
|
|
|
|
StyleInliner,
|
2015-03-03 11:32:19 +01:00
|
|
|
bind(CssProcessor).toFactory(() => new CssProcessor(null), []),
|
2014-11-07 14:30:04 -08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-02-11 21:04:52 -08:00
|
|
|
function _createVmZone(givenReporter:Function): VmTurnZone {
|
2014-12-11 11:36:05 -08:00
|
|
|
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.
|
2015-02-11 21:04:52 -08:00
|
|
|
export function bootstrap(appComponentType: Type, bindings: List<Binding>=null, givenBootstrapErrorReporter: Function=null): Promise {
|
2015-02-27 14:50:06 -08:00
|
|
|
BrowserDomAdapter.makeCurrent();
|
2014-12-11 11:36:05 -08:00
|
|
|
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
|
|
|
|
2015-02-09 15:11:31 +01:00
|
|
|
var appInjector = _createAppInjector(appComponentType, bindings, zone);
|
2014-12-10 10:48:17 -08:00
|
|
|
|
2015-02-03 17:05:29 -08:00
|
|
|
PromiseWrapper.then(appInjector.asyncGet(appViewToken),
|
|
|
|
(rootView) => {
|
|
|
|
// retrieve life cycle: may have already been created if injected in root component
|
2015-02-09 15:11:31 +01:00
|
|
|
var lc=appInjector.get(LifeCycle);
|
2015-02-03 17:05:29 -08:00
|
|
|
lc.registerWith(zone, rootView.changeDetector);
|
2014-12-11 11:36:05 -08:00
|
|
|
lc.tick(); //the first tick that will bootstrap the app
|
|
|
|
|
2015-02-25 12:46:50 +01:00
|
|
|
bootstrapProcess.resolve(appInjector);
|
2014-12-11 11:36:05 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
(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
|
|
|
}
|
2015-01-08 09:11:33 -08:00
|
|
|
|
2015-02-11 21:04:52 -08:00
|
|
|
function _createAppInjector(appComponentType: Type, bindings: List<Binding>, zone: VmTurnZone): Injector {
|
2015-01-08 09:11:33 -08:00
|
|
|
if (isBlank(_rootInjector)) _rootInjector = new Injector(_rootBindings);
|
|
|
|
var mergedBindings = isPresent(bindings) ?
|
|
|
|
ListWrapper.concat(_injectorBindings(appComponentType), bindings) :
|
|
|
|
_injectorBindings(appComponentType);
|
2015-02-09 15:11:31 +01:00
|
|
|
ListWrapper.push(mergedBindings, bind(VmTurnZone).toValue(zone));
|
2015-01-08 09:11:33 -08:00
|
|
|
return _rootInjector.createChild(mergedBindings);
|
|
|
|
}
|