solution.js 8.75 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
define([
    'jquery',
    'uiClass',
    'Magento_Paypal/js/rule',
    'mageUtils',
    'underscore'
], function ($, Class, Rule, utils, _) {
    'use strict';

    return Class.extend({
        defaults: {

            /**
             * The event corresponding to the state change
             */
            systemEvent: 'change',

            /**
             * The rules applied after the page is loaded
             */
            afterLoadRules: [],

            /**
             * An attribute of the element responsible for the activation of the payment method (data attribute)
             */
            enableButton:   '[data-enable="payment"]',

            /**
             * An attribute of the element responsible for the activation of the Payflow Express (data attribute)
             */
            enableExpress:  '[data-enable="express"]',

            /**
             * An attribute of the element responsible for the activation of the
             * PayPal Express In-Context Checkout Experience (data attribute)
             */
            enableInContextPayPal: '[data-enable="in-context-api"]',

            /**
             * An attribute of the element responsible for the activation of the Payflow Bml (data attribute)
             */
            enableBml:      '[data-enable="bml"]',

            /**
             * An attribute of the element responsible for the activation of the PayPal Bml (data attribute)
             */
            enableBmlPayPal:      '[data-enable="bml-api"]',

            /**
             * An attribute of the element responsible for the visibility of the PayPal Merchant Id (data attribute)
             */
            dependsMerchantId:  '[data-enable="merchant-id"]',

            /**
             * An attribute of the element responsible for the visibility of the Payflow Bml Sort Order (data attribute)
             */
            dependsBmlSortOrder:    '[data-enable="bml-sort-order"]',

            /**
             * An attribute of the element responsible for the visibility of the PayPal Bml Sort Order (data attribute)
             */
            dependsBmlApiSortOrder:    '[data-enable="bml-api-sort-order"]',

            /**
             * An attribute of the element responsible for the visibility of the
             * button Label credit option (data attribute)
             */
            dependsButtonLabel: '[data-enable="button-label"]',

            /**
             * An attribute of the element responsible for the visibility of the
             * button Label credit option on load (data attribute)
             */
            dependsDisableFundingOptions: '[data-enable="disable-funding-options"]',

            /**
             * Templates element selectors
             */
            templates: {
                elementSelector: 'div.section-config tr[id$="${ $.identifier }"]:first'
            }
        },

        /**
         * Constructor
         *
         * @param {Object} config
         * @param {String} identifier
         * @returns {exports.initialize}
         */
        initialize: function (config, identifier) {
            this.initConfig(config);
            this.$self = this.createElement(identifier);

            return this;
        },

        /**
         * Initialization events
         *
         * @returns {exports.initEvents}
         */
        initEvents: function () {
            _.each(this.config.events, function (elementEvents, selector) {

                var solution = this,
                    selectorButton = solution.$self.find(selector),
                    $self = solution.$self,
                    events = elementEvents;

                selectorButton.on(solution.systemEvent, function () {
                    _.each(events, function (elementEvent, name) {

                        var predicate = elementEvent.predicate,
                            result = true,

                            /**
                             * @param {Function} functionPredicate
                             */
                            predicateCallback = function (functionPredicate) {
                                result = functionPredicate(solution, predicate.message, predicate.argument);

                                if (result) {
                                    $self.trigger(name);
                                } else {
                                    $self.trigger(predicate.event);
                                }
                            };

                        if (solution.getValue($(this)) === elementEvent.value ||
                            $(this).prop('multiple') && solution.checkMultiselectValue($(this), elementEvent)
                        ) {
                            if (predicate.name) {
                                require([
                                    'Magento_Paypal/js/predicate/' + predicate.name
                                ], predicateCallback);
                            } else {
                                $self.trigger(name);
                            }
                        }
                    }, this);
                });
            }, this);

            return this;
        },

        /**
         * @param {Object} $element
         * @returns {*}
         */
        getValue: function ($element) {
            if ($element.is(':checkbox')) {
                return $element.prop('checked') ? '1' : '0';
            }

            return $element.val();
        },

        /**
         * Check multiselect value based on include value
         *
         * @param {Object} $element
         * @param {Object} elementEvent
         * @returns {Boolean}
         */
        checkMultiselectValue: function ($element, elementEvent) {
            var isValueSelected = $.inArray(elementEvent.value, $element.val()) >= 0;

            if (elementEvent.include) {
                isValueSelected = (isValueSelected ? 'true' : 'false') === elementEvent.include;
            }

            return isValueSelected;
        },

        /**
         * Adding event listeners
         *
         * @returns {exports.addListeners}
         */
        addListeners: function () {

            _.each(this.config.relations, function (rules, targetName) {

                var $target = this.createElement(targetName);

                _.each(rules, function (instances, instanceName) {

                    _.each(instances, function (instance) {
                        var handler = new Rule({
                            name: instanceName,
                            $target: $target,
                            $owner: this.$self,
                            data: {
                                buttonConfiguration: this.buttonConfiguration,
                                enableButton: this.enableButton,
                                enableExpress: this.enableExpress,
                                enableInContextPayPal: this.enableInContextPayPal,
                                enableBml: this.enableBml,
                                enableBmlPayPal: this.enableBmlPayPal,
                                dependsMerchantId: this.dependsMerchantId,
                                dependsBmlSortOrder: this.dependsBmlSortOrder,
                                dependsBmlApiSortOrder: this.dependsBmlApiSortOrder,
                                dependsButtonLabel: this.dependsButtonLabel,
                                dependsDisableFundingOptions: this.dependsDisableFundingOptions,
                                solutionsElements: this.solutionsElements,
                                argument: instance.argument
                            }
                        });

                        if (instance.event === ':load') {
                            this.afterLoadRules.push(handler);

                            return;
                        }

                        this.$self.on(instance.event, _.bind(handler.apply, handler));
                    }, this);
                }, this);
            }, this);

            return this;
        },

        /**
         * Create a jQuery element according to selector
         *
         * @param {String} identifier
         * @returns {*}
         */
        createElement: function (identifier) {
            if (identifier === ':self') {
                return this.$self;
            }

            return $(utils.template(this.templates.elementSelector, {
                'identifier': identifier
            }));
        },

        /**
         * Assign solutions elements
         *
         * @param {Object} elements
         * @returns {exports.setSolutionsElements}
         */
        setSolutionsElements: function (elements) {
            this.solutionsElements = elements;

            return this;
        }
    });
});