Files
gerrit/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js
Viktar Donich e8680ea43a Defer metrics reporting until plugins are loaded
Cache all events reported via gr-reporting instances in closure in order
to share the cahce. Flush the cache on next reporting after plugins are
loaded.

This allows plugins to receive all reporting events and unlocks Google
Analytics integration.

Change-Id: Ic0c1d37667208bfd55bbf77e2138d0170f7fb097
2016-10-06 14:59:08 -07:00

126 lines
3.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
(function(window) {
'use strict';
var API_VERSION = '0.1';
// GWT JSNI uses $wnd to refer to window.
// http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html
window.$wnd = window;
function Plugin(opt_url) {
if (!opt_url) {
console.warn('Plugin not being loaded from /plugins base path.',
'Unable to determine name.');
return;
}
this._url = new URL(opt_url);
if (this._url.pathname.indexOf('/plugins') !== 0) {
console.warn('Plugin not being loaded from /plugins base path:',
this._url.href, '— Unable to determine name.');
return;
}
this._name = this._url.pathname.split('/')[2];
}
Plugin._sharedAPIElement = document.createElement('gr-js-api-interface');
Plugin.prototype._name = '';
Plugin.prototype.getPluginName = function() {
return this._name;
};
Plugin.prototype.on = function(eventName, callback) {
Plugin._sharedAPIElement.addEventCallback(eventName, callback);
};
Plugin.prototype.url = function(opt_path) {
return this._url.origin + '/plugins/' + this._name + (opt_path || '/');
};
Plugin.prototype.changeActions = function() {
return new GrChangeActionsInterface(Plugin._sharedAPIElement.getElement(
Plugin._sharedAPIElement.Element.CHANGE_ACTIONS));
};
Plugin.prototype.changeReply = function() {
return new GrChangeReplyInterface(Plugin._sharedAPIElement.getElement(
Plugin._sharedAPIElement.Element.REPLY_DIALOG));
};
var Gerrit = window.Gerrit || {};
// Number of plugins to initialize, -1 means 'not yet known'.
Gerrit._pluginsPending = -1;
Gerrit.getPluginName = function() {
console.warn('Gerrit.getPluginName is not supported in PolyGerrit.',
'Please use self.getPluginName() instead.');
};
Gerrit.css = function(rulesStr) {
if (!Gerrit._customStyleSheet) {
var styleEl = document.createElement('style');
document.head.appendChild(styleEl);
Gerrit._customStyleSheet = styleEl.sheet;
}
var name = '__pg_js_api_class_' + Gerrit._customStyleSheet.cssRules.length;
Gerrit._customStyleSheet.insertRule('.' + name + '{' + rulesStr + '}', 0);
return name;
};
Gerrit.install = function(callback, opt_version, opt_src) {
if (opt_version && opt_version !== API_VERSION) {
console.warn('Only version ' + API_VERSION +
' is supported in PolyGerrit. ' + opt_version + ' was given.');
Gerrit._pluginInstalled();
return;
}
// TODO(andybons): Polyfill currentScript for IE10/11 (edge supports it).
var src = opt_src || (document.currentScript && document.currentScript.src);
callback(new Plugin(src));
Gerrit._pluginInstalled();
};
Gerrit.getLoggedIn = function() {
return document.createElement('gr-rest-api-interface').getLoggedIn();
};
Gerrit.installGwt = function() {
// NOOP since PolyGerrit doesnt support GWT plugins.
};
Gerrit._setPluginsCount = function(count) {
Gerrit._pluginsPending = count;
if (Gerrit._arePluginsLoaded()) {
document.createElement('gr-reporting').pluginsLoaded();
}
};
Gerrit._pluginInstalled = function() {
Gerrit._setPluginsCount(Gerrit._pluginsPending - 1);
};
Gerrit._arePluginsLoaded = function() {
return Gerrit._pluginsPending === 0;
};
window.Gerrit = Gerrit;
})(window);