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'; |
Faris Ansari | 725603c | 2018-08-27 19:51:36 +0530 | [diff] [blame] | 18 | import Selling from './pages/Selling.vue'; |
| 19 | import Messages from './pages/Messages.vue'; |
Prateeksha Singh | d0a952b | 2018-08-27 10:12:45 +0530 | [diff] [blame] | 20 | import Profile from './pages/Profile.vue'; |
| 21 | import NotFound from './pages/NotFound.vue'; |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 22 | |
| 23 | const route_map = { |
| 24 | 'marketplace/home': Home, |
Faris Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 25 | 'marketplace/search/:keyword': Search, |
Prateeksha Singh | d0a952b | 2018-08-27 10:12:45 +0530 | [diff] [blame] | 26 | '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 Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 36 | 'marketplace/buying': Buying, |
Faris Ansari | 725603c | 2018-08-27 19:51:36 +0530 | [diff] [blame] | 37 | 'marketplace/buying/:item': Messages, |
| 38 | 'marketplace/selling': Selling, |
| 39 | 'marketplace/selling/:buyer/:item': Messages |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | export 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 Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 58 | 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 Ansari | f089dad | 2018-08-26 22:20:16 +0530 | [diff] [blame] | 92 | if (!route) { |
| 93 | return NotFound; |
| 94 | } |
| 95 | |
Faris Ansari | fad623e | 2018-08-26 19:48:52 +0530 | [diff] [blame] | 96 | return route_map[route]; |
Faris Ansari | 888dd60 | 2018-08-26 12:41:02 +0530 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | } |
| 100 | </script> |