UNPKG

ngx-address

Version:

A simple address picker in angular.

256 lines 9 kB
import { Observable } from 'rxjs/Observable'; import 'rxjs/add/observable/forkJoin'; import { DataType } from "../index"; var AddressService = (function () { function AddressService() { this.inited = false; this.opened = false; this.hd = []; this.bd = []; this.max = 0; } /** * 初始化数据 */ /** * 初始化数据 */ AddressService.prototype.initData = /** * 初始化数据 */ function (options) { var _this = this; this.inited = false; this.title = ''; this.result = { id: '', name: '', paths: [] }; this.options = Object.assign({ separator: '/', placeholder: '请选择省市区', types: ['省份', '城市', '县区'], offset: { x: 0, y: 25 }, jumps: [] }, options); if (!this.options.data && !this.options.http) throw new Error('数据源 `data` 或 `http` 至少必须指定一项'); this.hd = []; this.bd = []; this.options.types.forEach(function (value, index) { _this.hd.push({ name: value, index: index, selected: index === 0 }); _this.bd.push({ index: index, selected: index === 0, data: {} }); }); this.max = this.options.types.length; this.load(0, this.result.id).subscribe(function (res) { _this.inited = true; }); return this.options; }; /** * 设置选项卡选中状态 */ /** * 设置选项卡选中状态 */ AddressService.prototype.setHead = /** * 设置选项卡选中状态 */ function (index) { this.hd.forEach(function (i) { i.selected = index === i.index; }); this.bd.forEach(function (i) { i.selected = index === i.index; }); }; AddressService.prototype.findItem = function (index, id) { var items = this.bd[index].data.items, res; for (var key in items) { res = items[key].find(function (i) { return id === i.id; }); if (res) break; } return res; }; AddressService.prototype.parseAddress = function (data) { // xx xx xx 不含街道 var notStree = data.length === 6; // xx xx xx xxx 含街道 var hasStree = data.length === 9; if (!notStree && !hasStree) return []; var res; if (notStree) { return Array(3).fill('').map(function (v, index) { return (data.substr(0, (index + 1) * 2) + '000000').substr(0, 6); }); } else if (hasStree) { return Array(4).fill('').map(function (v, index) { return index > 2 ? data : (data.substr(0, (index + 1) * 2) + '000000').substr(0, 6); }); } return []; }; AddressService.prototype.putResult = function (bdIndex, id, item, isClear) { if (isClear === void 0) { isClear = true; } var items = this.bd[bdIndex].data.items; for (var key in items) { items[key].forEach(function (i) { i.selected = id === i.id; }); } item = Object.assign({}, item); this.result.id = id; this.result.name = item.name; this.result.paths = this.result.paths.slice(0, bdIndex); delete item['selected']; this.result.paths.push(item); this.title = this.result.paths.map(function (i) { return i.name; }).join("<i class=\"separator\">" + this.options.separator + "</i>"); if (isClear) { this.bd.forEach(function (i) { if (i.index > bdIndex) i.data = {}; }); } }; /** * 设置地址 * * @param {any} 字符串数组,数组数量必须与 {options.types.length} 一致 */ /** * 设置地址 * * @param {any} 字符串数组,数组数量必须与 {options.types.length} 一致 */ AddressService.prototype.setAddress = /** * 设置地址 * * @param {any} 字符串数组,数组数量必须与 {options.types.length} 一致 */ function (data) { var _this = this; if (!data) return; var that = this, addresss; if (typeof data === 'string') { addresss = that.parseAddress(data.toString()); } else { addresss = data instanceof Array ? data : []; } var max = that.options.types.length; var load$ = []; addresss.forEach(function (value, index) { if (index >= max) return; load$.push(that.load(index, index > 0 ? addresss[index - 1] : '', value)); }); // 检查下一个面板是否存在,存在还需要加载数据 var isLimit = max > addresss.length; if (isLimit) { load$.push(that.load(addresss.length, addresss[addresss.length - 1])); } Observable.forkJoin.apply(Observable, load$).subscribe(function (ls) { for (var _i = 0, ls_1 = ls; _i < ls_1.length; _i++) { var item = ls_1[_i]; _this.putResult(item.index, item.value, _this.findItem(item.index, item.value), false); } if (isLimit) _this.setHead(addresss.length); }); }; AddressService.prototype.onSelected = function (bdIndex, id, item) { var that = this; that.putResult(bdIndex, id, item); // 自动关闭 ++bdIndex; if (bdIndex >= that.max) { that.opened = false; } else { that.setHead(bdIndex); that.load(bdIndex, that.result.id).subscribe(function (res) { if (bdIndex < that.max) that.jump(bdIndex); }); } }; AddressService.prototype.load = function (index, id, defaultValue) { var _this = this; return new Observable(function (obs) { var fn = _this.options.data || _this.options.http; fn(index, id).subscribe(function (res) { var returnValue = { index: index, value: defaultValue }; if (res) { _this._renderData(index, res, defaultValue); obs.next(returnValue); obs.complete(); return; } // 当只有HTTP请求时不再处理 if (_this.options.data && _this.options.http) { _this.options.http(index, id).subscribe(function (httpRes) { _this._renderData(index, httpRes, defaultValue); obs.next(returnValue); obs.complete(); }, function (err) { obs.error(err); obs.complete(); }); } }, function (err) { obs.error(err); obs.complete(); }); }); }; AddressService.prototype._renderData = function (index, res, defaultValue) { if (!res) res = { type: DataType.list, list: [], group: {} }; var items = res.type === DataType.list ? { '': res.list } : Object.assign({}, res.group); var tips = []; var count = 0; for (var key in items) { items[key].forEach(function (i) { ++count; i.selected = defaultValue === i.id; if (i.tips === true) tips.push(i); }); items[key] = items[key].filter(function (i) { return i.tips !== true; }); } this.bd[index].data = { type: res.type, items: items, tips: tips || [] }; if (count === 0) { this.opened = false; } }; AddressService.prototype.jump = function (index) { var _this = this; // jumps var items = this.bd[index].data.items; for (var key in items) { var jumpItem = items[key].find(function (i) { return ~_this.options.jumps.indexOf(i.id); }); if (jumpItem) { this.onSelected(index, jumpItem.id, jumpItem); } } }; return AddressService; }()); export { AddressService }; //# sourceMappingURL=address.service.js.map