Skip to content

Commit

Permalink
Merge pull request #27 from Carpenteri1/options-when-adding-component
Browse files Browse the repository at this point in the history
Options when adding component
  • Loading branch information
Carpenteri1 authored Nov 13, 2024
2 parents 02bf03f + 8998a87 commit 9c44014
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 35 deletions.
14 changes: 12 additions & 2 deletions src/app/components/basicComponent/basic.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@
<div *ngFor="let item of sharedService.flexItems"
class="action-btn"
style="height: 300px; width:330px; flex: 1 1 330px; margin: 10px; background:darkgrey;">
Flex item
<button (click)="Remove(item.id)">Remove Button </button>
<ul>
<li>
{{item.IComponentModel.Id}}
</li>
<li>
{{item.IComponentModel.Name}}
</li>
<li>
{{item.IComponentModel.Url}}
</li>
</ul>
<button (click)="Remove(item.IComponentModel.Id)">Remove Button </button>
</div>
</div>
4 changes: 2 additions & 2 deletions src/app/components/basicComponent/basic.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Component } from '@angular/core';

import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common'; // Import CommonModule for *ngFor
import { SharedService } from '../../shared.service';

@Component({
selector: 'basic-component',
standalone: true,
imports: [CommonModule],
imports: [CommonModule, FormsModule],
templateUrl: './basic.component.html',
styleUrls: ['./basic.component.css'],
})
Expand Down
45 changes: 24 additions & 21 deletions src/app/components/header/header.component.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
<div class="flex-wrap d-flex flex-row-reverse">

<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
...
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
<i class="bi bi-plus-lg"></i>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<li>
<a class="dropdown-item" href="#">
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
Öppna Modal
</button>
</a>
</li>
</ul>
</div>

<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
Expand All @@ -25,20 +16,32 @@ <h5 class="modal-title" id="exampleModalLabel">
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
Theme
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<li><a class="dropdown-item" href="#">Webpage</a></li>
<li><a class="dropdown-item" href="#">Service</a></li>
<form>
<div class="form-group">
<label>App name</label>
<input type="text" class="form-control" placeholder="App name" [(ngModel)]="IComponentModel.Name" name="name">
</div>
<div class="form-group">
<label>Service Url</label>
<input type="text" class="form-control" placeholder="Url" [(ngModel)]="IComponentModel.Url" name="url">
</div>
<!--<div class="form-group">
<label>App name</label>
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Välj en ikon
</button>
<ul class="dropdown-menu form-control">
<li><a class="dropdown-item" href="#"><i class="bi bi-house-door-fill me-2"></i></a></li>
<li><a class="dropdown-item" href="#"><i class="bi bi-person-fill me-2"></i></a></li>
<li><a class="dropdown-item" href="#"><i class="bi bi-gear-fill me-2"></i></a></li>
<li><a class="dropdown-item" href="#"><i class="bi bi-box-arrow-right me-2"></i></a></li>
</ul>
</div>
Detta är innehållet i modalen. Lägg till vad du vill här!
</div>-->
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" data-bs-dismiss="modal" (click)="AddComponent()">Add new component</button>
<button type="button" class="btn btn-primary" data-bs-dismiss="modal" (click)="AddComponent(IComponentModel)">Add</button>
</div>
</div>
</div>
Expand Down
15 changes: 11 additions & 4 deletions src/app/components/header/header.component.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import { Component } from "@angular/core";
import { SharedService } from '../../shared.service';
import { IComponentModel } from '../../interfaces/IComponent.Model'
import { FormsModule } from '@angular/forms';

@Component({
selector: 'header-component',
standalone: true,
templateUrl: './header.component.html',
styleUrl: './header.component.css',
imports: [FormsModule]
})

export class HeaderComponent{
IComponentModel: IComponentModel = {
Id:0,
Name:'test',
Url:'localhost'
}
constructor(public sharedService: SharedService){}

constructor(public sharedService: SharedService) {}

AddComponent() {
this.sharedService.AddItem();
AddComponent(IComponentModel: IComponentModel) {
this.sharedService.AddComponent(IComponentModel);
}
}
5 changes: 5 additions & 0 deletions src/app/interfaces/IComponent.Model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface IComponentModel{
Id:number;
Name:string;
Url:string;
}
13 changes: 7 additions & 6 deletions src/app/shared.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Injectable } from '@angular/core';
import { IComponentModel } from './interfaces/IComponent.Model';

@Injectable({
providedIn: 'root'
})

export class SharedService {
flexItems: { id: number }[] = [];
flexItems: { IComponentModel: IComponentModel }[] = [];

Remove(id: number) {
let index = this.GetId(id);
Expand All @@ -14,18 +15,18 @@ export class SharedService {
}
}

AddItem() {
AddComponent(IComponentModel: IComponentModel) {
const newId = Math.floor(Math.random() * 100) + 1;
let index = this.GetId(newId);
console.log(index);
if(index === -1){
this.flexItems.push({ id: newId });
IComponentModel.Id = newId;
this.flexItems.push({ IComponentModel: IComponentModel });
}
else
this.AddItem();
this.AddComponent(IComponentModel);
}

GetId(id: number): number{
return this.flexItems.findIndex(item => item.id == id);
return this.flexItems.findIndex(item => item.IComponentModel.Id == id);
}
}

0 comments on commit 9c44014

Please sign in to comment.