Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit, AfterViewInit, OnDestroy, ViewChild, AfterContentInit, HostListener } from '@angular/core';
  2. import { ActivatedRoute } from '@angular/router';
  3. import { HttpResponse, HttpErrorResponse } from '@angular/common/http';
  4. import { DatePipe } from '@angular/common';
  5. import { } from '@types/googlemaps';
  6.  
  7. import { Observable } from 'rxjs/Observable';
  8. import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
  9. import { JhiEventManager, JhiAlertService } from 'ng-jhipster';
  10.  
  11. import { Ticket } from './ticket.model';
  12. import { TicketPopupService } from './ticket-popup.service';
  13. import { TicketService } from './ticket.service';
  14. import { Status, StatusService } from '../status';
  15. import { Priority, PriorityService } from '../priority';
  16. import { Category, CategoryService } from '../category';
  17. import { Person, PersonService } from '../person';
  18.  
  19. declare var jquery: any;
  20. declare var $: any;
  21. declare var google: any;
  22. declare var ymaps: any;
  23.  
  24. @Component( {
  25.     selector: 'jhi-ticket-dialog',
  26.     templateUrl: './ticket-dialog.component.html',
  27.     styleUrls: ['./ticket-dialog.component.scss']
  28. } )
  29. export class TicketDialogComponent implements OnInit, AfterContentInit {
  30.  
  31.     @ViewChild( 'gmap' ) gmapElement: any;
  32.     map: google.maps.Map;
  33.     geocoder = new google.maps.Geocoder();
  34.     ticket: Ticket;
  35.     isSaving: boolean;
  36.     statuses: Status[];
  37.     priorities: Priority[];
  38.     categories: Category[];
  39.     people: Person[];
  40.     person: any = {
  41.         firstName: "",
  42.         lastName: "",
  43.         phone: "",
  44.         address: "",
  45.         email: "",
  46.         password: "",
  47.         city: "",
  48.     }
  49.     UserInput: any;
  50.     marker: any;
  51.     addPersonSection: boolean = false;
  52.     addMapC: boolean = false;
  53.     constructor(
  54.         public activeModal: NgbActiveModal,
  55.         private jhiAlertService: JhiAlertService,
  56.         private ticketService: TicketService,
  57.         private statusService: StatusService,
  58.         private priorityService: PriorityService,
  59.         private categoryService: CategoryService,
  60.         private personService: PersonService,
  61.         private eventManager: JhiEventManager,
  62.         private datePipe: DatePipe
  63.     ) {
  64.     }
  65.  
  66.     @HostListener( 'document:keyup', ['$event'] )
  67.     handleKeyboardEvent( event: KeyboardEvent ) {
  68.         if ( event.key == "+" ) {
  69.             this.addPersonSection = true;
  70.             $( "#field_firstName" ).focus();
  71.             this.switchButton();
  72.             event.preventDefault();
  73.         }
  74.         if ( event.key == "-" ) {
  75.             $( "#editForm" ).focus();
  76.             this.addPersonSection = false;
  77.             this.switchButton();
  78.             event.preventDefault();
  79.         }
  80.         if ( event.key == "Enter" ) {
  81.             this.save();
  82.         }
  83.         if ( event.key == "Insert" ) {
  84.             this.addUser();
  85.         }
  86.         if ( event.key == "Esc" ) {
  87.             this.activeModal.close( 'Esc' );
  88.         }
  89.     }
  90.  
  91.     ngOnInit() {
  92.         this.isSaving = false;
  93.         $( "#field_subject" ).focus();
  94.         this.ticket.ticketDate = this.datePipe.transform( new Date(), 'yyyy-MM-ddThh:mm' );
  95.         this.statusService.query()
  96.             .subscribe(( res: HttpResponse<Status[]> ) => { this.statuses = res.body; if ( this.ticket.statusId == null ) this.ticket.statusId = this.statuses[0]; }, ( res: HttpErrorResponse ) => this.onError( res.message ) );
  97.         this.priorityService.query()
  98.             .subscribe(( res: HttpResponse<Priority[]> ) => { this.priorities = res.body; if ( this.ticket.priorityId == null ) this.ticket.priorityId = this.priorities[0]; }, ( res: HttpErrorResponse ) => this.onError( res.message ) );
  99.         this.categoryService.query()
  100.             .subscribe(( res: HttpResponse<Category[]> ) => { this.categories = res.body; if ( this.ticket.categoryId == null ) this.ticket.categoryId = this.categories[0]; }, ( res: HttpErrorResponse ) => this.onError( res.message ) );
  101.         this.personService.query()
  102.             .subscribe(( res: HttpResponse<Person[]> ) => { this.peopleArray = this.people = res.body; }, ( res: HttpErrorResponse ) => this.onError( res.message ) );
  103.         if ( this.ticket != undefined && this.ticket.personId != undefined && this.ticket.personId["firstName"] && this.ticket.personId["lastName"] )
  104.             this.UserInput = this.ticket.personId["firstName"] + ' ' + this.ticket.personId["lastName"];
  105.     }
  106.  
  107.     switchButton() {
  108.         if ( this.addPersonSection ) {
  109.             $( "#addNewPerson" ).removeClass();
  110.             $( "#addNewPerson" ).addClass( "btn btn-danger float-right jh-create-entity create-person" );
  111.             $( "#minus" ).removeClass();
  112.             $( "#minus" ).addClass( "fa fa-minus" );
  113.         }
  114.         else {
  115.             $( "#addNewPerson" ).removeClass();
  116.             $( "#addNewPerson" ).addClass( "btn btn-primary float-right jh-create-entity create-person" );
  117.             $( "#minus" ).removeClass();
  118.             $( "#minus" ).addClass( "fa fa-plus" );
  119.         }
  120.     }
  121.     switchMap() {
  122.         if ( this.addMapC ) {
  123.             $( "#mapaDiv" ).css( "display", "block" )
  124.             $( "#addMap" ).removeClass();
  125.             $( "#addMap" ).addClass( "btn btn-danger float-right jh-create-entity create-person" );
  126.             $( "#mapa" ).removeClass();
  127.             $( "#mapa" ).addClass( "fa fa-minus" );
  128.         }
  129.         else {
  130.             $( "#mapaDiv" ).css( "display", "none" )
  131.             $( "#addMap" ).removeClass();
  132.             $( "#addMap" ).addClass( "btn btn-primary float-right jh-create-entity create-person" );
  133.             $( "#mapa" ).removeClass();
  134.             $( "#mapa" ).addClass( "fa fa-plus" );
  135.         }
  136.     }
  137.  
  138.     ngAfterContentInit() {
  139.         //        this.initMap();
  140.         this.initYamp();
  141.         $( "#mapaDiv" ).css( "display", "none" )
  142.         $( document ).on( 'click', 'input[name="address"]', function() { this.select(); } );
  143.     }
  144.  
  145.     initYamp() {
  146.         ymaps.ready( function() {
  147.             var myMap = new ymaps.Map( 'mmap', {
  148.                 center: [55.751574, 37.573856],
  149.                 zoom: 9
  150.             }, {
  151.                     searchControlProvider: 'yandex#search'
  152.                 } ),
  153.  
  154.                 geolocation = ymaps.geolocation.get( {
  155.                     provider: 'yandex',
  156.                     mapStateAutoApply: true
  157.                 } ).then( function( result ) {
  158.                     result.geoObjects.options.set( 'preset', 'islands#redCircleIcon' );
  159.                     result.geoObjects.get( 0 ).properties.set( {
  160.                         balloonContentBody: 'My location'
  161.                     } );
  162.                     myMap.geoObjects.add( result.geoObjects );
  163.                 } ),
  164.  
  165.                 myPlacemark = new ymaps.Placemark( myMap.getCenter(), {
  166.                     hintContent: 'A custom placemark icon',
  167.                     balloonContent: 'This is a pretty placemark'
  168.                 }, {
  169.                         /**
  170.                          * Options.
  171.                          * You must specify this type of layout.
  172.                          */
  173.                         iconLayout: 'default#image',
  174.                         // Custom image for the placemark icon.
  175.                         iconImageHref: 'images/myIcon.gif',
  176.                         // The size of the placemark.
  177.                         iconImageSize: [30, 42],
  178.                         /**
  179.                          * The offset of the upper left corner of the icon relative
  180.                          * to its "tail" (the anchor point).
  181.                          */
  182.                         iconImageOffset: [-5, -38]
  183.                     }
  184.                 );
  185.             myMap.geoObjects
  186.                 .add( myPlacemark );
  187.         } );
  188.     }
  189.  
  190.     initMap() {
  191.         this.map = new google.maps.Map( this.gmapElement.nativeElement, {
  192.             // center: new google.maps.LatLng(this.myLocation.lat, this.myLocation.lng),
  193.             zoom: 15
  194.         } );
  195.         this.geocoder = new google.maps.Geocoder();
  196.         this.addMyMarker();
  197.     }
  198.  
  199.     addMyMarker() {
  200.         navigator.geolocation.getCurrentPosition(( position ) => {
  201.             const myPoint = new google.maps.LatLng( position.coords.latitude, position.coords.longitude );
  202.  
  203.             this.marker = new google.maps.Marker( {
  204.                 position: myPoint,
  205.                 draggable: true,
  206.                 map: this.map
  207.             } );
  208.             this.marker.addListener( 'dragend', ( event ) => {
  209.                 this.geocodePosition( this.geocoder, event.latLng );
  210.                 this.ticket.latitude = event.latLng.lat();
  211.                 this.ticket.longitude = event.latLng.lng();
  212.  
  213.             } );
  214.             // Center the map on the new position
  215.             this.map.setCenter( myPoint );
  216.             this.geocodePosition( this.geocoder, this.marker.getPosition() );
  217.  
  218.         } );
  219.     }
  220.  
  221.     codeAddress( geocoder, map, ticket ) {
  222.         if ( this.ticket.address != null ) {
  223.             geocoder.geocode( { 'address': this.ticket.address }, ( results, status ) => {
  224.                 if ( status === 'OK' ) {
  225.                     if ( this.marker ) {
  226.                         this.marker.setMap( null );
  227.                     }
  228.                     map.setCenter( results[0].geometry.location );
  229.                     this.marker = new google.maps.Marker( {
  230.                         map: map,
  231.                         draggable: true,
  232.                         position: results[0].geometry.location,
  233.                         title: this.ticket.address
  234.  
  235.                     } );
  236.                     this.marker.addListener( 'dragend', ( event ) => {
  237.                         this.geocodePosition( geocoder, event.latLng );
  238.                         this.ticket.latitude = event.latLng.lat();
  239.                         this.ticket.longitude = event.latLng.lng();
  240.  
  241.                     } );
  242.                     this.ticket.latitude = results[0].geometry.location.lat();
  243.                     this.ticket.longitude = results[0].geometry.location.lng();
  244.  
  245.                 } else {
  246.                     alert( 'Geocode was not successful for the following reason: ' + status );
  247.                 }
  248.             } );
  249.         }
  250.     }
  251.  
  252.     geocodePosition( geocoder, pos ) {
  253.         geocoder.geocode( {
  254.             latLng: pos
  255.         }, ( responses ) => {
  256.             if ( responses && responses.length > 0 ) {
  257.                 this.ticket.address = responses[0].formatted_address;
  258.             } else {
  259.                 this.ticket.address = '';
  260.             }
  261.         } );
  262.     }
  263.  
  264.     clear() {
  265.         this.activeModal.dismiss( 'cancel' );
  266.     }
  267.  
  268.     save() {
  269.         this.isSaving = true;
  270.         if ( this.UserInput == undefined || this.UserInput.trim() == "" || this.UserInput.split( " " ).length < 2 ) {
  271.             return;
  272.         }
  273.         let ime = this.UserInput.split( " " )[0];
  274.         let prezime = this.UserInput.split( " " )[1];
  275.  
  276.         if ( this.ticket.subject == undefined || this.ticket.subject == null || this.ticket.description == undefined || this.ticket.description == null || this.ticket.statusId == null || this.ticket.statusId == undefined )
  277.             return;
  278.         this.ticket.personId = this.people.find( obj => obj.firstName == ime && obj.lastName == prezime );
  279.         if ( this.ticket.personId != null && this.ticket.personId != undefined ) {
  280.             if ( this.ticket.id !== undefined ) {
  281.                 this.subscribeToSaveResponse(
  282.                     this.ticketService.update( this.ticket ) );
  283.             } else {
  284.                 this.subscribeToSaveResponse(
  285.                     this.ticketService.create( this.ticket ) );
  286.             }
  287.         }
  288.         this.activeModal.dismiss( 'save' );
  289.     }
  290.  
  291.     addUser() {
  292.         if ( this.person.phone[0] === '0' ) {
  293.             this.person.phone = this.person.phone.substr( 1 );
  294.             this.person.phone = '+381' + this.person.phone;
  295.         }
  296.  
  297.         this.subscribeToAddUserResponse(
  298.             this.personService.create( this.person ) );
  299.         this.addPersonSection = !this.addPersonSection;
  300.         this.switchButton();
  301.     }
  302.  
  303.     private subscribeToAddUserResponse( result: Observable<HttpResponse<Person>> ) {
  304.         result.subscribe(( res: HttpResponse<Person> ) =>
  305.             this.onAddUserSuccess( res.body ), ( res: HttpErrorResponse ) => this.onAddUserError() );
  306.     }
  307.  
  308.     private onAddUserSuccess( result: Person ) {
  309.         this.personService.query()
  310.             .subscribe(( res: HttpResponse<Person[]> ) => { this.peopleArray = this.people = res.body; }, ( res: HttpErrorResponse ) => this.onError( res.message ) );
  311.         this.addPersonSection = false;
  312.         this.UserInput = this.person.firstName + " " + this.person.lastName;
  313.     }
  314.  
  315.     private onAddUserError() {
  316.         // @TODO ?
  317.         // prikazi gresku etc
  318.     }
  319.  
  320.     private subscribeToSaveResponse( result: Observable<HttpResponse<Ticket>> ) {
  321.         result.subscribe(( res: HttpResponse<Ticket> ) =>
  322.             this.onSaveSuccess( res.body ), ( res: HttpErrorResponse ) => this.onSaveError() );
  323.     }
  324.  
  325.     private onSaveSuccess( result: Ticket ) {
  326.         this.eventManager.broadcast( { name: 'ticketListModification', content: 'OK' } );
  327.         this.isSaving = false;
  328.         this.activeModal.dismiss( result );
  329.     }
  330.  
  331.     private onSaveError() {
  332.         this.isSaving = false;
  333.     }
  334.  
  335.     private onError( error: any ) {
  336.         this.jhiAlertService.error( error.message, null, null );
  337.     }
  338.  
  339.     trackStatusById( index: number, item: Status ) {
  340.         return item.id;
  341.     }
  342.  
  343.     trackPriorityById( index: number, item: Priority ) {
  344.         return item.id;
  345.     }
  346.  
  347.     trackCategoryById( index: number, item: Category ) {
  348.         return item.id;
  349.     }
  350.  
  351.     trackPersonById( index: number, item: Person ) {
  352.         return item.id;
  353.     }
  354.     count = 0;
  355.     peopleArray: Person[];
  356.     search_word( event ) {
  357.         this.peopleArray = [];
  358.         for ( var i = 0; i < this.people.length; i++ ) {
  359.             if ( this.people[i].firstName.toLowerCase().search( this.UserInput.toLowerCase() ) != -1 || this.people[i].lastName.toLowerCase().search( this.UserInput.toLowerCase() ) != -1 )
  360.                 this.peopleArray.push( this.people[i] );
  361.         }
  362.     }
  363. }
  364.  
  365. @Component( {
  366.     selector: 'jhi-ticket-popup',
  367.     template: ''
  368. } )
  369. export class TicketPopupComponent implements OnInit, OnDestroy {
  370.  
  371.     routeSub: any;
  372.  
  373.     constructor(
  374.         private route: ActivatedRoute,
  375.         private ticketPopupService: TicketPopupService
  376.     ) { }
  377.  
  378.     ngOnInit() {
  379.         this.routeSub = this.route.params.subscribe(( params ) => {
  380.             if ( params['id'] ) {
  381.                 this.ticketPopupService
  382.                     .open( TicketDialogComponent as Component, params['id'] );
  383.             } else {
  384.                 this.ticketPopupService
  385.                     .open( TicketDialogComponent as Component );
  386.             }
  387.         } );
  388.     }
  389.  
  390.     ngOnDestroy() {
  391.         this.routeSub.unsubscribe();
  392.     }
  393. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement