ngx-address
Version:
A simple address picker in angular.
1 lines • 15.4 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("@angular/core"),require("@angular/http"),require("@angular/common"),require("@angular/forms"),require("rxjs/Observable"),require("rxjs/add/observable/forkJoin")):"function"==typeof define&&define.amd?define(["@angular/core","@angular/http","@angular/common","@angular/forms","rxjs/Observable","rxjs/add/observable/forkJoin"],e):"object"==typeof exports?exports["ngx-address.umd.min"]=e(require("@angular/core"),require("@angular/http"),require("@angular/common"),require("@angular/forms"),require("rxjs/Observable"),require("rxjs/add/observable/forkJoin")):t["ngx-address.umd.min"]=e(t["@angular/core"],t["@angular/http"],t["@angular/common"],t["@angular/forms"],t["rxjs/Observable"],t["rxjs/add/observable/forkJoin"])}(this,function(t,e,n,o,i,r){return function(t){function e(o){if(n[o])return n[o].exports;var i=n[o]={i:o,l:!1,exports:{}};return t[o].call(i.exports,i,i.exports,e),i.l=!0,i.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,o){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:o})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="/",e(e.s=1)}([function(e,n){e.exports=t},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),n.d(e,"NgxAddressModule",function(){return y});var o=n(0),i=(n.n(o),n(5)),r=(n.n(i),n(6)),s=(n.n(r),n(7)),a=n(8),d=n(2),c=n(12);n.d(e,"DataType",function(){return c.a});var u=n(13);n.n(u);for(var p in u)["NgxAddressModule","DataType","default"].indexOf(p)<0&&function(t){n.d(e,t,function(){return u[t]})}(p);var l=n(14);n.n(l);for(var p in l)["NgxAddressModule","DataType","default"].indexOf(p)<0&&function(t){n.d(e,t,function(){return l[t]})}(p);var f=n(15);n.n(f);for(var p in f)["NgxAddressModule","DataType","default"].indexOf(p)<0&&function(t){n.d(e,t,function(){return f[t]})}(p);var h=n(3);n.n(h);for(var p in h)["NgxAddressModule","DataType","default"].indexOf(p)<0&&function(t){n.d(e,t,function(){return h[t]})}(p);var g=n(16);n.n(g);for(var p in g)["NgxAddressModule","DataType","default"].indexOf(p)<0&&function(t){n.d(e,t,function(){return g[t]})}(p);var b=n(17);n.n(b);for(var p in b)["NgxAddressModule","DataType","default"].indexOf(p)<0&&function(t){n.d(e,t,function(){return b[t]})}(p);var x=n(4);n.d(e,"AddressService",function(){return x.a}),n.d(e,"AddressComponent",function(){return d.a});var v=this&&this.__decorate||function(t,e,n,o){var i,r=arguments.length,s=r<3?e:null===o?o=Object.getOwnPropertyDescriptor(e,n):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,n,o);else for(var a=t.length-1;a>=0;a--)(i=t[a])&&(s=(r<3?i(s):r>3?i(e,n,s):i(e,n))||s);return r>3&&s&&Object.defineProperty(e,n,s),s},y=function(){function t(){}return t=v([Object(o.NgModule)({imports:[i.HttpModule,r.CommonModule],declarations:[a.a,d.a,s.a],exports:[d.a]})],t)}()},function(t,e,n){"use strict";n.d(e,"a",function(){return u});var o=n(3),i=(n.n(o),n(0)),r=(n.n(i),n(9)),s=(n.n(r),n(4)),a=this&&this.__decorate||function(t,e,n,o){var i,r=arguments.length,s=r<3?e:null===o?o=Object.getOwnPropertyDescriptor(e,n):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,n,o);else for(var a=t.length-1;a>=0;a--)(i=t[a])&&(s=(r<3?i(s):r>3?i(e,n,s):i(e,n))||s);return r>3&&s&&Object.defineProperty(e,n,s),s},d=this&&this.__metadata||function(t,e){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(t,e)},c=this&&this.__param||function(t,e){return function(n,o){e(n,o,t)}},u=function(){function t(t){this.element=t,this.onSelected=new i.EventEmitter,this.onChange=Function.prototype,this.onTouched=Function.prototype,this.onClose=this.onClose.bind(this),this._t=new s.a}return e=t,t.prototype.ngOnInit=function(){},t.prototype.onOpen=function(){this._t.opened=!0},t.prototype.onClose=function(){this._t.opened=!1},t.prototype.onTabChange=function(t){this._t.setHead(t)},t.prototype.notify=function(){this.onTouched(),this.onChange(this._t.result.id),this.onSelected.emit(this._t.result)},t.prototype.onItem=function(t,e,n,o){this._t.onSelected(t,n,o),this.notify()},t.prototype.ngOnChanges=function(t){var e=this;if(t.options)return this.options=this._t.initData(this.options),void this.notify();t.values&&setTimeout(function(){e._t.setAddress(t.values.currentValue)})},t.prototype.writeValue=function(t){var e=this;t&&t!==this._t.result.id&&setTimeout(function(){e._t.setAddress(t)})},t.prototype.registerOnChange=function(t){this.onChange=t},t.prototype.registerOnTouched=function(t){this.onTouched=t},t.prototype.setDisabledState=function(t){},a([Object(i.Input)(),d("design:type","function"==typeof(n=void 0!==o.Options&&o.Options)&&n||Object)],t.prototype,"options",void 0),a([Object(i.Input)(),d("design:type",Object)],t.prototype,"values",void 0),a([Object(i.Output)(),d("design:type","function"==typeof(u=void 0!==i.EventEmitter&&i.EventEmitter)&&u||Object)],t.prototype,"onSelected",void 0),t=e=a([Object(i.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:i.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:r.NG_VALUE_ACCESSOR,useExisting:Object(i.forwardRef)(function(){return e}),multi:!0}],host:{"[attr.class]":"options.className"}}),c(0,Object(i.Inject)(i.ElementRef)),d("design:paramtypes",["function"==typeof(p=void 0!==i.ElementRef&&i.ElementRef)&&p||Object])],t);var e,n,u,p}()},function(t,e){},function(t,e,n){"use strict";n.d(e,"a",function(){return s});var o=n(10),i=(n.n(o),n(11)),r=(n.n(i),n(1)),s=function(){function t(){this.inited=!1,this.opened=!1,this.hd=[],this.bd=[],this.max=0}return t.prototype.initData=function(t){var e=this;if(this.inited=!1,this.title="",this.result={id:"",name:"",paths:[]},this.options=Object.assign({separator:"/",placeholder:"请选择省市区",types:["省份","城市","县区"],offset:{x:0,y:25},jumps:[]},t),!this.options.data&&!this.options.http)throw new Error("数据源 `data` 或 `http` 至少必须指定一项");return this.hd=[],this.bd=[],this.options.types.forEach(function(t,n){e.hd.push({name:t,index:n,selected:0===n}),e.bd.push({index:n,selected:0===n,data:{}})}),this.max=this.options.types.length,this.load(0,this.result.id).subscribe(function(t){e.inited=!0}),this.options},t.prototype.setHead=function(t){this.hd.forEach(function(e){e.selected=t===e.index}),this.bd.forEach(function(e){e.selected=t===e.index})},t.prototype.findItem=function(t,e){var n,o=this.bd[t].data.items;for(var i in o)if(n=o[i].find(function(t){return e===t.id}))break;return n},t.prototype.parseAddress=function(t){var e=6===t.length,n=9===t.length;if(!e&&!n)return[];return e?Array(3).fill("").map(function(e,n){return(t.substr(0,2*(n+1))+"000000").substr(0,6)}):n?Array(4).fill("").map(function(e,n){return n>2?t:(t.substr(0,2*(n+1))+"000000").substr(0,6)}):[]},t.prototype.putResult=function(t,e,n,o){void 0===o&&(o=!0);var i=this.bd[t].data.items;for(var r in i)i[r].forEach(function(t){t.selected=e===t.id});n=Object.assign({},n),this.result.id=e,this.result.name=n.name,this.result.paths=this.result.paths.slice(0,t),delete n.selected,this.result.paths.push(n),this.title=this.result.paths.map(function(t){return t.name}).join('<i class="separator">'+this.options.separator+"</i>"),o&&this.bd.forEach(function(e){e.index>t&&(e.data={})})},t.prototype.setAddress=function(t){var e=this;if(t){var n,i=this;n="string"==typeof t?i.parseAddress(t.toString()):t instanceof Array?t:[];var r=i.options.types.length,s=[];n.forEach(function(t,e){e>=r||s.push(i.load(e,e>0?n[e-1]:"",t))});var a=r>n.length;a&&s.push(i.load(n.length,n[n.length-1])),o.Observable.forkJoin.apply(o.Observable,s).subscribe(function(t){for(var o=0,i=t;o<i.length;o++){var r=i[o];e.putResult(r.index,r.value,e.findItem(r.index,r.value),!1)}a&&e.setHead(n.length)})}},t.prototype.onSelected=function(t,e,n){var o=this;o.putResult(t,e,n),++t,t>=o.max?o.opened=!1:(o.setHead(t),o.load(t,o.result.id).subscribe(function(e){t<o.max&&o.jump(t)}))},t.prototype.load=function(t,e,n){var i=this;return new o.Observable(function(o){(i.options.data||i.options.http)(t,e).subscribe(function(r){var s={index:t,value:n};if(r)return i._renderData(t,r,n),o.next(s),void o.complete();i.options.data&&i.options.http&&i.options.http(t,e).subscribe(function(e){i._renderData(t,e,n),o.next(s),o.complete()},function(t){o.error(t),o.complete()})},function(t){o.error(t),o.complete()})})},t.prototype._renderData=function(t,e,n){e||(e={type:r.DataType.list,list:[],group:{}});var o=e.type===r.DataType.list?{"":e.list}:Object.assign({},e.group),i=[],s=0;for(var a in o)o[a].forEach(function(t){++s,t.selected=n===t.id,!0===t.tips&&i.push(t)}),o[a]=o[a].filter(function(t){return!0!==t.tips});this.bd[t].data={type:e.type,items:o,tips:i||[]},0===s&&(this.opened=!1)},t.prototype.jump=function(t){var e=this,n=this.bd[t].data.items;for(var o in n){var i=n[o].find(function(t){return~e.options.jumps.indexOf(t.id)});i&&this.onSelected(t,i.id,i)}},t}()},function(t,n){t.exports=e},function(t,e){t.exports=n},function(t,e,n){"use strict";n.d(e,"a",function(){return s});var o=n(0),i=(n.n(o),this&&this.__decorate||function(t,e,n,o){var i,r=arguments.length,s=r<3?e:null===o?o=Object.getOwnPropertyDescriptor(e,n):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,n,o);else for(var a=t.length-1;a>=0;a--)(i=t[a])&&(s=(r<3?i(s):r>3?i(e,n,s):i(e,n))||s);return r>3&&s&&Object.defineProperty(e,n,s),s}),r=this&&this.__metadata||function(t,e){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(t,e)},s=function(){function t(){}return t.prototype.onClick=function(t){t.stopPropagation()},t.prototype.ngOnInit=function(){var t=this;setTimeout(function(){"undefined"!=typeof document&&document.addEventListener("click",t.offClickHandler)},0)},t.prototype.ngOnDestroy=function(){"undefined"!=typeof document&&document.removeEventListener("click",this.offClickHandler)},i([Object(o.Input)("offClick"),r("design:type",Object)],t.prototype,"offClickHandler",void 0),i([Object(o.HostListener)("click",["$event"]),r("design:type",Function),r("design:paramtypes",[Object]),r("design:returntype",void 0)],t.prototype,"onClick",null),t=i([Object(o.Directive)({selector:"[offClick]"})],t)}()},function(t,e,n){"use strict";n.d(e,"a",function(){return r});var o=n(0),i=(n.n(o),this&&this.__decorate||function(t,e,n,o){var i,r=arguments.length,s=r<3?e:null===o?o=Object.getOwnPropertyDescriptor(e,n):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,n,o);else for(var a=t.length-1;a>=0;a--)(i=t[a])&&(s=(r<3?i(s):r>3?i(e,n,s):i(e,n))||s);return r>3&&s&&Object.defineProperty(e,n,s),s}),r=function(){function t(){}return t.prototype.transform=function(t,e){var n=[];for(var o in t)n.push({key:o,value:t[o]});return n},t=i([Object(o.Pipe)({name:"keys"})],t)}()},function(t,e){t.exports=o},function(t,e){t.exports=i},function(t,e){t.exports=r},function(t,e,n){"use strict";n.d(e,"a",function(){return o});var o;!function(t){t[t.list=1]="list",t[t.group=2]="group"}(o||(o={}))},function(t,e){},function(t,e){},function(t,e){},function(t,e){},function(t,e){}])});