all files / addon/services/ message.js

3.13% Statements 1/32
4.17% Branches 1/24
0% Functions 0/9
3.13% Lines 1/32
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                                                                                                                                                                                                                                               
import Service from '@ember/service';
import {assign, merge} from '@ember/polyfills';
import {A} from '@ember/array';
import {isEmpty} from '@ember/utils';
import EmberObject, {set, get} from '@ember/object';
import {later, cancel} from '@ember/runloop';
 
const messageAssign = assign || merge;
export default Service.extend({
 
  messages: A(),
 
  // Method for adding a message
  addMessage(options) {
    if (!options.message) {
      throw new Error("No message set");
    }
 
 
    const message = EmberObject.create({
      message: options.message,
      duration: (isEmpty(options.duration) ? 3000 : options.duration),
      type: options.type || 'info',
      iconClass: options.iconClass || '',
      customClass: options.customClass || '',
      onClose: options.onClose, //null,
      showClose: options.showClose, //false,
      timer: options.timer, //null,
      center: options.center, //false,
      dismiss: false,
      autoClear: (isEmpty(options.autoClear) ? true : options.autoClear),
      return: (isEmpty(options.return) ? null : options.return),
    });
 
    if (get(message, 'autoClear')) {
      set(message, 'remaining', get(message, 'duration'));
      this.startTimer(message);
    }
 
    get(this, 'messages').pushObject(message);
 
    return message;
  },
 
  // Helper methods for each type of message
  error(message, options) {
    return this.addMessage(messageAssign({
      message: message,
      type: 'error'
    }, options));
  },
 
  success(message, options) {
    return this.addMessage(messageAssign({
      message: message,
      type: 'success'
    }, options));
  },
 
  info(message, options) {
    return this.addMessage(messageAssign({
      message: message,
      type: 'info'
    }, options));
  },
 
  warning(message, options) {
    return this.addMessage(messageAssign({
      message: message,
      type: 'warning'
    }, options));
  },
 
  removeMessage(message) {
    if (!message) {
      return;
    }
 
    set(message, 'dismiss', true);
 
    if (typeof get(message, 'onClose') === 'function') {
      get(message, 'onClose')(message);
    }
 
 
    // Delay removal from DOM for dismissal animation
    later(this, () => {
      get(this, 'messages').removeObject(message);
    }, 1000);
  },
 
  startTimer(message) {
    set(message, 'startTime', Date.now());
 
    const timer = later(this, () => {
      // Hasn't been closed manually
      if (get(this, 'messages').indexOf(message) >= 0) {
        this.removeMessage(message);
      }
    }, get(message, 'remaining'));
    set(message, 'timer', timer);
  },
 
  clearTimer(message) {
    cancel(get(message, 'timer'));
    const elapsed = Date.now() - get(message, 'startTime');
    const remaining = get(message, 'duration') - elapsed;
    set(message, 'remaining', remaining);
  },
 
  clearAll() {
    get(this, 'messages').forEach(message => {
      this.removeMessage(message);
    });
 
    return this;
  },
 
});