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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/*
* This file is part of the MediaWiki extension MultimediaViewer.
*
* MultimediaViewer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* MultimediaViewer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
*/
( function () {
var VL;
/**
* Tracks how long users are viewing images for
*
* @class mw.mmv.logging.ViewLogger
* @extends mw.Api
* @constructor
* @param {mw.mmv.Config} config mw.mmv.Config object
* @param {Object} windowObject Browser window object
* @param {mw.mmv.logging.ActionLogger} actionLogger ActionLogger object
*/
function ViewLogger( config, windowObject, actionLogger ) {
/**
* Was the last image view logged or was logging skipped?
*
* @property {boolean}
*/
this.wasLastViewLogged = false;
/**
* Record when the user started looking at the current image
*
* @property {number}
*/
this.viewStartTime = 0;
/**
* How long the user has been looking at the current image
*
* @property {number}
*/
this.viewDuration = 0;
/**
* The image URL to record a virtual view for
*
* @property {string}
*/
this.url = '';
/**
* If set, URI to send the beacon request to in order to record the virtual view
*
* @property {string}
*/
this.recordVirtualViewBeaconURI = config.recordVirtualViewBeaconURI();
/**
* Browser window
*
* @property {Object}
*/
this.window = windowObject;
/**
* Action logger
*
* @property {mw.mmv.logging.ActionLogger}
*/
this.actionLogger = actionLogger;
}
VL = ViewLogger.prototype;
/**
* Tracks the unview event of the current image if appropriate
*/
VL.unview = function () {
if ( !this.wasLastViewLogged ) {
return;
}
this.wasLastViewLogged = false;
this.actionLogger.log( 'image-unview', true );
};
/**
* Starts recording a viewing window for the current image
*/
VL.startViewDuration = function () {
this.viewStartTime = ( new Date() ).getTime();
};
/**
* Stops recording the viewing window for the current image
*/
VL.stopViewDuration = function () {
if ( this.viewStartTime ) {
this.viewDuration += ( new Date() ).getTime() - this.viewStartTime;
this.viewStartTime = 0;
}
};
/**
* Records the amount of time the current image has been viewed
*/
VL.recordViewDuration = function () {
var uri;
this.stopViewDuration();
if ( this.recordVirtualViewBeaconURI ) {
uri = new mw.Uri( this.recordVirtualViewBeaconURI );
uri.extend( { duration: this.viewDuration,
uri: this.url } );
try {
navigator.sendBeacon( uri.toString() );
} catch ( e ) {
$.ajax( {
type: 'HEAD',
url: uri.toString()
} );
}
mw.log( 'Image has been viewed for ', this.viewDuration );
}
this.viewDuration = 0;
this.unview();
};
/**
* Sets up the view tracking for the current image
*
* @param {string} url URL of the image to record a virtual view for
*/
VL.attach = function ( url ) {
var view = this;
this.url = url;
this.startViewDuration();
$( this.window )
.off( '.mmv-view-logger' )
.on( 'beforeunload.mmv-view-logger', function () {
view.recordViewDuration();
} )
.on( 'focus.mmv-view-logger', function () {
view.startViewDuration();
} )
.on( 'blur.mmv-view-logger', function () {
view.stopViewDuration();
} );
};
/*
* Stops listening to events
*/
VL.unattach = function () {
$( this.window ).off( '.mmv-view-logger' );
this.stopViewDuration();
};
/**
* Tracks whether or not the image view event was logged or not (i.e. was it in the logging sample)
*
* @param {boolean} wasEventLogged Whether the image view event was logged
*/
VL.setLastViewLogged = function ( wasEventLogged ) {
this.wasLastViewLogged = wasEventLogged;
};
mw.mmv.logging.ViewLogger = ViewLogger;
}() );
|