Skip to content

Commit b361c63

Browse files
author
500Tech
committed
restructuring application
1 parent f228619 commit b361c63

File tree

12 files changed

+101
-53
lines changed

12 files changed

+101
-53
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"scripts": {
2828
"postinstall": "typings install",
2929
"prestart": "npm install",
30-
"start": "webpack-dev-server --hot",
30+
"start": "webpack-dev-server --hot --port 8081",
3131
"typings": "typings",
3232
"bundle": "rimraf dist & copyfiles -f src/index.html dist/ & copyfiles -f src/assets/* dist/assets & webpack -p ./dist/bundle.js"
3333
}

src/app.config.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Created by 500tech on 7/14/16.
3+
*/
4+
export const appName = 'app';
5+
6+
export function appConfig($urlRouterProvider, $stateProvider, tagsInputConfigProvider) {
7+
$urlRouterProvider.otherwise('/');
8+
$stateProvider
9+
.state('root', {
10+
url: '/',
11+
template: `<${appName}></${appName}>`
12+
});
13+
tagsInputConfigProvider
14+
.setDefaults('tagsInput', {
15+
placeholder: 'Search tags',
16+
addFromAutocompleteOnly: true
17+
})
18+
.setDefaults('autoComplete', {
19+
minLength: 1
20+
})
21+
}
22+
appConfig.$inject = ['$urlRouterProvider', '$stateProvider', 'tagsInputConfigProvider'];

