2015-12-10 16:24:34 +01:00
|
|
|
import {Component, Injectable} from 'angular2/core';
|
2015-07-09 10:25:58 -07:00
|
|
|
import {
|
|
|
|
RouterLink,
|
|
|
|
RouteConfig,
|
|
|
|
Router,
|
|
|
|
Route,
|
|
|
|
RouterOutlet,
|
|
|
|
Location,
|
|
|
|
RouteParams
|
|
|
|
} from 'angular2/router';
|
2015-08-12 13:03:28 -07:00
|
|
|
import * as db from './data';
|
2016-02-08 13:24:09 -08:00
|
|
|
import {ObservableWrapper, PromiseWrapper} from 'angular2/src/facade/async';
|
2015-11-06 17:34:07 -08:00
|
|
|
import {ListWrapper} from 'angular2/src/facade/collection';
|
|
|
|
import {isPresent, DateWrapper} from 'angular2/src/facade/lang';
|
2015-07-09 10:25:58 -07:00
|
|
|
|
|
|
|
class InboxRecord {
|
|
|
|
id: string = '';
|
|
|
|
subject: string = '';
|
|
|
|
content: string = '';
|
|
|
|
email: string = '';
|
|
|
|
firstName: string = '';
|
|
|
|
lastName: string = '';
|
2015-10-09 16:42:00 -07:00
|
|
|
date: string;
|
2015-07-09 10:25:58 -07:00
|
|
|
draft: boolean = false;
|
|
|
|
|
2015-10-06 19:05:09 -07:00
|
|
|
constructor(data: {
|
|
|
|
id: string,
|
|
|
|
subject: string,
|
|
|
|
content: string,
|
|
|
|
email: string,
|
|
|
|
firstName: string,
|
|
|
|
lastName: string,
|
|
|
|
date: string, draft?: boolean
|
|
|
|
} = null) {
|
2015-07-09 10:25:58 -07:00
|
|
|
if (isPresent(data)) {
|
|
|
|
this.setData(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-06 19:05:09 -07:00
|
|
|
setData(record: {
|
|
|
|
id: string,
|
|
|
|
subject: string,
|
|
|
|
content: string,
|
|
|
|
email: string,
|
|
|
|
firstName: string,
|
|
|
|
lastName: string,
|
|
|
|
date: string, draft?: boolean
|
|
|
|
}) {
|
2015-07-09 10:25:58 -07:00
|
|
|
this.id = record['id'];
|
|
|
|
this.subject = record['subject'];
|
|
|
|
this.content = record['content'];
|
|
|
|
this.email = record['email'];
|
|
|
|
this.firstName = record['first-name'];
|
|
|
|
this.lastName = record['last-name'];
|
|
|
|
this.date = record['date'];
|
2015-10-06 18:00:42 -07:00
|
|
|
this.draft = record['draft'] == true;
|
2015-07-09 10:25:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
class DbService {
|
2015-10-06 19:05:09 -07:00
|
|
|
getData(): Promise<any[]> {
|
2015-07-09 10:25:58 -07:00
|
|
|
var p = PromiseWrapper.completer();
|
2015-08-12 13:03:28 -07:00
|
|
|
p.resolve(db.data);
|
2015-07-09 10:25:58 -07:00
|
|
|
return p.promise;
|
|
|
|
}
|
|
|
|
|
2015-10-06 19:05:09 -07:00
|
|
|
drafts(): Promise<any[]> {
|
2015-10-09 09:07:58 -07:00
|
|
|
return PromiseWrapper.then(this.getData(), (data: any[]) => {
|
|
|
|
return data.filter(record => isPresent(record['draft']) && record['draft'] == true);
|
2015-07-09 10:25:58 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-10-06 19:05:09 -07:00
|
|
|
emails(): Promise<any[]> {
|
2015-10-09 09:07:58 -07:00
|
|
|
return PromiseWrapper.then(this.getData(), (data: any[]) => {
|
|
|
|
return data.filter(record => !isPresent(record['draft']));
|
2015-07-09 10:25:58 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-10-06 19:05:09 -07:00
|
|
|
email(id): Promise<any> {
|
2015-07-09 10:25:58 -07:00
|
|
|
return PromiseWrapper.then(this.getData(), (data) => {
|
|
|
|
for (var i = 0; i < data.length; i++) {
|
|
|
|
var entry = data[i];
|
|
|
|
if (entry['id'] == id) {
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:24:34 +01:00
|
|
|
@Component({selector: 'inbox-detail', directives: [RouterLink], templateUrl: 'inbox-detail.html'})
|
2015-07-09 10:25:58 -07:00
|
|
|
class InboxDetailCmp {
|
|
|
|
record: InboxRecord = new InboxRecord();
|
|
|
|
ready: boolean = false;
|
|
|
|
|
|
|
|
constructor(db: DbService, params: RouteParams) {
|
|
|
|
var id = params.get('id');
|
|
|
|
PromiseWrapper.then(db.email(id), (data) => { this.record.setData(data); });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:24:34 +01:00
|
|
|
@Component({selector: 'inbox', templateUrl: 'inbox.html', directives: [RouterLink]})
|
2015-07-09 10:25:58 -07:00
|
|
|
class InboxCmp {
|
2015-08-28 11:29:19 -07:00
|
|
|
items: InboxRecord[] = [];
|
2015-07-09 10:25:58 -07:00
|
|
|
ready: boolean = false;
|
|
|
|
|
2015-10-09 16:42:00 -07:00
|
|
|
constructor(public router: Router, db: DbService, params: RouteParams) {
|
|
|
|
var sortType = params.get('sort');
|
|
|
|
var sortEmailsByDate = isPresent(sortType) && sortType == "date";
|
|
|
|
|
2015-10-06 18:00:42 -07:00
|
|
|
PromiseWrapper.then(db.emails(), emails => {
|
2015-07-09 10:25:58 -07:00
|
|
|
this.ready = true;
|
2015-10-06 18:00:42 -07:00
|
|
|
this.items = emails.map(data => new InboxRecord(data));
|
2015-10-09 16:42:00 -07:00
|
|
|
|
|
|
|
if (sortEmailsByDate) {
|
|
|
|
ListWrapper.sort(this.items,
|
|
|
|
(a, b) => DateWrapper.toMillis(DateWrapper.fromISOString(a.date)) <
|
|
|
|
DateWrapper.toMillis(DateWrapper.fromISOString(b.date)) ?
|
|
|
|
-1 :
|
|
|
|
1);
|
|
|
|
}
|
2015-07-09 10:25:58 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-10 16:24:34 +01:00
|
|
|
@Component({selector: 'drafts', templateUrl: 'drafts.html', directives: [RouterLink]})
|
2015-07-09 10:25:58 -07:00
|
|
|
class DraftsCmp {
|
2015-08-28 11:29:19 -07:00
|
|
|
items: InboxRecord[] = [];
|
2015-07-09 10:25:58 -07:00
|
|
|
ready: boolean = false;
|
|
|
|
|
|
|
|
constructor(public router: Router, db: DbService) {
|
|
|
|
PromiseWrapper.then(db.drafts(), (drafts) => {
|
|
|
|
this.ready = true;
|
2015-10-06 18:00:42 -07:00
|
|
|
this.items = drafts.map(data => new InboxRecord(data));
|
2015-07-09 10:25:58 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:24:34 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'inbox-app',
|
|
|
|
viewProviders: [DbService],
|
|
|
|
templateUrl: 'inbox-app.html',
|
|
|
|
directives: [RouterOutlet, RouterLink]
|
|
|
|
})
|
2015-07-09 10:25:58 -07:00
|
|
|
@RouteConfig([
|
2015-10-25 15:00:27 +05:30
|
|
|
new Route({path: '/', component: InboxCmp, name: 'Inbox'}),
|
|
|
|
new Route({path: '/drafts', component: DraftsCmp, name: 'Drafts'}),
|
|
|
|
new Route({path: '/detail/:id', component: InboxDetailCmp, name: 'DetailPage'})
|
2015-07-09 10:25:58 -07:00
|
|
|
])
|
|
|
|
export class InboxApp {
|
|
|
|
router: Router;
|
|
|
|
location: Location;
|
|
|
|
constructor(router: Router, location: Location) {
|
|
|
|
this.router = router;
|
|
|
|
this.location = location;
|
|
|
|
}
|
|
|
|
inboxPageActive() { return this.location.path() == ''; }
|
|
|
|
draftsPageActive() { return this.location.path() == '/drafts'; }
|
|
|
|
}
|