diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/website_sale_wishlist/static/src | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/website_sale_wishlist/static/src')
| -rw-r--r-- | addons/website_sale_wishlist/static/src/js/website_sale_wishlist.js | 274 | ||||
| -rw-r--r-- | addons/website_sale_wishlist/static/src/scss/website_sale_wishlist.scss | 23 |
2 files changed, 297 insertions, 0 deletions
diff --git a/addons/website_sale_wishlist/static/src/js/website_sale_wishlist.js b/addons/website_sale_wishlist/static/src/js/website_sale_wishlist.js new file mode 100644 index 00000000..075dfc92 --- /dev/null +++ b/addons/website_sale_wishlist/static/src/js/website_sale_wishlist.js @@ -0,0 +1,274 @@ +odoo.define('website_sale_wishlist.wishlist', function (require) { +"use strict"; + +var publicWidget = require('web.public.widget'); +var wSaleUtils = require('website_sale.utils'); +var VariantMixin = require('sale.VariantMixin'); + +// VariantMixin events are overridden on purpose here +// to avoid registering them more than once since they are already registered +// in website_sale.js +publicWidget.registry.ProductWishlist = publicWidget.Widget.extend(VariantMixin, { + selector: '.oe_website_sale', + events: { + 'click .o_wsale_my_wish': '_onClickMyWish', + 'click .o_add_wishlist, .o_add_wishlist_dyn': '_onClickAddWish', + 'change input.product_id': '_onChangeVariant', + 'change input.js_product_change': '_onChangeProduct', + 'click .wishlist-section .o_wish_rm': '_onClickWishRemove', + 'click .wishlist-section .o_wish_add': '_onClickWishAdd', + }, + + /** + * @constructor + */ + init: function (parent) { + this._super.apply(this, arguments); + this.wishlistProductIDs = []; + }, + /** + * Gets the current wishlist items. + * In editable mode, do nothing instead. + * + * @override + */ + willStart: function () { + var self = this; + var def = this._super.apply(this, arguments); + + var wishDef = $.get('/shop/wishlist', { + count: 1, + }).then(function (res) { + self.wishlistProductIDs = JSON.parse(res); + }); + + return Promise.all([def, wishDef]); + }, + /** + * Updates the wishlist view (navbar) & the wishlist button (product page). + * In editable mode, do nothing instead. + * + * @override + */ + start: function () { + var def = this._super.apply(this, arguments); + + this._updateWishlistView(); + // trigger change on only one input + if (this.$('input.js_product_change').length) { // manage "List View of variants" + this.$('input.js_product_change:checked').first().trigger('change'); + } else { + this.$('input.product_id').first().trigger('change'); + } + + return def; + }, + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * @private + */ + _addNewProducts: function ($el) { + var self = this; + var productID = $el.data('product-product-id'); + if ($el.hasClass('o_add_wishlist_dyn')) { + productID = $el.parent().find('.product_id').val(); + if (!productID) { // case List View Variants + productID = $el.parent().find('input:checked').first().val(); + } + productID = parseInt(productID, 10); + } + var $form = $el.closest('form'); + var templateId = $form.find('.product_template_id').val(); + // when adding from /shop instead of the product page, need another selector + if (!templateId) { + templateId = $el.data('product-template-id'); + } + $el.prop("disabled", true).addClass('disabled'); + var productReady = this.selectOrCreateProduct( + $el.closest('form'), + productID, + templateId, + false + ); + + productReady.then(function (productId) { + productId = parseInt(productId, 10); + + if (productId && !_.contains(self.wishlistProductIDs, productId)) { + return self._rpc({ + route: '/shop/wishlist/add', + params: { + product_id: productId, + }, + }).then(function () { + var $navButton = $('header .o_wsale_my_wish').first(); + self.wishlistProductIDs.push(productId); + self._updateWishlistView(); + wSaleUtils.animateClone($navButton, $el.closest('form'), 25, 40); + }).guardedCatch(function () { + $el.prop("disabled", false).removeClass('disabled'); + }); + } + }).guardedCatch(function () { + $el.prop("disabled", false).removeClass('disabled'); + }); + }, + /** + * @private + */ + _updateWishlistView: function () { + const $wishButton = $('.o_wsale_my_wish'); + if ($wishButton.hasClass('o_wsale_my_wish_hide_empty')) { + $wishButton.toggleClass('d-none', !this.wishlistProductIDs.length); + } + $wishButton.find('.my_wish_quantity').text(this.wishlistProductIDs.length); + }, + /** + * @private + */ + _removeWish: function (e, deferred_redirect) { + var tr = $(e.currentTarget).parents('tr'); + var wish = tr.data('wish-id'); + var product = tr.data('product-id'); + var self = this; + + this._rpc({ + route: '/shop/wishlist/remove/' + wish, + }).then(function () { + $(tr).hide(); + }); + + this.wishlistProductIDs = _.without(this.wishlistProductIDs, product); + if (this.wishlistProductIDs.length === 0) { + if (deferred_redirect) { + deferred_redirect.then(function () { + self._redirectNoWish(); + }); + } + } + this._updateWishlistView(); + }, + /** + * @private + */ + _addOrMoveWish: function (e) { + var $navButton = $('header .o_wsale_my_cart').first(); + var tr = $(e.currentTarget).parents('tr'); + var product = tr.data('product-id'); + $('.o_wsale_my_cart').removeClass('d-none'); + wSaleUtils.animateClone($navButton, tr, 25, 40); + + if ($('#b2b_wish').is(':checked')) { + return this._addToCart(product, tr.find('add_qty').val() || 1); + } else { + var adding_deffered = this._addToCart(product, tr.find('add_qty').val() || 1); + this._removeWish(e, adding_deffered); + return adding_deffered; + } + }, + /** + * @private + */ + _addToCart: function (productID, qty_id) { + return this._rpc({ + route: "/shop/cart/update_json", + params: { + product_id: parseInt(productID, 10), + add_qty: parseInt(qty_id, 10), + display: false, + }, + }).then(function (resp) { + if (resp.warning) { + if (! $('#data_warning').length) { + $('.wishlist-section').prepend('<div class="mt16 alert alert-danger alert-dismissable" role="alert" id="data_warning"></div>'); + } + var cart_alert = $('.wishlist-section').parent().find('#data_warning'); + cart_alert.html('<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> ' + resp.warning); + } + $('.my_cart_quantity').html(resp.cart_quantity || '<i class="fa fa-warning" /> '); + }); + }, + /** + * @private + */ + _redirectNoWish: function () { + window.location.href = '/shop/cart'; + }, + + + //-------------------------------------------------------------------------- + // Handlers + //-------------------------------------------------------------------------- + + /** + * @private + */ + _onClickMyWish: function () { + if (this.wishlistProductIDs.length === 0) { + this._updateWishlistView(); + this._redirectNoWish(); + return; + } + window.location = '/shop/wishlist'; + }, + /** + * @private + * @param {Event} ev + */ + _onClickAddWish: function (ev) { + this._addNewProducts($(ev.currentTarget)); + }, + /** + * @private + * @param {Event} ev + */ + _onChangeVariant: function (ev) { + var $input = $(ev.target); + var $parent = $input.closest('.js_product'); + var $el = $parent.find("[data-action='o_wishlist']"); + if (!_.contains(this.wishlistProductIDs, parseInt($input.val(), 10))) { + $el.prop("disabled", false).removeClass('disabled').removeAttr('disabled'); + } else { + $el.prop("disabled", true).addClass('disabled').attr('disabled', 'disabled'); + } + $el.data('product-product-id', parseInt($input.val(), 10)); + }, + /** + * @private + * @param {Event} ev + */ + _onChangeProduct: function (ev) { + var productID = ev.currentTarget.value; + var $el = $(ev.target).closest('.js_add_cart_variants').find("[data-action='o_wishlist']"); + + if (!_.contains(this.wishlistProductIDs, parseInt(productID, 10))) { + $el.prop("disabled", false).removeClass('disabled').removeAttr('disabled'); + } else { + $el.prop("disabled", true).addClass('disabled').attr('disabled', 'disabled'); + } + $el.data('product-product-id', productID); + }, + /** + * @private + * @param {Event} ev + */ + _onClickWishRemove: function (ev) { + this._removeWish(ev, false); + }, + /** + * @private + * @param {Event} ev + */ + _onClickWishAdd: function (ev) { + var self = this; + this.$('.wishlist-section .o_wish_add').addClass('disabled'); + this._addOrMoveWish(ev).then(function () { + self.$('.wishlist-section .o_wish_add').removeClass('disabled'); + }); + }, +}); +}); diff --git a/addons/website_sale_wishlist/static/src/scss/website_sale_wishlist.scss b/addons/website_sale_wishlist/static/src/scss/website_sale_wishlist.scss new file mode 100644 index 00000000..13a1ec4b --- /dev/null +++ b/addons/website_sale_wishlist/static/src/scss/website_sale_wishlist.scss @@ -0,0 +1,23 @@ +.oe_website_sale { + .td-wish-btn { + width: 140px; + } + + div.css_not_available .o_add_wishlist_dyn { + display: none; + } +} + +// XS size +@include media-breakpoint-down(sm) { + .oe_website_sale { + .td-wish-btn { + width: 100px; + } + } +} + +table.table-comparator .td-img img { + // allows sizing the placeholder image to the "image" size of 100px + max-height: 100px; +}
\ No newline at end of file |