src/components/app.component.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {Component} from '../decorators';
2+
import {appName} from "../app.config";
3+
import {CommentsComponent} from "./comment-list/comments.component";
4+
5+
@Component({
6+
selector: appName,
7+
directives: [CommentsComponent],
8+
template: `<comments></comments>`
9+
})
10+
export class AppComponent {
11+
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
import {Component} from '../../decorators';
55
import './comment-list.scss';
66
import {IComment} from "../../interfaces";
7+
import {CommentComponent} from "../comment/comment.component";
8+
import {CommentsService} from "../../services/comments.service";
9+
import {FilterByTagsPipe} from "../../pipes/filterByTags";
710

811
@Component({
912
selector: 'comments',
13+
directives: [CommentComponent],
14+
providers: [CommentsService],
15+
pipes: [FilterByTagsPipe],
1016
template: `
1117
<div class="container-fluid">
1218
<div class="discussion-timeline">
@@ -18,17 +24,17 @@ import {IComment} from "../../interfaces";
1824
</div>
1925
</div>`
2026
})
21-
export class CommentsController {
27+
export class CommentsComponent {
2228
comments: IComment[];
2329
emptyComment: IComment;
2430
tags: string[];
2531
tagFilter: any[];
2632

27-
static $inject = ['Comments'];
28-
constructor(private Comments) {
33+
static $inject = ['CommentsService'];
34+
constructor(private CommentsService) {
2935
this.emptyComment = {};
3036
this.tagFilter = [];
31-
Comments.getComments().then((comments) => {
37+
CommentsService.getComments().then((comments) => {
3238
this.comments = comments;
3339
this.tags = this.comments
3440
.map((el) => el.tags)

src/components/comment/comment.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import {IComment} from "../../interfaces";
5555
</div>
5656
</div>`
5757
})
58-
export class CommentController {
58+
export class CommentComponent {
5959
editMode: boolean;
6060
comment: IComment;
6161
commentCopy: IComment;

src/components/components.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
* Created by vlad on 5/29/16.
33
*/
44
export default angular.module('app.components', []);
5-
export {CommentController} from './comment/comment.component';
6-
export {CommentsController} from './comment-list/comments.component';
5+
// export {CommentController} from './comment/comment.component';
6+
// export {CommentsController} from './comment-list/comments.component';

src/decorators.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/**
2-
* Created by voland on 4/2/16.
3-
*/
1+
import {appName} from "./app.config";
42

53
const module = function(moduleOrName) {
64
return typeof moduleOrName === "string"
@@ -13,36 +11,58 @@ export function Component(options: {
1311
controllerAs?: string,
1412
template?: string,
1513
templateUrl?: string,
16-
bindings? : any
17-
}, moduleOrName: string | ng.IModule = 'app.components') {
18-
return (controller: Function) => {
19-
let selector = options.selector;
14+
bindings?: any,
15+
directives?: any[]
16+
pipes?: any[]
17+
providers?: any[]
18+
}, moduleOrName: string | ng.IModule = `${appName}.components`) {
19+
return (controller: any) => {
20+
const selector = options.selector;
2021
delete options.selector;
22+
delete options.directives;
23+
delete options.pipes;
24+
delete options.providers;
2125
module(moduleOrName).component(selector, angular.extend(options, { controller: controller }));
2226
}
2327
}
2428

25-
export function Service(moduleOrName: string | ng.IModule = 'app.services') {
29+
export function Service(moduleOrName: string | ng.IModule = `${appName}.services`) {
2630
return (service: any) => {
27-
let name = service.name;
31+
const name = service.name;
32+
const isProvider = service.hasOwnProperty('$get');
2833
if (!name) {
2934
console.error('Service decorator can be used with named class only');
3035
}
31-
module(moduleOrName).service(name, service);
36+
module(moduleOrName)[isProvider ? 'provider' : 'service'](name, service);
3237
}
3338
}
3439

40+
interface PipeTransformStatic {
41+
new(...args: any[]): PipeTransform;
42+
}
43+
3544
export interface PipeTransform {
3645
transform(value: any, ...args: any[]): any;
3746
}
3847

39-
interface PipeTransformStatic {
40-
new(...args: any[]): PipeTransform;
48+
export function Pipe(options: {name: string}, moduleOrName: string | ng.IModule = `${appName}.pipes`) {
49+
return (Pipe: PipeTransformStatic) => {
50+
const filter = () => {
51+
const $injector = angular.injector(['ng']);
52+
const instance:any = $injector.instantiate(Pipe);
53+
return instance.transform.bind(instance);
54+
};
55+
module(moduleOrName).filter(options.name, filter);
56+
}
4157
}
4258

43-
export function Pipe(options: {name: string}, moduleOrName: string | ng.IModule = 'app.pipes') {
44-
return (Pipe: PipeTransformStatic) => {
45-
let instance = new Pipe();
46-
module(moduleOrName).filter(options.name, () => instance.transform);
59+
export function Bootstrap(appName: string, appClass: any) {
60+
return (anything: any) => {
61+
if (!appClass) {
62+
console.error(`Please provide main component class as a second argument to @Bootstrap decorator`);
63+
}
64+
angular.element(document).ready(() => {
65+
angular.bootstrap(document, [appName]);
66+
});
4767
}
4868
}

src/main.ts

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,16 @@ import 'angular';
55
import 'angular-ui-router';
66
import 'angular-sanitize';
77
import 'ng-tags-input';
8-
9-
108
import services from './services/services.module';
119
import components from './components/components.module';
1210
import pipes from './pipes/pipes.module';
11+
import {appConfig, appName} from "./app.config";
12+
import {AppComponent} from "./components/app.component";
13+
import {Bootstrap} from "./decorators";
1314

14-
function appConfig ($urlRouterProvider, $stateProvider, tagsInputConfigProvider) {
15-
$urlRouterProvider.otherwise('/');
16-
$stateProvider
17-
.state('index', {
18-
url: '/',
19-
template: '<comments></comments>'
20-
});
21-
tagsInputConfigProvider
22-
.setDefaults('tagsInput', {
23-
placeholder: 'Search tags',
24-
addFromAutocompleteOnly: true
25-
})
26-
.setDefaults('autoComplete', {
27-
minLength: 1
28-
})
29-
}
30-
appConfig.$inject = ['$urlRouterProvider', '$stateProvider', 'tagsInputConfigProvider'];
31-
32-
// configure the main module
15+
// configure
3316
angular
34-
.module('app', [
17+
.module(appName, [
3518
'ui.router',
3619
'ngTagsInput',
3720
'ngSanitize',
@@ -41,7 +24,6 @@ angular
4124
])
4225
.config(appConfig);
4326

44-
// bootstrap angular
45-
angular.element(document).ready(() => {
46-
angular.bootstrap(document, ['app']);
47-
});
27+
// bootstrap
28+
@Bootstrap(appName, AppComponent)
29+
class App {}

src/pipes/filterByTags.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33
*/
44

55
import {PipeTransform, Pipe} from '../decorators';
6+
// import {CommentsService} from "../services/comments.service";
67

78
@Pipe({name: 'filterByTags'})
89
export class FilterByTagsPipe implements PipeTransform {
10+
static $inject = ['$q'];
11+
constructor(private _$q: ng.IQService) {
12+
}
13+
914
transform(comments: any, tags: any) {
15+
let deferred = this._$q.defer;
16+
// console.log(this._comments.getComments);
1017
if (!tags.length) return comments;
1118
function check(comment) {
1219
let filterArray = tags.map((tag: any) => tag.text);

src/pipes/pipes.module.ts

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

0 commit comments

Comments
 (0)