brotherton-erpnext/erpnext/public/js/hub/marketplace.js

267 lines
7.0 KiB
JavaScript
Raw Normal View History

// pages
import './pages/home';
import './pages/favourites';
import './pages/search';
import './pages/category';
import './pages/item';
2018-08-01 20:12:23 +00:00
import './pages/seller';
import './pages/register';
import './pages/profile';
import './pages/publish';
import './pages/published_products';
2018-08-01 10:59:40 +00:00
import './pages/messages';
2018-08-20 13:03:46 +00:00
import './pages/buying_messages';
import './pages/not_found';
// components
import { ProfileDialog } from './components/profile_dialog';
// helpers
import './hub_call';
import EventEmitter from './event_emitter';
frappe.provide('hub');
frappe.provide('erpnext.hub');
$.extend(erpnext.hub, EventEmitter.prototype);
erpnext.hub.Marketplace = class Marketplace {
constructor({ parent }) {
this.$parent = $(parent);
this.page = parent.page;
frappe.db.get_doc('Hub Settings')
.then(doc => {
2018-07-27 20:24:06 +00:00
hub.settings = doc;
this.registered = doc.registered;
this.setup_header();
this.make_sidebar();
this.make_body();
this.setup_events();
this.refresh();
});
}
setup_header() {
this.page.set_title(__('Marketplace'));
}
setup_events() {
this.$parent.on('click', '[data-route]', (e) => {
const $target = $(e.currentTarget);
const route = $target.data().route;
frappe.set_route(route);
});
// generic action handler
this.$parent.on('click', '[data-action]', e => {
const $target = $(e.currentTarget);
const action = $target.data().action;
if (action && this[action]) {
this[action].apply(this, $target);
}
})
}
make_sidebar() {
this.$sidebar = this.$parent.find('.layout-side-section').addClass('hidden-xs');
this.make_sidebar_nav_buttons();
this.make_sidebar_categories();
}
make_sidebar_nav_buttons() {
let $nav_group = this.$sidebar.find('[data-nav-buttons]');
if (!$nav_group.length) {
$nav_group = $('<ul class="list-unstyled hub-sidebar-group" data-nav-buttons>').appendTo(this.$sidebar);
}
$nav_group.empty();
const user_specific_items_html = this.registered
2018-08-01 10:59:40 +00:00
? `<li class="hub-sidebar-item" data-route="marketplace/favourites">
${__('Favorites')}
</li>
<li class="hub-sidebar-item text-muted" data-route="marketplace/profile">
${__('Your Profile')}
</li>
<li class="hub-sidebar-item text-muted" data-route="marketplace/publish">
${__('Publish Products')}
2018-08-01 10:59:40 +00:00
</li>
<li class="hub-sidebar-item text-muted" data-route="marketplace/messages">
${__('Messages')}
</li>`
: `<li class="hub-sidebar-item text-muted" data-action="show_register_dialog">
${__('Become a seller')}
</li>`;
$nav_group.append(`
<li class="hub-sidebar-item" data-route="marketplace/home">
${__('Browse')}
</li>
${user_specific_items_html}
`);
}
make_sidebar_categories() {
2018-07-31 13:36:11 +00:00
hub.call('get_categories')
.then(categories => {
2018-07-31 13:36:11 +00:00
categories = categories.map(d => d.name);
const sidebar_items = [
`<li class="hub-sidebar-item bold is-title">
${__('Category')}
</li>`,
`<li class="hub-sidebar-item active" data-route="marketplace/home">
${__('All')}
</li>`,
...(this.registered
? [`<li class="hub-sidebar-item active" data-route="marketplace/my-products">
${__('Your Products')}
</li>`]
: []),
...categories.map(category => `
<li class="hub-sidebar-item text-muted" data-route="marketplace/category/${category}">
${__(category)}
</li>
`)
];
this.$sidebar.append(`
<ul class="list-unstyled">
${sidebar_items.join('')}
</ul>
`);
this.update_sidebar();
});
}
make_body() {
this.$body = this.$parent.find('.layout-main-section');
2018-08-20 13:03:46 +00:00
erpnext.hub.on('seller-registered', () => {
this.registered = 1;
this.make_sidebar_nav_buttons();
});
}
update_sidebar() {
2018-08-01 13:49:53 +00:00
const route = frappe.get_route();
const route_str = route.join('/');
const part_route_str = route.slice(0, 2).join('/');
const $sidebar_item = this.$sidebar.find(`[data-route="${route_str}"], [data-route="${part_route_str}"]`);
const $siblings = this.$sidebar.find('[data-route]');
$siblings.removeClass('active').addClass('text-muted');
$sidebar_item.addClass('active').removeClass('text-muted');
}
refresh() {
const route = frappe.get_route();
this.subpages = this.subpages || {};
for (let page in this.subpages) {
this.subpages[page].hide();
}
if (route[1] === 'home' && !this.subpages.home) {
this.subpages.home = new erpnext.hub.Home(this.$body);
}
if (route[1] === 'search' && !this.subpages.search) {
2018-07-27 10:03:00 +00:00
this.subpages.search = new erpnext.hub.SearchPage(this.$body);
}
if (route[1] === 'category' && route[2] && !this.subpages.category) {
this.subpages.category = new erpnext.hub.Category(this.$body);
}
if (route[1] === 'item' && route[2] && !this.subpages.item) {
this.subpages.item = new erpnext.hub.Item(this.$body);
}
2018-08-01 20:12:23 +00:00
if (route[1] === 'seller' && !this.subpages['seller']) {
this.subpages['seller'] = new erpnext.hub.Seller(this.$body);
}
if (route[1] === 'register' && !this.subpages.register) {
if (this.registered) {
frappe.set_route('marketplace', 'home');
return;
}
this.subpages.register = new erpnext.hub.Register(this.$body);
}
2018-08-01 10:59:40 +00:00
// registered seller routes
if (route[1] === 'favourites' && !this.subpages.favourites) {
this.subpages.favourites = new erpnext.hub.Favourites(this.$body);
}
if (route[1] === 'profile' && !this.subpages.profile) {
2018-07-27 20:24:06 +00:00
this.subpages.profile = new erpnext.hub.Profile(this.$body);
}
if (route[1] === 'publish' && !this.subpages.publish) {
2018-07-27 20:24:06 +00:00
this.subpages.publish = new erpnext.hub.Publish(this.$body);
}
if (route[1] === 'my-products' && !this.subpages['my-products']) {
this.subpages['my-products'] = new erpnext.hub.PublishedProducts(this.$body);
}
2018-08-01 10:59:40 +00:00
if (route[1] === 'messages' && !this.subpages['messages']) {
this.subpages['messages'] = new erpnext.hub.Messages(this.$body);
}
2018-08-20 13:03:46 +00:00
if (route[1] === 'buy' && !this.subpages['buy']) {
this.subpages['buy'] = new erpnext.hub.BuyingMessages(this.$body);
}
2018-08-01 10:59:40 +00:00
// dont allow unregistered users to access registered routes
const registered_routes = ['favourites', 'profile', 'publish', 'my-products', 'messages'];
if (!hub.settings.registered && registered_routes.includes(route[1])) {
frappe.set_route('marketplace', 'home');
return;
}
if (!Object.keys(this.subpages).includes(route[1])) {
2018-07-26 13:52:58 +00:00
if (!this.subpages.not_found) {
this.subpages.not_found = new erpnext.hub.NotFound(this.$body);
}
route[1] = 'not_found';
}
this.update_sidebar();
frappe.utils.scroll_to(0);
this.subpages[route[1]].show();
}
show_register_dialog() {
this.register_dialog = ProfileDialog(
__('Become a Seller'),
{
label: __('Register'),
on_submit: this.register_seller.bind(this)
}
);
this.register_dialog.show();
}
register_seller(form_values) {
frappe.call({
method: 'erpnext.hub_node.doctype.hub_settings.hub_settings.register_seller',
args: form_values,
btn: $(e.currentTarget)
}).then(() => {
this.register_dialog.hide();
frappe.set_route('marketplace', 'publish');
2018-08-20 13:03:46 +00:00
erpnext.hub.trigger('seller-registered');
});
}
}