blob: e814be834b426488d048b3bdb54e2f2cdca51387 [file] [log] [blame]
Faris Ansari888dd602018-08-26 12:41:02 +05301<template>
2 <div class="hub-page-container">
3 <component :is="current_page"></component>
4 </div>
5</template>
Prateeksha Singhd0a952b2018-08-27 10:12:45 +05306
Faris Ansari888dd602018-08-26 12:41:02 +05307<script>
Prateeksha Singhd0a952b2018-08-27 10:12:45 +05308
Faris Ansari888dd602018-08-26 12:41:02 +05309import Home from './pages/Home.vue';
Faris Ansari888dd602018-08-26 12:41:02 +053010import Search from './pages/Search.vue';
Prateeksha Singhd0a952b2018-08-27 10:12:45 +053011import Category from './pages/Category.vue';
12import SavedProducts from './pages/SavedProducts.vue';
Faris Ansari888dd602018-08-26 12:41:02 +053013import PublishedProducts from './pages/PublishedProducts.vue';
Prateeksha Singhd0a952b2018-08-27 10:12:45 +053014import Item from './pages/Item.vue';
15import Seller from './pages/Seller.vue';
16import Publish from './pages/Publish.vue';
Faris Ansarifad623e2018-08-26 19:48:52 +053017import Buying from './pages/Buying.vue';
Faris Ansari725603c2018-08-27 19:51:36 +053018import Selling from './pages/Selling.vue';
19import Messages from './pages/Messages.vue';
Prateeksha Singhd0a952b2018-08-27 10:12:45 +053020import Profile from './pages/Profile.vue';
21import NotFound from './pages/NotFound.vue';
Faris Ansari888dd602018-08-26 12:41:02 +053022
23const route_map = {
24 'marketplace/home': Home,
Faris Ansarifad623e2018-08-26 19:48:52 +053025 'marketplace/search/:keyword': Search,
Prateeksha Singhd0a952b2018-08-27 10:12:45 +053026 'marketplace/category/:category': Category,
27 'marketplace/item/:item': Item,
28 'marketplace/seller/:seller': Seller,
29 'marketplace/not-found': NotFound,
30
31 // Registered seller routes
32 'marketplace/profile': Profile,
33 'marketplace/saved-products': SavedProducts,
34 'marketplace/publish': Publish,
35 'marketplace/my-products': PublishedProducts,
Faris Ansarifad623e2018-08-26 19:48:52 +053036 'marketplace/buying': Buying,
Faris Ansari725603c2018-08-27 19:51:36 +053037 'marketplace/buying/:item': Messages,
38 'marketplace/selling': Selling,
39 'marketplace/selling/:buyer/:item': Messages
Faris Ansari888dd602018-08-26 12:41:02 +053040}
41
42export default {
43 data() {
44 return {
45 current_page: this.get_current_page()
46 }
47 },
48 mounted() {
49 frappe.route.on('change', () => {
50 this.set_current_page();
51 });
52 },
53 methods: {
54 set_current_page() {
55 this.current_page = this.get_current_page();
56 },
57 get_current_page() {
Faris Ansarifad623e2018-08-26 19:48:52 +053058 const curr_route = frappe.get_route_str();
59 let route = Object.keys(route_map).filter(route => route == curr_route)[0];
60
61 if (!route) {
62 // find route by matching it with dynamic part
63 const curr_route_parts = curr_route.split('/');
64 const weighted_routes = Object.keys(route_map)
65 .map(route_str => route_str.split('/'))
66 .filter(route_parts => route_parts.length === curr_route_parts.length)
67 .reduce((obj, route_parts) => {
68 const key = route_parts.join('/');
69 let weight = 0;
70 route_parts.forEach((part, i) => {
71 const curr_route_part = curr_route_parts[i];
72 if (part === curr_route_part || curr_route_part.includes(':')) {
73 weight += 1;
74 }
75 });
76
77 obj[key] = weight;
78 return obj;
79 }, {});
80
81 // get the route with the highest weight
82 let weight = 0
83 for (let key in weighted_routes) {
84 const route_weight = weighted_routes[key];
85 if (route_weight > weight) {
86 route = key;
87 weight = route_weight;
88 }
89 }
90 }
91
Faris Ansarif089dad2018-08-26 22:20:16 +053092 if (!route) {
93 return NotFound;
94 }
95
Faris Ansarifad623e2018-08-26 19:48:52 +053096 return route_map[route];
Faris Ansari888dd602018-08-26 12:41:02 +053097 }
98 }
99}
100</script>