[IMP] website_sale, *: save the layout mode in the user session

* website

Part of https://github.com/odoo/odoo/pull/31147
task-1925327
This commit is contained in:
qsm-odoo
2019-05-28 15:05:42 +00:00
parent c65233aa9b
commit d0573de2b6
5 changed files with 47 additions and 25 deletions
+4
View File
@@ -283,6 +283,10 @@ class Website(Home):
views = views.sorted(key=lambda v: (v.inherit_id.id, v.name))
return views.read(['name', 'id', 'key', 'xml_id', 'arch', 'active', 'inherit_id'])
@http.route('/website/toggle_switchable_view', type='json', auth='user', website=True)
def toggle_switchable_view(self, view_key):
request.website.viewref(view_key).toggle()
@http.route('/website/reset_template', type='http', auth='user', methods=['POST'], website=True, csrf=False)
def reset_template(self, view_id, mode='soft', redirect='/', **kwargs):
""" This method will try to reset a broken view.
+10 -9
View File
@@ -12,7 +12,7 @@ var CustomizeMenu = Widget.extend({
xmlDependencies: ['/website/static/src/xml/website.editor.xml'],
events: {
'show.bs.dropdown': '_onDropdownShow',
'click .dropdown-item[data-view-id]': '_onCustomizeOptionClick',
'click .dropdown-item[data-view-key]': '_onCustomizeOptionClick',
},
/**
@@ -44,17 +44,18 @@ var CustomizeMenu = Widget.extend({
* Enables/Disables a view customization whose id is given.
*
* @private
* @param {integer} viewID
* @param {string} viewKey
* @returns {Promise}
* Unresolved if the customization succeeded as the page will be
* reloaded.
* Rejected otherwise.
*/
_doCustomize: function (viewID) {
_doCustomize: function (viewKey) {
return this._rpc({
model: 'ir.ui.view',
method: 'toggle',
args: [[viewID]],
route: '/website/toggle_switchable_view',
params: {
'view_key': viewKey,
},
}).then(function () {
window.location.reload();
return new Promise(function () {});
@@ -92,7 +93,7 @@ var CustomizeMenu = Widget.extend({
currentGroup = item.inherit_id[1];
$menu.append('<li class="dropdown-header">' + currentGroup + '</li>');
}
var $a = $('<a/>', {href: '#', class: 'dropdown-item', 'data-view-id': item.id, role: 'menuitem'})
var $a = $('<a/>', {href: '#', class: 'dropdown-item', 'data-view-key': item.key, role: 'menuitem'})
.append(qweb.render('website.components.switch', {id: 'switch-' + item.id, label: item.name}));
$a.find('input').prop('checked', !!item.active);
$menu.append($a);
@@ -113,8 +114,8 @@ var CustomizeMenu = Widget.extend({
*/
_onCustomizeOptionClick: function (ev) {
ev.preventDefault();
var viewID = parseInt($(ev.currentTarget).data('view-id'), 10);
this._doCustomize(viewID);
var viewKey = $(ev.currentTarget).data('viewKey');
this._doCustomize(viewKey);
},
/**
* @private
+19
View File
@@ -126,6 +126,12 @@ class Website(Website):
views = [v for v in views if v['id'] != view_product_variants.id]
return views
@http.route()
def toggle_switchable_view(self, view_key):
super(Website, self).toggle_switchable_view(view_key)
if view_key in ('website_sale.products_list_view', 'website_sale.add_grid_or_list_option'):
request.session.pop('website_sale_shop_layout_mode', None)
class WebsiteSale(http.Controller):
@@ -268,6 +274,13 @@ class WebsiteSale(http.Controller):
compute_currency = self._get_compute_currency(pricelist, products[:1])
layout_mode = request.session.get('website_sale_shop_layout_mode')
if not layout_mode:
if request.website.viewref('website_sale.products_list_view').active:
layout_mode = 'list'
else:
layout_mode = 'grid'
values = {
'search': search,
'category': category,
@@ -287,6 +300,7 @@ class WebsiteSale(http.Controller):
'keep': keep,
'parent_category_ids': parent_category_ids,
'search_categories_ids': search_categories and search_categories.ids,
'layout_mode': layout_mode,
}
if category:
values['main_object'] = category
@@ -475,6 +489,11 @@ class WebsiteSale(http.Controller):
})
return value
@http.route('/shop/save_shop_layout_mode', type='json', auth='public', website=True)
def save_shop_layout_mode(self, layout_mode):
assert layout_mode in ('grid', 'list'), "Invalid shop layout mode"
request.session['website_sale_shop_layout_mode'] = layout_mode
# ------------------------------------------------------
# Checkout
# ------------------------------------------------------
@@ -189,15 +189,6 @@ publicWidget.registry.WebsiteSale = publicWidget.Widget.extend(VariantMixin, {
this._startZoom();
var $active = null;
if (this.$('#products_grid').hasClass('o_wsale_layout_list')) {
$active = this.$('.o_wsale_apply_list');
} else {
$active = this.$('.o_wsale_apply_grid');
}
$active.find('input').prop('checked', true);
$active.button('toggle').blur();
return def;
},
/**
@@ -442,6 +433,12 @@ publicWidget.registry.WebsiteSale = publicWidget.Widget.extend(VariantMixin, {
*/
_onApplyShopLayoutChange: function (ev) {
var switchToList = $(ev.currentTarget).find('.o_wsale_apply_list input').is(':checked');
this._rpc({
route: '/shop/save_shop_layout_mode',
params: {
'layout_mode': switchToList ? 'list' : 'grid',
},
});
var $grid = this.$('#products_grid');
// Disable transition on all list elements, then switch to the new
// layout then reenable all transitions after having forced a redraw
+8 -7
View File
@@ -215,7 +215,7 @@
</div>
<div class="row o_wsale_products_main_row">
<div t-if="enable_left_column" id="products_grid_before" class="col-lg-3"/>
<div id="products_grid" class="col">
<div id="products_grid" t-attf-class="col #{'o_wsale_layout_list' if layout_mode == 'list' else ''}">
<t t-if="category">
<t t-set='editor_msg'>Drag building blocks here to customize the header for "<t t-esc='category.name'/>" category.</t>
<div class="mb16" id="category_header" t-att-data-editor-message="editor_msg" t-field="category.website_description"/>
@@ -308,11 +308,11 @@
<template id="add_grid_or_list_option" inherit_id="website_sale.products" active="True" customize_show="True" name="Grid or List button">
<xpath expr="//div[hasclass('products_pager')]/t[@t-call][last()]" position="after">
<div class="btn-group btn-group-toggle mt-2 ml-md-2 d-none d-sm-inline-flex o_wsale_apply_layout" data-toggle="buttons">
<label class="btn btn-secondary fa fa-th-large o_wsale_apply_grid" title="Grid">
<input type="radio" name="wsale_products_layout"/>
<label t-attf-class="btn btn-secondary #{'active' if layout_mode != 'list' else None} fa fa-th-large o_wsale_apply_grid" title="Grid">
<input type="radio" name="wsale_products_layout" t-att-checked="'checked' if layout_mode != 'list' else None"/>
</label>
<label class="btn btn-secondary fa fa-th-list o_wsale_apply_list" title="List">
<input type="radio" name="wsale_products_layout"/>
<label t-attf-class="btn btn-secondary #{'active' if layout_mode == 'list' else None} fa fa-th-list o_wsale_apply_list" title="List">
<input type="radio" name="wsale_products_layout" t-att-checked="'checked' if layout_mode == 'list' else None"/>
</label>
</div>
</xpath>
@@ -435,8 +435,9 @@
</template>
<template id="products_list_view" inherit_id="website_sale.products" active="False" customize_show="True" name="List View (by default)">
<xpath expr="//div[@id='products_grid']" position="attributes">
<attribute name="class" add="o_wsale_layout_list" separator=" "/>
<xpath expr="//div[@id='products_grid']" position="after">
<!-- Nothing to do, this view is only meant to allow the server -->
<!-- to know if the list view layout should be used -->
</xpath>
</template>