2018-08-01 08:39:07 +00:00
|
|
|
// 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';
|
2018-08-01 08:39:07 +00:00
|
|
|
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';
|
2018-08-01 08:39:07 +00:00
|
|
|
import './pages/not_found';
|
|
|
|
|
2018-08-19 18:55:26 +00:00
|
|
|
// components
|
|
|
|
import { ProfileDialog } from './components/profile_dialog';
|
|
|
|
|
2018-08-01 08:39:07 +00:00
|
|
|
// helpers
|
|
|
|
import './hub_call';
|
2018-08-17 10:34:14 +00:00
|
|
|
import EventEmitter from './event_emitter';
|
2018-08-01 08:39:07 +00:00
|
|
|
|
2018-07-26 13:07:01 +00:00
|
|
|
frappe.provide('hub');
|
|
|
|
frappe.provide('erpnext.hub');
|
|
|
|
|
2018-08-17 10:34:14 +00:00
|
|
|
$.extend(erpnext.hub, EventEmitter.prototype);
|
|
|
|
|
2018-07-26 13:07:01 +00:00
|
|
|
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;
|
2018-07-26 13:07:01 +00:00
|
|
|
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);
|
|
|
|
});
|
2018-08-19 18:55:26 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
})
|
2018-07-26 13:07:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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">
|
2018-07-26 13:07:01 +00:00
|
|
|
${__('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')}
|
2018-07-26 13:07:01 +00:00
|
|
|
</li>`
|
|
|
|
|
2018-08-19 18:55:26 +00:00
|
|
|
: `<li class="hub-sidebar-item text-muted" data-action="show_register_dialog">
|
2018-07-26 13:07:01 +00:00
|
|
|
${__('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')
|
2018-07-31 12:32:13 +00:00
|
|
|
.then(categories => {
|
2018-07-31 13:36:11 +00:00
|
|
|
categories = categories.map(d => d.name);
|
|
|
|
|
2018-07-26 13:07:01 +00:00
|
|
|
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', () => {
|
2018-07-26 13:07:01 +00:00
|
|
|
this.registered = 1;
|
|
|
|
this.make_sidebar_nav_buttons();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
update_sidebar() {
|
2018-08-01 13:49:53 +00:00
|
|
|
const route = frappe.get_route();
|
2018-08-21 13:42:10 +00:00
|
|
|
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}"]`);
|
|
|
|
|
2018-07-26 13:07:01 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-07-27 15:37:56 +00:00
|
|
|
if (route[1] === 'search' && !this.subpages.search) {
|
2018-07-27 10:03:00 +00:00
|
|
|
this.subpages.search = new erpnext.hub.SearchPage(this.$body);
|
|
|
|
}
|
|
|
|
|
2018-07-26 13:07:01 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-07-26 13:07:01 +00:00
|
|
|
if (route[1] === 'register' && !this.subpages.register) {
|
2018-07-26 13:16:38 +00:00
|
|
|
if (this.registered) {
|
|
|
|
frappe.set_route('marketplace', 'home');
|
|
|
|
return;
|
|
|
|
}
|
2018-07-26 13:07:01 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-07-26 13:16:38 +00:00
|
|
|
if (route[1] === 'profile' && !this.subpages.profile) {
|
2018-07-27 20:24:06 +00:00
|
|
|
this.subpages.profile = new erpnext.hub.Profile(this.$body);
|
2018-07-26 13:07:01 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 13:16:38 +00:00
|
|
|
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-07-26 13:16:38 +00:00
|
|
|
}
|
2018-07-26 13:07:01 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-26 13:07:01 +00:00
|
|
|
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';
|
2018-07-26 13:07:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.update_sidebar();
|
|
|
|
frappe.utils.scroll_to(0);
|
|
|
|
this.subpages[route[1]].show();
|
|
|
|
}
|
2018-08-19 18:55:26 +00:00
|
|
|
|
|
|
|
show_register_dialog() {
|
2018-08-19 19:48:41 +00:00
|
|
|
this.register_dialog = ProfileDialog(
|
|
|
|
__('Become a Seller'),
|
|
|
|
{
|
|
|
|
label: __('Register'),
|
|
|
|
on_submit: this.register_seller.bind(this)
|
|
|
|
}
|
|
|
|
);
|
2018-08-19 18:55:26 +00:00
|
|
|
|
2018-08-19 19:48:41 +00:00
|
|
|
this.register_dialog.show();
|
2018-08-19 18:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
register_seller(form_values) {
|
|
|
|
frappe.call({
|
|
|
|
method: 'erpnext.hub_node.doctype.hub_settings.hub_settings.register_seller',
|
|
|
|
args: form_values,
|
|
|
|
btn: $(e.currentTarget)
|
|
|
|
}).then(() => {
|
2018-08-19 19:48:41 +00:00
|
|
|
this.register_dialog.hide();
|
2018-08-19 18:55:26 +00:00
|
|
|
frappe.set_route('marketplace', 'publish');
|
|
|
|
|
2018-08-20 13:03:46 +00:00
|
|
|
erpnext.hub.trigger('seller-registered');
|
2018-08-19 18:55:26 +00:00
|
|
|
});
|
|
|
|
}
|
2018-07-26 13:07:01 +00:00
|
|
|
}
|