![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Soon/QtyUpdater/view/frontend/web/js/ |
/** * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * @author Hervé Guétin <[email protected]> <@herveguetin> * @copyright Copyright (c) 2017 Agence Soon (http://www.agence-soon.fr) */ define([ 'jquery', 'mage/template' ], function ($, template) { 'use strict'; $.widget('soon.qtyUpdater', { options: { form: undefined, step: 1 }, template: template('<span class="soon-qty-updater <%- code %>"><%- sign %></span>'), buttons: {}, _create: function () { this.form = (this.options.form === undefined) ? undefined : $(this.options.form); this.button = (this.options.button === undefined) ? undefined : $(this.options.button); $(this.element).wrap("<div class='soon-qty-updater-wrap'></div>"); this.buildButtons(); this.ux(); }, buildButtons: function () { this.buildButton({code: 'dec', where: 'before', sign: '−'}); this.buildButton({code: 'inc', where: 'after', sign: '+'}); }, buildButton: function (config) { var button = $(this.template(config)); this.element[config.where](button); this.buttons[config.code] = button; }, ux: function () { var that = this; $.each(this.buttons, function (code, button) { button.on('click', function (e) { e.preventDefault(); if (that.update(code)) { that.submit(); } }); }); }, update: function (code) { var k = (code === 'inc') ? 1 : -1; var currentValue = parseInt(this.element.val()); var newValue = currentValue + k * this.options.step; var minValue = 1; if (typeof this.element.attr('min') != 'undefined') { minValue = this.element.attr('min'); } var maxValue = 99999; if (typeof this.element.attr('max') != 'undefined') { maxValue = this.element.attr('max'); } if (newValue < minValue) { newValue = minValue; } else if (newValue > maxValue) { newValue = maxValue; } newValue = parseInt(newValue); if (newValue !== currentValue) { this.element.val(newValue).trigger("change"); return true; } return false; }, submit: _.debounce(function () { if (this.form === undefined && this.button === undefined) { return; } if (this.button !== undefined) { this.button.click(); } else { this.form.addClass('loading'); this.form.submit(); } }, 500) }); return $.soon.qtyUpdater; });