UNPKG

@mornya/react-social-libs

Version:

The project of React.js Social Share and Widget modules.

144 lines (143 loc) 5.88 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; import React from 'react'; var BaseComponent = (function (_super) { __extends(BaseComponent, _super); function BaseComponent() { return _super !== null && _super.apply(this, arguments) || this; } BaseComponent.prototype.mapToParam = function (prefix, params) { if (prefix === void 0) { prefix = ''; } var hasSeparators = decodeURIComponent(prefix).includes('?'); var hasPrepend = !hasSeparators || (hasSeparators && !prefix.endsWith('?')); if (params) { return Object.entries(params).reduce(function (acc, _a) { var _b = __read(_a, 2), key = _b[0], value = _b[1]; var param = acc; if (value) { var encodedValue = void 0; if (Array.isArray(value)) { encodedValue = value.map(function (item) { return encodeURIComponent(item); }).join(','); } else if (typeof value === 'boolean') { encodedValue = value ? 'true' : 'false'; } else { encodedValue = encodeURIComponent(String(value)); } param += (hasPrepend ? (hasSeparators ? '&' : '?') : '') + "".concat(key, "=").concat(encodedValue); hasSeparators = true; hasPrepend = true; } return param; }, prefix); } return prefix; }; BaseComponent.prototype.getUID = function () { var randomized = Math.floor((1 + Math.random()) * 0x10000); return randomized.toString(16).substring(1); }; BaseComponent.prototype.getDefaultMetaContent = function () { var metaContent = { title: '', description: '', url: '', type: '', siteName: '', image: '', keywords: '', videoDuration: '', musicDuration: '', }; document.querySelectorAll('meta[property]').forEach(function (item) { if (item) { var property = item.getAttribute('property'); var content = item.getAttribute('content'); if (property && content) { var key = property.trim(); var value = content.trim(); switch (key) { case 'og:title': metaContent.title = value; break; case 'og:description': metaContent.description = value; break; case 'og:url': metaContent.url = value; break; case 'og:type': metaContent.type = value; break; case 'og:site_name': metaContent.siteName = value; break; case 'og:image': metaContent.image = value; break; case 'video:duration': metaContent.videoDuration = value; break; case 'music:duration': metaContent.musicDuration = value; break; default: break; } } } }); var keywordsEl = document.querySelector('meta[name="keywords"]'); if (keywordsEl) { var content = keywordsEl.getAttribute('content'); if (content) { metaContent.keywords = content; } } return metaContent; }; BaseComponent.prototype.getDefaultHashtags = function (keywords) { if (keywords === void 0) { keywords = ''; } if (keywords) { return keywords.split(',').map(function (item) { return item.trim().replace(/\s/g, '_'); }); } return []; }; BaseComponent.prototype.getHashtag = function (hashtags, indent) { if (hashtags === void 0) { hashtags = []; } if (indent === void 0) { indent = ''; } return hashtags.length ? indent + hashtags.map(function (item) { return "#".concat(item.trim()); }).join(' ') : ''; }; return BaseComponent; }(React.Component)); export default BaseComponent;