ngx-address
Version:
A simple address picker in angular.
703 lines (626 loc) • 36.9 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("@angular/core"), require("@angular/http"), require("@angular/common"), require("@angular/forms"), require("rxjs/Observable"), require("rxjs/add/observable/forkJoin"));
else if(typeof define === 'function' && define.amd)
define(["@angular/core", "@angular/http", "@angular/common", "@angular/forms", "rxjs/Observable", "rxjs/add/observable/forkJoin"], factory);
else if(typeof exports === 'object')
exports["ngx-address.umd"] = factory(require("@angular/core"), require("@angular/http"), require("@angular/common"), require("@angular/forms"), require("rxjs/Observable"), require("rxjs/add/observable/forkJoin"));
else
root["ngx-address.umd"] = factory(root["@angular/core"], root["@angular/http"], root["@angular/common"], root["@angular/forms"], root["rxjs/Observable"], root["rxjs/add/observable/forkJoin"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE_0__, __WEBPACK_EXTERNAL_MODULE_5__, __WEBPACK_EXTERNAL_MODULE_6__, __WEBPACK_EXTERNAL_MODULE_9__, __WEBPACK_EXTERNAL_MODULE_10__, __WEBPACK_EXTERNAL_MODULE_11__) {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "/";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 1);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_0__;
/***/ }),
/* 1 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
;
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "NgxAddressModule", function() { return NgxAddressModule; });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__angular_core__ = __webpack_require__(0);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__angular_core___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0__angular_core__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__angular_http__ = __webpack_require__(5);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__angular_http___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_1__angular_http__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__angular_common__ = __webpack_require__(6);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__angular_common___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_2__angular_common__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_3__components_off_click__ = __webpack_require__(7);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_4__components_pipes_keys_pipe__ = __webpack_require__(8);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_5__components_address_component__ = __webpack_require__(2);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_6__components_interfaces_data_type__ = __webpack_require__(12);
/* harmony namespace reexport (by provided) */ __webpack_require__.d(__webpack_exports__, "DataType", function() { return __WEBPACK_IMPORTED_MODULE_6__components_interfaces_data_type__["a"]; });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_7__components_interfaces_data_item__ = __webpack_require__(13);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_7__components_interfaces_data_item___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_7__components_interfaces_data_item__);
/* harmony namespace reexport (unknown) */ for(var __WEBPACK_IMPORT_KEY__ in __WEBPACK_IMPORTED_MODULE_7__components_interfaces_data_item__) if(["NgxAddressModule","DataType","default"].indexOf(__WEBPACK_IMPORT_KEY__) < 0) (function(key) { __webpack_require__.d(__webpack_exports__, key, function() { return __WEBPACK_IMPORTED_MODULE_7__components_interfaces_data_item__[key]; }) }(__WEBPACK_IMPORT_KEY__));
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_8__components_interfaces_data__ = __webpack_require__(14);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_8__components_interfaces_data___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_8__components_interfaces_data__);
/* harmony namespace reexport (unknown) */ for(var __WEBPACK_IMPORT_KEY__ in __WEBPACK_IMPORTED_MODULE_8__components_interfaces_data__) if(["NgxAddressModule","DataType","default"].indexOf(__WEBPACK_IMPORT_KEY__) < 0) (function(key) { __webpack_require__.d(__webpack_exports__, key, function() { return __WEBPACK_IMPORTED_MODULE_8__components_interfaces_data__[key]; }) }(__WEBPACK_IMPORT_KEY__));
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_9__components_interfaces_result__ = __webpack_require__(15);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_9__components_interfaces_result___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_9__components_interfaces_result__);
/* harmony namespace reexport (unknown) */ for(var __WEBPACK_IMPORT_KEY__ in __WEBPACK_IMPORTED_MODULE_9__components_interfaces_result__) if(["NgxAddressModule","DataType","default"].indexOf(__WEBPACK_IMPORT_KEY__) < 0) (function(key) { __webpack_require__.d(__webpack_exports__, key, function() { return __WEBPACK_IMPORTED_MODULE_9__components_interfaces_result__[key]; }) }(__WEBPACK_IMPORT_KEY__));
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_10__components_interfaces_options__ = __webpack_require__(3);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_10__components_interfaces_options___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_10__components_interfaces_options__);
/* harmony namespace reexport (unknown) */ for(var __WEBPACK_IMPORT_KEY__ in __WEBPACK_IMPORTED_MODULE_10__components_interfaces_options__) if(["NgxAddressModule","DataType","default"].indexOf(__WEBPACK_IMPORT_KEY__) < 0) (function(key) { __webpack_require__.d(__webpack_exports__, key, function() { return __WEBPACK_IMPORTED_MODULE_10__components_interfaces_options__[key]; }) }(__WEBPACK_IMPORT_KEY__));
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_11__components_interfaces_tab_item__ = __webpack_require__(16);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_11__components_interfaces_tab_item___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_11__components_interfaces_tab_item__);
/* harmony namespace reexport (unknown) */ for(var __WEBPACK_IMPORT_KEY__ in __WEBPACK_IMPORTED_MODULE_11__components_interfaces_tab_item__) if(["NgxAddressModule","DataType","default"].indexOf(__WEBPACK_IMPORT_KEY__) < 0) (function(key) { __webpack_require__.d(__webpack_exports__, key, function() { return __WEBPACK_IMPORTED_MODULE_11__components_interfaces_tab_item__[key]; }) }(__WEBPACK_IMPORT_KEY__));
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_12__components_interfaces_external_data__ = __webpack_require__(17);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_12__components_interfaces_external_data___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_12__components_interfaces_external_data__);
/* harmony namespace reexport (unknown) */ for(var __WEBPACK_IMPORT_KEY__ in __WEBPACK_IMPORTED_MODULE_12__components_interfaces_external_data__) if(["NgxAddressModule","DataType","default"].indexOf(__WEBPACK_IMPORT_KEY__) < 0) (function(key) { __webpack_require__.d(__webpack_exports__, key, function() { return __WEBPACK_IMPORTED_MODULE_12__components_interfaces_external_data__[key]; }) }(__WEBPACK_IMPORT_KEY__));
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_13__components_address_service__ = __webpack_require__(4);
/* harmony namespace reexport (by provided) */ __webpack_require__.d(__webpack_exports__, "AddressService", function() { return __WEBPACK_IMPORTED_MODULE_13__components_address_service__["a"]; });
/* harmony namespace reexport (by provided) */ __webpack_require__.d(__webpack_exports__, "AddressComponent", function() { return __WEBPACK_IMPORTED_MODULE_5__components_address_component__["a"]; });
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var NgxAddressModule = (function () {
function NgxAddressModule() {
}
NgxAddressModule = __decorate([
Object(__WEBPACK_IMPORTED_MODULE_0__angular_core__["NgModule"])({
imports: [__WEBPACK_IMPORTED_MODULE_1__angular_http__["HttpModule"], __WEBPACK_IMPORTED_MODULE_2__angular_common__["CommonModule"]],
declarations: [__WEBPACK_IMPORTED_MODULE_4__components_pipes_keys_pipe__["a" /* KeysPipe */], __WEBPACK_IMPORTED_MODULE_5__components_address_component__["a" /* AddressComponent */], __WEBPACK_IMPORTED_MODULE_3__components_off_click__["a" /* OffClickDirective */]],
exports: [__WEBPACK_IMPORTED_MODULE_5__components_address_component__["a" /* AddressComponent */]]
})
], NgxAddressModule);
return NgxAddressModule;
}());
/***/ }),
/* 2 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
;
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return AddressComponent; });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__interfaces_options__ = __webpack_require__(3);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__interfaces_options___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0__interfaces_options__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__angular_core__ = __webpack_require__(0);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__angular_core___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_1__angular_core__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__angular_forms__ = __webpack_require__(9);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__angular_forms___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_2__angular_forms__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_3__address_service__ = __webpack_require__(4);
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
/**
* 城市选择器组件
* Usage:
* <ngx-address [(ngModel)]="city.id" [options]="{...}"></ngx-address>
*/
var AddressComponent = (function () {
function AddressComponent(element) {
this.element = element;
this.onSelected = new __WEBPACK_IMPORTED_MODULE_1__angular_core__["EventEmitter"]();
this.onChange = Function.prototype;
this.onTouched = Function.prototype;
this.onClose = this.onClose.bind(this);
this._t = new __WEBPACK_IMPORTED_MODULE_3__address_service__["a" /* AddressService */]();
}
AddressComponent_1 = AddressComponent;
AddressComponent.prototype.ngOnInit = function () {
};
AddressComponent.prototype.onOpen = function () {
this._t.opened = true;
};
AddressComponent.prototype.onClose = function () {
this._t.opened = false;
};
AddressComponent.prototype.onTabChange = function (index) {
this._t.setHead(index);
};
AddressComponent.prototype.notify = function () {
this.onTouched();
this.onChange(this._t.result.id);
this.onSelected.emit(this._t.result);
};
AddressComponent.prototype.onItem = function (bdIndex, key, id, item) {
this._t.onSelected(bdIndex, id, item);
this.notify();
};
AddressComponent.prototype.ngOnChanges = function (changes) {
var _this = this;
if (changes.options) {
this.options = this._t.initData(this.options);
this.notify();
return;
}
if (changes.values) {
setTimeout(function () {
_this._t.setAddress(changes.values.currentValue);
});
}
};
AddressComponent.prototype.writeValue = function (obj) {
var _this = this;
if (obj && obj !== this._t.result.id) {
setTimeout(function () {
_this._t.setAddress(obj);
});
}
};
AddressComponent.prototype.registerOnChange = function (fn) { this.onChange = fn; };
AddressComponent.prototype.registerOnTouched = function (fn) { this.onTouched = fn; };
AddressComponent.prototype.setDisabledState = function (isDisabled) {
};
__decorate([
Object(__WEBPACK_IMPORTED_MODULE_1__angular_core__["Input"])(),
__metadata("design:type", typeof (_a = typeof __WEBPACK_IMPORTED_MODULE_0__interfaces_options__["Options"] !== "undefined" && __WEBPACK_IMPORTED_MODULE_0__interfaces_options__["Options"]) === "function" && _a || Object)
], AddressComponent.prototype, "options", void 0);
__decorate([
Object(__WEBPACK_IMPORTED_MODULE_1__angular_core__["Input"])(),
__metadata("design:type", Object)
], AddressComponent.prototype, "values", void 0);
__decorate([
Object(__WEBPACK_IMPORTED_MODULE_1__angular_core__["Output"])(),
__metadata("design:type", typeof (_b = typeof __WEBPACK_IMPORTED_MODULE_1__angular_core__["EventEmitter"] !== "undefined" && __WEBPACK_IMPORTED_MODULE_1__angular_core__["EventEmitter"]) === "function" && _b || Object)
], AddressComponent.prototype, "onSelected", void 0);
AddressComponent = AddressComponent_1 = __decorate([
Object(__WEBPACK_IMPORTED_MODULE_1__angular_core__["Component"])({
selector: 'ngx-address',
template: "<div class=\"ngx-address\" [offClick]=\"onClose\">\n <div class=\"ngx-address-title\" [ngClass]=\"{'has':_t.title.length > 0}\"\n (click)=\"onOpen()\">\n <span [innerHTML]=\"_t.title ? _t.title : options.placeholder\"></span>\n <i class=\"arrow\"></i>\n </div>\n <div class=\"ngx-address-overlay\" *ngIf=\"_t.inited\" [hidden]=\"!_t.opened\" [ngStyle]=\"{'top.px': options.offset.y,'left.px': options.offset.x}\">\n <div class=\"ngx-address-select-warp\">\n <div class=\"ngx-address-select-tab\">\n <a *ngFor=\"let item of _t.hd\" (click)=\"onTabChange(item.index)\" [ngClass]=\"{'current': item.selected}\">{{item.name}}</a>\n </div>\n <div class=\"ngx-address-select\" *ngFor=\"let bitem of _t.bd\" [hidden]=\"!bitem.selected\">\n <dl *ngFor=\"let g of bitem.data.items | keys\">\n <dt *ngIf=\"g.key\">{{g.key}}</dt>\n <dd>\n <a *ngFor=\"let i of g.value\"\n (click)=\"onItem(bitem.index, g.key, i.id, i)\"\n [ngClass]=\"{'current': i.selected}\" title=\"{{i.name}}\" data-id=\"{{i.id}}\"\n href=\"javascript:;\">{{i.name}}</a>\n </dd>\n </dl>\n <dl class=\"ngx-address-tips\" *ngIf=\"bitem.data.tips && bitem.data.tips.length > 0\">\n <dd>\n <p *ngFor=\"let ti of bitem.data.tips\" data-id=\"{{ti.id}}\">{{ti.name}}</p>\n </dd>\n </dl>\n </div>\n </div>\n </div>\n</div>",
encapsulation: __WEBPACK_IMPORTED_MODULE_1__angular_core__["ViewEncapsulation"].None,
styles: ["ngx-address { width: 100%; } .ngx-address { font-size: 12px; outline: 0; position: relative; cursor: pointer; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } .ngx-address * { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } .ngx-address-title { padding: 4px 8px; border: 1px solid #ccc; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: #ccc; } .ngx-address-title .arrow { position: absolute; top: 10px; right: 8px; width: 10px; height: 5px; background: url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAFCAIAAADzBuo/AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAD1JREFUeNpinDlzJgMDQ1paGgMqmDVrFpBkgkhAOGhyQCkmuFa4CrgckGT8//8/sigEwO1iwhRCdgdAgAEAbbUZNeMBbuoAAAAASUVORK5CYII=\") 0 0 no-repeat; } .ngx-address-title.has { color: #333; } .ngx-address-title.has .separator { color: #cfcfcf; padding: 0 4px; } .ngx-address-overlay { position: absolute; left: -9999px; top: -9999px; z-index: 999999; outline: 0; width: 100%; -webkit-tap-highlight-color: transparent; } .ngx-address-select-tab { border-bottom: 1px solid #ccc; border-right: 1px solid #ccc; border-top: 1px solid #ccc; height: 35px; display: flex; align-items: center; justify-content: space-around; background: #f0f0f0; } .ngx-address-select-tab a { display: block; height: 100%; width: 100%; color: #333; text-align: center; line-height: 35px; border-left: 1px solid #ccc; border-bottom: 1px solid transparent; text-decoration: none; outline: 0; } .ngx-address-select-tab .current { background: #fff; border-bottom: 1px solid #fff; color: #f60; } .ngx-address-select { border: 1px #ccc solid; border-top: 0; padding: 10px 15px; background: #fff; } .ngx-address-select dl { display: flex; margin: 0; padding: 3px 0; line-height: 2; } .ngx-address-select dt { width: 35px; line-height: 2; padding-right: 10px; font-weight: 700; text-align: right; } .ngx-address-select dd { margin-left: 0; flex: 1; } .ngx-address-select a { display: inline-block; line-height: 2; text-decoration: none; color: #333; padding: 0 10px; outline: 0; text-decoration: none; white-space: nowrap; } .ngx-address-select a:hover, .ngx-address-select a:focus { background-color: #fff8f3; border-radius: 2px; color: #f60; } .ngx-address-select a.current { background-color: #f60; color: #fff; border-radius: 2px; } .ngx-address-tips { border-top: 1px solid #ccc; color: #ccc; } .ngx-address-tips p { margin: 0; line-height: 2; } "],
providers: [{
provide: __WEBPACK_IMPORTED_MODULE_2__angular_forms__["NG_VALUE_ACCESSOR"],
useExisting: Object(__WEBPACK_IMPORTED_MODULE_1__angular_core__["forwardRef"])(function () { return AddressComponent_1; }),
multi: true
}],
host: {
'[attr.class]': 'options.className'
}
}),
__param(0, Object(__WEBPACK_IMPORTED_MODULE_1__angular_core__["Inject"])(__WEBPACK_IMPORTED_MODULE_1__angular_core__["ElementRef"])),
__metadata("design:paramtypes", [typeof (_c = typeof __WEBPACK_IMPORTED_MODULE_1__angular_core__["ElementRef"] !== "undefined" && __WEBPACK_IMPORTED_MODULE_1__angular_core__["ElementRef"]) === "function" && _c || Object])
], AddressComponent);
return AddressComponent;
var AddressComponent_1, _a, _b, _c;
}());
/***/ }),
/* 3 */
/***/ (function(module, exports) {
/***/ }),
/* 4 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
;
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return AddressService; });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_rxjs_Observable__ = __webpack_require__(10);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_rxjs_Observable___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0_rxjs_Observable__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1_rxjs_add_observable_forkJoin__ = __webpack_require__(11);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1_rxjs_add_observable_forkJoin___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_1_rxjs_add_observable_forkJoin__);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__index__ = __webpack_require__(1);
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} 一致
*/
AddressService.prototype.setAddress = 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]));
}
__WEBPACK_IMPORTED_MODULE_0_rxjs_Observable__["Observable"].forkJoin.apply(__WEBPACK_IMPORTED_MODULE_0_rxjs_Observable__["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 __WEBPACK_IMPORTED_MODULE_0_rxjs_Observable__["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: __WEBPACK_IMPORTED_MODULE_2__index__["DataType"].list, list: [], group: {} };
var items = res.type === __WEBPACK_IMPORTED_MODULE_2__index__["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;
}());
/***/ }),
/* 5 */
/***/ (function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_5__;
/***/ }),
/* 6 */
/***/ (function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_6__;
/***/ }),
/* 7 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
;
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return OffClickDirective; });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__angular_core__ = __webpack_require__(0);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__angular_core___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0__angular_core__);
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var OffClickDirective = (function () {
function OffClickDirective() {
}
OffClickDirective.prototype.onClick = function ($event) {
$event.stopPropagation();
};
OffClickDirective.prototype.ngOnInit = function () {
var _this = this;
setTimeout(function () {
if (typeof document !== 'undefined') {
document.addEventListener('click', _this.offClickHandler);
}
}, 0);
};
OffClickDirective.prototype.ngOnDestroy = function () {
if (typeof document !== 'undefined') {
document.removeEventListener('click', this.offClickHandler);
}
};
__decorate([
Object(__WEBPACK_IMPORTED_MODULE_0__angular_core__["Input"])('offClick'),
__metadata("design:type", Object)
], OffClickDirective.prototype, "offClickHandler", void 0);
__decorate([
Object(__WEBPACK_IMPORTED_MODULE_0__angular_core__["HostListener"])('click', ['$event']),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], OffClickDirective.prototype, "onClick", null);
OffClickDirective = __decorate([
Object(__WEBPACK_IMPORTED_MODULE_0__angular_core__["Directive"])({
selector: '[offClick]'
})
], OffClickDirective);
return OffClickDirective;
}());
/***/ }),
/* 8 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
;
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return KeysPipe; });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__angular_core__ = __webpack_require__(0);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__angular_core___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0__angular_core__);
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var KeysPipe = (function () {
function KeysPipe() {
}
KeysPipe.prototype.transform = function (value, args) {
var keys = [];
for (var key in value) {
keys.push({ key: key, value: value[key] });
}
return keys;
};
KeysPipe = __decorate([
Object(__WEBPACK_IMPORTED_MODULE_0__angular_core__["Pipe"])({ name: 'keys' })
], KeysPipe);
return KeysPipe;
}());
/***/ }),
/* 9 */
/***/ (function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_9__;
/***/ }),
/* 10 */
/***/ (function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_10__;
/***/ }),
/* 11 */
/***/ (function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_11__;
/***/ }),
/* 12 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
;
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return DataType; });
/**
* 数据类型
*/
var DataType;
(function (DataType) {
DataType[DataType["list"] = 1] = "list";
DataType[DataType["group"] = 2] = "group";
})(DataType || (DataType = {}));
/***/ }),
/* 13 */
/***/ (function(module, exports) {
/***/ }),
/* 14 */
/***/ (function(module, exports) {
/***/ }),
/* 15 */
/***/ (function(module, exports) {
/***/ }),
/* 16 */
/***/ (function(module, exports) {
/***/ }),
/* 17 */
/***/ (function(module, exports) {
/***/ })
/******/ ]);
});
//# sourceMappingURL=ngx-address.umd.js.map