Code coverage report for app/components/modal/modal.service.js

Statements: 100% (61 / 61)      Branches: 100% (20 / 20)      Functions: 100% (15 / 15)      Lines: 100% (32 / 32)      Ignored: 2 statements, 5 branches     

All files » app/components/modal/ » modal.service.js
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                                                                                                                   
import angular from 'angular';
 
class ModalService {
    constructor ($timeout) {
        Object.assign(this, {$timeout});
    }
 
    open (title, content, buttons, callback) {
        const modalView = angular.element('<div/>', {
            class: 'modal-view'
        });
        const modalDiv = angular.element('<div/>', {
            id: 'modal',
            class: 'modal'
        });
        modalDiv
            .append(this._buildHeaderAndContent(title, content))
            .append(this._buildFooter(buttons, callback));
        modalView
            .append(modalDiv)
            .appendTo('body');
        this.$timeout(() => {
            $('#modal').openModal({
                dismissible: false
            });
        }, 100);
    }
 
    _buildHeaderAndContent (title, content) {
        const headerDiv = angular.element('<div/>', {
            class: 'modal-content'
        });
        // title
        const titleDiv = angular.element('<h4/>', {
            text: title
        });
        // content
        const contentDiv = angular.element('<p/>', {
            text: content
        });
        headerDiv.append(titleDiv).append(contentDiv);
        return headerDiv;
    }
 
    _buildFooter (buttons, callback) {
        const footerDiv = angular.element('<div/>', {
            class: 'modal-footer'
        });
        // ok button
        const okBtn = angular.element('<a/>', {
            class: 'btn btn-ok modal-action modal-close waves-effect waves-light mr3',
            text: buttons.ok
        });
        okBtn.on('click', () => {
            if (angular.isDefined(callback)) {
                callback(true);
            }
            this._close();
        });
        footerDiv.append(okBtn);
        // cancel button
        if (angular.isDefined(buttons.cancel)) {
            const cancelBtn = angular.element('<a/>', {
                class: 'btn btn-cancel white black-text modal-action modal-close waves-effect waves-light mr2',
                text: buttons.cancel
            });
            footerDiv.append(cancelBtn);
            cancelBtn.on('click', () => {
                if (angular.isDefined(callback)) {
                    callback(false);
                }
                this._close();
            });
        }
        return footerDiv;
    }
 
    _close () {
        this.$timeout(() => {
            angular.element('.modal-view').remove();
        }, 100);
    }
}
 
ModalService.$inject = ['$timeout'];
 
export default ModalService;