[hub] Add hub category filters, fix minor issues (#11079)

This commit is contained in:
Faris Ansari 2017-10-05 19:43:08 +05:30 committed by Nabin Hait
parent 455c3ebb27
commit e3a5899980
3 changed files with 93 additions and 91 deletions

View File

@ -15,32 +15,40 @@ def enable_hub():
return hub_settings return hub_settings
@frappe.whitelist() @frappe.whitelist()
def get_items(start=0, page_length=20): def get_items(start=0, limit=20, category=None, order_by=None, text=None):
connection = get_connection() connection = get_connection()
response = connection.connection.get_list('Hub Item', limit_start=start, limit_page_length=page_length) filters = {
'hub_category': category,
}
if text:
filters.update({'item_name': ('like', '%' + text + '%')})
response = connection.get_list('Hub Item',
limit_start=start, limit_page_length=limit,
filters=filters)
return response return response
@frappe.whitelist() @frappe.whitelist()
def get_categories(): def get_categories():
connection = get_connection() connection = get_connection()
response = connection.get_list('Hub Category') response = connection.get_list('Hub Category')
return response.list return response
@frappe.whitelist() @frappe.whitelist()
def get_item_details(hub_sync_id): def get_item_details(hub_sync_id):
connection = get_connection() connection = get_connection()
return connection.connection.get_doc('Hub Item', hub_sync_id) return connection.get_doc('Hub Item', hub_sync_id)
@frappe.whitelist() @frappe.whitelist()
def get_company_details(hub_sync_id): def get_company_details(hub_sync_id):
connection = get_connection() connection = get_connection()
return connection.connection.get_doc('Hub Company', hub_sync_id) return connection.get_doc('Hub Company', hub_sync_id)
def get_connection(): def get_connection():
hub_connector = frappe.get_doc( hub_connector = frappe.get_doc(
'Data Migration Connector', 'Hub Connector') 'Data Migration Connector', 'Hub Connector')
hub_connection = hub_connector.get_connection() hub_connection = hub_connector.get_connection()
return hub_connection # frappeclient connection
return hub_connection.connection
def make_opportunity(buyer_name, email_id): def make_opportunity(buyer_name, email_id):
buyer_name = "HUB-" + buyer_name buyer_name = "HUB-" + buyer_name

View File

@ -62,9 +62,9 @@ class HubSettings(Document):
'Data Migration Connector', 'Hub Connector') 'Data Migration Connector', 'Hub Connector')
connection = hub_connector.get_connection() connection = hub_connector.get_connection()
response = connection.update('User', frappe._dict({'enabled': 0}), hub_connector.username) response_doc = connection.update('User', frappe._dict({'enabled': 0}), hub_connector.username)
if response.ok: if response_doc['enabled'] == 0:
self.enabled = 0 self.enabled = 0
self.save() self.save()

View File

