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> |
Prateeksha Singh | d0a952b | 2018-08-27 10:12:45 +0530 | [diff] [blame] | 6 | |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 7 | <script> |
Prateeksha Singh | d0a952b | 2018-08-27 10:12:45 +0530 | [diff] [blame] | 8 | |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 9 | import Home from './pages/Home.vue'; |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 10 | import Search from './pages/Search.vue'; |
Prateeksha Singh | d0a952b | 2018-08-27 10:12:45 +0530 | [diff] [blame] | 11 | import Category from './pages/Category.vue'; |
| 12 | import SavedProducts from './pages/SavedProducts.vue'; |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 13 | import PublishedProducts from './pages/PublishedProducts.vue'; |
Prateeksha Singh | d0a952b | 2018-08-27 10:12:45 +0530 | [diff] [blame] | 14 | import Item from './pages/Item.vue'; |
| 15 | import Seller from './pages/Seller.vue'; |
| 16 | import Publish from './pages/Publish.vue'; |
Faris Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 17 | import Buying from './pages/Buying.vue'; |
| 18 | import BuyingMessages from './pages/BuyingMessages.vue'; |
Prateeksha Singh | d0a952b | 2018-08-27 10:12:45 +0530 | [diff] [blame] | 19 | import Profile from './pages/Profile.vue'; |
| 20 | import NotFound from './pages/NotFound.vue'; |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 21 | |
| 22 | const route_map = { |
| 23 | 'marketplace/home': Home, |
Faris Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 24 | 'marketplace/search/:keyword': Search, |
Prateeksha Singh | d0a952b | 2018-08-27 10:12:45 +0530 | [diff] [blame] | 25 | '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 Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 35 | 'marketplace/buying': Buying, |
Prateeksha Singh | d0a952b | 2018-08-27 10:12:45 +0530 | [diff] [blame] | 36 | 'marketplace/buying/:item': BuyingMessages, |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | export 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 Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 55 | 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 Ansari | f089dad | 2018-08-26 22:20:16 +0530 | [diff] [blame] | 89 | if (!route) { |
| 90 | return NotFound; |
| 91 | } |
| 92 | |
Faris Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 93 | return route_map[route]; |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | } |
| 97 | </script> |