{"version":3,"sources":["node_modules/browserify/node_modules/browser-pack/_prelude.js","src/javascript/embed-api/components/view-selector2","view-selector2.js","node_modules/javascript-api-utils/lib/account-summaries/account-summaries.js","node_modules/javascript-api-utils/lib/account-summaries/index.js"],"names":["e","t","n","r","s","o","u","a","require","i","f","Error","code","l","exports","call","length",1,"accountSummaries","gapi","analytics","ready","updateSelect","select","options","id","innerHTML","map","option","selected","name","join","getIdProp","opts","ids","viewId","prop","value","replace","propertyId","accountId","createComponent","execute","this","setup_","updateAccounts_","changed_","render_","onChange_","bind","set","container","prevOpts","get","Component","prototype","cb","onAuthorize","then","summaries","self","accounts","all","err","emit","auth","isAuthorized","on","view","account","property","getProfile","getAccountByProfileId","getWebPropertyByProfileId","getWebProperty","getAccountByWebPropertyId","views","getAccount","properties","slice","document","getElementById","template","selects","querySelectorAll","onchange","onUserSelect_","props","data","javascript-api-utils/lib/account-summaries",2,"module","AccountSummaries","accounts_","webProperties_","profiles_","accountsById_","webPropertiesById_","propertiesById_","profilesById_","viewsById_","webProperties","alias","webProperty","j","push","parent","profiles","profile","k","grandParent","object","referenceProp","aliasName","Object","defineProperty","allWebProperties","allProfiles","obj","webPropertyId","profileId",3,"requestAccountSummaries","promise","client","management","list","resp","constructor","resolve","reject","fn","result","items","concat","startIndex","itemsPerPage","totalResults","start-index","cache","noCache","./account-summaries"],"mappings":"CAAA,QAAAA,GAAAC,EAAAC,EAAAC,GAAA,QAAAC,GAAAC,EAAAC,GAAA,IAAAJ,EAAAG,GAAA,CAAA,IAAAJ,EAAAI,GAAA,CAAA,GAAAE,GAAA,kBAAAC,UAAAA,OAAA,KAAAF,GAAAC,EAAA,MAAAA,GAAAF,GAAA,EAAA,IAAAI,EAAA,MAAAA,GAAAJ,GAAA,EAAA,IAAAK,GAAA,GAAAC,OAAA,uBAAAN,EAAA,IAAA,MAAAK,GAAAE,KAAA,mBAAAF,EAAA,GAAAG,GAAAX,EAAAG,IAAAS,WAAAb,GAAAI,GAAA,GAAAU,KAAA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AAwD,cAAAqB,GAAAV,aAIAR,EAAAV,EAAArC,UAAA,wBACA,sBASAqC,EAAArC,UAAAkB,0BAAA,SAAA+C,GACA,MAAA7E,MAAAwD,cAAAqB,IACA7E,KAAAwD,cAAAqB,GAAAd,QAGAJ,EAAAV,EAAArC,UAAA,4BACA,uBAQAqC,EAAArC,UAAAoB,0BAAA,SAAA4C,GACA,MAAA5E,MAAAsD,mBAAAsB,IACA5E,KAAAsD,mBAAAsB,GAAAb,QAGAJ,EAAAV,EAAArC,UAAA,4BACA,0BAwBAoC,EAAA7E,QAAA8E,OD4UM6B,GAAG,SAASjH,EAAQmF,GE9iB1B,QAAA+B,KAEA,GAAAC,GAAAxG,KAAAyG,OAAAxG,UAAAyG,WAAA3G,iBAAA4G,OAIApE,KAAA,SAAAqE,GAAA,MAAAA,IAEA,OAAA,IAAAJ,GAAAK,YAAA,SAAAC,EAAAC,GAIA,GAAAvE,KAEAgE,GAAAjE,KAAA,QAAAyE,GAAAJ,GACA,GAAAK,GAAAL,EAAAK,MACAA,GAAAC,MACA1E,EAAAA,EAAA2E,OAAAF,EAAAC,OAGAH,EAAA,GAAAvH,OAAA,iGAIAyH,EAAAG,WAAAH,EAAAI,cAAAJ,EAAAK,aACAtH,KAAAyG,OAAAxG,UAAAyG,WAAA3G,iBACA4G,MAAAY,cAAAN,EAAAG,WAAAH,EAAAI,eAEA9E,KAAAyE,GAGAF,EAAA,GAAArC,GAAAjC,MAIAD,KAAA,KAAAwE,KApDA,GAOAS,GAPA/C,EAAApF,EAAA,sBA+DAmF,GAAA7E,SAUAuC,IAAA,SAAAuF,GAEA,MADAA,KAAAD,EAAA,MACAA,IAAAA,EAAAjB,SFqlBGmB,sBAAsB,SAAS","file":"view-selector2.js","sourcesContent":["(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require==\"function\"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error(\"Cannot find module '\"+o+\"'\");throw f.code=\"MODULE_NOT_FOUND\",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require==\"function\"&&require;for(var o=0;o 1) {\n\n throw new Error('You cannot specify more than one of the following ' +\n 'options: \"ids\", \"viewId\", \"accountId\", \"propertyId\"');\n }\n\n if (opts.container && this.container) {\n throw new Error('You cannot change containers once a view selector ' +\n 'has been rendered on the page.');\n }\n\n var prevOpts = this.get();\n\n if (prevOpts.ids != opts.ids ||\n prevOpts.viewId != opts.viewId ||\n prevOpts.propertyId != opts.propertyId ||\n prevOpts.accountId != opts.accountId) {\n\n // If new ID data is being set, first unset all existing ID data.\n // This prevents the problem where you set an account ID then set a\n // view ID for a new view in a different account. Both IDs should not\n // persist or there will be problems.\n prevOpts.ids = null;\n prevOpts.viewId = null;\n prevOpts.propertyId = null;\n prevOpts.accountId = null;\n }\n\n // Call super.\n return gapi.analytics.Component.prototype.set.call(this, opts);\n },\n\n /**\n * Set up the view selector instance with values from the Management API's\n * accountSummaries.list method via the `accountSummaries` module.\n * If the user has not authorized, wait until that happens before\n * requesting the account summaries.\n * @param {Function} cb A function to be invoked once authorization has\n * succeeded and the accountSummaries have been retrieved.\n */\n setup_: function(cb) {\n var self = this;\n\n function onAuthorize() {\n accountSummaries.get().then(\n function(summaries) {\n self.summaries = summaries;\n self.accounts = self.summaries.all();\n cb();\n },\n function(err) {\n self.emit('error', err);\n }\n );\n }\n\n if (gapi.analytics.auth.isAuthorized()) {\n onAuthorize();\n }\n else {\n gapi.analytics.auth.on('success', onAuthorize);\n }\n },\n\n /**\n * Update the view selector instance properties with new account\n * information.\n * @throws {Error} If the user has set an ID value for an account they\n * don't have access to.\n */\n updateAccounts_: function() {\n\n var opts = this.get();\n var ids = getIdProp(opts);\n var view, account, property;\n\n // If there are no id props, set the defaults.\n if (!ids) {\n account = this.accounts[0];\n property = account && account.properties && account.properties[0];\n view = property && property.views && property.views[0];\n }\n else {\n switch (ids.prop) {\n case 'viewId':\n view = this.summaries.getProfile(ids.value);\n account = this.summaries.getAccountByProfileId(ids.value);\n property = this.summaries.getWebPropertyByProfileId(ids.value);\n break;\n case 'propertyId':\n property = this.summaries.getWebProperty(ids.value);\n account = this.summaries.getAccountByWebPropertyId(ids.value);\n view = property && property.views && property.views[0];\n break;\n case 'accountId':\n account = this.summaries.getAccount(ids.value);\n property = account && account.properties && account.properties[0];\n view = property && property.views && property.views[0];\n break;\n }\n }\n\n if (account || property || view) {\n\n // Only update if something has changed.\n if (account != this.account ||\n property != this.property ||\n view != this.view) {\n\n // Store what value changed.\n this.changed_ = {\n account: account && account != this.account,\n property: property && property != this.property,\n view: view && view != this.view\n };\n\n this.account = account;\n this.properties = account.properties;\n this.property = property;\n this.views = property && property.views;\n this.view = view;\n this.ids = view && 'ga:' + view.id;\n }\n }\n else {\n this.emit('error', new Error('You do not have access to ' +\n ids.prop.slice(0, -2) + ' : ' + ids.value));\n }\n },\n\n /**\n * Render the view selector based on the users accounts and the\n * pre-defined template. Also add event handlers to watch for\n * changes.\n */\n render_: function() {\n\n var opts = this.get();\n\n this.container = typeof opts.container == 'string' ?\n document.getElementById(opts.container) : opts.container;\n\n this.container.innerHTML = opts.template || this.template;\n var selects = this.container.querySelectorAll('select');\n\n var accounts = this.accounts;\n var properties = this.properties || [{name: '(Empty)', id: ''}];\n var views = this.views || [{name: '(Empty)', id: ''}];\n\n updateSelect(selects[0], accounts, this.account.id);\n updateSelect(selects[1], properties, this.property && this.property.id);\n updateSelect(selects[2], views, this.view && this.view.id);\n\n selects[0].onchange =\n this.onUserSelect_.bind(this, selects[0], 'accountId');\n selects[1].onchange =\n this.onUserSelect_.bind(this, selects[1], 'propertyId');\n selects[2].onchange =\n this.onUserSelect_.bind(this, selects[2], 'viewId');\n },\n\n /**\n * A callback that is invoked from the `execute` method whenever the ID\n * data has changed. Most of the time this change happens when the user\n * has selected a new view in the UI, but it can also happen\n * programmatically via the `set` method.\n */\n onChange_: function() {\n\n var props = {\n account: this.account,\n property: this.property,\n view: this.view,\n ids: this.view && 'ga:' + this.view.id\n };\n\n if (this.changed_) {\n if (this.changed_.account) this.emit('accountChange', props);\n if (this.changed_.property) this.emit('propertyChange', props);\n if (this.changed_.view) {\n this.emit('viewChange', props);\n this.emit('idsChange', props);\n\n // For backwards compatibility with the original ViewSelector.\n this.emit('change', props.ids);\n }\n }\n\n this.changed_ = null;\n },\n\n /**\n * The handler assigned to the `onchange` method of each of the select\n * elements. The context is bound to the view selector instance and it is\n * invoked with the element and property as its arguments.\n * @param {HTMLSelectElement} select The select element.\n * @param {string} property The property key to be set on the instance.\n */\n onUserSelect_: function(select, property) {\n var data = {};\n data[property] = select.value;\n\n this.set(data);\n this.execute();\n },\n\n /**\n * The html structure used to build the component. Developers can override\n * this by passing it to the component constructor. The only requirement\n * is that the structure contain three selects. The first will be the\n * account select, the second will be the property select, and the third\n * will be the view select. Order is important.\n */\n template:\n '
' +\n '
' +\n ' ' +\n ' ' +\n '
' +\n '
' +\n ' ' +\n ' ' +\n '
' +\n '
' +\n ' ' +\n ' ' +\n '
' +\n '
'\n });\n\n\n /**\n * Update a select with the specified options and optionally choose the\n * selected option based on the matching ID.\n * @param {HTMLSelectElement} select The select element to update.\n * @param {Array} options An Array of objects with the keys\n * `name` and `id`.\n * @param {string} [id] An optional value used to determine the selected\n * option.\n */\n function updateSelect(select, options, id) {\n select.innerHTML = options.map(function(option) {\n var selected = option.id == id ? 'selected ' : ' ';\n return '';\n }).join('');\n }\n\n\n /**\n * Given an options object containing a single key that could be either\n * \"ids\", \"viewId\", \"propertyId\", or \"accountId\", return a new object\n * specifying that key in its `prop` property. If the passed property is\n * \"ids\" convert it to \"viewId\".\n * @param {Object} opts An options object.\n * @return {Object} An object specifying what ID property and value were\n * passed.\n */\n function getIdProp(opts) {\n if (opts.ids || opts.viewId) {\n return {prop: 'viewId', value: opts.viewId ||\n (opts.ids && opts.ids.replace(/^ga:/, ''))};\n }\n else if (opts.propertyId) {\n return {prop: 'propertyId', value: opts.propertyId};\n }\n else if (opts.accountId) {\n return {prop: 'accountId', value: opts.accountId};\n }\n }\n\n});\n",null,"// Copyright 2015 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/**\n * @constuctor AccountSummaries\n *\n * Takes an array of accounts and writes the following new properties:\n * `accounts_`, `webProperties_`, `profiles_`, `accountsById_`,\n * `webPropertiesById_`, and `profilesById_`.\n * Each of the ___ById properties contains an array of objects where the\n * key is the entity ID and the value is an object containing the entity and\n * the entity's parents. For example, an object in the `profilesById_` array\n * might look like this:\n * {\n * \"1234\": {\n * self: {...},\n * parent: {...},\n * grandParent: {...}\n * }\n * }\n *\n * It also aliases the properties `webProperties` to `properties` and\n * `profiles` to `views` within the `accounts` array tree.\n\n * @param {Array} accounts A list of accounts in the format returned by the\n * management API's accountSummaries#list method.\n * @returns {AccountSummaries}\n */\nfunction AccountSummaries(accounts) {\n\n this.accounts_ = accounts;\n this.webProperties_ = [];\n this.profiles_ = [];\n\n this.accountsById_ = {};\n this.webPropertiesById_ = this.propertiesById_ = {};\n this.profilesById_ = this.viewsById_ = {};\n\n for (var i = 0, account; account = this.accounts_[i]; i++) {\n\n this.accountsById_[account.id] = {\n self: account\n };\n\n if (!account.webProperties) continue;\n\n // Add aliases.\n alias(account, 'webProperties', 'properties');\n\n for (var j = 0, webProperty; webProperty = account.webProperties[j]; j++) {\n\n this.webProperties_.push(webProperty);\n this.webPropertiesById_[webProperty.id] = {\n self: webProperty,\n parent: account\n };\n\n if (!webProperty.profiles) continue;\n\n // Add aliases.\n alias(webProperty, 'profiles', 'views');\n\n for (var k = 0, profile; profile = webProperty.profiles[k]; k++) {\n\n this.profiles_.push(profile);\n this.profilesById_[profile.id] = {\n self: profile,\n parent: webProperty,\n grandParent: account\n };\n }\n }\n }\n}\n\n\n/**\n * Return a list of all accounts this user has access to.\n * Since the accounts contain the web properties and the web properties contain\n * the profiles, this list contains everything.\n * @return {Array}\n */\nAccountSummaries.prototype.all = function() {\n return this.accounts_;\n};\n\nalias(AccountSummaries.prototype, 'all',\n 'allAccounts');\n\n\n/**\n * Return a list of all web properties this user has access to.\n * @return {Array}\n */\nAccountSummaries.prototype.allWebProperties = function() {\n return this.webProperties_;\n};\n\nalias(AccountSummaries.prototype, 'allWebProperties',\n 'allProperties');\n\n\n/**\n * Return a list of all profiles this user has access to.\n * @return {Array}\n */\nAccountSummaries.prototype.allProfiles = function() {\n return this.profiles_;\n};\n\nalias(AccountSummaries.prototype, 'allProfiles',\n 'allViews');\n\n\n/**\n * Returns an account, web property or profile given the passed ID in the\n * `idData` object. The ID data object can contain only one of the\n * following properties: \"accountId\", \"webPropertyId\", \"propertyId\",\n * \"profileId\", or \"viewId\". If more than one key is passed, an error is\n * thrown.\n *\n * @param {Object} obj An object with no more than one of the following\n * keys: \"accountId\", \"webPropertyId\", \"propertyId\", \"profileId\" or\n * \"viewId\".\n * @return {Object|undefined} The matching account, web property, or\n * profile. If none are found, undefined is returned.\n */\nAccountSummaries.prototype.get = function(obj) {\n if (!!obj.accountId +\n !!obj.webPropertyId +\n !!obj.propertyId +\n !!obj.profileId +\n !!obj.viewId > 1) {\n\n throw new Error('get() only accepts an object with a single ' +\n 'property: either \"accountId\", \"webPropertyId\", \"propertyId\", ' +\n '\"profileId\" or \"viewId\"');\n }\n return this.getProfile(obj.profileId || obj.viewId) ||\n this.getWebProperty(obj.webPropertyId || obj.propertyId) ||\n this.getAccount(obj.accountId);\n};\n\n\n/**\n * Get an account given its ID.\n * @param {string|number} accountId\n * @return {Object} The account with the given ID.\n */\nAccountSummaries.prototype.getAccount = function(accountId) {\n return this.accountsById_[accountId] &&\n this.accountsById_[accountId].self;\n};\n\n\n/**\n * Get a web property given its ID.\n * @param {string} webPropertyId\n * @return {Object} The web property with the given ID.\n */\nAccountSummaries.prototype.getWebProperty = function(webPropertyId) {\n return this.webPropertiesById_[webPropertyId] &&\n this.webPropertiesById_[webPropertyId].self;\n};\n\nalias(AccountSummaries.prototype, 'getWebProperty',\n 'getProperty');\n\n\n/**\n * Get a profile given its ID.\n * @param {string|number} profileId\n * @return {Object} The profile with the given ID.\n */\nAccountSummaries.prototype.getProfile = function(profileId) {\n return this.profilesById_[profileId] &&\n this.profilesById_[profileId].self;\n};\n\nalias(AccountSummaries.prototype, 'getProfile',\n 'getView');\n\n\n/**\n * Get an account given the ID of one of its profiles.\n * @param {string|number} profileId\n * @return {Object} The account containing this profile.\n */\nAccountSummaries.prototype.getAccountByProfileId = function(profileId) {\n return this.profilesById_[profileId] &&\n this.profilesById_[profileId].grandParent;\n};\n\n\nalias(AccountSummaries.prototype, 'getAccountByProfileId',\n 'getAccountByViewId');\n\n\n\n/**\n * Get a web property given the ID of one of its profile.\n * @param {string|number} profileId\n * @return {Object} The web property containing this profile.\n */\nAccountSummaries.prototype.getWebPropertyByProfileId = function(profileId) {\n return this.profilesById_[profileId] &&\n this.profilesById_[profileId].parent;\n};\n\nalias(AccountSummaries.prototype, 'getWebPropertyByProfileId',\n 'getPropertyByViewId');\n\n\n/**\n * Get an account given the ID of one of its web properties.\n * @param {string|number} webPropertyId\n * @return {Object} The account containing this web property.\n */\nAccountSummaries.prototype.getAccountByWebPropertyId = function(webPropertyId) {\n return this.webPropertiesById_[webPropertyId] &&\n this.webPropertiesById_[webPropertyId].parent;\n};\n\nalias(AccountSummaries.prototype, 'getAccountByWebPropertyId',\n 'getAccountByPropertyId');\n\n\n/**\n * Alias a property of an object using es5 getters. If es5 getters are not\n * supported, just add the aliased property directly to the object.\n * @param {Object} object The object for which you want to alias properties.\n * @param {string} referenceProp The reference property.\n * @param {string} aliasName The reference property's alias name.\n */\nfunction alias(object, referenceProp, aliasName) {\n if (Object.defineProperty) {\n Object.defineProperty(object, aliasName, {\n get: function() {\n return object[referenceProp];\n }\n });\n }\n else {\n object[aliasName] = object[referenceProp];\n }\n}\n\n\nmodule.exports = AccountSummaries;\n","// Copyright 2015 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\nvar AccountSummaries = require('./account-summaries');\n\n\n/**\n * Store the accountSummaries result in a promise so the API isn't\n * queried unneccesarily.\n */\nvar cache;\n\n\n/**\n * Make a request to the Management API's accountSummaries#list method.\n * If the requests returns a partial, paginated response, query again\n * until the full summaries are retrieved.\n * @return {goog.Promise} A promise that will be resolved with an\n * AccountSummaries object.\n */\nfunction requestAccountSummaries() {\n\n var promise = gapi.client.analytics.management.accountSummaries.list()\n // An extra `then` is needed here because `.list` doesn't return a\n // \"real\" promise, just a thenable. Calling `.then` gets us access\n // to the underlying goog.Promise instance and thus its constructor.\n .then(function(resp) { return resp; });\n\n return new promise.constructor(function(resolve, reject) {\n\n // Store the summaries array in the closure so multiple requests can\n // concat to it.\n var summaries = [];\n\n promise.then(function fn(resp) {\n var result = resp.result;\n if (result.items) {\n summaries = summaries.concat(result.items);\n }\n else {\n reject(new Error('You do not have any Google Analytics accounts. ' +\n 'Go to http://google.com/analytics to sign up.'));\n }\n\n if (result.startIndex + result.itemsPerPage <= result.totalResults) {\n gapi.client.analytics.management.accountSummaries\n .list({'start-index': result.startIndex + result.itemsPerPage})\n // Recursively call this function until the full results are in.\n .then(fn);\n }\n else {\n resolve(new AccountSummaries(summaries));\n }\n })\n // Reject the promise if there are any uncaught errors;\n .then(null, reject);\n });\n}\n\n\n/**\n * @module accountSummaries\n *\n * This module requires the `gapi.client.analytics` library to be installed\n * and the user to be authenticated.\n */\nmodule.exports = {\n\n /**\n * Return the `requestAccountSummaries` promise. If the promise exists,\n * return it to avoid multiple requests. If the promise does not exist,\n * initiate the request and cache the promise.\n *\n * @param {boolean} noCache When true make a request no matter what.\n * @return {Promise} A promise fulfilled with an AccountSummaries instance.\n */\n get: function(noCache) {\n if (noCache) cache = null;\n return cache || (cache = requestAccountSummaries());\n }\n};\n"],"sourceRoot":"/source/"}