@WidgetComponent

nm-add-content

File

src/app/shared/widgets/apps/ipim-content-object/content-add/add-content-popup.component.ts

Implements

OnDestroy OnInit

Metadata

selector nm-add-content
styleUrls add-content-popup.component.scss
templateUrl ./add-content-popup.component.html

Index

Widget inputs
Widget outputs
Properties
Methods

Constructor

constructor(langSwitcherService: LangSwitcherService, dialogRef: MatDialogRef, authHttp: HttpClient, _notificationService: NotificationsService, _translateService: TranslateService, owlDateTimeIntl: OwlDateTimeIntl, dateAdapter: DateAdapter, dateTimeAdapter: DateTimeAdapter, _currentLocaleService: CurrentLocaleService)
Parameters :
Name Type Optional
langSwitcherService LangSwitcherService no
dialogRef MatDialogRef<AddContentPopupComponent> no
authHttp HttpClient no
_notificationService NotificationsService no
_translateService TranslateService no
owlDateTimeIntl OwlDateTimeIntl no
dateAdapter DateAdapter<Date> no
dateTimeAdapter DateTimeAdapter<any> no
_currentLocaleService CurrentLocaleService no

Methods

closePopup
closePopup()
Returns : void
constructTypesUri
constructTypesUri(selectedWidgetLocale: )
Parameters :
Name Optional
selectedWidgetLocale no
Returns : void
getContentTypes
getContentTypes()
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void
onLocaleChange
onLocaleChange()
Returns : void
perfomrGetContentTypesRequest
perfomrGetContentTypesRequest(url: )
Parameters :
Name Optional
url no
Returns : Observable<any>
resetValidFromDateTime
resetValidFromDateTime(event: )
Parameters :
Name Optional
event no
Returns : void
resetValidUntilDateTime
resetValidUntilDateTime(event: )
Parameters :
Name Optional
event no
Returns : void
saveContentObject
saveContentObject()
Returns : void
validateDateRange
validateDateRange()
Returns : void

Properties

Public contentLocale
contentLocale: string
Type : string
Public contentName
contentName: any
Type : any
Public contentType
contentType: any
Type : any
Public contentValidFrom
contentValidFrom: Date
Type : Date
Public contentValidUntil
contentValidUntil: Date
Type : Date
Public defaultLocale
defaultLocale: string
Type : string
Public dialogRef
dialogRef: MatDialogRef<AddContentPopupComponent>
Type : MatDialogRef<AddContentPopupComponent>
Public isDateRangeValid
isDateRangeValid: boolean
Type : boolean
Default value : true
Public langSwitcherService
langSwitcherService: LangSwitcherService
Type : LangSwitcherService
Public locales
locales: Locale[]
Type : Locale[]
Public types
types: any[]
Type : any[]
Private typesUri
typesUri: string
Type : string
Private typesUriWithLocale
typesUriWithLocale: string
Type : string
Private unsubscribe
unsubscribe:
Default value : NgUnsubscribe.create()
Public uri
uri: string
Type : string
import { OnDestroy, Component, OnInit } from "@angular/core";
import { MatDialogRef } from "@angular/material/dialog";
import { NgUnsubscribe } from "../../../../ng-unsubscribe";
import { LangSwitcherService } from "../../../../components/i18n/lang-switcher.service";
import { Locale } from "../../../../components/i18n";
import { map, takeUntil } from "rxjs/operators";
import { HttpClient } from "@angular/common/http";
import { NotificationsService } from "../../../../components/notifications/services/notifications.service";
import { TranslateService } from "@ngx-translate/core";
import * as uriTemplates_ from "uri-templates";
import { Observable } from "rxjs";
import { CurrentLocaleService } from "../../../../components/i18n/currentLocale.service";
import { DateTimeAdapter, OwlDateTimeIntl } from "ng-pick-datetime";
import { DateAdapter } from "@angular/material/core";

const uriTemplates = uriTemplates_;

@Component({
  selector: "nm-add-content",
  templateUrl: "./add-content-popup.component.html",
  styleUrls: ["./add-content-popup.component.scss"],
})
export class AddContentPopupComponent implements OnDestroy, OnInit {
  private unsubscribe = NgUnsubscribe.create();

