All files / sp-confirm ViewModel.js

94.74% Statements 18/19
66.67% Branches 4/6
90% Functions 9/10
94.74% Lines 18/19

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                1x                     4x 2x 2x   2x 2x         4x                                                                             2x 2x 2x 2x       2x     2x                 2x             1x 1x               1x 1x                  
import ModalViewModel from '../sp-modal/ViewModel';
import {ActionList} from '../util/actions/Action';
 
/**
 * A `<sp-confirm />` component's ViewModel
 * @module sp-confirm/ViewModel
 * @extends sp-modal/ViewModel
 */
const ViewModel = ModalViewModel.extend('ConfirmDialog', {
    /** @lends sp-confirm/ViewModel.prototype */
    /**
     * When this is set to true, a promise gets created that 
     * is resolved once the user accepts or rejects the message.
     * 
     * @type {Boolean}
     * @override
     */
    active: {
        set (active) {
            if (active) {
                this.promise = new Promise((resolve) => {
                    this.resolver = resolve;
                });
            } else Eif (this.resolver) {
                this.resolver({
                    status: 'rejected',
                    reason: 'active-set'
                });
            }
            return active;
        }
    },
    /**
     * Array of modal actions
     * @override
     * @type {util/actions/Action[]}
     */
    actions: {
        Type: ActionList,
        default () {
            return [{
                onclick: this.accept,
                label: this.acceptText,
                buttonClass: 'btn btn-primary'
            }, {
                onclick: this.reject,
                label: this.rejectText,
                buttonClass: 'btn btn-secondary'
            }];
        }
    },
    /**
     * Text to display as the accept button. Default is `'OK'`.
     * @type {String}
     */
    acceptText: {default: 'OK'},
    /**
     * Text to display as the reject button. Default is `'Cancel'`.
     * @type {String}
     */
    rejectText: {default: 'Cancel'},
    /**
     * Promise that resolves once the accept or reject button is clicked.
     * In order to access this property, `active` must be set to `true`.
     * @type {Promise<sp-confirm/ViewModel~ConfirmResult>}
     */
    promise: {
        set (promise) {
            Eif (promise) {
                this.outcome = {status: 'pending'};
                promise.then((outcome) => {
                    this.assign({
                        outcome,
                        active: false
                    });
                    return outcome;
                });
            }
            return promise;
        }
    },
    /**
     * Current promise status.
     * @type {String}
     */
    outcome: {
        default () {
            return {status: 'ready'};
        }
    },
    // resolves the promise 
    resolver: {},
    accept: {
        default () {
            return () => {
                this.resolver({
                    status: 'accepted'
                });
            };
        }
    },
    reject: {
        default () {
            return () => {
                this.resolver({
                    status: 'rejected',
                    reason: 'cancel'
                });
            };
        }
    }
});
 
export default ViewModel;