@onwave/ui
Version:
onwave ui library
312 lines • 12.4 kB
JavaScript
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) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
import React, { Component } from "react";
/** URL of Channel Talk plugin. */
var PLUGIN_URL = "//cdn.channel.io/plugin/ch-plugin-web.js";
/**
* Channel Talk plugin helper component.
* - ref: https://developers.channel.io/docs/web-chplugin
*/
var RawChannelTalk = /** @class */ (function (_super) {
__extends(RawChannelTalk, _super);
function RawChannelTalk(props) {
var _this = _super.call(this, props) || this;
_this.state = {
isInit: false,
};
return _this;
}
RawChannelTalk.prototype.componentDidMount = function () {
// If plug in key not provided, return error.
if (!this.props.pluginKey) {
return;
}
// Init plugin on mount.
this.preInitPlugIn();
this.initPlugIn();
};
RawChannelTalk.prototype.componentWillUnmount = function () {
this.destroyPlugIn();
};
/**
* Make ready before plug in init.
*/
RawChannelTalk.prototype.preInitPlugIn = function () {
var ch = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
ch.c(args);
};
ch.q = [];
ch.c = function (args) {
ch.q.push(args);
};
window.ChannelIO = ch;
};
/**
* Initialize Channel Talk plugin.
*/
RawChannelTalk.prototype.initPlugIn = function () {
return __awaiter(this, void 0, void 0, function () {
var plugInSettings, err_1;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, , 4]);
// If plugin already init, skip init.
if (this.state.isInit) {
return [2 /*return*/];
}
// Wait before init script.
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, _this.props.timeout); })];
case 1:
// Wait before init script.
_a.sent();
// Inject script.
return [4 /*yield*/, scriptInjector(PLUGIN_URL)];
case 2:
// Inject script.
_a.sent();
// Register event listeners.
this.registerEventListeners();
plugInSettings = __assign({}, this.props);
// Boot up with settings.
window.ChannelIO("boot", plugInSettings);
// Set init status.
this.setState({ isInit: true });
return [3 /*break*/, 4];
case 3:
err_1 = _a.sent();
this.handlePlugInError(err_1);
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
});
};
/**
* Destory Channel Talk plugin.
*/
RawChannelTalk.prototype.destroyPlugIn = function () {
this.setState({ isInit: false });
if (typeof window.ChannelIO !== "function") {
return;
}
window.ChannelIO("hide");
window.ChannelIO("clearCallbacks");
setTimeout(function () {
// Shutdown channel plugin on unmount.
window.ChannelIO("shutdown");
}, 1000);
};
/**
* Register event listeners for Channel Talk plugin.
* - ref: https://developers.channel.io/docs/web-chplugin
*/
RawChannelTalk.prototype.registerEventListeners = function () {
var _this = this;
if (typeof window.ChannelIO !== "function") {
return;
}
// Register a callback function when boot was completed.
window.ChannelIO("onBoot", function (guest) {
if (guest) {
if (_this.props.onBoot)
_this.props.onBoot(guest);
}
else {
_this.handlePlugInError(new Error("ERR_BOOT_FAILED"));
}
});
// Register a callback function when the chat list is shown.
window.ChannelIO("onShow", function () {
if (_this.props.onShow)
_this.props.onShow();
});
// Register a callback function when the chat list is hidden.
window.ChannelIO("onHide", function () {
if (_this.props.onHide)
_this.props.onHide();
});
// Register a callback when `unreadCount` is changed.
window.ChannelIO("onChangeBadge", function (unreadCount) {
if (_this.props.onChangeBadge)
_this.props.onChangeBadge(unreadCount);
});
// Register a callback when a user success to create a chat.
window.ChannelIO("onCreateChat", function () {
if (_this.props.onCreateChat)
_this.props.onCreateChat();
});
// Register a callback when a user success to change their profile in the settings page and chats.
// `profile` is an object of the user's profile.
window.ChannelIO("onChangeProfile", function (profile) {
if (_this.props.onChangeProfile)
_this.props.onChangeProfile(profile);
});
// Register a callback function when the chat list is hidden.
window.ChannelIO("onClickRedirect", function (url) {
if (_this.props.onClickRedirect)
_this.props.onClickRedirect(url);
});
};
/**
* Handle error of plug in.
*/
RawChannelTalk.prototype.handlePlugInError = function (err) {
if (this.props.onError)
this.props.onError(err);
this.destroyPlugIn();
};
RawChannelTalk.prototype.render = function () {
return null;
};
RawChannelTalk.defaultProps = {
pluginKey: "",
locale: "en",
timeout: 1000,
};
/**
* Open Channel Talk messenger.
* - ref: https://developers.channel.io/docs/web-chplugin#section-show
*/
RawChannelTalk.show = function () {
if (typeof window.ChannelIO === "function") {
window.ChannelIO("show");
}
};
/**
* Open chatroom directly at Channel Talk messenger.
* - ref: https://developers.channel.io/docs/web-chplugin#section-open-chat
*/
RawChannelTalk.openChat = function (chatId) {
if (typeof window.ChannelIO === "function") {
window.ChannelIO("openChat", chatId);
}
};
/**
* Go to the lounge view.
* - ref: https://developers.channel.io/docs/web-chplugin#section-lounge
*/
RawChannelTalk.lounge = function () {
if (typeof window.ChannelIO === "function") {
window.ChannelIO("lounge");
}
};
/**
* Close Channel Talk messenger.
* - ref: https://developers.channel.io/docs/web-chplugin#section-hide
*/
RawChannelTalk.hide = function () {
if (typeof window.ChannelIO === "function") {
window.ChannelIO("hide");
}
};
/**
* Track an event for Channel Talk.
* - ref: https://developers.channel.io/docs/web-chplugin#section-track
*/
RawChannelTalk.track = function (eventName, eventProperty) {
if (typeof window.ChannelIO === "function") {
window.ChannelIO("track", eventName, eventProperty);
}
};
/**
* Clear all registered callbacks of Channel Talk.
* - ref: https://developers.channel.io/docs/web-chplugin#section-clear-callbacks
*/
RawChannelTalk.clearCallbacks = function () {
if (typeof window.ChannelIO === "function") {
window.ChannelIO("clearCallbacks");
}
};
return RawChannelTalk;
}(Component));
function scriptInjector(scriptUrl) {
return new Promise(function (resolve, reject) {
var ele = document.createElement("script");
// Set attributes for script.
ele.src = scriptUrl;
ele.async = true;
ele.defer = true;
// Set attributes for event listeners.
ele.onload = function () {
resolve();
ele.onload = null;
ele.onerror = null;
};
ele.onerror = function () {
reject("ERR_SCRIPT_INJECTION_FAILED");
ele.onload = null;
ele.onerror = null;
};
// Append to head.
document.head.appendChild(ele);
});
}
export var ChannelTalk = function (_a) {
var pluginKey = _a.pluginKey;
return React.createElement(RawChannelTalk, { pluginKey: pluginKey, locale: "ko" });
};
//# sourceMappingURL=ChannelTalk.js.map