dialog.js 3.55 KB
/*
 * ADOBE CONFIDENTIAL
 *
 * Copyright 2015 Adobe Systems Incorporated
 * All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 */
define('screens/player/ui/dialog', [
    'jquery'
], function($) {
    'use strict';

    /**
     * Default options for the component.
     *
     * @typedef {Object} Dialog.ActivityOptions
     * @type {Object}
     * @property {String} [baseCssClass]        The base CSS class for the component.
     */
    var DEFAULTS = {
        baseCssClass: 'aem-ScreensPlayer-dialog',
        parent: '.channel-container'
    };

    /**
     * Dialog for the player.
     *
     * @class Dialog
     *
     * @param {Dialog.DialogOptions} [options]    Options for the component.
     */
    var Dialog = function(options) {
        this.options = $.extend({}, DEFAULTS, options);
        this.$el = $(document.createElement('div')).addClass(this.options.baseCssClass);
        this.$el.append('<p class="aem-ScreensPlayer-dialog-message"></p>\
                        <div class="aem-ScreensPlayer-dialog-buttons">\
                            <span class="aem-ScreensPlayer-dialog-button btn-dialog-cancel">Cancel</span>\
                            <span class="aem-ScreensPlayer-dialog-button btn-dialog-submit">Confirm</span>\
                        </div>');
        this.$cancel = this.$el.find('.btn-dialog-cancel');
        this.$submit = this.$el.find('.btn-dialog-submit');
        this.hide();
        $('body').append(this.$el);
    };

    /**
     * Show the dialog.
     * @memberof Dialog
     *
     * @param {Function} [cb] The function to call once the dialog is shown
     */
    Dialog.prototype.show = function(cb) {
        $(this.options.parent).addClass(this.options.baseCss + '--blur');
        this.$el.removeClass('is-hidden').addClass('is-visible').fadeIn(300, cb);
    };

    /**
     * Hide the dialog.
     * @memberof Dialog
     *
     * @param {Function} [cb] The function to call once the dialog is hidden
     */
    Dialog.prototype.hide = function(cb) {
        $(this.options.parent).removeClass(this.options.baseCss + '--blur');
        this.$el.removeClass('is-visible').addClass('is-hidden').fadeOut(300, cb);
        this.$cancel.off('click');
        this.$submit.off('click');
    };

    Dialog.prototype.confirm = function(msg) {
        var self = this;
        var message = this.$el.find('.aem-ScreensPlayer-dialog-message');
        message.text(msg);
        this.show();
        return new Promise(function(resolve, reject) {
            // cancel button closes the dialog box
            self.$cancel.one('click', function listener() {
                self.hide();
                resolve(false);
            });

            // submit button
            self.$submit.one('click', function listener() {
                self.hide();
                resolve(true);
            });
        });
    };


    /**
     * Destroy the dialog.
     * @memberof Dialog
     */
    Dialog.prototype.destroy = function() {
        this.hide();
        this.$el.remove();
        this.$el = null;
    };

    return Dialog;

});