Notify Library

Very Simple JS Notifications Library

Tính đến 18-06-2020. Xem phiên bản mới nhất.

Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta // @require https://update.greasyfork.org/scripts/401626/817875/Notify%20Library.js

// ==UserScript==
// @version      1.1.3
// @license      MIT
// ==/UserScript==

/* **************

  Notify Library
  Developed by pizidavi

  Options:
    - text    required
    - type    optional
      - success
      - info/information
      - warn/warning
      - error
    - timeout   optional

************** */

var Notify = function Notify(options) {
  var _this = this;

  if (!$('#notify').length) {
    $('body').append('<div id="notify"><style media="screen"> #notify { position: fixed; top: 20px; right: 25px; z-index: 9999; } #notify > div { display: none; position: relative; width: 300px; padding: .4em .8em; margin-bottom: .6em; border-radius: 2px; background-color: white; color: #2c3e50; font-size: 17px; cursor: pointer; transition: .4s; } </style></div>');
  }

  _this.options = options;
  _this.container = $('#notify');
  _this.template = $('<div><span></span></div>');

  if (!_this.options || typeof _this.options != 'object') {
    throw 'Options required';
  }
  if (!_this.options.text) {
    throw 'Options TEXT must not be empty';
  }

  if (_this.options.type) {
    var background = '';
    switch (_this.options.type) {
      case 'success':
        background = '#2ecc71';
      break;
      case 'info':
      case 'information':
        background = '#3498db';
      break;
      case 'warn':
      case 'warning':
        background = '#f9ca24';
      break;
      case 'error':
        background = '#e74c3c';
      break;
      default:
        throw 'Type not found';
    }
    if (background) {
      _this.template.css('background-color', background);
      _this.template.css('color', 'white');
    }
  }

  _this.id = 'noty_'+Math.random().toString(36).substring(2);
  _this.template.attr('id', _this.id);
  _this.template.find('span').text(_this.options.text);
  _this.template.css('right', -(300+parseInt(_this.container.css('right')))+'px');

};

Notify.prototype.show = function () {
  var _this = this;

  if (!_this.container.find('#'+_this.id).length) {
    _this.container.prepend(_this.template); }

  _this.template.css('display', 'block');
  _this.template.off('click').on('click', function() {
    _this.close();
  });
  setTimeout(function() {
    _this.template.css('right', '0');
  }, 100);

  if (_this.options.timeout !== false) {
    _this.closeTime = setTimeout(function() {
      _this.close();
    }, (_this.options.timeout || 4500));
  }

  return _this;
};

Notify.prototype.close = function () {
  var _this = this;
  if (!_this.container.find('#'+_this.id).length) return;

  clearTimeout(_this.closeTime);
  _this.template.css('right', -(300+parseInt(_this.container.css('right')))+'px');

  setTimeout(function() {
    _this.template.remove();
  }, 500);

  return _this;
};