UNPKG

ngx-zalo

Version:

Use Zalo SDK with Angular5~ (https://developers.zalo.me/docs/sdk/javascript-sdk/tai-lieu)

235 lines (232 loc) 8.12 kB
import { Injectable, Directive, HostListener, EventEmitter, Output, NgModule } from '@angular/core'; import { Observable } from 'rxjs/Observable'; import { CommonModule } from '@angular/common'; var NgxZaloService = /** @class */ (function () { function NgxZaloService() { this.SCRIPT_URL = 'https://zjs.zdn.vn/zalo/sdk.js'; this.STATUS_NOT_INIT = 0; this.STATUS_INITIALIZING = 1; this.STATUS_INIT_FAILURE = 2; this.STATUS_INIT_SUCCESSFULLY = 3; this._initStatus = this.STATUS_NOT_INIT; this._isLogin = false; } Object.defineProperty(NgxZaloService.prototype, "isLogin", { get: function () { return this._isLogin; }, enumerable: true, configurable: true }); Object.defineProperty(NgxZaloService.prototype, "accessToken", { get: function () { return this.isInitSuccessfully ? Zalo.getAccessToken() : null; }, enumerable: true, configurable: true }); NgxZaloService.prototype.init = function (configs) { var _this = this; this._initStatus = this.STATUS_INITIALIZING; this.initZaloScript(function () { Zalo.init(configs); _this.getLoginStatus(function () { _this._initStatus = _this.STATUS_INIT_SUCCESSFULLY; }); }, function () { _this._initStatus = _this.STATUS_INIT_FAILURE; }); }; NgxZaloService.prototype.login = function () { if (this.isInitSuccessfully) { Zalo.login(); } }; NgxZaloService.prototype.logout = function () { var _this = this; return new Observable(function (observer) { if (_this.isInitSuccessfully) { Zalo.logout(); } _this._isLogin = false; _this.breakObservable(observer); }); }; NgxZaloService.prototype.updateLoginInfo = function () { var _this = this; return new Observable(function (observer) { if (_this.isNotInit) { _this._isLogin = false; _this.breakObservable(observer, false); return; } _this.pendingInit().subscribe(function () { Zalo.getLoginStatus(function (response) { _this._isLogin = response.status === 'connected'; _this.breakObservable(observer, _this._isLogin); }); }); }); }; NgxZaloService.prototype.getMyProfile = function (fields) { var _this = this; if (fields === void 0) { fields = 'id, name, birthday, picture, gender'; } return new Observable(function (observer) { if (!_this._initStatus) { _this.breakObservable(observer); return; } _this.pendingInit().subscribe(function () { Zalo.api('/me', 'GET', { fields: fields }, function (myProfile) { _this.breakObservable(observer, myProfile); }); }); }); }; Object.defineProperty(NgxZaloService.prototype, "isNotInit", { get: function () { return this.STATUS_NOT_INIT === this._initStatus; }, enumerable: true, configurable: true }); Object.defineProperty(NgxZaloService.prototype, "isInitializing", { get: function () { return this.STATUS_INITIALIZING === this._initStatus; }, enumerable: true, configurable: true }); Object.defineProperty(NgxZaloService.prototype, "isInitSuccessfully", { get: function () { return this.STATUS_INIT_SUCCESSFULLY === this._initStatus; }, enumerable: true, configurable: true }); NgxZaloService.prototype.pendingInit = function () { var _this = this; return new Observable(function (observer) { if (!_this.isInitializing) { _this.breakObservable(observer); return; } var timeInterval = setInterval(function () { if (!_this.isInitializing) { clearInterval(timeInterval); _this.breakObservable(observer); } }, 500); }); }; NgxZaloService.prototype.initZaloScript = function (successCallback, errorCallback) { if ('undefined' === typeof window) { return; } var script = document.createElement('script'); script.src = this.SCRIPT_URL; script.onload = function () { if (successCallback) { successCallback(); } }; script.onerror = function () { if (errorCallback) { errorCallback(); } }; document.head.appendChild(script); }; NgxZaloService.prototype.getLoginStatus = function (callback) { var _this = this; Zalo.getLoginStatus(function (response) { if (response.status === 'connected') { _this._isLogin = true; } if (callback) { callback(response); } }); }; NgxZaloService.prototype.breakObservable = function (observer, data) { if (data === void 0) { data = null; } observer.next(data); observer.complete(); }; return NgxZaloService; }()); NgxZaloService.decorators = [ { type: Injectable }, ]; NgxZaloService.ctorParameters = function () { return []; }; var LoginZaloDirective = /** @class */ (function () { function LoginZaloDirective(_ngxZaloService) { this._ngxZaloService = _ngxZaloService; } LoginZaloDirective.prototype.onClick = function () { this._ngxZaloService.login(); }; return LoginZaloDirective; }()); LoginZaloDirective.decorators = [ { type: Directive, args: [{ selector: '[ngxLoginZalo]' },] }, ]; LoginZaloDirective.ctorParameters = function () { return [ { type: NgxZaloService, }, ]; }; LoginZaloDirective.propDecorators = { "onClick": [{ type: HostListener, args: ['click',] },], }; var LogoutZaloDirective = /** @class */ (function () { function LogoutZaloDirective(_ngxZaloService) { this._ngxZaloService = _ngxZaloService; this.successEvent = new EventEmitter(); } LogoutZaloDirective.prototype.onClick = function () { var _this = this; this._ngxZaloService.logout().subscribe(function () { _this.successEvent.emit(); }); }; return LogoutZaloDirective; }()); LogoutZaloDirective.decorators = [ { type: Directive, args: [{ selector: '[ngxLogoutZalo]' },] }, ]; LogoutZaloDirective.ctorParameters = function () { return [ { type: NgxZaloService, }, ]; }; LogoutZaloDirective.propDecorators = { "successEvent": [{ type: Output },], "onClick": [{ type: HostListener, args: ['click',] },], }; var NgxZaloModule = /** @class */ (function () { function NgxZaloModule() { } return NgxZaloModule; }()); NgxZaloModule.decorators = [ { type: NgModule, args: [{ imports: [ CommonModule, ], providers: [ NgxZaloService, ], declarations: [ LoginZaloDirective, LogoutZaloDirective, ], exports: [ LoginZaloDirective, LogoutZaloDirective, ] },] }, ]; NgxZaloModule.ctorParameters = function () { return []; }; export { NgxZaloModule, NgxZaloService, LoginZaloDirective as ɵa, LogoutZaloDirective as ɵb }; //# sourceMappingURL=ngx-zalo.js.map