Source: managers/admin/adminDebugger.js

/**
 * @module managers/adminDebugger
 * @description This module manages the admin debugger pages
 */

define([
], 

function ()
{

    // ---------------------------------------------------------------
    //
    // ADMIN DEBUGGER
    //
    // ---------------------------------------------------------------

    var Class = function (admin)
    {
        console.log(" * <adminDebugger>", admin);   

        window.tEvent.eventStr.EVENT_ADMIN_DEBUGGER = "EVENT_ADMIN_DEBUGGER";

        this.ui = {
            debuggerSubmit      : ".js-admin-debugger-submit",
            debuggerEventInput  : ".js-debugger-event-input",
            debuggerIntentInput : ".js-debugger-intent-input",          
            debuggerInput       : ".js-debugger-input",
            debuggerSelect      : ".js-debugger-select",
            feedback            : ".js-feedback",
            feedbackCopy        : ".js-feedback-copy",
            testMode            : ".js-test-mode",
            testState           : ".js-test-state",
            testRequiredCopy    : ".js-required-copy",   
            dropdownIntents     : ".js-admin-select-intent",
            dropdownEvents      : ".js-admin-select-event",
            checkbox            : ".js-admin-checkbox",
            sendKeyBtn          : ".js-admin-sendkey",
            liveStatusNote      : ".js-sendkey-note",
            
            sonarPortStatus     : ".js-sonar-port-status",
            currentLanguage     : ".js-current-language",
            toggleBtn           : ".js-admin-toggle"
        };

        this.oAdmin = admin;
        this.oModel = this.oAdmin.oModel;
        this.oDxpTemplates = this.oAdmin.oDxpTemplates;

        this.def = window.helpers.clone(this.oAdmin.def);
        
        this.init();
    };

    Class.prototype =
    {    
       
        // ______________________________________________________________
        //                                                           init
        /**
            * init
            * @description Initializes class
            * @fires assignListeners()
            * @memberOf module:managers/adminDebugger
            * @instance
        */
        init: function()
        { 
            this.assignListeners();
        },


        // ______________________________________________________________
        //                                                assignListeners
        /** 
        * assignListeners
        * @description Assigns listeners to events used in this class.
        * @memberOf module:managers/adminDebugger
        * @instance
        */
        assignListeners: function()
        {           
            var self = this;
           
            window.tEvent.addListener(window.tEvent.eventStr.EVENT_ADMIN_DEBUGGER, function(evt, data) {
                if ($(".js-license-status").length === 0){
                    self.onOpenDebugger();
                }                
            });

            // debugger submit 
            $("body").hammer().on("click", this.ui.debuggerSubmit,function(evt){  
                var id = $(this).attr("data-id");                
                self.onDebuggerSubmit(id);
            });

        
            // checkboxes
            $("body").hammer().on("click", this.ui.checkbox,function(evt){   
                var $target = $(evt.target);               
                var id = $target.attr("data-id");
                var checkState = $target.prop("checked");

                if (id === "test-mode"){
                    self.onTestModeToggle($target, checkState);
                } 
                                
            });


            // sendkey status button
            $("body").hammer().on("click", this.ui.sendKeyBtn,function(evt){  
                var $target = $(evt.target);               
                var sendKey = $target.attr("data-sendkey");
                var id = $target.attr("data-id");

                self.onSendkeyBtn({
                    sendkey: sendKey,
                    id: id
                })
                                             
            });

        },
        


        // --------------------------------------------------------------
        // HELPERS
        // --------------------------------------------------------------
        
     


        // --------------------------------------------------------------
        // EVENTS
        // --------------------------------------------------------------
        


        // ______________________________________________________________
        //                                                   onSendkeyBtn
        /** 
        * onSendkeyBtn
        * @description Fires off send key from element object
        * @param {Object} dataObj - Object data from button
        * @fires oPrsonas.sendKey()
        * @memberOf module:managers/adminDebugger
        * @instance
        */  
        onSendkeyBtn: function(dataObj) { 

            $(this.ui.liveStatusNote).each(function(){
                $(this).css("display", "none");
            });
            $(this.ui.liveStatusNote + "[data-id='" + dataObj.id + "']").css("display", "block");
            window.oPrsonas.sendKey(dataObj.sendkey);  
            
        },



        // ______________________________________________________________
        //                                                 onOpenDebugger
        /** 
        * onOpenDebugger
        * @description Opens debugger
        * @memberOf module:managers/adminDebugger
        * @instance
        */ 
        onOpenDebugger: function() { 
            console.log(" * <admin.onOpenDebugger> ");

            this.oAdmin.killMinimizer();    

            // close existing admin modal
            if (typeof window.oModal.modals[this.def.TEMPLATE_ID] !== "undefined"){
                window.oModal.modals[this.def.TEMPLATE_ID].close(false, true);
            }

            var templateObj = {
                sendKeyEvents: this.oModel.jConfig.sendKeyEvents,
                IntentMetaData: this.oAdmin.manifest.IntentMetaData,
                jConfig: this.oModel.jConfig
            };

            // open admin debugger
            window.oModal.load({
                id          : this.def.TEMPLATE_DEBUGGER,
                modalSource : "alt",                
                content     :  window.oDxpTemplates[this.def.TEMPLATE_DEBUGGER](templateObj),
                backdrop    : 'static',
                css : {
                    width  : "90%",
                    height : "90%"
                }

            });
        },

        // ______________________________________________________________
        //                                               onTestModeToggle
        /** 
        * onTestModeToggle
        * @description Turns on test mode for IHA email and SMS testing
        * @param {JqueryObject} $check - Jquery object of check pressed
        * @param {String} checkState - Current check state
        * @fires oModel.setTestMode()
        * @memberOf module:managers/adminDebugger
        * @instance
        */ 
        onTestModeToggle: function($check, checkState) {    

            var $emailInput = $(this.ui.debuggerInput + "[name='test-mode'][data-id='email']");
            var $smsInput = $(this.ui.debuggerInput + "[name='test-mode'][data-id='sms']");
            var $requiredCopy = $(this.ui.testRequiredCopy);
            var validTestState = true;

            // reset
            $emailInput.removeClass(this.def.CLASS_INCOMPLETE);
            $smsInput.removeClass(this.def.CLASS_INCOMPLETE);
            $requiredCopy.removeClass(this.def.CLASS_INCOMPLETE);

            // turn off active state
            if (!checkState){
                $(this.ui.testMode).removeClass(this.def.CLASS_ACTIVE);
                $(this.ui.testState).html("Inactive");
                this.oAdmin.testMode = {
                    isActive: false,
                    emailTo: "",
                    smsTo: ""
                };
                window.oModel.setTestMode(this.oAdmin.testMode);  
                return
            } 

            // check email field
            if ($emailInput.val().trim() === ""){
                $emailInput.addClass(this.def.CLASS_INCOMPLETE);
                validTestState = false;
            }

            // check sms field
            if ($smsInput.val().trim() === ""){
                $smsInput.addClass(this.def.CLASS_INCOMPLETE);
                validTestState = false;
            }

            // take action
            if (validTestState){
                $(this.ui.testMode).addClass(this.def.CLASS_ACTIVE);
                $(this.ui.testState).html("Active");       
                this.oAdmin.testMode = {
                    isActive: true,
                    emailTo: $emailInput.val(),
                    smsTo: $smsInput.val()
                };
                window.oModel.setTestMode(this.oAdmin.testMode);    

            } else {
                $check.prop("checked", false);
                $requiredCopy.addClass(this.def.CLASS_INCOMPLETE);
            }


        },


        // ______________________________________________________________
        //                                               onDebuggerSubmit
        /** 
        * onDebuggerSubmit
        * @description Submits various admin functionality
        * @param {String} id - ID of what is being submitted
        * @memberOf module:managers/adminDebugger
        * @instance
        */ 
        onDebuggerSubmit: function(id) {
            var self = this;
            var keyVal = "",
                evt = "";

            switch(id){

                case "intent-select": 
                    keyVal = $(this.ui.debuggerSelect).val();
                    window.oPrsonas.sendKey(keyVal); 
                    break;

                case "intent-input": 
                    keyVal = $(this.ui.debuggerIntentInput).val();
                    $(this.ui.debuggerIntentInput).val("");
                    window.oPrsonas.sendKey(keyVal); 
                    break;

                case "intent-event-select":
                    keyVal = $(this.ui.dropdownIntents).val();
                    window.oPrsonas.sendKey(keyVal);
                    break;

                case "event-select":
                    evt = $(this.ui.dropdownEvents).val();
                    window.tEvent.fire(evt);
                    break;

                case "event-input": 
                    evt = $(this.ui.debuggerEventInput).val();
                    $(this.ui.debuggerEventInput).val("");
                    window.tEvent.fire(evt);
                    break;

                case "send-email":                                       
                case "send-sms":                                       
                    self.onSendEmailSms(id);
                    break;

            } 

                       
        },

        // ______________________________________________________________
        //                                                 onSendEmailSms
         /** 
        * onSendEmailSms
        * @description Sends IHA test email or SMS
        * @param {String} id - ID of what is being submitted [send-email, send-sms]
        * @memberOf module:managers/adminDebugger
        * @instance
        */ 
        onSendEmailSms: function(id) { 
            var self = this;
            var $feedback = $(this.ui.feedback + "[data-id='" + id + "']");
            var $feedbackCopy = $feedback.find(this.ui.feedbackCopy);
            var $input = $(this.ui.debuggerInput + "[name='" + id + "']");
            var val = $input.val().trim();

            // reset
            $feedbackCopy.html("");
            $feedback.removeClass(this.def.CLASS_ERROR);
            $input.removeClass(this.def.CLASS_INCOMPLETE);

            // check for errors
            if (val === ""){
                switch(id){
                    case "send-email":
                        $feedbackCopy.html("Missing Email.");
                        break;

                    case "send-sms":
                        $feedbackCopy.html("Missing Phone Number.");
                        break;    
                }                
                $feedback.addClass(this.def.CLASS_ERROR);
                $input.addClass(this.def.CLASS_INCOMPLETE);
                return;
            }

            // submit
            $feedbackCopy.html("Sending...");

            switch(id){
                case "send-email":
                    window.oIha.oIhaServices.sendEmail({
                        subjectText : "Prsonas Admin Test Email",
                        bodyText    : "Prsonas Admin Test Email",                        
                        fromEmail   : "prsonastest@prsonas.com",    
                        sendToEmail : val,
                        doneCallback: function(){
                            $feedbackCopy.html("Email Sent.");
                        },
                        failCallback: function(){
                            $feedback.addClass(self.def.CLASS_ERROR);
                            $feedbackCopy.html("Email did not send.");
                        }                
                    }, true);               
                    break;

                case "send-sms":
                    window.oIha.oIhaServices.sendSms({
                        bodyText    : "Prsonas Admin Test SMS",     
                        smsTo : val,
                        doneCallback: function(){
                            $feedbackCopy.html("SMS Sent.");
                        },
                        failCallback: function(){
                            $feedback.addClass(self.def.CLASS_ERROR);
                            $feedbackCopy.html("SMS did not send.");
                        }                
                    }, true);
                    break;    
            }            

        }


    };
        
    return (Class);        
   
});