notiflix / Notiflix

Notiflix is a pure JavaScript library for client-side non-blocking notifications, popup boxes, loading indicators, and more that makes your web projects much better.
https://notiflix.github.io
MIT License
647 stars 55 forks source link

SOLVED: Allow temporal options for an specific message #24

Closed virgiliogm closed 4 years ago

virgiliogm commented 4 years ago

I'm facing a problem: I initialize the Notify component globally for my application but I need to use specific settings for certain messages. Using Merge is not an option because that overrides the global settings and I realized that it would be great and not a hard work to allow to pass an options object for a specific message. I did a test for Notify.Info but the changes could easily be applied to all functions:

var Notiflix = {
  // Notify on
  Notify: {
    // ...
    Info: function (message, options, callback) {
      // if not initialized pretend like init
      if (!newNotifySettings) {
        Notiflix.Notify.Init({});
      }
      var theType = newNotifySettings.info;
      if (!callback && typeof options === 'function') {
        // received message & callback as second parameter
        calback = options;
        options = undefined;
      }
      NotiflixNotify(message, callback, theType, 'Info', options);
    },
  },
};
var NotiflixNotify = function (message, callback, theType, staticType, tempOptions) {
  if (arguments && arguments.length = 5) {

    // if tempOptions on
    let newNotifySettings_backup;
    if (tempOptions) {
      newNotifySettings_backup = { ...newNotifySettings };
      newNotifySettings = { ...newNotifySettings, ...tempOptions };
    }
    // if tempOptions off

    // after all the code, at the end of the block

    // if tempOptions on
    if (newNotifySettings_backup) {
      newNotifySettings = { ...newNotifySettings_backup };
    }
    // if tempOptions off

  }
};

After that, it is possible to do something like this:

import { Notify } from 'notiflix';

var messages = [
  { text: 'Lorem ipsum' },
  { text: 'dolor sit amet', important: true },
  { text: 'consectetur adipiscing elit' },
];

messages.forEach((message) => {
  Notify.Info(message.text, message.important ? { timeout: 10000 } : null);
});

This way, the first and second messages will use the default timeout of 3 seconds but the important one will stay visible for 10 seconds.

furcan commented 4 years ago

Hi @VirgilioGM

Firstly thanks for using the Notiflix.

It would be nice as you said. I will create a solution as you suggested asap.

Thank you.

furcan commented 4 years ago

Hi @VirgilioGM

Notiflix v2.3.0 just released.

You can use custom options for each Notify or Report module elements now.

Thanks.

Release Notes - Example

virgiliogm commented 4 years ago

Wow! I'm very happy that you took my suggestion into account and implemented the changes so quickly. Thank you very much!