angular2
Version:
Angular 2 - a web framework for modern web apps
421 lines • 14 kB
JavaScript
;var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
switch (arguments.length) {
case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
}
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var lang_1 = require('angular2/src/facade/lang');
var di_1 = require('angular2/src/core/di');
var metadata_1 = require('angular2/src/core/di/metadata');
/**
* Specifies that a constant attribute value should be injected.
*
* The directive can inject constant string literals of host element attributes.
*
* ### Example
*
* Suppose we have an `<input>` element and want to know its `type`.
*
* ```html
* <input type="text">
* ```
*
* A decorator can inject string literal `text` like so:
*
* ```javascript
* @Directive({
* selector: `input'
* })
* class InputDirective {
* constructor(@Attribute('type') type) {
* // type would be `text` in this example
* }
* }
* ```
*/
var AttributeMetadata = (function (_super) {
__extends(AttributeMetadata, _super);
function AttributeMetadata(attributeName) {
_super.call(this);
this.attributeName = attributeName;
}
Object.defineProperty(AttributeMetadata.prototype, "token", {
get: function () {
// Normally one would default a token to a type of an injected value but here
// the type of a variable is "string" and we can't use primitive type as a return value
// so we use instance of Attribute instead. This doesn't matter much in practice as arguments
// with @Attribute annotation are injected by ElementInjector that doesn't take tokens into
// account.
return this;
},
enumerable: true,
configurable: true
});
AttributeMetadata.prototype.toString = function () { return "@Attribute(" + lang_1.stringify(this.attributeName) + ")"; };
AttributeMetadata = __decorate([
lang_1.CONST(),
__metadata('design:paramtypes', [String])
], AttributeMetadata);
return AttributeMetadata;
})(metadata_1.DependencyMetadata);
exports.AttributeMetadata = AttributeMetadata;
/**
* Declares an injectable parameter to be a live list of directives or variable
* bindings from the content children of a directive.
*
* ### Example ([live demo](http://plnkr.co/edit/lY9m8HLy7z06vDoUaSN2?p=preview))
*
* Assume that `<tabs>` component would like to get a list its children `<pane>`
* components as shown in this example:
*
* ```html
* <tabs>
* <pane title="Overview">...</pane>
* <pane *ng-for="#o of objects" [title]="o.title">{{o.text}}</pane>
* </tabs>
* ```
*
* The preferred solution is to query for `Pane` directives using this decorator.
*
* ```javascript
* @Component({
* selector: 'pane',
* inputs: ['title']
* })
* class Pane {
* title:string;
* }
*
* @Component({
* selector: 'tabs',
* template: `
* <ul>
* <li *ng-for="#pane of panes">{{pane.title}}</li>
* </ul>
* <content></content>
* `
* })
* class Tabs {
* panes: QueryList<Pane>;
* constructor(@Query(Pane) panes:QueryList<Pane>) {
* this.panes = panes;
* }
* }
* ```
*
* A query can look for variable bindings by passing in a string with desired binding symbol.
*
* ### Example ([live demo](http://plnkr.co/edit/sT2j25cH1dURAyBRCKx1?p=preview))
* ```html
* <seeker>
* <div #findme>...</div>
* </seeker>
*
* @Component({ selector: 'seeker' })
* class Seeker {
* constructor(@Query('findme') elList: QueryList<ElementRef>) {...}
* }
* ```
*
* In this case the object that is injected depend on the type of the variable
* binding. It can be an ElementRef, a directive or a component.
*
* Passing in a comma separated list of variable bindings will query for all of them.
*
* ```html
* <seeker>
* <div #find-me>...</div>
* <div #find-me-too>...</div>
* </seeker>
*
* @Component({
* selector: 'seeker'
* })
* class Seeker {
* constructor(@Query('findMe, findMeToo') elList: QueryList<ElementRef>) {...}
* }
* ```
*
* Configure whether query looks for direct children or all descendants
* of the querying element, by using the `descendants` parameter.
* It is set to `false` by default.
*
* ### Example ([live demo](http://plnkr.co/edit/wtGeB977bv7qvA5FTYl9?p=preview))
* ```html
* <container #first>
* <item>a</item>
* <item>b</item>
* <container #second>
* <item>c</item>
* </container>
* </container>
* ```
*
* When querying for items, the first container will see only `a` and `b` by default,
* but with `Query(TextDirective, {descendants: true})` it will see `c` too.
*
* The queried directives are kept in a depth-first pre-order with respect to their
* positions in the DOM.
*
* Query does not look deep into any subcomponent views.
*
* Query is updated as part of the change-detection cycle. Since change detection
* happens after construction of a directive, QueryList will always be empty when observed in the
* constructor.
*
* The injected object is an unmodifiable live list.
* See {@link QueryList} for more details.
*/
var QueryMetadata = (function (_super) {
__extends(QueryMetadata, _super);
function QueryMetadata(_selector, _a) {
var _b = _a === void 0 ? {} : _a, _c = _b.descendants, descendants = _c === void 0 ? false : _c, _d = _b.first, first = _d === void 0 ? false : _d;
_super.call(this);
this._selector = _selector;
this.descendants = descendants;
this.first = first;
}
Object.defineProperty(QueryMetadata.prototype, "isViewQuery", {
/**
* always `false` to differentiate it with {@link ViewQueryMetadata}.
*/
get: function () { return false; },
enumerable: true,
configurable: true
});
Object.defineProperty(QueryMetadata.prototype, "selector", {
/**
* what this is querying for.
*/
get: function () { return di_1.resolveForwardRef(this._selector); },
enumerable: true,
configurable: true
});
Object.defineProperty(QueryMetadata.prototype, "isVarBindingQuery", {
/**
* whether this is querying for a variable binding or a directive.
*/
get: function () { return lang_1.isString(this.selector); },
enumerable: true,
configurable: true
});
Object.defineProperty(QueryMetadata.prototype, "varBindings", {
/**
* returns a list of variable bindings this is querying for.
* Only applicable if this is a variable bindings query.
*/
get: function () { return this.selector.split(','); },
enumerable: true,
configurable: true
});
QueryMetadata.prototype.toString = function () { return "@Query(" + lang_1.stringify(this.selector) + ")"; };
QueryMetadata = __decorate([
lang_1.CONST(),
__metadata('design:paramtypes', [Object, Object])
], QueryMetadata);
return QueryMetadata;
})(metadata_1.DependencyMetadata);
exports.QueryMetadata = QueryMetadata;
// TODO: add an example after ContentChildren and ViewChildren are in master
/**
* Configures a content query.
*
* Content queries are set before the `ngAfterContentInit` callback is called.
*
* ### Example
*
* ```
* @Directive({
* selector: 'someDir'
* })
* class SomeDir {
* @ContentChildren(ChildDirective) contentChildren: QueryList<ChildDirective>;
*
* ngAfterContentInit() {
* // contentChildren is set
* }
* }
* ```
*/
var ContentChildrenMetadata = (function (_super) {
__extends(ContentChildrenMetadata, _super);
function ContentChildrenMetadata(_selector, _a) {
var _b = (_a === void 0 ? {} : _a).descendants, descendants = _b === void 0 ? false : _b;
_super.call(this, _selector, { descendants: descendants });
}
ContentChildrenMetadata = __decorate([
lang_1.CONST(),
__metadata('design:paramtypes', [Object, Object])
], ContentChildrenMetadata);
return ContentChildrenMetadata;
})(QueryMetadata);
exports.ContentChildrenMetadata = ContentChildrenMetadata;
// TODO: add an example after ContentChild and ViewChild are in master
/**
* Configures a content query.
*
* Content queries are set before the `ngAfterContentInit` callback is called.
*
* ### Example
*
* ```
* @Directive({
* selector: 'someDir'
* })
* class SomeDir {
* @ContentChild(ChildDirective) contentChild;
*
* ngAfterContentInit() {
* // contentChild is set
* }
* }
* ```
*/
var ContentChildMetadata = (function (_super) {
__extends(ContentChildMetadata, _super);
function ContentChildMetadata(_selector) {
_super.call(this, _selector, { descendants: true, first: true });
}
ContentChildMetadata = __decorate([
lang_1.CONST(),
__metadata('design:paramtypes', [Object])
], ContentChildMetadata);
return ContentChildMetadata;
})(QueryMetadata);
exports.ContentChildMetadata = ContentChildMetadata;
/**
* Similar to {@link QueryMetadata}, but querying the component view, instead of
* the content children.
*
* ### Example ([live demo](http://plnkr.co/edit/eNsFHDf7YjyM6IzKxM1j?p=preview))
*
* ```javascript
* @Component({...})
* @View({
* template: `
* <item> a </item>
* <item> b </item>
* <item> c </item>
* `
* })
* class MyComponent {
* shown: boolean;
*
* constructor(private @Query(Item) items:QueryList<Item>) {
* items.onChange(() => console.log(items.length));
* }
* }
* ```
*
* Supports the same querying parameters as {@link QueryMetadata}, except
* `descendants`. This always queries the whole view.
*
* As `shown` is flipped between true and false, items will contain zero of one
* items.
*
* Specifies that a {@link QueryList} should be injected.
*
* The injected object is an iterable and observable live list.
* See {@link QueryList} for more details.
*/
var ViewQueryMetadata = (function (_super) {
__extends(ViewQueryMetadata, _super);
function ViewQueryMetadata(_selector, _a) {
var _b = _a === void 0 ? {} : _a, _c = _b.descendants, descendants = _c === void 0 ? false : _c, _d = _b.first, first = _d === void 0 ? false : _d;
_super.call(this, _selector, { descendants: descendants, first: first });
}
Object.defineProperty(ViewQueryMetadata.prototype, "isViewQuery", {
/**
* always `true` to differentiate it with {@link QueryMetadata}.
*/
get: function () { return true; },
enumerable: true,
configurable: true
});
ViewQueryMetadata.prototype.toString = function () { return "@ViewQuery(" + lang_1.stringify(this.selector) + ")"; };
ViewQueryMetadata = __decorate([
lang_1.CONST(),
__metadata('design:paramtypes', [Object, Object])
], ViewQueryMetadata);
return ViewQueryMetadata;
})(QueryMetadata);
exports.ViewQueryMetadata = ViewQueryMetadata;
/**
* Configures a view query.
*
* View queries are set before the `ngAfterViewInit` callback is called.
*
* ### Example
*
* ```
* @Component({
* selector: 'someDir',
* templateUrl: 'someTemplate',
* directives: [ItemDirective]
* })
* class SomeDir {
* @ViewChildren(ItemDirective) viewChildren: QueryList<ItemDirective>;
*
* ngAfterViewInit() {
* // viewChildren is set
* }
* }
* ```
*/
var ViewChildrenMetadata = (function (_super) {
__extends(ViewChildrenMetadata, _super);
function ViewChildrenMetadata(_selector) {
_super.call(this, _selector, { descendants: true });
}
ViewChildrenMetadata = __decorate([
lang_1.CONST(),
__metadata('design:paramtypes', [Object])
], ViewChildrenMetadata);
return ViewChildrenMetadata;
})(ViewQueryMetadata);
exports.ViewChildrenMetadata = ViewChildrenMetadata;
/**
* Configures a view query.
*
* View queries are set before the `ngAfterViewInit` callback is called.
*
* ### Example
*
* ```
* @Component({
* selector: 'someDir',
* templateUrl: 'someTemplate',
* directives: [ItemDirective]
* })
* class SomeDir {
* @ViewChild(ItemDirective) viewChild:ItemDirective;
*
* ngAfterViewInit() {
* // viewChild is set
* }
* }
* ```
*/
var ViewChildMetadata = (function (_super) {
__extends(ViewChildMetadata, _super);
function ViewChildMetadata(_selector) {
_super.call(this, _selector, { descendants: true, first: true });
}
ViewChildMetadata = __decorate([
lang_1.CONST(),
__metadata('design:paramtypes', [Object])
], ViewChildMetadata);
return ViewChildMetadata;
})(ViewQueryMetadata);
exports.ViewChildMetadata = ViewChildMetadata;
//# sourceMappingURL=di.js.map