| OLD | NEW |
| (Empty) |
| 1 library portfolio; | |
| 2 | |
| 3 import 'dart:html'; | |
| 4 | |
| 5 import 'package:web_ui/web_ui.dart'; | |
| 6 import 'package:route_hierarchical/client.dart'; | |
| 7 | |
| 8 import 'index.dart'; | |
| 9 import 'data.dart' as data; | |
| 10 | |
| 11 | |
| 12 class PortfolioComponent extends WebComponent { | |
| 13 RouteHandle route; | |
| 14 RouteHandle companyRoute; | |
| 15 @observable var tabs = toObservable([]); | |
| 16 @observable var activeTab; | |
| 17 @observable var companies; | |
| 18 @observable String searchQuery; | |
| 19 | |
| 20 inserted() { | |
| 21 tabs.add({ | |
| 22 'name': 'Portfolio', | |
| 23 'link': router.url('list', startingFrom: route) | |
| 24 }); | |
| 25 | |
| 26 route.getRoute('list').onRoute.listen((RouteEvent e) { | |
| 27 searchQuery = | |
| 28 e.parameters.containsKey('query') ? e.parameters['query'] : ''; | |
| 29 activeTab = tabs[0]; | |
| 30 if (e.path != '/list') { | |
| 31 router.go('list', {}, startingFrom: route, replace: true); | |
| 32 } else { | |
| 33 companies = null; | |
| 34 data.fetchCompanies(searchQuery).then((result) => companies = result); | |
| 35 } | |
| 36 }); | |
| 37 companyRoute = route.getRoute('company'); | |
| 38 companyRoute.onRoute.listen(showCompanyTab); | |
| 39 } | |
| 40 | |
| 41 removed() { | |
| 42 route.discard(); | |
| 43 } | |
| 44 | |
| 45 void showCompanyTab(RouteEvent e) { | |
| 46 var tokenInt = int.parse(e.parameters['tabId']); | |
| 47 // If it's one of the current tabs, we show that tab | |
| 48 for (var tab in tabs) { | |
| 49 if (tab['userValue'] != null && tab['userValue']['id'] == tokenInt) { | |
| 50 activeTab = tab; | |
| 51 return; | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 // Otherwise we try to load the company | |
| 56 var newTab = toObservable({ | |
| 57 'name': 'Loading...', | |
| 58 }); | |
| 59 tabs.add(newTab); | |
| 60 activeTab = tabs[tabs.length - 1]; | |
| 61 data.fetchCompany(tokenInt).then((company) { | |
| 62 if (company != null) { | |
| 63 newTab['name'] = company['name']; | |
| 64 newTab['userValue'] = company; | |
| 65 newTab['link'] = companyLink(company); | |
| 66 } else { | |
| 67 // TODO: show a message that company id is invalid or something | |
| 68 } | |
| 69 }); | |
| 70 } | |
| 71 | |
| 72 void openCompany(company, MouseEvent e) { | |
| 73 if (e != null) { | |
| 74 e.preventDefault(); | |
| 75 } | |
| 76 // set new history token... | |
| 77 router.go('company', { | |
| 78 'tabId': '${company['id']}' | |
| 79 }, startingFrom: route); | |
| 80 } | |
| 81 | |
| 82 String companyLink(company) { | |
| 83 return router.url('company', | |
| 84 parameters: {'tabId': '${company['id']}'}, startingFrom: route); | |
| 85 } | |
| 86 | |
| 87 String activeClass(tab) { | |
| 88 if (tab == activeTab) { | |
| 89 return "active"; | |
| 90 } | |
| 91 return ""; | |
| 92 } | |
| 93 | |
| 94 void search() { | |
| 95 router.go('list', {'list.query': searchQuery}, startingFrom: route); | |
| 96 } | |
| 97 } | |
| OLD | NEW |