Skip to content

Commit 02f0580

Browse files
author
vsternbach
committed
added decorators for service and pipes, modified component decorator
1 parent 898f116 commit 02f0580

File tree

10 files changed

+86
-41
lines changed

10 files changed

+86
-41
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
"ng-tags-input": "^3.0.0"
2626
},
2727
"scripts": {
28-
"prestart": "npm install & typings install",
28+
"postinstall": "typings install",
29+
"prestart": "npm install",
2930
"start": "webpack-dev-server --hot",
31+
"typings": "typings",
3032
"bundle": "rimraf dist & copyfiles -f src/index.html dist/ & copyfiles -f src/assets/* dist/assets & webpack -p ./dist/bundle.js"
3133
}
3234
}

src/components/comment-list/comments.component.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/**
22
* Created by voland on 4/2/16.
33
*/
4-
import Component from '../../decorators';
4+
import {Component} from '../../decorators';
55
import './comment-list.scss';
66
import {IComment} from "../../interfaces";
77

8-
@Component('app.components', 'comments', {
8+
@Component({
9+
selector: 'comments',
910
template: `
1011
<div class="container-fluid">
1112
<div class="discussion-timeline">
@@ -17,7 +18,7 @@ import {IComment} from "../../interfaces";
1718
</div>
1819
</div>`
1920
})
20-
class CommentsController {
21+
export class CommentsController {
2122
comments: IComment[];
2223
emptyComment: IComment;
2324
tags: string[];

src/components/comment/comment.component.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
* Created by voland on 4/2/16.
33
*/
44

5-
import Component from '../../decorators';
5+
import {Component} from '../../decorators';
66
import './comment.scss';
77
import {IComment} from "../../interfaces";
88

9-
@Component('app.components', 'comment', {
9+
@Component({
10+
selector: 'comment',
1011
bindings: {
1112
comment: '=',
1213
tags: '=',
@@ -54,7 +55,7 @@ import {IComment} from "../../interfaces";
5455
</div>
5556
</div>`
5657
})
57-
class CommentController {
58+
export class CommentController {
5859
editMode: boolean;
5960
comment: IComment;
6061
commentCopy: IComment;

src/components/components.module.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
/**
2-
* Created by voland on 4/2/16.
2+
* Created by vlad on 5/29/16.
33
*/
4-
5-
6-
export default angular.module('app.components', [])
7-
.filter('filterByTags', () => (comments, tags) => {
8-
if (!tags.length) return comments;
9-
function check(comment) {
10-
let filterArray = tags.map((tag: any) => tag.text);
11-
let findCount = filterArray
12-
.map((tag) => comment.tags.indexOf(tag) > -1 ? 1 : 0)
13-
.reduce((prev, curr) => prev + curr);
14-
return findCount === filterArray.length;
15-
}
16-
return comments.filter(check);
17-
})
4+
export default angular.module('app.components', []);
5+
export {CommentController} from './comment/comment.component';
6+
export {CommentsController} from './comment-list/comments.component';

src/decorators.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
11
/**
22
* Created by voland on 4/2/16.
33
*/
4-
export default function Component(moduleOrName: string | ng.IModule, selector: string, options: {
4+
5+
export function Component(options: {
6+
selector: string,
57
controllerAs?: string,
68
template?: string,
79
templateUrl?: string,
810
bindings? : any
9-
}) {
11+
}, moduleOrName: string | ng.IModule = 'app.components') {
1012
return (controller: Function) => {
13+
var selector = options.selector;
1114
var module = typeof moduleOrName === "string"
1215
? angular.module(moduleOrName)
1316
: moduleOrName;
17+
delete options.selector;
1418
module.component(selector, angular.extend(options, { controller: controller }));
1519
}
20+
}
21+
22+
export function Service(moduleOrName: string | ng.IModule = 'app.services') {
23+
return (service: any) => {
24+
var name = service.name;
25+
var module = typeof moduleOrName === "string"
26+
? angular.module(moduleOrName)
27+
: moduleOrName;
28+
module.service(name, service);
29+
}
30+
}
31+
32+
export interface PipeTransform {
33+
transform(value: any, ...args: any[]): any;
34+
}
35+
36+
export function Pipe(options: {name: string}, moduleOrName: string | ng.IModule = 'app.pipes') {
37+
return (Pipe: any) => {
38+
var instance = new Pipe();
39+
var module = typeof moduleOrName === "string"
40+
? angular.module(moduleOrName)
41+
: moduleOrName;
42+
module.filter(options.name, () => instance.transform);
43+
}
1644
}

src/main.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
* Created by voland on 4/2/16.
33
*/
44
import 'angular';
5-
import 'angular-sanitize';
65
import 'angular-ui-router';
6+
import 'angular-sanitize';
77
import 'ng-tags-input';
88

9-
import services from './services/services.module';
9+
10+
import services from './services/services.module';
1011
import components from './components/components.module';
11-
import './components/comment/comment.component';
12-
import './components/comment-list/comments.component';
12+
import pipes from './pipes/pipes.module';
1313

1414
function appConfig ($urlRouterProvider, $stateProvider, tagsInputConfigProvider) {
1515
$urlRouterProvider.otherwise('/');
@@ -30,16 +30,18 @@ function appConfig ($urlRouterProvider, $stateProvider, tagsInputConfigProvider)
3030
appConfig.$inject = ['$urlRouterProvider', '$stateProvider', 'tagsInputConfigProvider'];
3131

3232
// configure the main module
33-
angular.module('app', [
34-
'ngTagsInput',
35-
'ngSanitize',
36-
'ui.router',
37-
services.name,
38-
components.name
39-
])
40-
.config(appConfig);
33+
angular
34+
.module('app', [
35+
'ui.router',
36+
'ngTagsInput',
37+
'ngSanitize',
38+
services.name,
39+
components.name,
40+
pipes.name
41+
])
42+
.config(appConfig);
4143

4244
// bootstrap angular
4345
angular.element(document).ready(() => {
4446
angular.bootstrap(document, ['app']);
45-
});
47+
});

src/pipes/filterByTags.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Created by voland on 4/2/16.
3+
*/
4+
5+
import {PipeTransform, Pipe} from '../decorators';
6+
7+
@Pipe({name: 'filterByTags'})
8+
export class FilterByTagsPipe implements PipeTransform {
9+
transform(comments:any, tags:any) {
10+
if (!tags.length) return comments;
11+
function check(comment) {
12+
let filterArray = tags.map((tag: any) => tag.text);
13+
let findCount = filterArray
14+
.map((tag) => comment.tags.indexOf(tag) > -1 ? 1 : 0)
15+
.reduce((prev, curr) => prev + curr);
16+
return findCount === filterArray.length;
17+
}
18+
return comments.filter(check);
19+
}
20+
}

src/pipes/pipes.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export default angular.module('app.pipes', []);
2+
export {FilterByTagsPipe} from './filterByTags';

src/services/comments.service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
* Created by voland on 4/2/16.
33
*/
44
import {IComment} from "../interfaces";
5+
import {Service} from "../decorators";
56

7+
@Service()
68
export class Comments {
79

810
static $inject = ['$http'];

src/services/services.module.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/**
22
* Created by voland on 4/2/16.
33
*/
4-
import {Comments} from './comments.service'
5-
6-
export default angular.module('app.services', [])
7-
.service('Comments', Comments)
4+
export default angular.module('app.services', []);
5+
export {Comments} from './comments.service'

0 commit comments

Comments
 (0)