Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 1 | <template> |
| 2 | <div class="hub-page-container"> |
| 3 | <component :is="current_page"></component> |
| 4 | </div> |
| 5 | </template> |
| 6 | <script> |
| 7 | import Home from './pages/Home.vue'; |
| 8 | import SavedProducts from './pages/SavedProducts.vue'; |
| 9 | import Publish from './pages/Publish.vue'; |
| 10 | import Category from './pages/Category.vue'; |
| 11 | import Search from './pages/Search.vue'; |
| 12 | import PublishedProducts from './pages/PublishedProducts.vue'; |
Faris Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 13 | import Buying from './pages/Buying.vue'; |
| 14 | import BuyingMessages from './pages/BuyingMessages.vue'; |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 15 | |
| 16 | const route_map = { |
| 17 | 'marketplace/home': Home, |
| 18 | 'marketplace/saved-products': SavedProducts, |
Faris Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 19 | 'marketplace/my-products': PublishedProducts, |
| 20 | 'marketplace/publish': Publish, |
| 21 | 'marketplace/category/:category': Category, |
| 22 | 'marketplace/search/:keyword': Search, |
| 23 | 'marketplace/buying': Buying, |
| 24 | 'marketplace/buying/:item': BuyingMessages |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | export default { |
| 28 | data() { |
| 29 | return { |
| 30 | current_page: this.get_current_page() |
| 31 | } |
| 32 | }, |
| 33 | mounted() { |
| 34 | frappe.route.on('change', () => { |
| 35 | this.set_current_page(); |
| 36 | }); |
| 37 | }, |
| 38 | methods: { |
| 39 | set_current_page() { |
| 40 | this.current_page = this.get_current_page(); |
| 41 | }, |
| 42 | get_current_page() { |
Faris Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 43 | const curr_route = frappe.get_route_str(); |
| 44 | let route = Object.keys(route_map).filter(route => route == curr_route)[0]; |
| 45 | |
| 46 | if (!route) { |
| 47 | // find route by matching it with dynamic part |
| 48 | const curr_route_parts = curr_route.split('/'); |
| 49 | const weighted_routes = Object.keys(route_map) |
| 50 | .map(route_str => route_str.split('/')) |
| 51 | .filter(route_parts => route_parts.length === curr_route_parts.length) |
| 52 | .reduce((obj, route_parts) => { |
| 53 | const key = route_parts.join('/'); |
| 54 | let weight = 0; |
| 55 | route_parts.forEach((part, i) => { |
| 56 | const curr_route_part = curr_route_parts[i]; |
| 57 | if (part === curr_route_part || curr_route_part.includes(':')) { |
| 58 | weight += 1; |
| 59 | } |
| 60 | }); |
| 61 | |
| 62 | obj[key] = weight; |
| 63 | return obj; |
| 64 | }, {}); |
| 65 | |
| 66 | // get the route with the highest weight |
| 67 | let weight = 0 |
| 68 | for (let key in weighted_routes) { |
| 69 | const route_weight = weighted_routes[key]; |
| 70 | if (route_weight > weight) { |
| 71 | route = key; |
| 72 | weight = route_weight; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return route_map[route]; |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
| 81 | </script> |