All files / sp-toast ViewModel.js

85% Statements 17/20
75% Branches 9/12
85.71% Functions 6/7
85% Lines 17/20

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                                          4x 2x   4x                       2x 2x       2x                                               3x 3x   3x                                                                     6x 2x   6x                                                                       2x     2x 2x 2x 2x            
import DefineMap from 'can-define/map/map';
 
/**
 * A `<sp-toast />` component's ViewModel
 * @class ViewModel
 * @memberof sp-toast
 */
export default DefineMap.extend('ToastItem', {
    /** @lends sp-toast.ViewModel.prototype */
    /**
   * whether or not to fade the sp-toast out using animate.css
   * @type {Boolean}
   * @memberof sp-toast.ViewModel.prototype
   *
   */
    fade: {
        type: 'boolean',
        default: true
    },
    timer: {
        set (timer) {
            if (this.timer) {
                window.clearTimeout(this.timer);
            }
            return timer;
        }
    },
    /**
     * the time to autohide this sp-toast. Set to 0 to disable auto hide
     * @type {Number}
     * @memberof sp-toast.ViewModel.prototype
     */
    autoHide: {
        type: 'number',
        default: 5000,
        set (autohide) {
            Eif (autohide) {
                this.timer = setTimeout(() => {
                    this.hide();
                }, autohide);
            }
            return autohide;
        }
    },
    /**
     * Whether or not to use the content tag, that will display whatever
     * is inside the `<sp-toast></sp-toast>` tags. This overrides the
     * body property of this sp-toast
     * @type {Boolean}
     * @memberof sp-toast.ViewModel.prototype
     */
    custom: {
        type: 'boolean',
        default: false
    },
    /**
     * @type {string}
     * @memberof sp-toast.ViewModel.prototype
     * @description The class that gives the sp-toast context. Must be either
     * info, success, warning, or danger.
     * @option {string} Defaults to `info`.
     */
    severity: {
        default: 'info',
        type: function (val) {
            var allowed = ['primary', 'info', 'success', 'warning', 'error'],
                isValid = allowed.indexOf(val) > -1;
 
            return isValid ? val : allowed[0];
        }
    },
 
    /**
     * @type {boolean}
     * @memberof sp-toast.ViewModel.prototype
     * @description Marks the sp-toast as dismissable, which adds a "close" icon to the sp-toast.
     * The default is true
     */
    dismissable: {
        default: true,
        type: 'boolean'
    },
    /**
     * @type {boolean}
     * @memberof sp-toast.ViewModel.prototype
     * @description Displays a helpful icon next to the toast text
     * Set to `null` or empty string to exclude icon completely. The default is
     * `icon icon-error_outline`
     */
    iconClass: {
        type: 'string',
        default: 'icon icon-error_outline'
    },
 
    /**
     * @type {boolean}
     * @memberof sp-toast.ViewModel.prototype
     * @description Toggles visiblity of the sp-toast. The default is false.
     */
    visible: {
        default: true,
        type: 'boolean',
        set (visible) {
            if (!visible) {
                this.timer = null;
            }
            return visible;
        }
    },
 
    /**
     * @type {string}
     * @memberof sp-toast.ViewModel.prototype
     * @description The content displayed in the toast. The default is an empty string.
     */
    body: {
        default: '',
        type: 'string'
    },
    /**
     * @type {string}
     * @memberof sp-toast.ViewModel.prototype
     * @description Optional. The title of the sp-toast. The default is an empty string.
     */
    heading: {
        default: '',
        type: 'string'
    },
    /**
     * Time in miliseconds to fade out this sp-toast
     * @memberof sp-toast.ViewModel.prototype
     * @type {Number}
     */
    fadeTime: {
        type: 'number',
        default: 1000
    },
    element: '*',
    /**
     * Hide this sp-toast
     */
    hide () {
        Iif (!this.visible) {
            return;
        }
        this.visible = false;
        Eif (this.fade) {
            setTimeout(() => {
                this.dispatch('hide', [this]);
            }, this.fadeTime);
        } else {
            this.dispatch('hide', [this]);
        }
    }
});