  public uri: string;
  public contentLocale: string;
  public contentName: any;
  public contentType: any;
  public contentValidFrom: Date;
  public contentValidUntil: Date;
  public defaultLocale: string;
  public types: any[];
  public locales: Locale[];
  public isDateRangeValid: boolean = true;

  private typesUri: string;
  private typesUriWithLocale: string;

  constructor(
    public langSwitcherService: LangSwitcherService,
    public dialogRef: MatDialogRef<AddContentPopupComponent>,
    private authHttp: HttpClient,
    private _notificationService: NotificationsService,
    private _translateService: TranslateService,
    private owlDateTimeIntl: OwlDateTimeIntl,
    private dateAdapter: DateAdapter<Date>,
    private dateTimeAdapter: DateTimeAdapter<any>,
    private _currentLocaleService: CurrentLocaleService
  ) {
    langSwitcherService
      .getSupportedLanguages("/api/public/locales?type=ipim&sort=description")
      .subscribe((data: any) => {
        let localesTemp = [];
        if (data._embedded !== undefined) {
          for (const locale of data._embedded["locales"]) {
            localesTemp = localesTemp.concat(locale);
          }
        }
        this.locales = localesTemp;
        let dataLocale = this.dialogRef.componentInstance["dataLocale"];
        if (dataLocale) {
          this.contentLocale = dataLocale;
        } else {
          this.contentLocale =
            this.locales.find((local) => local.default).id ||
            this.locales[0].id;
        }
        this.constructTypesUri(this.contentLocale);
      });
  }

  constructTypesUri(selectedWidgetLocale) {
    if (!selectedWidgetLocale) {
      return;
    }
    let template = uriTemplates(this.typesUri);
    let uriParams = {};

    uriParams["locale"] = selectedWidgetLocale;

    this.typesUriWithLocale = template.fill(uriParams);
    this.getContentTypes();
  }

  saveContentObject() {
    let content = JSON.stringify({
      locale: this.contentLocale,
      description: this.contentName,
      type: this.contentType,
      validFrom: this.contentValidFrom,
      validUntil: this.contentValidUntil,
    });

    this.authHttp
      .post(this.uri, content)
      .pipe(map((res) => <any>res))
      .subscribe((response) => {
        if (response.contentNo && response.contentNo !== "") {
          this._notificationService.success(response.title, response.messages);
          this.dialogRef.close(response.contentNo);
        } else {
          this._notificationService.error(response.title, response.messages);
          this.dialogRef.close();
        }
      });
  }

  onLocaleChange() {
    this.constructTypesUri(this.contentLocale);
  }

  getContentTypes() {
    this.perfomrGetContentTypesRequest(this.typesUriWithLocale).subscribe(
      (data: any) => {
        this.types = [];
        if (data._embedded !== undefined) {
          for (const type of data._embedded["content-type"]) {
            this.types = this.types.concat(type);
          }
        }
      }
    );
  }

  validateDateRange() {
    if (this.contentValidUntil) {
      this.contentValidUntil.setSeconds(0, 0);
    }
    if (this.contentValidFrom) {
      this.contentValidFrom.setSeconds(0, 0);
    }
    if (this.contentValidUntil && this.contentValidFrom) {
      this.isDateRangeValid = this.contentValidUntil > this.contentValidFrom;
    } else {
      this.isDateRangeValid = true;
    }
  }

  perfomrGetContentTypesRequest(url): Observable<any> {
    return this.authHttp.get(url, {}).pipe(map((res) => <any>res));
  }

  resetValidFromDateTime(event) {
    event.stopPropagation();
    this.contentValidFrom = null;
    this.validateDateRange();
  }

  resetValidUntilDateTime(event) {
    event.stopPropagation();
    this.contentValidUntil = null;
    this.validateDateRange();
  }

  closePopup() {
    this.dialogRef.close();
  }

  ngOnDestroy() {
    this.unsubscribe.destroy();
  }

