{"version":3,"sources":["active-users.js"],"names":["gapi","analytics","ready","createComponent","initialize","this","activeUsers","execute","polling_","stop","render_","auth","isAuthorized","getActiveUsers_","once","bind","clearTimeout","timeout_","emit","opts","get","container","document","getElementById","innerHTML","template","querySelector","options","pollingInterval","isNaN","Error","client","data","realtime","ids","metrics","response","newValue","totalResults","rows","oldValue","onChange_","setTimeout","delta","valueContainer"],"mappings":"AAiBAA,KAAKC,UAAUC,MAAM,WAEnBF,KAAKC,UAAUE,gBAAgB,eAE7BC,WAAY,WACVC,KAAKC,YAAc,GAGrBC,QAAS,WAEHF,KAAKG,UACPH,KAAKI,OAGPJ,KAAKK,UAGDV,KAAKC,UAAUU,KAAKC,eACtBP,KAAKQ,kBAGLb,KAAKC,UAAUU,KAAKG,KAAK,UAAWT,KAAKQ,gBAAgBE,KAAKV,QAIlEI,KAAM,WACJO,aAAaX,KAAKY,UAClBZ,KAAKG,UAAW,EAChBH,KAAKa,KAAK,QAASZ,YAAaD,KAAKC,eAGvCI,QAAS,WACP,GAAIS,GAAOd,KAAKe,KAGhBf,MAAKgB,UAAqC,gBAAlBF,GAAKE,UACzBC,SAASC,eAAeJ,EAAKE,WAAaF,EAAKE,UAEnDhB,KAAKgB,UAAUG,UAAYL,EAAKM,UAAYpB,KAAKoB,SACjDpB,KAAKgB,UAAUK,cAAc,KAAKF,UAAYnB,KAAKC,aAGrDO,gBAAiB,WACf,GAAIc,GAAUtB,KAAKe,MACfQ,EAAmD,KAAhCD,EAAQC,iBAAmB,EAElD,IAAIC,MAAMD,IAAsC,IAAlBA,EAC5B,KAAM,IAAIE,OAAM,uCAGlBzB,MAAKG,UAAW,EAChBR,KAAK+B,OAAO9B,UAAU+B,KAAKC,SACxBb,KAAKc,IAAIP,EAAQO,IAAKC,QAAQ,mBAC9B5B,QAAQ,SAAS6B,GAEhB,GAAIC,GAAWD,EAASE,cAAgBF,EAASG,KAAK,GAAG,GAAK,EAC1DC,EAAWnC,KAAKC,WAEpBD,MAAKa,KAAK,WAAYZ,YAAaD,KAAKC,cAEpC+B,GAAYG,IACdnC,KAAKC,YAAc+B,EACnBhC,KAAKoC,UAAUJ,EAAWG,KAGxBnC,KAAKG,UAAW,KAClBH,KAAKY,SAAWyB,WAAWrC,KAAKQ,gBAAgBE,KAAKV,MACjDuB,KAENb,KAAKV,QAGXoC,UAAW,SAASE,GAClB,GAAIC,GAAiBvC,KAAKgB,UAAUK,cAAc,IAC9CkB,KAAgBA,EAAepB,UAAYnB,KAAKC,aAEpDD,KAAKa,KAAK,UAAWZ,YAAaD,KAAKC,YAAaqC,MAAOA,IACvDA,EAAQ,EACVtC,KAAKa,KAAK,YAAaZ,YAAaD,KAAKC,YAAaqC,MAAOA,IAG7DtC,KAAKa,KAAK,YAAaZ,YAAaD,KAAKC,YAAaqC,MAAOA,KAIjElB,SACE","file":"active-users.js","sourcesContent":["// Copyright 2014 Google Inc. All rights reserved.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n\n/* global gapi */\n\ngapi.analytics.ready(function() {\n\n gapi.analytics.createComponent('ActiveUsers', {\n\n initialize: function() {\n this.activeUsers = 0;\n },\n\n execute: function() {\n // Stop any polling currently going on.\n if (this.polling_) {\n this.stop();\n }\n\n this.render_();\n\n // Wait until the user is authorized.\n if (gapi.analytics.auth.isAuthorized()) {\n this.getActiveUsers_();\n }\n else {\n gapi.analytics.auth.once('success', this.getActiveUsers_.bind(this));\n }\n },\n\n stop: function() {\n clearTimeout(this.timeout_);\n this.polling_ = false;\n this.emit('stop', {activeUsers: this.activeUsers});\n },\n\n render_: function() {\n var opts = this.get();\n\n // Render the component inside the container.\n this.container = typeof opts.container == 'string' ?\n document.getElementById(opts.container) : opts.container;\n\n this.container.innerHTML = opts.template || this.template;\n this.container.querySelector('b').innerHTML = this.activeUsers;\n },\n\n getActiveUsers_: function() {\n var options = this.get();\n var pollingInterval = (options.pollingInterval || 5) * 1000;\n\n if (isNaN(pollingInterval) || pollingInterval < 5000) {\n throw new Error('Frequency must be 5 seconds or more.');\n }\n\n this.polling_ = true;\n gapi.client.analytics.data.realtime\n .get({ids:options.ids, metrics:'rt:activeUsers'})\n .execute(function(response) {\n\n var newValue = response.totalResults ? +response.rows[0][0] : 0;\n var oldValue = this.activeUsers;\n\n this.emit('success', {activeUsers: this.activeUsers});\n\n if (newValue != oldValue) {\n this.activeUsers = newValue;\n this.onChange_(newValue - oldValue);\n }\n\n if (this.polling_ = true) {\n this.timeout_ = setTimeout(this.getActiveUsers_.bind(this),\n pollingInterval);\n }\n }.bind(this));\n },\n\n onChange_: function(delta) {\n var valueContainer = this.container.querySelector('b');\n if (valueContainer) valueContainer.innerHTML = this.activeUsers;\n\n this.emit('change', {activeUsers: this.activeUsers, delta: delta});\n if (delta > 0) {\n this.emit('increase', {activeUsers: this.activeUsers, delta: delta});\n }\n else {\n this.emit('decrease', {activeUsers: this.activeUsers, delta: delta});\n }\n },\n\n template:\n '
' +\n 'Active Users: ' +\n '
'\n\n });\n\n});\n"],"sourceRoot":"/source/"}