UNPKG

ns2-front-module-common

Version:
195 lines 8.03 kB
import { URLSearchParams } from '@angular/http'; import { HttpService } from './http.service'; import 'rxjs/operator/map'; import { Injectable, Inject } from '@angular/core'; var AbstractRefsService = (function () { function AbstractRefsService(httpService, cache) { this.httpService = httpService; this.cache = cache; } AbstractRefsService.prototype.setRefsEndPoint = function (_refsEndPoint) { this.refsEndPoint = _refsEndPoint; }; /** * Получить информацию об одной записе * * @param guid * @returns {Promise<T>} */ AbstractRefsService.prototype.getOne = function (guid) { var _this = this; return new Promise(function (resolve, reject) { var cacheKey = _this.cache.getCacheKey(_this.refName, guid); _this.cache.get(cacheKey).then(resolve).catch(function () { var url = _this.refsEndPoint + 'data/' + _this.refName + '/' + guid; _this.httpService.get(url) .map(function (response) { return response.json(); }) .map(function (response) { return response.data; }) .subscribe(function (data) { var doResolve = function () { resolve(data); }; _this.cache.set(cacheKey, data, 0, [data.guid, _this.refName]) .then(doResolve) .catch(doResolve); }, function (err) { reject(err); }); }); }); }; AbstractRefsService.prototype.treeUp = function (guid, params) { var _this = this; return new Promise(function (resolve, reject) { var cacheKey = _this.cache.getCacheKey('tree', _this.refName, guid, JSON.stringify(params)); _this.cache.get(cacheKey) .then(resolve) .catch(function () { var url = _this.refsEndPoint + 'tree/' + _this.refName + '/up/' + guid; var search = params || {}; _this.httpService.get(url, { search: search }) .map(function (response) { return response.json(); }) .map(function (response) { return response.data; }) .subscribe(function (data) { var doResolve = function () { resolve(data); }; _this.cache.set(cacheKey, data, 0, [data.guid, _this.refName]) .then(doResolve) .catch(doResolve); }, function (err) { reject(err); }); }); }); }; AbstractRefsService.prototype.treeDown = function (guid, params) { var _this = this; return new Promise(function (resolve, reject) { var cacheKey = _this.cache.getCacheKey('tree', _this.refName, guid, JSON.stringify(params)); _this.cache.get(cacheKey) .then(resolve) .catch(function () { var url = _this.refsEndPoint + 'tree/' + _this.refName + '/down/' + guid; var search = params || {}; _this.httpService.get(url, { search: search }) .map(function (response) { return response.json(); }) .map(function (response) { return response.data; }) .subscribe(function (data) { var doResolve = function () { resolve(data); }; _this.cache.set(cacheKey, data, 0, [data.guid, _this.refName]) .then(doResolve) .catch(doResolve); }, function (err) { reject(err); }); }); }); }; AbstractRefsService.prototype.getList = function (params) { var _this = this; return new Promise(function (resolve, reject) { var url = _this.refsEndPoint + 'data/' + _this.refName; var keys = Object.keys(params); var _filter = new URLSearchParams(); for (var i = 0; i < keys.length; i++) { var key = keys[i]; _filter.set(key, params[key]); } var cacheKey = _this.cache.getCacheKey(_this.refName, _filter.toString()); _this.cache.get(cacheKey) .then(resolve) .catch(function () { _this.httpService .get(url, { search: _filter }) .map(function (response) { return response.json(); }) .map(function (response) { return response.data; }) .subscribe(function (data) { var doResolve = function () { resolve(data); }; var tags = [_this.refName]; var items = data ? data.items || [] : []; items.map(function (v) { if (v && v.guid) { tags.push(v.guid); } }); _this.cache.set(cacheKey, data, 0, tags) .then(doResolve) .catch(doResolve); }, function (err) { reject(err); }); }); }); }; /** * Загружает весь справочник * @param params * @param sort * @returns {Promise<T>} */ AbstractRefsService.prototype.getListAll = function (params, sort) { var _this = this; if (sort === void 0) { sort = ''; } return new Promise(function (resolve, reject) { var url = _this.refsEndPoint + 'data/' + _this.refName + '/full'; var keys = Object.keys(params); var _filter = new URLSearchParams(); for (var i = 0; i < keys.length; i++) { var key = keys[i]; _filter.set(key, params[key]); } if (sort != '') { _filter.set(AbstractRefsService.SORT_FIELD, sort); } var cacheKey = _this.cache.getCacheKey(_this.refName, _filter.toString()); _this.cache.get(cacheKey) .then(resolve) .catch(function () { _this.httpService .get(url, { search: _filter }) .map(function (response) { return response.json(); }) .map(function (response) { return response.data; }) .subscribe(function (data) { var doResolve = function () { resolve(data); }; var tags = [_this.refName]; data.map(function (v) { if (v && v.guid) { tags.push(v.guid); } }); _this.cache.set(cacheKey, data, 0, tags) .then(doResolve) .catch(doResolve); }, function (err) { reject(err); }); }); }); }; return AbstractRefsService; }()); export { AbstractRefsService }; AbstractRefsService.MAX_ITEMS = 1000; AbstractRefsService.PER_PAGE_FIELD = 'per-page'; AbstractRefsService.PAGE_FIELD = 'page'; AbstractRefsService.SORT_FIELD = 'sort'; AbstractRefsService.decorators = [ { type: Injectable }, ]; /** @nocollapse */ AbstractRefsService.ctorParameters = function () { return [ { type: HttpService, }, { type: undefined, decorators: [{ type: Inject, args: ['StorageInterface',] },] }, ]; }; //# sourceMappingURL=abstract-refs.service.js.map