gift-message.js 6.24 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
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * @api
 */
define([
    'uiElement',
    'underscore',
    'mage/url'
], function (uiElement, _, url) {
    'use strict';

    var provider = uiElement();

    return function (itemId) {
        var model = {
            id: 'message-' + itemId,
            itemId: itemId,
            observables: {},
            additionalOptions: [],
            submitParams: [
                'recipient',
                'sender',
                'message'
            ],

            /**
             * Initialize.
             */
            initialize: function () {
                var message = false;

                this.getObservable('alreadyAdded')(false);

                if (this.itemId == 'orderLevel') { //eslint-disable-line eqeqeq
                    message = window.giftOptionsConfig.giftMessage.hasOwnProperty(this.itemId) ?
                        window.giftOptionsConfig.giftMessage[this.itemId] :
                        null;
                } else {
                    message =
                        window.giftOptionsConfig.giftMessage.hasOwnProperty('itemLevel') &&
                        window.giftOptionsConfig.giftMessage.itemLevel.hasOwnProperty(this.itemId) ?
                            window.giftOptionsConfig.giftMessage.itemLevel[this.itemId].message :
                            null;
                }

                if (_.isObject(message)) {
                    this.getObservable('recipient')(message.recipient);
                    this.getObservable('sender')(message.sender);
                    this.getObservable('message')(message.message);
                    this.getObservable('alreadyAdded')(true);
                }
            },

            /**
             * @param {String} key
             * @return {*}
             */
            getObservable: function (key) {
                this.initObservable(this.id, key);

                return provider[this.getUniqueKey(this.id, key)];
            },

            /**
             * @param {String} node
             * @param {String} key
             */
            initObservable: function (node, key) {
                if (node && !this.observables.hasOwnProperty(node)) {
                    this.observables[node] = [];
                }

                if (key && this.observables[node].indexOf(key) === -1) {
                    this.observables[node].push(key);
                    provider.observe(this.getUniqueKey(node, key));
                }
            },

            /**
             * @param {String} node
             * @param {String} key
             * @return {String}
             */
            getUniqueKey: function (node, key) {
                return node + '-' + key;
            },

            /**
             * @param {String} key
             * @return {null}
             */
            getConfigValue: function (key) {
                return window.giftOptionsConfig.hasOwnProperty(key) ?
                    window.giftOptionsConfig[key]
                    : null;
            },

            /**
             * Reset.
             */
            reset: function () {
                this.getObservable('isClear')(true);
            },

            /**
             * @return {Array}
             */
            getAfterSubmitCallbacks: function () {
                var callbacks = [];

                callbacks.push(this.afterSubmit);
                _.each(this.additionalOptions, function (option) {
                    if (_.isFunction(option.afterSubmit)) {
                        callbacks.push(option.afterSubmit);
                    }
                });

                return callbacks;
            },

            /**
             * After submit.
             */
            afterSubmit: function () {
                window.location.href = url.build('checkout/cart/updatePost') +
                    '?form_key=' + window.checkoutConfig.formKey +
                    '&cart[]';
            },

            /**
             * @param {Boolean} remove
             * @return {Object}
             */
            getSubmitParams: function (remove) {
                var params = {},
                    self = this;

                _.each(this.submitParams, function (key) {
                    var observable = provider[self.getUniqueKey(self.id, key)];

                    if (_.isFunction(observable)) {
                        params[key] = remove ? null : observable();
                    }
                });

                if (this.additionalOptions.length) {
                    params['extension_attributes'] = {};
                }
                _.each(this.additionalOptions, function (option) {
                    if (_.isFunction(option.getSubmitParams)) {
                        params['extension_attributes'] = _.extend(
                            params['extension_attributes'],
                            option.getSubmitParams(remove)
                        );
                    }
                });

                return params;
            },

            /**
             * Check if gift message can be displayed
             *
             * @returns {Boolean}
             */
            isGiftMessageAvailable: function () {
                var isGloballyAvailable,
                    giftMessageConfig,
                    itemConfig;

                // itemId represent gift message level: 'orderLevel' constant or cart item ID
                if (this.itemId === 'orderLevel') {
                    return this.getConfigValue('isOrderLevelGiftOptionsEnabled');
                }

                // gift message product configuration must override system configuration
                isGloballyAvailable = this.getConfigValue('isItemLevelGiftOptionsEnabled');
                giftMessageConfig = window.giftOptionsConfig.giftMessage;
                itemConfig = giftMessageConfig.hasOwnProperty('itemLevel') &&
                    giftMessageConfig.itemLevel.hasOwnProperty(this.itemId) ?
                    giftMessageConfig.itemLevel[this.itemId] :
                    {};

                return itemConfig.hasOwnProperty('is_available') ? itemConfig['is_available'] : isGloballyAvailable;
            }
        };

        model.initialize();

        return model;
    };
});