ngx-zalo
Version:
Use Zalo SDK with Angular5~ (https://developers.zalo.me/docs/sdk/javascript-sdk/tai-lieu)
244 lines (239 loc) • 8.4 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('rxjs/Observable'), require('@angular/common')) :
typeof define === 'function' && define.amd ? define('ngx-zalo', ['exports', '@angular/core', 'rxjs/Observable', '@angular/common'], factory) :
(factory((global['ngx-zalo'] = {}),global.ng.core,global.Rx,global.ng.common));
}(this, (function (exports,core,Observable,common) { 'use strict';
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.Observable(function (observer) {
if (_this.isInitSuccessfully) {
Zalo.logout();
}
_this._isLogin = false;
_this.breakObservable(observer);
});
};
NgxZaloService.prototype.updateLoginInfo = function () {
var _this = this;
return new Observable.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.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.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: core.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: core.Directive, args: [{
selector: '[ngxLoginZalo]'
},] },
];
LoginZaloDirective.ctorParameters = function () { return [
{ type: NgxZaloService, },
]; };
LoginZaloDirective.propDecorators = {
"onClick": [{ type: core.HostListener, args: ['click',] },],
};
var LogoutZaloDirective = /** @class */ (function () {
function LogoutZaloDirective(_ngxZaloService) {
this._ngxZaloService = _ngxZaloService;
this.successEvent = new core.EventEmitter();
}
LogoutZaloDirective.prototype.onClick = function () {
var _this = this;
this._ngxZaloService.logout().subscribe(function () {
_this.successEvent.emit();
});
};
return LogoutZaloDirective;
}());
LogoutZaloDirective.decorators = [
{ type: core.Directive, args: [{
selector: '[ngxLogoutZalo]'
},] },
];
LogoutZaloDirective.ctorParameters = function () { return [
{ type: NgxZaloService, },
]; };
LogoutZaloDirective.propDecorators = {
"successEvent": [{ type: core.Output },],
"onClick": [{ type: core.HostListener, args: ['click',] },],
};
var NgxZaloModule = /** @class */ (function () {
function NgxZaloModule() {
}
return NgxZaloModule;
}());
NgxZaloModule.decorators = [
{ type: core.NgModule, args: [{
imports: [
common.CommonModule,
],
providers: [
NgxZaloService,
],
declarations: [
LoginZaloDirective,
LogoutZaloDirective,
],
exports: [
LoginZaloDirective,
LogoutZaloDirective,
]
},] },
];
NgxZaloModule.ctorParameters = function () { return []; };
exports.NgxZaloModule = NgxZaloModule;
exports.NgxZaloService = NgxZaloService;
exports.ɵa = LoginZaloDirective;
exports.ɵb = LogoutZaloDirective;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=ngx-zalo.umd.js.map