2016-06-23 16:47:54 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
|
*/
|
|
|
|
|
|
2017-01-23 19:23:45 +00:00
|
|
|
import * as angular from './angular_js';
|
|
|
|
|
|
2015-08-06 20:19:29 +00:00
|
|
|
export function onError(e: any) {
|
|
|
|
|
// TODO: (misko): We seem to not have a stack trace here!
|
2016-11-22 23:06:09 +00:00
|
|
|
if (console.error) {
|
|
|
|
|
console.error(e, e.stack);
|
|
|
|
|
} else {
|
|
|
|
|
// tslint:disable-next-line:no-console
|
|
|
|
|
console.log(e, e.stack);
|
|
|
|
|
}
|
2015-08-06 20:19:29 +00:00
|
|
|
throw e;
|
|
|
|
|
}
|
2015-10-11 18:18:11 +00:00
|
|
|
|
|
|
|
|
export function controllerKey(name: string): string {
|
|
|
|
|
return '$' + name + 'Controller';
|
|
|
|
|
}
|
2016-11-03 14:48:11 +00:00
|
|
|
|
2016-12-27 23:23:49 +00:00
|
|
|
export function getAttributesAsArray(node: Node): [string, string][] {
|
2016-11-02 22:38:00 +00:00
|
|
|
const attributes = node.attributes;
|
2016-12-27 23:23:49 +00:00
|
|
|
let asArray: [string, string][];
|
2016-11-02 22:38:00 +00:00
|
|
|
if (attributes) {
|
|
|
|
|
let attrLen = attributes.length;
|
|
|
|
|
asArray = new Array(attrLen);
|
|
|
|
|
for (let i = 0; i < attrLen; i++) {
|
|
|
|
|
asArray[i] = [attributes[i].nodeName, attributes[i].nodeValue];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return asArray || [];
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 14:48:11 +00:00
|
|
|
export class Deferred<R> {
|
|
|
|
|
promise: Promise<R>;
|
|
|
|
|
resolve: (value?: R|PromiseLike<R>) => void;
|
|
|
|
|
reject: (error?: any) => void;
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
this.promise = new Promise((res, rej) => {
|
|
|
|
|
this.resolve = res;
|
|
|
|
|
this.reject = rej;
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-11-02 22:38:00 +00:00
|
|
|
}
|
2017-01-23 19:23:45 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return true if the passed-in component implements the subset of
|
|
|
|
|
* ControlValueAccessor needed for AngularJS ng-model compatibility.
|
|
|
|
|
*/
|
|
|
|
|
function supportsNgModel(component: any) {
|
|
|
|
|
return typeof component.writeValue === 'function' &&
|
|
|
|
|
typeof component.registerOnChange === 'function';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Glue the AngularJS ngModelController if it exists to the component if it
|
|
|
|
|
* implements the needed subset of ControlValueAccessor.
|
|
|
|
|
*/
|
|
|
|
|
export function hookupNgModel(ngModel: angular.INgModelController, component: any) {
|
|
|
|
|
if (ngModel && supportsNgModel(component)) {
|
|
|
|
|
ngModel.$render = () => { component.writeValue(ngModel.$viewValue); };
|
|
|
|
|
component.registerOnChange(ngModel.$setViewValue.bind(ngModel));
|
|
|
|
|
}
|
|
|
|
|
}
|