All files / web/bundles/pimui/js/form form-modal.js

78.57% Statements 22/28
33.33% Branches 1/3
54.55% Functions 6/11
78.57% Lines 22/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                        359x                                 359x                                                                                       17x 17x 17x 17x                   17x   17x     17x   17x 17x 17x   17x       17x       17x 17x 17x     17x 9x 9x   9x         17x                                                    
'use strict';
 
/**
 * This service instantiates a modal with a custom form.
 * The custom form must be passed in as a service.
 *
 * A deferred object is returned on modal opening:
 * - Success: Resolved when the user clicks on the OK button, the callback contains the form data.
 * - Fail: the user canceled the modal form.
 *
 * Typical use example
 * ===================
 *
 * var onDataSubmission = function (form) {
 *     var deferred = $.Deferred();
 *     var formData = form.getFormData();
 *
 *     // validate your data...
 *
 *     if (validData) {
 *          deferred.resolve();
 *     } else {
 *          deferred.reject();
 *          // display errors on form, or whatever
 *     }
 *
 *     return deferred;
 * }
 *
 * var myFormModal = new FormModal('pim-product-edit-form', onDataSubmission);
 *
 * myFormModal.open()
 *      .then(function(myFormData) {
 *          // on success
 *      })
 *      .fail(function() {
 *          // user clicked Cancel button
 *      });
 *
 * @author    Adrien Pétremann <adrien.petremann@akeneo.com>
 * @author    Julien Sanchez <julien@akeneo.com>
 * @copyright 2015 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
define(
    [
        'jquery',
        'underscore',
        'oro/translator',
        'backbone',
        'oro/mediator',
        'pim/form-builder'
    ],
    function (
        $,
        _,
        __,
        Backbone,
        mediator,
        FormBuilder
    ) {
        return Backbone.View.extend({
            /**
             * The form name the modal should display.
             * This service must be registered with RequireJS, eg: 'pim-product-edit-form'
             */
            formName: '',
 
            /**
             * Instance of the UI modal element.
             */
            modal: null,
 
            /**
             * Callback triggered on form submission.
             * This callback should return a promise, resolved when data validation check is OK.
             */
            submitCallback: null,
 
            /**
             * UI modal parameters
             */
            modalParameters: {
                okCloses:    false,
                content:     '',
                title:       '[modal_title]',
                okText:      __('pim_common.save'),
                modalOptions: {
                    backdrop: 'static',
                    keyboard: false
                }
            },
 
            /**
             * Initial form data of the modal
             */
            initialFormData: {},
 
            /**
             * @param {string}   formName
             * @param {function} submitCallback
             * @param {Object}   modalParameters
             * @param {Object}   initialFormData
             */
            initialize: function (formName, submitCallback, modalParameters, initialFormData = {}) {
                this.formName        = formName;
                this.submitCallback  = submitCallback;
                this.modalParameters = _.extend(this.modalParameters, modalParameters);
                this.initialFormData = initialFormData;
            },
 
            /**
             * Render the modal with the custom form service.
             * Returns the deferred object to catch success (OK) & fail (Cancel) event of the modal.
             *
             * @return {Promise}
             */
            open: function () {
                var deferred = $.Deferred();
 
                FormBuilder
                    .build(this.formName)
                    .then(function (form) {
                        form.setData(this.initialFormData, {silent: true});
 
                        this.modal = new Backbone.BootstrapModal(this.modalParameters);
                        this.modal.open();
                        form.setElement(this.modal.$('.modal-body')).render();
 
                        mediator.on('pim_enrich:form:modal:ok_button:disable', function () {
                            this.disableOkBtn();
                        }.bind(this));
 
                        mediator.on('pim_enrich:form:modal:ok_button:enable', function () {
                            this.enableOkBtn();
                        }.bind(this));
 
                        this.modal.on('cancel', deferred.reject);
                        this.modal.on('ok', function () {
                            Iif (this.modal.$('.modal-footer .ok').hasClass('disabled')) {
                                return;
                            }
                            this.submitCallback(form).then(function () {
                                var data = form.getFormData();
                                deferred.resolve(data);
 
                                this.modal.close();
                            }.bind(this));
                        }.bind(this));
                    }.bind(this));
 
                return deferred;
            },
 
            /**
             * Close the modal UI element.
             */
            close: function () {
                this.modal.close();
            },
 
            /**
             * Enable the modal ok button.
             */
            enableOkBtn: function () {
                this.modal.$('.ok').attr('disabled', null).removeClass('AknButton--disabled');
            },
 
            /**
             * Disable the modal ok button.
             */
            disableOkBtn: function () {
                this.modal.$('.ok').attr('disabled', 'disabled').addClass('AknButton--disabled');
            }
        });
    }
);