  ngOnInit() {
    this._currentLocaleService
      .getCurrentLocale()
      .pipe(takeUntil(this.unsubscribe))
      .subscribe((selectedLocale) => {
        this.dateAdapter.setLocale(selectedLocale);
        this.dateTimeAdapter.setLocale(selectedLocale);
        this.owlDateTimeIntl.cancelBtnLabel =
          this._translateService.instant("button.cancel");
        this.owlDateTimeIntl.setBtnLabel =
          this._translateService.instant("button.accept");
      });
  }
}
<nm-dialog [dialogRef]="dialogRef" class="nm-dialog">
  <ng-container slot="title">
    {{ "app.iPimContentMGT.frontend.add-content.title" | translate }}
  </ng-container>

  <ng-container slot="content" style="min-width: 1200px">
    <mat-form-field class="full-width">
      <input
        type="text"
        matInput
        #name="ngModel"
        [(ngModel)]="contentName"
        placeholder="{{
          'app.iPimContentMGT.frontend.content-name' | translate
        }}"
        required
      />
    </mat-form-field>
    <br />
    <div class="nm-select">
      <mat-form-field>
        <mat-select
          class="full-width"
          [value]=""
          #type="ngModel"
          [(ngModel)]="contentType"
          required
          placeholder="{{
            'app.iPimContentMGT.frontend.content-type' | translate
          }}"
        >
          <mat-option *ngFor="let type of types" [value]="type.identifier">
            {{ type.description }}
          </mat-option>
        </mat-select>
      </mat-form-field>
    </div>
    <br />
    <div class="full-width">
      <mat-form-field
        class="full-width"
        (click)="datetimepickervalidfrom.open()"
        [ngClass]="{ 'nm-date-time-input': true }"
      >
        <input
          matInput
          [owlDateTime]="datetimepickervalidfrom"
          [(ngModel)]="contentValidFrom"
          placeholder="{{
            'app.iPimContentMGT.frontend.add-content.placholder.validFrom'
              | translate
          }}"
          (dateTimeInput)="validateDateRange()"
        />
        <button
          mat-icon-button
          primary
          matSuffix
          class="remove-action"
          *ngIf="contentValidFrom"
          (click)="resetValidFromDateTime($event)"
        >
          <mat-icon class="fade-in">close</mat-icon>
        </button>
        <button
          mat-button
          matSuffix
          mat-icon-button
          aria-label="date_range"
          [owlDateTimeTrigger]="datetimepickervalidfrom"
        >
          <mat-icon>event</mat-icon>
        </button>
      </mat-form-field>
      <owl-date-time #datetimepickervalidfrom></owl-date-time>
    </div>
    <div class="full-width">
      <mat-form-field
        class="full-width"
        (click)="datetimepickervaliduntil.open()"
        [ngClass]="{ 'nm-date-time-input': true }"
      >
        <input
          matInput
          [(ngModel)]="contentValidUntil"
          placeholder="{{
            'app.iPimContentMGT.frontend.add-content.placholder.validUntil'
              | translate
          }}"
          [owlDateTime]="datetimepickervaliduntil"
          (dateTimeInput)="validateDateRange()"
        />
        <button
          mat-icon-button
          primary
          matSuffix
          class="remove-action"
          *ngIf="contentValidUntil"
          (click)="resetValidUntilDateTime($event)"
        >
          <mat-icon class="fade-in">close</mat-icon>
        </button>
        <button
          mat-button
          matSuffix
          mat-icon-button
          aria-label="date_range"
          [owlDateTimeTrigger]="datetimepickervaliduntil"
        >
          <mat-icon>event</mat-icon>
        </button>
      </mat-form-field>
      <mat-error *ngIf="!isDateRangeValid">{{
        "app.iPimContentMGT.frontend.error.date-range-invalid" | translate
      }}</mat-error>
      <owl-date-time #datetimepickervaliduntil></owl-date-time>
    </div>
  </ng-container>
  <ng-container slot="actions">
    <button
      id="close-add-popup-button"
      mat-button
      type="button"
      (click)="closePopup()"
    >
      {{ "button.cancel" | translate }}
    </button>
    <button
      id="save-content-button"
      mat-raised-button
      color="primary"
      type="button"
      [disabled]="!name.valid || !type.valid || !isDateRangeValid"
      (click)="name.valid && type.valid ? saveContentObject() : false"
    >
      {{ "button.save" | translate }}
    </button>
  </ng-container>
</nm-dialog>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""