upgrade.dev.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. "format register";
  2. System.register("angular2/src/upgrade/metadata", ["angular2/core"], true, function(require, exports, module) {
  3. var global = System.global,
  4. __define = global.define;
  5. global.define = undefined;
  6. var core_1 = require("angular2/core");
  7. var COMPONENT_SELECTOR = /^[\w|-]*$/;
  8. var SKEWER_CASE = /-(\w)/g;
  9. var directiveResolver = new core_1.DirectiveResolver();
  10. function getComponentInfo(type) {
  11. var resolvedMetadata = directiveResolver.resolve(type);
  12. var selector = resolvedMetadata.selector;
  13. if (!selector.match(COMPONENT_SELECTOR)) {
  14. throw new Error('Only selectors matching element names are supported, got: ' + selector);
  15. }
  16. var selector = selector.replace(SKEWER_CASE, function(all, letter) {
  17. return letter.toUpperCase();
  18. });
  19. return {
  20. type: type,
  21. selector: selector,
  22. inputs: parseFields(resolvedMetadata.inputs),
  23. outputs: parseFields(resolvedMetadata.outputs)
  24. };
  25. }
  26. exports.getComponentInfo = getComponentInfo;
  27. function parseFields(names) {
  28. var attrProps = [];
  29. if (names) {
  30. for (var i = 0; i < names.length; i++) {
  31. var parts = names[i].split(':');
  32. var prop = parts[0].trim();
  33. var attr = (parts[1] || parts[0]).trim();
  34. var capitalAttr = attr.charAt(0).toUpperCase() + attr.substr(1);
  35. attrProps.push({
  36. prop: prop,
  37. attr: attr,
  38. bracketAttr: "[" + attr + "]",
  39. parenAttr: "(" + attr + ")",
  40. bracketParenAttr: "[(" + attr + ")]",
  41. onAttr: "on" + capitalAttr,
  42. bindAttr: "bind" + capitalAttr,
  43. bindonAttr: "bindon" + capitalAttr
  44. });
  45. }
  46. }
  47. return attrProps;
  48. }
  49. exports.parseFields = parseFields;
  50. global.define = __define;
  51. return module.exports;
  52. });
  53. System.register("angular2/src/upgrade/util", [], true, function(require, exports, module) {
  54. var global = System.global,
  55. __define = global.define;
  56. global.define = undefined;
  57. function stringify(obj) {
  58. if (typeof obj == 'function')
  59. return obj.name || obj.toString();
  60. return '' + obj;
  61. }
  62. exports.stringify = stringify;
  63. function onError(e) {
  64. console.log(e, e.stack);
  65. throw e;
  66. }
  67. exports.onError = onError;
  68. function controllerKey(name) {
  69. return '$' + name + 'Controller';
  70. }
  71. exports.controllerKey = controllerKey;
  72. global.define = __define;
  73. return module.exports;
  74. });
  75. System.register("angular2/src/upgrade/constants", [], true, function(require, exports, module) {
  76. var global = System.global,
  77. __define = global.define;
  78. global.define = undefined;
  79. exports.NG2_APP_VIEW_MANAGER = 'ng2.AppViewManager';
  80. exports.NG2_COMPILER = 'ng2.Compiler';
  81. exports.NG2_INJECTOR = 'ng2.Injector';
  82. exports.NG2_PROTO_VIEW_REF_MAP = 'ng2.ProtoViewRefMap';
  83. exports.NG2_ZONE = 'ng2.NgZone';
  84. exports.NG1_CONTROLLER = '$controller';
  85. exports.NG1_SCOPE = '$scope';
  86. exports.NG1_ROOT_SCOPE = '$rootScope';
  87. exports.NG1_COMPILE = '$compile';
  88. exports.NG1_HTTP_BACKEND = '$httpBackend';
  89. exports.NG1_INJECTOR = '$injector';
  90. exports.NG1_PARSE = '$parse';
  91. exports.NG1_TEMPLATE_CACHE = '$templateCache';
  92. exports.REQUIRE_INJECTOR = '^' + exports.NG2_INJECTOR;
  93. global.define = __define;
  94. return module.exports;
  95. });
  96. System.register("angular2/src/upgrade/downgrade_ng2_adapter", ["angular2/core", "angular2/src/upgrade/constants"], true, function(require, exports, module) {
  97. var global = System.global,
  98. __define = global.define;
  99. global.define = undefined;
  100. var core_1 = require("angular2/core");
  101. var constants_1 = require("angular2/src/upgrade/constants");
  102. var INITIAL_VALUE = {__UNINITIALIZED__: true};
  103. var DowngradeNg2ComponentAdapter = (function() {
  104. function DowngradeNg2ComponentAdapter(id, info, element, attrs, scope, parentInjector, parse, viewManager, protoView) {
  105. this.id = id;
  106. this.info = info;
  107. this.element = element;
  108. this.attrs = attrs;
  109. this.scope = scope;
  110. this.parentInjector = parentInjector;
  111. this.parse = parse;
  112. this.viewManager = viewManager;
  113. this.protoView = protoView;
  114. this.component = null;
  115. this.inputChangeCount = 0;
  116. this.inputChanges = null;
  117. this.hostViewRef = null;
  118. this.changeDetector = null;
  119. this.contentInserctionPoint = null;
  120. this.element[0].id = id;
  121. this.componentScope = scope.$new();
  122. this.childNodes = element.contents();
  123. }
  124. DowngradeNg2ComponentAdapter.prototype.bootstrapNg2 = function() {
  125. var childInjector = this.parentInjector.resolveAndCreateChild([core_1.provide(constants_1.NG1_SCOPE, {useValue: this.componentScope})]);
  126. this.hostViewRef = this.viewManager.createRootHostView(this.protoView, '#' + this.id, childInjector);
  127. var renderer = this.hostViewRef.render;
  128. var hostElement = this.viewManager.getHostElement(this.hostViewRef);
  129. this.changeDetector = this.hostViewRef.changeDetectorRef;
  130. this.component = this.viewManager.getComponent(hostElement);
  131. this.contentInserctionPoint = renderer.rootContentInsertionPoints[0];
  132. };
  133. DowngradeNg2ComponentAdapter.prototype.setupInputs = function() {
  134. var _this = this;
  135. var attrs = this.attrs;
  136. var inputs = this.info.inputs;
  137. for (var i = 0; i < inputs.length; i++) {
  138. var input = inputs[i];
  139. var expr = null;
  140. if (attrs.hasOwnProperty(input.attr)) {
  141. var observeFn = (function(prop) {
  142. var prevValue = INITIAL_VALUE;
  143. return function(value) {
  144. if (_this.inputChanges !== null) {
  145. _this.inputChangeCount++;
  146. _this.inputChanges[prop] = new Ng1Change(value, prevValue === INITIAL_VALUE ? value : prevValue);
  147. prevValue = value;
  148. }
  149. _this.component[prop] = value;
  150. };
  151. })(input.prop);
  152. attrs.$observe(input.attr, observeFn);
  153. } else if (attrs.hasOwnProperty(input.bindAttr)) {
  154. expr = attrs[input.bindAttr];
  155. } else if (attrs.hasOwnProperty(input.bracketAttr)) {
  156. expr = attrs[input.bracketAttr];
  157. } else if (attrs.hasOwnProperty(input.bindonAttr)) {
  158. expr = attrs[input.bindonAttr];
  159. } else if (attrs.hasOwnProperty(input.bracketParenAttr)) {
  160. expr = attrs[input.bracketParenAttr];
  161. }
  162. if (expr != null) {
  163. var watchFn = (function(prop) {
  164. return function(value, prevValue) {
  165. if (_this.inputChanges != null) {
  166. _this.inputChangeCount++;
  167. _this.inputChanges[prop] = new Ng1Change(prevValue, value);
  168. }
  169. _this.component[prop] = value;
  170. };
  171. })(input.prop);
  172. this.componentScope.$watch(expr, watchFn);
  173. }
  174. }
  175. var prototype = this.info.type.prototype;
  176. if (prototype && prototype.ngOnChanges) {
  177. this.inputChanges = {};
  178. this.componentScope.$watch(function() {
  179. return _this.inputChangeCount;
  180. }, function() {
  181. var inputChanges = _this.inputChanges;
  182. _this.inputChanges = {};
  183. _this.component.ngOnChanges(inputChanges);
  184. });
  185. }
  186. this.componentScope.$watch(function() {
  187. return _this.changeDetector && _this.changeDetector.detectChanges();
  188. });
  189. };
  190. DowngradeNg2ComponentAdapter.prototype.projectContent = function() {
  191. var childNodes = this.childNodes;
  192. if (this.contentInserctionPoint) {
  193. var parent = this.contentInserctionPoint.parentNode;
  194. for (var i = 0,
  195. ii = childNodes.length; i < ii; i++) {
  196. parent.insertBefore(childNodes[i], this.contentInserctionPoint);
  197. }
  198. }
  199. };
  200. DowngradeNg2ComponentAdapter.prototype.setupOutputs = function() {
  201. var _this = this;
  202. var attrs = this.attrs;
  203. var outputs = this.info.outputs;
  204. for (var j = 0; j < outputs.length; j++) {
  205. var output = outputs[j];
  206. var expr = null;
  207. var assignExpr = false;
  208. var bindonAttr = output.bindonAttr ? output.bindonAttr.substring(0, output.bindonAttr.length - 6) : null;
  209. var bracketParenAttr = output.bracketParenAttr ? "[(" + output.bracketParenAttr.substring(2, output.bracketParenAttr.length - 8) + ")]" : null;
  210. if (attrs.hasOwnProperty(output.onAttr)) {
  211. expr = attrs[output.onAttr];
  212. } else if (attrs.hasOwnProperty(output.parenAttr)) {
  213. expr = attrs[output.parenAttr];
  214. } else if (attrs.hasOwnProperty(bindonAttr)) {
  215. expr = attrs[bindonAttr];
  216. assignExpr = true;
  217. } else if (attrs.hasOwnProperty(bracketParenAttr)) {
  218. expr = attrs[bracketParenAttr];
  219. assignExpr = true;
  220. }
  221. if (expr != null && assignExpr != null) {
  222. var getter = this.parse(expr);
  223. var setter = getter.assign;
  224. if (assignExpr && !setter) {
  225. throw new Error("Expression '" + expr + "' is not assignable!");
  226. }
  227. var emitter = this.component[output.prop];
  228. if (emitter) {
  229. emitter.subscribe({next: assignExpr ? (function(setter) {
  230. return function(value) {
  231. return setter(_this.scope, value);
  232. };
  233. })(setter) : (function(getter) {
  234. return function(value) {
  235. return getter(_this.scope, {$event: value});
  236. };
  237. })(getter)});
  238. } else {
  239. throw new Error("Missing emitter '" + output.prop + "' on component '" + this.info.selector + "'!");
  240. }
  241. }
  242. }
  243. };
  244. DowngradeNg2ComponentAdapter.prototype.registerCleanup = function() {
  245. var _this = this;
  246. this.element.bind('$remove', function() {
  247. return _this.viewManager.destroyRootHostView(_this.hostViewRef);
  248. });
  249. };
  250. return DowngradeNg2ComponentAdapter;
  251. })();
  252. exports.DowngradeNg2ComponentAdapter = DowngradeNg2ComponentAdapter;
  253. var Ng1Change = (function() {
  254. function Ng1Change(previousValue, currentValue) {
  255. this.previousValue = previousValue;
  256. this.currentValue = currentValue;
  257. }
  258. Ng1Change.prototype.isFirstChange = function() {
  259. return this.previousValue === this.currentValue;
  260. };
  261. return Ng1Change;
  262. })();
  263. global.define = __define;
  264. return module.exports;
  265. });
  266. System.register("angular2/src/upgrade/angular_js", [], true, function(require, exports, module) {
  267. var global = System.global,
  268. __define = global.define;
  269. global.define = undefined;
  270. function noNg() {
  271. throw new Error('AngularJS v1.x is not loaded!');
  272. }
  273. var angular = {
  274. bootstrap: noNg,
  275. module: noNg,
  276. element: noNg,
  277. version: noNg
  278. };
  279. try {
  280. if (window.hasOwnProperty('angular')) {
  281. angular = window.angular;
  282. }
  283. } catch (e) {}
  284. exports.bootstrap = angular.bootstrap;
  285. exports.module = angular.module;
  286. exports.element = angular.element;
  287. exports.version = angular.version;
  288. global.define = __define;
  289. return module.exports;
  290. });
  291. System.register("angular2/src/upgrade/upgrade_ng1_adapter", ["angular2/core", "angular2/src/upgrade/constants", "angular2/src/upgrade/util", "angular2/src/upgrade/angular_js"], true, function(require, exports, module) {
  292. var global = System.global,
  293. __define = global.define;
  294. global.define = undefined;
  295. var core_1 = require("angular2/core");
  296. var constants_1 = require("angular2/src/upgrade/constants");
  297. var util_1 = require("angular2/src/upgrade/util");
  298. var angular = require("angular2/src/upgrade/angular_js");
  299. var CAMEL_CASE = /([A-Z])/g;
  300. var INITIAL_VALUE = {__UNINITIALIZED__: true};
  301. var NOT_SUPPORTED = 'NOT_SUPPORTED';
  302. var UpgradeNg1ComponentAdapterBuilder = (function() {
  303. function UpgradeNg1ComponentAdapterBuilder(name) {
  304. this.name = name;
  305. this.inputs = [];
  306. this.inputsRename = [];
  307. this.outputs = [];
  308. this.outputsRename = [];
  309. this.propertyOutputs = [];
  310. this.checkProperties = [];
  311. this.propertyMap = {};
  312. this.linkFn = null;
  313. this.directive = null;
  314. this.$controller = null;
  315. var selector = name.replace(CAMEL_CASE, function(all, next) {
  316. return '-' + next.toLowerCase();
  317. });
  318. var self = this;
  319. this.type = core_1.Directive({
  320. selector: selector,
  321. inputs: this.inputsRename,
  322. outputs: this.outputsRename
  323. }).Class({
  324. constructor: [new core_1.Inject(constants_1.NG1_SCOPE), core_1.ElementRef, function(scope, elementRef) {
  325. return new UpgradeNg1ComponentAdapter(self.linkFn, scope, self.directive, elementRef, self.$controller, self.inputs, self.outputs, self.propertyOutputs, self.checkProperties, self.propertyMap);
  326. }],
  327. ngOnChanges: function() {},
  328. ngDoCheck: function() {}
  329. });
  330. }
  331. UpgradeNg1ComponentAdapterBuilder.prototype.extractDirective = function(injector) {
  332. var directives = injector.get(this.name + 'Directive');
  333. if (directives.length > 1) {
  334. throw new Error('Only support single directive definition for: ' + this.name);
  335. }
  336. var directive = directives[0];
  337. if (directive.replace)
  338. this.notSupported('replace');
  339. if (directive.terminal)
  340. this.notSupported('terminal');
  341. var link = directive.link;
  342. if (typeof link == 'object') {
  343. if (link.post)
  344. this.notSupported('link.post');
  345. }
  346. return directive;
  347. };
  348. UpgradeNg1ComponentAdapterBuilder.prototype.notSupported = function(feature) {
  349. throw new Error("Upgraded directive '" + this.name + "' does not support '" + feature + "'.");
  350. };
  351. UpgradeNg1ComponentAdapterBuilder.prototype.extractBindings = function() {
  352. var scope = this.directive.scope;
  353. if (typeof scope == 'object') {
  354. for (var name in scope) {
  355. if (scope.hasOwnProperty(name)) {
  356. var localName = scope[name];
  357. var type = localName.charAt(0);
  358. localName = localName.substr(1) || name;
  359. var outputName = 'output_' + name;
  360. var outputNameRename = outputName + ': ' + name;
  361. var outputNameRenameChange = outputName + ': ' + name + 'Change';
  362. var inputName = 'input_' + name;
  363. var inputNameRename = inputName + ': ' + name;
  364. switch (type) {
  365. case '=':
  366. this.propertyOutputs.push(outputName);
  367. this.checkProperties.push(localName);
  368. this.outputs.push(outputName);
  369. this.outputsRename.push(outputNameRenameChange);
  370. this.propertyMap[outputName] = localName;
  371. case '@':
  372. this.inputs.push(inputName);
  373. this.inputsRename.push(inputNameRename);
  374. this.propertyMap[inputName] = localName;
  375. break;
  376. case '&':
  377. this.outputs.push(outputName);
  378. this.outputsRename.push(outputNameRename);
  379. this.propertyMap[outputName] = localName;
  380. break;
  381. default:
  382. var json = JSON.stringify(scope);
  383. throw new Error("Unexpected mapping '" + type + "' in '" + json + "' in '" + this.name + "' directive.");
  384. }
  385. }
  386. }
  387. }
  388. };
  389. UpgradeNg1ComponentAdapterBuilder.prototype.compileTemplate = function(compile, templateCache, httpBackend) {
  390. var _this = this;
  391. if (this.directive.template !== undefined) {
  392. this.linkFn = compileHtml(this.directive.template);
  393. } else if (this.directive.templateUrl) {
  394. var url = this.directive.templateUrl;
  395. var html = templateCache.get(url);
  396. if (html !== undefined) {
  397. this.linkFn = compileHtml(html);
  398. } else {
  399. return new Promise(function(resolve, err) {
  400. httpBackend('GET', url, null, function(status, response) {
  401. if (status == 200) {
  402. resolve(_this.linkFn = compileHtml(templateCache.put(url, response)));
  403. } else {
  404. err("GET " + url + " returned " + status + ": " + response);
  405. }
  406. });
  407. });
  408. }
  409. } else {
  410. throw new Error("Directive '" + this.name + "' is not a component, it is missing template.");
  411. }
  412. return null;
  413. function compileHtml(html) {
  414. var div = document.createElement('div');
  415. div.innerHTML = html;
  416. return compile(div.childNodes);
  417. }
  418. };
  419. UpgradeNg1ComponentAdapterBuilder.resolve = function(exportedComponents, injector) {
  420. var promises = [];
  421. var compile = injector.get(constants_1.NG1_COMPILE);
  422. var templateCache = injector.get(constants_1.NG1_TEMPLATE_CACHE);
  423. var httpBackend = injector.get(constants_1.NG1_HTTP_BACKEND);
  424. var $controller = injector.get(constants_1.NG1_CONTROLLER);
  425. for (var name in exportedComponents) {
  426. if (exportedComponents.hasOwnProperty(name)) {
  427. var exportedComponent = exportedComponents[name];
  428. exportedComponent.directive = exportedComponent.extractDirective(injector);
  429. exportedComponent.$controller = $controller;
  430. exportedComponent.extractBindings();
  431. var promise = exportedComponent.compileTemplate(compile, templateCache, httpBackend);
  432. if (promise)
  433. promises.push(promise);
  434. }
  435. }
  436. return Promise.all(promises);
  437. };
  438. return UpgradeNg1ComponentAdapterBuilder;
  439. })();
  440. exports.UpgradeNg1ComponentAdapterBuilder = UpgradeNg1ComponentAdapterBuilder;
  441. var UpgradeNg1ComponentAdapter = (function() {
  442. function UpgradeNg1ComponentAdapter(linkFn, scope, directive, elementRef, $controller, inputs, outputs, propOuts, checkProperties, propertyMap) {
  443. this.directive = directive;
  444. this.inputs = inputs;
  445. this.outputs = outputs;
  446. this.propOuts = propOuts;
  447. this.checkProperties = checkProperties;
  448. this.propertyMap = propertyMap;
  449. this.destinationObj = null;
  450. this.checkLastValues = [];
  451. var element = elementRef.nativeElement;
  452. var childNodes = [];
  453. var childNode;
  454. while (childNode = element.firstChild) {
  455. element.removeChild(childNode);
  456. childNodes.push(childNode);
  457. }
  458. var componentScope = scope.$new(!!directive.scope);
  459. var $element = angular.element(element);
  460. var controllerType = directive.controller;
  461. var controller = null;
  462. if (controllerType) {
  463. var locals = {
  464. $scope: componentScope,
  465. $element: $element
  466. };
  467. controller = $controller(controllerType, locals, null, directive.controllerAs);
  468. $element.data(util_1.controllerKey(directive.name), controller);
  469. }
  470. var link = directive.link;
  471. if (typeof link == 'object')
  472. link = link.pre;
  473. if (link) {
  474. var attrs = NOT_SUPPORTED;
  475. var transcludeFn = NOT_SUPPORTED;
  476. var linkController = this.resolveRequired($element, directive.require);
  477. directive.link(componentScope, $element, attrs, linkController, transcludeFn);
  478. }
  479. this.destinationObj = directive.bindToController && controller ? controller : componentScope;
  480. linkFn(componentScope, function(clonedElement, scope) {
  481. for (var i = 0,
  482. ii = clonedElement.length; i < ii; i++) {
  483. element.appendChild(clonedElement[i]);
  484. }
  485. }, {parentBoundTranscludeFn: function(scope, cloneAttach) {
  486. cloneAttach(childNodes);
  487. }});
  488. for (var i = 0; i < inputs.length; i++) {
  489. this[inputs[i]] = null;
  490. }
  491. for (var j = 0; j < outputs.length; j++) {
  492. var emitter = this[outputs[j]] = new core_1.EventEmitter();
  493. this.setComponentProperty(outputs[j], (function(emitter) {
  494. return function(value) {
  495. return emitter.emit(value);
  496. };
  497. })(emitter));
  498. }
  499. for (var k = 0; k < propOuts.length; k++) {
  500. this[propOuts[k]] = new core_1.EventEmitter();
  501. this.checkLastValues.push(INITIAL_VALUE);
  502. }
  503. }
  504. UpgradeNg1ComponentAdapter.prototype.ngOnChanges = function(changes) {
  505. for (var name in changes) {
  506. if (changes.hasOwnProperty(name)) {
  507. var change = changes[name];
  508. this.setComponentProperty(name, change.currentValue);
  509. }
  510. }
  511. };
  512. UpgradeNg1ComponentAdapter.prototype.ngDoCheck = function() {
  513. var count = 0;
  514. var destinationObj = this.destinationObj;
  515. var lastValues = this.checkLastValues;
  516. var checkProperties = this.checkProperties;
  517. for (var i = 0; i < checkProperties.length; i++) {
  518. var value = destinationObj[checkProperties[i]];
  519. var last = lastValues[i];
  520. if (value !== last) {
  521. if (typeof value == 'number' && isNaN(value) && typeof last == 'number' && isNaN(last)) {} else {
  522. var eventEmitter = this[this.propOuts[i]];
  523. eventEmitter.emit(lastValues[i] = value);
  524. }
  525. }
  526. }
  527. return count;
  528. };
  529. UpgradeNg1ComponentAdapter.prototype.setComponentProperty = function(name, value) {
  530. this.destinationObj[this.propertyMap[name]] = value;
  531. };
  532. UpgradeNg1ComponentAdapter.prototype.resolveRequired = function($element, require) {
  533. if (!require) {
  534. return undefined;
  535. } else if (typeof require == 'string') {
  536. var name = require;
  537. var isOptional = false;
  538. var startParent = false;
  539. var searchParents = false;
  540. var ch;
  541. if (name.charAt(0) == '?') {
  542. isOptional = true;
  543. name = name.substr(1);
  544. }
  545. if (name.charAt(0) == '^') {
  546. searchParents = true;
  547. name = name.substr(1);
  548. }
  549. if (name.charAt(0) == '^') {
  550. startParent = true;
  551. name = name.substr(1);
  552. }
  553. var key = util_1.controllerKey(name);
  554. if (startParent)
  555. $element = $element.parent();
  556. var dep = searchParents ? $element.inheritedData(key) : $element.data(key);
  557. if (!dep && !isOptional) {
  558. throw new Error("Can not locate '" + require + "' in '" + this.directive.name + "'.");
  559. }
  560. return dep;
  561. } else if (require instanceof Array) {
  562. var deps = [];
  563. for (var i = 0; i < require.length; i++) {
  564. deps.push(this.resolveRequired($element, require[i]));
  565. }
  566. return deps;
  567. }
  568. throw new Error("Directive '" + this.directive.name + "' require syntax unrecognized: " + this.directive.require);
  569. };
  570. return UpgradeNg1ComponentAdapter;
  571. })();
  572. global.define = __define;
  573. return module.exports;
  574. });
  575. System.register("angular2/src/upgrade/upgrade_adapter", ["angular2/core", "angular2/src/facade/async", "angular2/platform/browser", "angular2/src/upgrade/metadata", "angular2/src/upgrade/util", "angular2/src/upgrade/constants", "angular2/src/upgrade/downgrade_ng2_adapter", "angular2/src/upgrade/upgrade_ng1_adapter", "angular2/src/upgrade/angular_js"], true, function(require, exports, module) {
  576. var global = System.global,
  577. __define = global.define;
  578. global.define = undefined;
  579. var core_1 = require("angular2/core");
  580. var async_1 = require("angular2/src/facade/async");
  581. var browser_1 = require("angular2/platform/browser");
  582. var metadata_1 = require("angular2/src/upgrade/metadata");
  583. var util_1 = require("angular2/src/upgrade/util");
  584. var constants_1 = require("angular2/src/upgrade/constants");
  585. var downgrade_ng2_adapter_1 = require("angular2/src/upgrade/downgrade_ng2_adapter");
  586. var upgrade_ng1_adapter_1 = require("angular2/src/upgrade/upgrade_ng1_adapter");
  587. var angular = require("angular2/src/upgrade/angular_js");
  588. var upgradeCount = 0;
  589. var UpgradeAdapter = (function() {
  590. function UpgradeAdapter() {
  591. this.idPrefix = "NG2_UPGRADE_" + upgradeCount++ + "_";
  592. this.upgradedComponents = [];
  593. this.downgradedComponents = {};
  594. this.providers = [];
  595. }
  596. UpgradeAdapter.prototype.downgradeNg2Component = function(type) {
  597. this.upgradedComponents.push(type);
  598. var info = metadata_1.getComponentInfo(type);
  599. return ng1ComponentDirective(info, "" + this.idPrefix + info.selector + "_c");
  600. };
  601. UpgradeAdapter.prototype.upgradeNg1Component = function(name) {
  602. if (this.downgradedComponents.hasOwnProperty(name)) {
  603. return this.downgradedComponents[name].type;
  604. } else {
  605. return (this.downgradedComponents[name] = new upgrade_ng1_adapter_1.UpgradeNg1ComponentAdapterBuilder(name)).type;
  606. }
  607. };
  608. UpgradeAdapter.prototype.bootstrap = function(element, modules, config) {
  609. var _this = this;
  610. var upgrade = new UpgradeAdapterRef();
  611. var ng1Injector = null;
  612. var platformRef = core_1.platform(browser_1.BROWSER_PROVIDERS);
  613. var applicationRef = platformRef.application([browser_1.BROWSER_APP_PROVIDERS, core_1.provide(constants_1.NG1_INJECTOR, {useFactory: function() {
  614. return ng1Injector;
  615. }}), core_1.provide(constants_1.NG1_COMPILE, {useFactory: function() {
  616. return ng1Injector.get(constants_1.NG1_COMPILE);
  617. }}), this.providers]);
  618. var injector = applicationRef.injector;
  619. var ngZone = injector.get(core_1.NgZone);
  620. var compiler = injector.get(core_1.Compiler);
  621. var delayApplyExps = [];
  622. var original$applyFn;
  623. var rootScopePrototype;
  624. var rootScope;
  625. var protoViewRefMap = {};
  626. var ng1Module = angular.module(this.idPrefix, modules);
  627. var ng1compilePromise = null;
  628. ng1Module.value(constants_1.NG2_INJECTOR, injector).value(constants_1.NG2_ZONE, ngZone).value(constants_1.NG2_COMPILER, compiler).value(constants_1.NG2_PROTO_VIEW_REF_MAP, protoViewRefMap).value(constants_1.NG2_APP_VIEW_MANAGER, injector.get(core_1.AppViewManager)).config(['$provide', function(provide) {
  629. provide.decorator(constants_1.NG1_ROOT_SCOPE, ['$delegate', function(rootScopeDelegate) {
  630. rootScopePrototype = rootScopeDelegate.constructor.prototype;
  631. if (rootScopePrototype.hasOwnProperty('$apply')) {
  632. original$applyFn = rootScopePrototype.$apply;
  633. rootScopePrototype.$apply = function(exp) {
  634. return delayApplyExps.push(exp);
  635. };
  636. } else {
  637. throw new Error("Failed to find '$apply' on '$rootScope'!");
  638. }
  639. return rootScope = rootScopeDelegate;
  640. }]);
  641. }]).run(['$injector', '$rootScope', function(injector, rootScope) {
  642. ng1Injector = injector;
  643. async_1.ObservableWrapper.subscribe(ngZone.onTurnDone, function(_) {
  644. ngZone.run(function() {
  645. return rootScope.$apply();
  646. });
  647. });
  648. ng1compilePromise = upgrade_ng1_adapter_1.UpgradeNg1ComponentAdapterBuilder.resolve(_this.downgradedComponents, injector);
  649. }]);
  650. angular.element(element).data(util_1.controllerKey(constants_1.NG2_INJECTOR), injector);
  651. ngZone.run(function() {
  652. angular.bootstrap(element, [_this.idPrefix], config);
  653. });
  654. Promise.all([this.compileNg2Components(compiler, protoViewRefMap), ng1compilePromise]).then(function() {
  655. ngZone.run(function() {
  656. if (rootScopePrototype) {
  657. rootScopePrototype.$apply = original$applyFn;
  658. while (delayApplyExps.length) {
  659. rootScope.$apply(delayApplyExps.shift());
  660. }
  661. upgrade._bootstrapDone(applicationRef, ng1Injector);
  662. rootScopePrototype = null;
  663. }
  664. });
  665. }, util_1.onError);
  666. return upgrade;
  667. };
  668. UpgradeAdapter.prototype.addProvider = function(provider) {
  669. this.providers.push(provider);
  670. };
  671. UpgradeAdapter.prototype.upgradeNg1Provider = function(name, options) {
  672. var token = options && options.asToken || name;
  673. this.providers.push(core_1.provide(token, {
  674. useFactory: function(ng1Injector) {
  675. return ng1Injector.get(name);
  676. },
  677. deps: [constants_1.NG1_INJECTOR]
  678. }));
  679. };
  680. UpgradeAdapter.prototype.downgradeNg2Provider = function(token) {
  681. var factory = function(injector) {
  682. return injector.get(token);
  683. };
  684. factory.$inject = [constants_1.NG2_INJECTOR];
  685. return factory;
  686. };
  687. UpgradeAdapter.prototype.compileNg2Components = function(compiler, protoViewRefMap) {
  688. var _this = this;
  689. var promises = [];
  690. var types = this.upgradedComponents;
  691. for (var i = 0; i < types.length; i++) {
  692. promises.push(compiler.compileInHost(types[i]));
  693. }
  694. return Promise.all(promises).then(function(protoViews) {
  695. var types = _this.upgradedComponents;
  696. for (var i = 0; i < protoViews.length; i++) {
  697. protoViewRefMap[metadata_1.getComponentInfo(types[i]).selector] = protoViews[i];
  698. }
  699. return protoViewRefMap;
  700. }, util_1.onError);
  701. };
  702. return UpgradeAdapter;
  703. })();
  704. exports.UpgradeAdapter = UpgradeAdapter;
  705. function ng1ComponentDirective(info, idPrefix) {
  706. directiveFactory.$inject = [constants_1.NG2_PROTO_VIEW_REF_MAP, constants_1.NG2_APP_VIEW_MANAGER, constants_1.NG1_PARSE];
  707. function directiveFactory(protoViewRefMap, viewManager, parse) {
  708. var protoView = protoViewRefMap[info.selector];
  709. if (!protoView)
  710. throw new Error('Expecting ProtoViewRef for: ' + info.selector);
  711. var idCount = 0;
  712. return {
  713. restrict: 'E',
  714. require: constants_1.REQUIRE_INJECTOR,
  715. link: {post: function(scope, element, attrs, parentInjector, transclude) {
  716. var domElement = element[0];
  717. var facade = new downgrade_ng2_adapter_1.DowngradeNg2ComponentAdapter(idPrefix + (idCount++), info, element, attrs, scope, parentInjector, parse, viewManager, protoView);
  718. facade.setupInputs();
  719. facade.bootstrapNg2();
  720. facade.projectContent();
  721. facade.setupOutputs();
  722. facade.registerCleanup();
  723. }}
  724. };
  725. }
  726. return directiveFactory;
  727. }
  728. var UpgradeAdapterRef = (function() {
  729. function UpgradeAdapterRef() {
  730. this._readyFn = null;
  731. this.ng1RootScope = null;
  732. this.ng1Injector = null;
  733. this.ng2ApplicationRef = null;
  734. this.ng2Injector = null;
  735. }
  736. UpgradeAdapterRef.prototype._bootstrapDone = function(applicationRef, ng1Injector) {
  737. this.ng2ApplicationRef = applicationRef;
  738. this.ng2Injector = applicationRef.injector;
  739. this.ng1Injector = ng1Injector;
  740. this.ng1RootScope = ng1Injector.get(constants_1.NG1_ROOT_SCOPE);
  741. this._readyFn && this._readyFn(this);
  742. };
  743. UpgradeAdapterRef.prototype.ready = function(fn) {
  744. this._readyFn = fn;
  745. };
  746. UpgradeAdapterRef.prototype.dispose = function() {
  747. this.ng1Injector.get(constants_1.NG1_ROOT_SCOPE).$destroy();
  748. this.ng2ApplicationRef.dispose();
  749. };
  750. return UpgradeAdapterRef;
  751. })();
  752. exports.UpgradeAdapterRef = UpgradeAdapterRef;
  753. global.define = __define;
  754. return module.exports;
  755. });
  756. System.register("angular2/upgrade", ["angular2/src/upgrade/upgrade_adapter"], true, function(require, exports, module) {
  757. var global = System.global,
  758. __define = global.define;
  759. global.define = undefined;
  760. var upgrade_adapter_1 = require("angular2/src/upgrade/upgrade_adapter");
  761. exports.UpgradeAdapter = upgrade_adapter_1.UpgradeAdapter;
  762. exports.UpgradeAdapterRef = upgrade_adapter_1.UpgradeAdapterRef;
  763. global.define = __define;
  764. return module.exports;
  765. });
  766. //# sourceMappingURLDisabled=upgrade.dev.js.map