Source: managers/timer-manager.js

/**
 * @module managers/timer-manager
 * @description Handles interaction timer
 * @requires jHammer
 */
define([
    "jHammer"  
], 

function ()
{

    // ---------------------------------------------------------------
    //
    // TIMER
    //
    // ---------------------------------------------------------------

    var Timer = function ()
    {
        window.tEvent.eventStr.EVENT_KILL_INTERACTION_TIMER  = "EVENT_KILL_INTERACTION_TIMER";
        window.tEvent.eventStr.EVENT_INTERACTION_BLOCK_STATE = "EVENT_INTERACTION_BLOCK_STATE";
        window.tEvent.eventStr.EVENT_SET_INTERACTION_BLOCK_TIMER   = "EVENT_SET_INTERACTION_BLOCK_TIMER";

        console.log(" * <timer>");        

        this.ui = {
            timeoutLeft : ".js-time-out-left"
        };
        
        this.jConfigTimes = -1;
        this.jLangConfig = -1;
        this.startingEvent = -1;

        this.timers = {
            interaction: -1,
            interactionDelay: -1,
            countdownInterval: -1,
            countdown: -1
        };

        this.interactionBlocked = false;
        this.countdown = -1;
        this.countdownInterval = -1;
    };

    Timer.prototype =
    {    
       
        // ______________________________________________________________
        //                                                           init
        /**
            * init
            * @description Initializes class
            * @param {Object} jconfig - Project JSON config
            * @fires assignListeners()
            * @memberOf module:managers/timer-manager
            * @instance
        */
        init: function(jconfig)
        { 
            var qs = window.helpers.parseQuerystring();

            if (typeof qs.interactiontimer !== "undefined"){
                configJson = qs.interactiontimer;
            }

            this.jConfigTimes = jconfig.prsonas.timers;

            if (typeof qs.interactiontimer !== "undefined"){
                this.jConfigTimes.interaction = Number(qs.interactiontimer);
            }

            // load from Prsonas viewer commandline
            if (typeof window.oPrsonas !== "undefined" &&
                typeof window.oPrsonas.app !== "undefined" &&
                window.oPrsonas.app.getCommandLineParameters() !== null){
                
                if (typeof window.oPrsonas.app.getCommandLineParameters().interactiontimer !== "undefined"){
                     this.jConfigTimes.interaction =  Number(window.oPrsonas.app.getCommandLineParameters().interactiontimer);
                }  
            }


            this.startingEvent = jconfig.document.startEvent;
            this.assignListeners();            
        },


        // ______________________________________________________________
        //                                                assignListeners
        /** 
        * assignListeners
        * @description Assigns listeners to events used in this class.
        * @listens  mouseup
        * @listens  mousedown
        * @listens  keydown
        * @listens  EVENT_LANGUAGE_CONFIG_LOADED
        * @listens  EVENT_VIDEO_END
        * @listens  EVENT_VIDEO_START
        * @listens  EVENT_VIDEO_PAUSE
        * @listens  EVENT_KILL_INTERACTION_TIMER
        * @listens  EVENT_SET_INTERACTION_BLOCK_TIMER
        * @memberOf module:managers/timer-manager
        * @instance
        */
        assignListeners: function()
        {           
            var self = this;

            // capture all swipe
            $("body").hammer().on("mouseup", function(e){
                if (self.timers.interaction !== -1){
                    self.resetTimeout();
                }
            });

            $("body").hammer().on("mousedown", function(e){                
                if (self.timers.interaction !== -1){
                    self.resetTimeout();
                }
            });

            // capture keypress
            $(document).keydown(function(e){               
                if (self.timers.interaction !== -1){
                    self.resetTimeout();
                }
            });

            // get language config
            window.tEvent.addListener("EVENT_LANGUAGE_CONFIG_LOADED", function(evt, data){
                self.jLangConfig = data;
            });

            // video - ended
            window.tEvent.addListener(window.tEvent.eventStr.EVENT_VIDEO_END, function(evt, data){
                self.resetTimeout();
            });
            
            // video - start
            window.tEvent.addListener(window.tEvent.eventStr.EVENT_VIDEO_START, function(evt, data){
                self.killTimer();
            });

            // video - paused
            window.tEvent.addListener(window.tEvent.eventStr.EVENT_VIDEO_PAUSE, function(evt, data){
                self.resetTimeout();
            });

            // kill interaction timer
            window.tEvent.addListener(window.tEvent.eventStr.EVENT_KILL_INTERACTION_TIMER, function(evt, data){
                self.killTimer();
            });

            // set interaction timer block
            window.tEvent.addListener(window.tEvent.eventStr.EVENT_SET_INTERACTION_BLOCK_TIMER, function(evt, data){
                self.setInteractionDelayTimer();
            });

            // cc event
            window.oPrsonas.sdk.subscribe(window.oPrsonas.avatar.eventTypes["CC"], function(evt) {
                self.resetTimeout();
            });

            // endcc event
            window.oPrsonas.sdk.subscribe(window.oPrsonas.avatar.eventTypes["END_CC"], function(evt) {
                self.resetTimeout();
            });

        },

        


        // --------------------------------------------------------------
        // HELPERS
        // --------------------------------------------------------------
       
        // ______________________________________________________________
        //                                                      killTimer
        /**
            * killTimer
            * @description Kills the interactive timer
            * @memberOf module:managers/timer-manager
            * @instance
        */
        killTimer: function()
        {  

            //console.log(" * <timer.killTimer>");
            clearTimeout(this.timers.interaction);
            this.timers.interaction = -1;

            clearInterval(this.timers.countdownInterval);
            this.timers.countdownInterval = -1;

            this.timers.countdown = -1;

            if (typeof window.oModal.modals["m_timer_expired"] !== "undefined") {
                window.oModal.close();
            }
        },

        // ______________________________________________________________
        //                                       setInteractionDelayTimer
        /**
            * setInteractionDelayTimer
            * @description Sets a delay between clicks to prevent rapid click issues
            * @fire EVENT_INTERACTION_BLOCK_STATE
            * @memberOf module:managers/timer-manager
            * @instance
        */
        setInteractionDelayTimer: function()
        {  
            if (typeof this.jConfigTimes.interactionDelay === "undefined" || this.jConfigTimes.interactionDelay === 0 || this.jConfigTimes.interactionDelay === -1){
                return;
            }

            var self = this;   

            this.interactionBlocked = true;
            window.tEvent.fire(window.tEvent.eventStr.EVENT_INTERACTION_BLOCK_STATE, this.interactionBlocked);   

            this.timers.interactionDelay = setTimeout(function(){ 
                clearTimeout(self.timers.interactionDelay);
                self.timers.interactionDelay = -1;
                self.interactionBlocked = false;
                window.tEvent.fire(window.tEvent.eventStr.EVENT_INTERACTION_BLOCK_STATE, self.interactionBlocked);
                console.log(" * <timer.setInteractionDelayTimer> released");
            }, self.jConfigTimes.interactionDelay); 

        },  

        // ______________________________________________________________
        //                                                   resetTimeout
        /**
            * resetTimeout
            * @description Resets the interactive timer
            * @memberOf module:managers/timer-manager
            * @instance
        */
        resetTimeout: function()
        {  
            var self = this;           
            var doModal = false;
            var timer = self.jConfigTimes.interaction;
            var language = $("body").attr("data-lang");          
            this.killTimer();
            
            this.timers.countdown = 10;
            var warningTimer = 1000 * this.timers.countdown;

            if (window.oModel.queryString.is_timer === "false" || self.jConfigTimes.interaction === -1){
                return;
            }
            
            if (self.jConfigTimes.interaction > warningTimer){
                doModal = true;
                timer -= warningTimer;
            }

            var expireTimer = function(){
                window.tEvent.fire(self.jConfigTimes.expiredEvent);
                window.tEvent.fire(window.tEvent.eventStr.EVENT_TIMER_EXPIRED);                
                self.killTimer();                
            }


            this.timers.interaction = setTimeout(function(){ 

                // check if avatar is speaking.
                if ( window.oPrsonas.avatar.isAvatarSpeaking ){
                    self.killTimer();
                    return;
                }

                if (doModal){
                    window.oModal.load({
                        id       : "m_timer_expired",
                        content  :  window.oDxpTemplates.m_timer_expired({
                            "iha_expired_warning_countdown_1" : self.jLangConfig.languageCopy.iha_expired_warning_countdown_1[language],
                            "iha_expired_warning_countdown_2" : self.jLangConfig.languageCopy.iha_expired_warning_countdown_2[language],
                            "iha_expired_warning_continue"    : self.jLangConfig.languageCopy.iha_expired_warning_continue[language],
                            "countdown"                       : self.timers.countdown
                        }),
                        backdrop : 'static',
                        close    : false
                    });
                    window.oPrsonas.sendKey("Timeout");

                    self.timers.countdownInterval = setInterval(function(){
                        self.timers.countdown--;
                        if (self.timers.countdown === 0) {
                            expireTimer();
                        } else {
                            $(self.ui.timeoutLeft).html(self.timers.countdown);
                        }
                    }, 1000);


                } else {
                    expireTimer();
                }

            }, timer);           

        }



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



    };

    /** 
    * @global 
    */
    window.oTimer = new Timer();
    
    return (window.oTimer);        
   
});