@ -9,28 +9,26 @@ frappe.pages['hub'].on_page_load = function(wrapper) {
single_col: false single_col: false
}); });
erpnext.hub.Hub = new Hub({ page }); wrapper.hub_page = new erpnext.hub.Hub({ page });
}; };
frappe.pages['hub'].on_page_show = function(wrapper) { frappe.pages['hub'].on_page_show = function(wrapper) {
const current_route = frappe.get_route(); const hub_page = wrapper.hub_page;
if(current_route[1] === "Products") { const [hub, type, id] = frappe.get_route();
const item_code = current_route[2];
if(item_code) { if (!(hub || type || id)) {
erpnext.hub.Hub.go_to_item_page(item_code); hub_page.go_to_home_page();
} return;
} }
if(current_route[1] === "Company") { if (type === "Products") {
const company_name = current_route[2]; hub_page.go_to_item_page(id);
if(company_name) { } else if (type === "Company") {
erpnext.hub.Hub.go_to_company_page(company_name); hub_page.go_to_company_page(id);
}
} }
} }
window.Hub = class Hub { erpnext.hub.Hub = class Hub {
constructor({ page }) { constructor({ page }) {
this.page = page; this.page = page;
frappe.require('/assets/erpnext/css/hub.css', () => { frappe.require('/assets/erpnext/css/hub.css', () => {
@ -88,6 +86,9 @@ window.Hub = class Hub {
} }
register_for_hub() { register_for_hub() {
if (frappe.session.user.includes('Administrator')) {
frappe.throw(__('Please login as another user.'))
}
frappe.verify_password(() => { frappe.verify_password(() => {
frappe.call({ frappe.call({
method: 'erpnext.hub_node.enable_hub', method: 'erpnext.hub_node.enable_hub',
@ -204,7 +205,7 @@ window.Hub = class Hub {
this.refresh_item_only_page(); this.refresh_item_only_page();
}); });
this.$search = this.page.add_data(__('Search')); this.setup_hub_category_filter();
this.setup_search(); this.setup_search();
} }
@ -279,7 +280,7 @@ window.Hub = class Hub {
} }
setup_lists() { setup_lists() {
this.home_item_list = new HubList({ this.home_item_list = new erpnext.hub.HubList({
parent: this.$main_list_section, parent: this.$main_list_section,
title: 'New', title: 'New',
page_length: 20, page_length: 20,
@ -295,7 +296,44 @@ window.Hub = class Hub {
this.home_item_list.setup(); this.home_item_list.setup();
} }
setup_hub_category_filter() {
const me = this;
this.hub_category_field = this.page.add_field({
fieldtype: 'Autocomplete',
label: 'Hub Category',
change() {
let value = this.get_value();
let title = value;
if (value === 'All Categories') {
// show all items
value = null;
}
me.home_item_list.title = title;
me.home_item_list.refresh({
text: '',
start: 0,
limit: 20,
category: value
});
}
});
frappe.call('erpnext.hub_node.get_categories')
.then((r) => {
if (r.message) {
const categories = r.message;
this.hub_category_field.set_data(
categories.map(c => c.hub_category_name)
);
}
});
}
setup_search() { setup_search() {
this.$search = this.page.add_data(__('Search'));
this.$search.on('keypress', (e) => { this.$search.on('keypress', (e) => {
if(e.which === 13) { if(e.which === 13) {
var search_term = ($(this.$search).val() || '').toLowerCase(); var search_term = ($(this.$search).val() || '').toLowerCase();
@ -312,7 +350,7 @@ window.Hub = class Hub {
go_to_items_only_page(route, title, class_name, filters = {text: ''}, by_item_codes=0) { go_to_items_only_page(route, title, class_name, filters = {text: ''}, by_item_codes=0) {
frappe.set_route(route); frappe.set_route(route);
this.$hub_main_section.empty(); this.$hub_main_section.empty();
this.filtered_item_list = new HubList({ this.filtered_item_list = new erpnext.hub.HubList({
parent: this.$hub_main_section, parent: this.$hub_main_section,
title: title, title: title,
page_length: 20, page_length: 20,
@ -361,7 +399,7 @@ window.Hub = class Hub {
let $company_items = $item_page.find('.company-items'); let $company_items = $item_page.find('.company-items');
let company_item_list = new HubList({ let company_item_list = new erpnext.hub.HubList({
parent: $company_items, parent: $company_items,
title: 'More by ' + item.company_name, title: 'More by ' + item.company_name,
page_length: 5, page_length: 5,
@ -465,7 +503,7 @@ window.Hub = class Hub {
let $company_items = $company_page.find('.company-items'); let $company_items = $company_page.find('.company-items');
let company_item_list = new HubList({ let company_item_list = new erpnext.hub.HubList({
parent: $company_items, parent: $company_items,
title: 'More by ' + company_details.company_name, title: 'More by ' + company_details.company_name,
page_length: 5, page_length: 5,
@ -574,12 +612,14 @@ window.Hub = class Hub {
} }
setup_menu() { setup_menu() {
if (this.menu_setup) return;
this.page.add_menu_item(__('Hub Settings'), this.page.add_menu_item(__('Hub Settings'),
() => frappe.set_route('Form', 'Hub Settings')); () => frappe.set_route('Form', 'Hub Settings'));
this.page.add_menu_item(__('Refresh'), () => this.refresh()); this.page.add_menu_item(__('Refresh'), () => this.refresh());
this.page.add_menu_item(__('Sync'), () => this.sync_items_to_hub()); this.page.add_menu_item(__('Sync'), () => this.sync_items_to_hub());
this.menu_setup = true;
} }
sync_items_to_hub() { sync_items_to_hub() {
@ -588,12 +628,12 @@ window.Hub = class Hub {
setup_sidebar() { setup_sidebar() {
var me = this; var me = this;
this.sidebar = new HubSidebar({ this.sidebar = new frappe.ui.Sidebar({
wrapper: this.page.wrapper.find('.layout-side-section') wrapper: this.page.wrapper.find('.layout-side-section'),
css_class: 'hub-sidebar'
}); });
this.add_account_to_sidebar(); this.add_account_to_sidebar();
} }
add_account_to_sidebar() { add_account_to_sidebar() {
@ -603,8 +643,8 @@ window.Hub = class Hub {
}, __("Account")); }, __("Account"));
this.sidebar.add_item({ this.sidebar.add_item({
label: __("Requested Products"), label: __("My Orders"),
on_click: () => this.go_to_seen_items() on_click: () => frappe.set_route('List', 'Request for Quotation')
}, __("Account")); }, __("Account"));
} }
@ -638,7 +678,7 @@ window.Hub = class Hub {
} }
} }
class HubList { erpnext.hub.HubList = class HubList {
constructor({ constructor({
parent = null, parent = null,
title = 'Products', title = 'Products',
@ -744,6 +784,8 @@ class HubList {
render_items(items) { render_items(items) {
if(items) { if(items) {
// clear any filler divs
this.$list.find('.filler').remove();
let done = 0; let done = 0;
console.log("items length", items.length); console.log("items length", items.length);
if(items.length && items.length > this.page_length) { if(items.length && items.length > this.page_length) {
@ -755,10 +797,16 @@ class HubList {
items.forEach((item) => { items.forEach((item) => {
this.make_item_card(item).appendTo(this.$list); this.make_item_card(item).appendTo(this.$list);
}); });
console.log(done);
const remainder = items.length % 4;
if (remainder > 0) {
// fill with filler divs to make flexbox happy
Array.from(Array(remainder))
.map(r => $('<div class="filler">').css('width', '200px').appendTo(this.$list));
}
this.update_list_state(done); this.update_list_state(done);
} else { } else {
this.$item_list_title.html('No results found'); this.update_list_state(1);
} }
} }
@ -814,57 +862,3 @@ class HubList {
</div>`; </div>`;
} }
} }
class HubSidebar {
constructor({ wrapper }) {
this.wrapper = wrapper;
this.make_dom();
}
make_dom() {
this.wrapper.html(`
<div class="hub-sidebar overlay-sidebar hidden-xs hidden-sm">
</div>
`);
this.$sidebar = this.wrapper.find('.hub-sidebar');
}
add_item(item, section) {
let $section;
if(!section && this.wrapper.find('.sidebar-menu').length === 0) {
// if no section, add section with no heading
$section = this.get_section();
} else {
$section = this.get_section(section);
}
const $li_item = $(`
<li><a ${item.href ? `href="${item.href}"` : ''}>${item.label}</a></li>
`).click(
() => item.on_click && item.on_click()
);
$section.append($li_item);
}
get_section(section_heading="") {
let $section = $(this.wrapper.find(
`[data-section-heading="${section_heading}"]`));
if($section.length) {
return $section;
}
const $section_heading = section_heading ?
`<li class="h6">${section_heading}</li>` : '';
$section = $(`
<ul class="list-unstyled sidebar-menu" data-section-heading="${section_heading || 'default'}">
${$section_heading}
</ul>
`);
this.$sidebar.append($section);
return $section;
}
}