Modules and packages

NPM packages

To improve the performance of Kommo and enhance the speed of its operation in the client's browser, it is recommended to reduce the number of downloaded resources from the network. One way to achieve this is by utilizing the vendor libraries provided by the system in your widgets.

To add any of these libraries to your widget, you can use the API of the specified version. You can learn more about the API by clicking on the links to NPM. In order to properly include the library as a dependency in your script.js file, be sure to specify the module code from the table in the widget dependencies:

define(['jquery', 'moment'], function ($, moment) {
  $('#my_widget_selector').css('color', 'red');

  console.log(moment().format('DD-MM-YYYY'));
});

Kommo modules

It's worth noting that widgets in Kommo can utilize certain built-in features to seamlessly integrate with the system. One of the most frequently utilized features is the modal window, which is located in the lib/components/base/modal module. This allows for a more native and cohesive user experience within the platform.

Here is an example of the module use in script.js:

define(['jquery', 'underscore', 'lib/components/base/modal'], function ($, _, Modal) {
 return function () {
   var self = this;

   this.callbacks = {
     init: function () { return true; },
     bind_actions: function () {
       $(document).on(
         'click.' + self.get_settings().widget_code,
         '.my_widget_button',
         function () {
           new Modal({
             class_name: '',
             init: _.noop,
             destroy: _.noop,
             container: document.body,
             disable_overlay_click: false,
             disable_escape_keydown: false,
             disable_enter_keydown: false,
             init_animation: false,
             default_overlay: false,
             focus_element: '.js-modal-accept',
           });
         }
       )
     },
     render: function () { return true; },
     destroy: function () {
       $(document).off('.' + self.get_settings().widget_code);

       return true;
     },
     settings: function () { return true; },
     onSave: function () { return true; }
   }
 };
});

Parameters that can be passed to the modal

ParameterDescription
class_nameAdditional classes for a modal window if you need to change some styles in it .
can_centrifyCentering option for mobile devices. Some modal windows need to be re-centered intentionally after closing the keyboard on a tablet.
initThe method runs when the modal window is up and receives the jQuery object $modal_body of the body of the modal window as a parameter, contains everything from the modal window.
destroyCustom destroy, if returns false, the window will not close.
container Сontainer with the modal window. It tells which element it will be centered in relation to.
disable_overlay_clickPass it if you need to prevent the modal window from closing by clicking on the overlay
disable_escape_keydownPass it if you need to prevent the modal window from closing by pressing ESC
disable_enter_keydownPass to disable default processing of ENTER
init_animationResponsible for the animation of the pop-up of the modal window. If you pass true, it will appear with an animation of expanding.
default_overlayModal windows have white overlay by default, you should switch it, if you want to change it to dark.
preload_templatesYou can pass an array of necessary twig templates for loading.
focus_elementThe element that receives focus is the accept button, by default.
This parameter is used to remove focus from the button that caused an event.
centrify_animationDetermines whether animation is needed when centering the modal window.
disable_cancel_clickDisables closing the modal window by clicking on the cross or overlay.
disable_resizeDisables modal window resize on initialization.

Modal object to work with a modal window

If you want to utilize a modal window object in your code, you will need to connect it through the require function (define at the beginning of script.js) and pass the necessary parameters: class_name, init(), and destroy().

The init() method should accept the data that you want to display within the modal window, and the trigger events(which will enable the methods of the modal object to run and display the modal window in the DOM).

This example shows the use of the Modal window object:

define(['jquery', 'lib/components/base/modal'], function ($, Modal) {
    var CustomWidget = function () {
        this.callbacks = {
            // ...
            bind_actions: function () {
                // ...
                var data = '<h1>Test</h1><p>Some text</p>';
                modal = new Modal({
                    class_name: 'modal-window',
                    init: function ($modal_body) {
                        var $this = $(this);
                        $modal_body
                            .trigger('modal:loaded') // triggers displaying the modal window
                            .html(data)
                            .trigger('modal:centrify')  // sets up the modal window
                            .append('');
                    },
                    destroy: function () {
                    }
                });
                // ...
                return true;
            }
        }
    }
    return CustomWidget;
});