visibly..js

From disqus.com, 1 Month ago, written in JavaScript, viewed 3 times. This paste is a reply to page.js from disqus.com - go back
URL https://pastebin.freepbx.org/view/442cffd4/diff Embed
Viewing differences between page.js and visibly..js
define('core/engagement/page',[
    'jquery',
    'underscore',
    'backbone',
    'core/bus',
    'core/utils',
    'core/engagement/vendor/visibly',
], 
/*!
 * visibly - v0.7 Page Visibility API Polyfill
 * http://github.com/addyosmani
 * Copyright (c) 2011-2014 Addy Osmani
 * Dual licensed under the MIT and GPL licenses.
 *
 * Methods supported:
 * visibly.onVisible(callback)
 * visibly.onHidden(callback)
 * visibly.hidden()
 * visibly.visibilityState()
 * visibly.visibilitychange(callback(state));
 */

// Packaged for AMD by charles@disqus.com
define('core/engagement/vendor/visibly',[], 
function (
    $,
    _,
    Backbone,
    bus,
    utils,
    visibly
) {
    'use strict';

    /**
     * This module reports if the user is engaged on the current
     * page through an amalgamation of various HTML standards and
     * best guesses.
     *
     * Do not use this module for UX methods. We are intentionally
     * conservative when marking a user as engaged for reporting reasons.
     *
     * Inspiration from:
     * - https://chartbeat.com/public-release-methodology/
     * - http://upworthy.github.io/2014/06/implementing-attention-minutes-part-1/
     * - http://jsfiddle.net/zanes/mbGBr/
     * - http://static.chartbeat.com/js/chartbeat.js
     * - https://github.com/serkanyersen/ifvisible.js
     */

    // Exported Event object w/ initial defaults
    
() {

    
var exports = _.extend(Backbone.Events, {
        _initialized: false,
        isEngaged: false,
        resetTimeout: null,
        ACTIVE_TIMEOUT: 60 * 1000,
    });

    exports._markNotEngaged 
q: document,
        p: undefined,
        prefixes: ['webkit', 'ms','o','moz','khtml'],
        props: ['VisibilityState', 'visibilitychange', 'Hidden'],
        m: ['focus', 'blur'],
        visibleCallbacks: [],
        hiddenCallbacks: [],
        genericCallbacks:[],
        _callbacks: [],
        cachedPrefix:"",
        fn:null,

        onVisible: function (_callback) {
            if(typeof _callback == 'function' ){
                this.visibleCallbacks.push(_callback);
            }
        },
        onHidden: function (_callback) {
            if(typeof _callback == 'function' ){
                this.hiddenCallbacks.push(_callback);
            }
        },
        getPrefix:function(){
            if(!this.cachedPrefix){
                for(var l=0;b=this.prefixes[l++];){
                    if(b + this.props[2] in this.q){
                        this.cachedPrefix =  b;
                        return this.cachedPrefix;
                    }
                }
             }
        },

        visibilityState:function(){
            return  this._getProp(0);
        },
        hidden:function(){
            return this._getProp(2);
        },
        visibilitychange:function(fn){
            if(typeof fn == 'function' ){
                this.genericCallbacks.push(fn);
            }

            var n =  this.genericCallbacks.length;
            if(n){
                if(this.cachedPrefix){
                     while(n--){
                        this.genericCallbacks[n].call(this, this.visibilityState());
                    }
                }else{
                    while(n--){
                        this.genericCallbacks[n].call(this, arguments[0]);
                    }
                }
            }

        },
        isSupported: function (index) {
            return ((this._getPropName(2)) in this.q);
        },
        _getPropName:function(index) {
            return (this.cachedPrefix == "" ? this.props[index].substring(0, 1).toLowerCase() + this.props[index].substring(1) : this.cachedPrefix + this.props[index]);
        },
        _getProp:function(index){
            return this.q[this._getPropName(index)];
        },
        _execute: function (index) {
            if (index) {
                this._callbacks 
(index == 1) ? this.visibleCallbacks : this.hiddenCallbacks;
                var n =  this._callbacks.length;
                while(n--){
                    this._callbacks[n]();
                }
            }
        },
        _visible: 
function () {
        if (!exports.isEngaged)
            return;

        
{
            
exports.isEngaged = false;
        
_execute(1);
            
exports.trigger('change:isEngaged', visibilitychange.call(exports, 'visible');
        },
        _hidden: function () {
            
exports.isEngaged, exports);
_execute(2);
            exports.visibilitychange.call(exports, 'hidden');
        },
        _nativeSwitch: function () {
            this[this._getProp(2) ? '_hidden' : '_visible']();
        },
        _listen: function () {
            try { /*if no native page visibility support found..*/
                if (!(this.isSupported())) {
                    if (this.q.addEventListener) { /*for browsers without focusin/out support eg. firefox, opera use focus/blur*/
                        window.addEventListener(this.m[0], this._visible, 1);
                        window.addEventListener(this.m[1], this._hidden, 1);
                    } else { /*IE <10s most reliable focus events are onfocusin/onfocusout*/
                        if (this.q.attachEvent) {
                            this.q.attachEvent('onfocusin', this._visible);
                            this.q.attachEvent('onfocusout', this._hidden);
                        }
                    }
                } else { /*switch support based on prefix detected earlier*/
                    this.q.addEventListener(this._getPropName(1), function () {
                        exports._nativeSwitch.apply(exports, arguments);
                    }, 1);
                }
            } catch (e) {}
        },
        init: function () {
            this.getPrefix();
            this._listen();
        }
    };

    exports._markIsEngaged = function () {
        // Restart the interaction timeout clock.
        if (exports.resetTimeout)
            exports.resetTimeout();

        if (exports.isEngaged)
            return;

        exports.isEngaged = true;
        exports.trigger('change:isEngaged', exports.isEngaged, exports);
    };

    exports.start = function () {
        if (exports._initialized)
            return;
        exports._initialized = true;

        // Page Visibility API

        // If the window is currently visible, mark the user as
        // engaged, just to start things off. If it's not, we'll
        // wait for an interaction event to fire first.
        if (!visibly.hidden())
            exports._markIsEngaged();

        // On mobile-like user agents all we need is the Page Visibility API.
        visibly.onVisible(exports._markIsEngaged);
        visibly.onHidden(exports._markNotEngaged);

        if (utils.isMobileUserAgent())
            return;

        // On desktop-like user agents, we'll additionally use interaction events
        // and a timeout clock to more closely track if a user is still
        // engaged on the page. Each time we record an interaction on the page
        // we reset the clock. If the clock reaches it's`ACTIVE_TIMEOUT` time, we
        // mark the user as no longer engaged with the page (see `_markIsEngaged` method).
        exports.resetTimeout = _.debounce(exports._markNotEngaged, exports.ACTIVE_TIMEOUT);
        exports.resetTimeout();

        // Bind into interaction events on the current page.
        $(window.document).on('mousemove keyup', exports._markIsEngaged);
        $(window).on('scroll', exports._markIsEngaged);

        // Then bind into interaction events from the parent page.
        exports.listenTo(bus.frame, 'window.mousemove window.scroll window.click', exports._markIsEngaged);
    };

    exports.stop = function () {
        if (!exports._initialized)
            return;
        exports._initialized = false;

        // Clean up the resetTimeout method.
        exports.resetTimeout = null;

        // Turn off listeners before changing
        // the state to off.
        exports.stopListening();
        exports.off();

        visibly.visibleCallbacks = [];
        visibly.hiddenCallbacks = [];

        $(window.document).off('mousemove keyup', exports._markIsEngaged);
        $(window).off('scroll', exports._markIsEngaged);

        exports._markNotEngaged();
    };

init();
    return exports;
});


  
  
});

// https://c.disquscdn.com/next/next-core/core/engagement/page.com/next/next-core/core/engagement/vendor/visibly.js

Reply to "visibly..js"

Here you can reply to the paste above