@gravatar-com/quick-editor
Version:
A lightweight library to provide seamless Gravatar profile management for third-party sites
191 lines (184 loc) • 7.07 kB
JavaScript
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ // The require scope
/******/ var __webpack_require__ = {};
/******/
/************************************************************************/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// ESM COMPAT FLAG
__webpack_require__.r(__webpack_exports__);
// EXPORTS
__webpack_require__.d(__webpack_exports__, {
GravatarQuickEditor: () => (/* reexport */ GravatarQuickEditor),
GravatarQuickEditorCore: () => (/* reexport */ GravatarQuickEditorCore)
});
;// CONCATENATED MODULE: ./src/quick-editor-core.ts
var ScopeList = ['about', 'avatars', 'verified-accounts', 'links', 'interests', 'contact-info', 'wallet', 'photos', 'design', 'privacy'];
var WINDOW_CHECK_INTERVAL = 500;
var GravatarQuickEditorCore = /*#__PURE__*/function () {
function GravatarQuickEditorCore(_ref) {
var _this = this;
var _email = _ref.email,
_ref$scope = _ref.scope,
_scope = _ref$scope === void 0 ? [] : _ref$scope,
locale = _ref.locale,
onProfileUpdated = _ref.onProfileUpdated,
onOpened = _ref.onOpened,
onClosed = _ref.onClosed,
_utm = _ref.utm;
this._window = null;
this.open = function (email) {
email = email || _this._email;
if (!email) {
// eslint-disable-next-line
console.error('Gravatar Quick Editor: Email not provided');
return false;
}
email = encodeURIComponent(email);
var scope = encodeURIComponent(_this._scope.join(','));
var width = 400;
var height = 720;
var left = window.screenLeft + (window.outerWidth - width) / 2;
var top = window.screenTop + (window.outerHeight - height) / 2;
var options = "popup,width=" + width + ",height=" + height + ",top=" + top + ",left=" + left;
var host = _this._locale ? "https://" + _this._locale + ".gravatar.com" : 'https://gravatar.com';
var utm = _this._utm ? "&utm=" + encodeURIComponent(_this._utm) : '';
var url = host + "/profile?email=" + email + "&scope=" + scope + "&is_quick_editor=true" + utm;
_this._window = window.open(url, _this._name, options);
if (_this._window === null) {
// eslint-disable-next-line
console.error('Gravatar Quick Editor: Could not open window');
return false;
}
if (_this._onOpened) {
_this._onOpened();
}
if (_this._onClosed) {
var timer = setInterval(function () {
if (_this._window.closed) {
clearInterval(timer);
_this._onClosed();
}
}, WINDOW_CHECK_INTERVAL);
}
return true;
};
this.close = function () {
if (_this._window) {
_this._window.close();
}
};
this.isOpen = function () {
return _this._window !== null && !_this._window.closed;
};
this._name = this._getName();
this._email = _email;
this._scope = _scope;
this._locale = locale;
this._onProfileUpdated = onProfileUpdated;
this._onOpened = onOpened;
this._onClosed = onClosed;
this._utm = _utm;
if (!this._scope.every(function (s) {
return ScopeList.includes(s);
})) {
// eslint-disable-next-line
console.error('Gravatar Quick Editor: Invalid scope definition. Available scope: ' + ScopeList.join(', '));
this._scope = this._scope.filter(function (s) {
return ScopeList.includes(s);
});
}
window.addEventListener('message', this._onMessage.bind(this));
}
var _proto = GravatarQuickEditorCore.prototype;
_proto._getName = function _getName() {
return "GravatarQuickEditor_" + new Date().getTime() + Math.floor(Math.random() * (9999 - 1000) + 1000);
};
_proto._onMessage = function _onMessage(event) {
var _event$data;
if (!this._onProfileUpdated || !event.origin.match(/https:\/\/([a-z\-]{2,5}\.)?gravatar.com/)) {
return;
}
if (((_event$data = event.data) == null ? void 0 : _event$data.name) !== this._name) {
return;
}
this._onProfileUpdated(event.data.type);
};
return GravatarQuickEditorCore;
}();
;// CONCATENATED MODULE: ./src/quick-editor.ts
var GravatarQuickEditor = /*#__PURE__*/function () {
function GravatarQuickEditor(_ref) {
var email = _ref.email,
editorTriggerSelector = _ref.editorTriggerSelector,
avatarSelector = _ref.avatarSelector,
scope = _ref.scope,
locale = _ref.locale,
avatarRefreshDelay = _ref.avatarRefreshDelay;
this._avatarList = document.querySelectorAll(avatarSelector);
this._avatarRefreshDelay = avatarRefreshDelay || 1000;
var editorTrigger = document.querySelector(editorTriggerSelector);
var quickEditor = new GravatarQuickEditorCore({
email: email,
scope: scope,
locale: locale,
onProfileUpdated: this._onProfileUpdated.bind(this)
});
editorTrigger == null || editorTrigger.addEventListener('click', function () {
return quickEditor.open();
});
}
var _proto = GravatarQuickEditor.prototype;
_proto._onProfileUpdated = function _onProfileUpdated(type) {
var _this = this;
if (type !== 'avatar_updated' || this._avatarList.length === 0) {
return;
}
this._avatarList.forEach(function (avatarElement) {
if (!URL.canParse(avatarElement.src) || !avatarElement.src.includes('gravatar.com/avatar')) {
return;
}
var avatarURL = new URL(avatarElement.src);
avatarURL.searchParams.set('t', new Date().getTime().toString());
//To give it some time for the cache to be cleaned
setTimeout(function () {
avatarElement.src = avatarURL.toString();
}, _this._avatarRefreshDelay);
});
};
return GravatarQuickEditor;
}();
;// CONCATENATED MODULE: ./src/index.ts
module.exports = __webpack_exports__;
/******/ })()
;
//# sourceMappingURL=index.js.map