blob: bb9ba3e8d23fc53e5e0fd386141d2ac2fc7ee427 [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';
18import BuyingMessages from './pages/BuyingMessages.vue';
Prateeksha Singhd0a952b2018-08-27 10:12:45 +053019import Profile from './pages/Profile.vue';
20import NotFound from './pages/NotFound.vue';
Faris Ansari888dd602018-08-26 12:41:02 +053021
22const route_map = {
23 'marketplace/home': Home,
Faris Ansarifad623e2018-08-26 19:48:52 +053024 'marketplace/search/:keyword': Search,
Prateeksha Singhd0a952b2018-08-27 10:12:45 +053025 'marketplace/category/:category': Category,
26 'marketplace/item/:item': Item,
27 'marketplace/seller/:seller': Seller,
28 'marketplace/not-found': NotFound,
29
30 // Registered seller routes
31 'marketplace/profile': Profile,
32 'marketplace/saved-products': SavedProducts,
33 'marketplace/publish': Publish,
34 'marketplace/my-products': PublishedProducts,
Faris Ansarifad623e2018-08-26 19:48:52 +053035 'marketplace/buying': Buying,
Prateeksha Singhd0a952b2018-08-27 10:12:45 +053036 'marketplace/buying/:item': BuyingMessages,
Faris Ansari888dd602018-08-26 12:41:02 +053037}
38
39export default {
40 data() {
41 return {
42 current_page: this.get_current_page()
43 }
44 },
45 mounted() {
46 frappe.route.on('change', () => {
47 this.set_current_page();
48 });
49 },
50 methods: {
51 set_current_page() {
52 this.current_page = this.get_current_page();
53 },
54 get_current_page() {
Faris Ansarifad623e2018-08-26 19:48:52 +053055 const curr_route = frappe.get_route_str();
56 let route = Object.keys(route_map).filter(route => route == curr_route)[0];
57
58 if (!route) {
59 // find route by matching it with dynamic part
60 const curr_route_parts = curr_route.split('/');
61 const weighted_routes = Object.keys(route_map)
62 .map(route_str => route_str.split('/'))
63 .filter(route_parts => route_parts.length === curr_route_parts.length)
64 .reduce((obj, route_parts) => {
65 const key = route_parts.join('/');
66 let weight = 0;
67 route_parts.forEach((part, i) => {
68 const curr_route_part = curr_route_parts[i];
69 if (part === curr_route_part || curr_route_part.includes(':')) {
70 weight += 1;
71 }
72 });
73
74 obj[key] = weight;
75 return obj;
76 }, {});
77
78 // get the route with the highest weight
79 let weight = 0
80 for (let key in weighted_routes) {
81 const route_weight = weighted_routes[key];
82 if (route_weight > weight) {
83 route = key;
84 weight = route_weight;
85 }
86 }
87 }
88
Faris Ansarif089dad2018-08-26 22:20:16 +053089 if (!route) {
90 return NotFound;
91 }
92
Faris Ansarifad623e2018-08-26 19:48:52 +053093 return route_map[route];
Faris Ansari888dd602018-08-26 12:41:02 +053094 }
95 }
96}
97</script>