nm-copy-assets-popup.component
src/app/shared/widgets/apps/my-client-copy/copy-item-attributes/copy-assets-popup.component.ts
host | { |
selector | nm-copy-assets-popup.component |
templateUrl | ./copy-assets-popup.component.html |
Widget inputs |
Widget outputs |
Properties |
|
Methods |
constructor(dialogRef: MatDialogRef
|
|||||||||||||||||||||||||||
Parameters :
|
addAssetRelationToChangedAssets | ||||
addAssetRelationToChangedAssets(assetRelation: )
|
||||
Parameters :
Returns :
void
|
clearAllOtherSelectedRows | ||||||
clearAllOtherSelectedRows(index: , assetRelation: )
|
||||||
Parameters :
Returns :
void
|
clearAllSelections |
clearAllSelections()
|
Returns :
void
|
copyAssets | ||||
copyAssets(assetRelation: )
|
||||
Parameters :
Returns :
void
|
delteAssets | ||||
delteAssets(assetRelation: )
|
||||
Parameters :
Returns :
void
|
imageClick | ||||||||
imageClick(index: , asset: , assetRelation: )
|
||||||||
Parameters :
Returns :
void
|
initSourceAsset |
initSourceAsset()
|
Returns :
void
|
initTargetAsset |
initTargetAsset()
|
Returns :
void
|
keydown | ||||||
keydown(event: any)
|
||||||
Parameters :
Returns :
void
|
keyup | ||||||
keyup(event: any)
|
||||||
Parameters :
Returns :
void
|
ngAfterViewInit |
ngAfterViewInit()
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
refeshDnD |
refeshDnD()
|
Returns :
void
|
selectedAssetById | ||||||||
selectedAssetById(identifier: , side: , assetRelationIdentifer: )
|
||||||||
Parameters :
Returns :
void
|
selectRowsBetweenIndexes | ||||||
selectRowsBetweenIndexes(assetRelation: , indexes: )
|
||||||
Parameters :
Returns :
void
|
toggleRow | ||||||
toggleRow(asset: , assetRelation: )
|
||||||
Parameters :
Returns :
void
|
Public _dragAndDropService |
_dragAndDropService:
|
Type : DragAndDropService
|
Public _halService |
_halService:
|
Type : HalService
|
Public _notificationService |
_notificationService:
|
Type : CustomNotificationService
|
Public assetRelationsReturn |
assetRelationsReturn:
|
Type : any[]
|
Default value : []
|
Public assetRows |
assetRows:
|
Type : any[]
|
Default value : []
|
Public assets |
assets:
|
Type : AssetContainer[]
|
Default value : []
|
Public changedAssets |
changedAssets:
|
Type : AssetContainer[]
|
Default value : []
|
Public changedAssetsOutput |
changedAssetsOutput:
|
Default value : new Subject<any>()
|
Public cols |
cols:
|
Type : any[]
|
Public ctrlIsPressed |
ctrlIsPressed:
|
Type : boolean
|
Public data |
data:
|
Type : any
|
Decorators : Inject
|
Public dialogRef |
dialogRef:
|
Type : MatDialogRef<CopyAssetsPopupComponent>
|
Public lastSelectedAsset |
lastSelectedAsset:
|
Type : Asset
|
Public productNo |
productNo:
|
selectedAssets |
selectedAssets:
|
Type : any[]
|
Public selectedDataLocaleString |
selectedDataLocaleString:
|
Public shiftIsPressed |
shiftIsPressed:
|
Type : boolean
|
Public sourceAssetOriginales |
sourceAssetOriginales:
|
Type : AssetContainer[]
|
Default value : []
|
Public sourceAssets |
sourceAssets:
|
Type : AssetContainer[]
|
Default value : []
|
Public targetAssetOriginales |
targetAssetOriginales:
|
Type : AssetContainer[]
|
Default value : []
|
Public targetAssets |
targetAssets:
|
Type : AssetContainer[]
|
Default value : []
|
Public unsubscribe |
unsubscribe:
|
Default value : NgUnsubscribe.create()
|
import {
AfterViewInit,
Component,
EventEmitter,
Input,
NgZone,
OnDestroy,
OnInit,
Output,
Inject,
} from "@angular/core";
import { Subject, Observable, BehaviorSubject } from "rxjs";
import { WidgetComponent } from "../../../widget.metadata";
import { Asset, AssetContainer } from "../../../../components/image/index";
import { HalService } from "../../../../components/hal/hal.service";
import { MatDialogRef, MAT_DIALOG_DATA } from "@angular/material/dialog";
import { NgUnsubscribe } from "../../../../ng-unsubscribe";
import { CustomNotificationService } from "../../../../components/notification/customnotification.service";
import { DragAndDropService } from "../../../../components/util/drag-and-drop.service";
import { TranslateService } from "@ngx-translate/core";
declare var $;
declare var jQuery: any;
@WidgetComponent("nm-copy-assets-popup")
@Component({
selector: "nm-copy-assets-popup.component",
templateUrl: "./copy-assets-popup.component.html",
host: {
"(window:keydown)": "keydown($event)",
"(window:keyup)": "keyup($event)",
},
})
export class CopyAssetsPopupComponent implements OnInit, AfterViewInit {
selectedAssets: any[];
public cols: any[];
public assets: AssetContainer[] = [];
public sourceAssets: AssetContainer[] = [];
public sourceAssetOriginales: AssetContainer[] = [];
public targetAssetOriginales: AssetContainer[] = [];
public targetAssets: AssetContainer[] = [];
public changedAssets: AssetContainer[] = [];
public lastSelectedAsset: Asset;
public assetRelationsReturn: any[] = [];
public assetRows: any[] = [];
public unsubscribe = NgUnsubscribe.create();
public productNo;
public selectedDataLocaleString;
public shiftIsPressed: boolean;
public ctrlIsPressed: boolean;
public changedAssetsOutput = new Subject<any>();
constructor(
public dialogRef: MatDialogRef<CopyAssetsPopupComponent>,
@Inject(MAT_DIALOG_DATA) public data: any,
private halService: HalService,
private zone: NgZone,
public _halService: HalService,
public _notificationService: CustomNotificationService,
public _dragAndDropService: DragAndDropService,
private translateService: TranslateService
) {}
ngOnInit() {
if (
this.data.selectedRowsTarget._embedded &&
this.data.selectedRowsTarget._embedded.assets.length > 0
) {
this.targetAssetOriginales = this.data.selectedRowsTarget._embedded.assets;
this.initTargetAsset();
}
}
ngAfterViewInit(): void {}
initSourceAsset() {
if (
this.data.selectedRowsSource[0]._embedded &&
this.data.selectedRowsSource[0]._embedded.assets.length > 0
) {
this.sourceAssetOriginales = this.data.selectedRowsSource[0]._embedded.assets;
}
this.sourceAssets = [];
this.sourceAssets = jQuery.extend(true, [], this.sourceAssetOriginales);
for (let assetRelationRow of this.assetRows) {
let matchedAttribute = this.sourceAssets.filter(
(attribute) => attribute.identifier === assetRelationRow["identifier"]
);
if (matchedAttribute.length > 0) {
assetRelationRow["sourceAssetRelation"] = matchedAttribute[0];
this.sourceAssets.splice(
this.sourceAssets.indexOf(matchedAttribute[0]),
1
);
}
}
for (const assetRelation of this.sourceAssets) {
let assetRelationrow = {};
assetRelationrow["identifier"] = assetRelation.identifier;
assetRelationrow["description"] = assetRelation.description;
assetRelationrow["targetAssetRelation"] = null;
assetRelationrow["sourceAssetRelation"] = assetRelation;
this.assetRows.push(assetRelationrow);
}
}
initTargetAsset() {
this.assetRows = [];
this.targetAssets = jQuery.extend(true, [], this.targetAssetOriginales);
for (let assetRelation of this.targetAssets) {
if (assetRelation._embedded === undefined) {
assetRelation._embedded = {};
assetRelation._embedded.assets = [];
}
assetRelation._embedded.assets.sort(function (a, b) {
return a["sequence"] - b["sequence"];
});
for (let asset of assetRelation._embedded.assets) {
asset["assetRelationIdentifier"] = assetRelation.identifier;
}
}
for (const assetRelation of this.targetAssets) {
let assetRelationrow = {};
assetRelationrow["identifier"] = assetRelation.identifier;
assetRelationrow["description"] = assetRelation.description;
assetRelationrow["targetAssetRelation"] = assetRelation;
assetRelationrow["sourceAssetRelation"] = null;
this.assetRows.push(assetRelationrow);
}
if (this.data.selectedRowsSource[0]) {
this.initSourceAsset();
}
this.changedAssets = [];
this.changedAssetsOutput.next(this.changedAssets);
this.refeshDnD();
}
imageClick(index, asset, assetRelation) {
if (this.ctrlIsPressed) {
this.toggleRow(asset, assetRelation);
}
if (this.shiftIsPressed) {
this.selectRowsBetweenIndexes(assetRelation, [
assetRelation._embedded["assets"].indexOf(this.lastSelectedAsset),
index,
]);
}
if (!this.ctrlIsPressed && !this.shiftIsPressed) {
this.clearAllOtherSelectedRows(index, assetRelation);
this.toggleRow(asset, assetRelation);
}
}
selectedAssetById(identifier, side, assetRelationIdentifer) {
this.clearAllSelections();
for (let assetRelationRow of this.assetRows) {
if (assetRelationRow.identifier == assetRelationIdentifer) {
for (let asset of assetRelationRow[side]["_embedded"]["assets"]) {
if (asset.title === identifier) {
asset["selected"] = true;
this.selectedAssets.push(asset);
}
}
}
}
}
clearAllOtherSelectedRows(index, assetRelation) {
this.selectedAssets = [];
for (var asset of assetRelation._embedded["assets"]) {
if (!(index === assetRelation._embedded["assets"].indexOf(asset)))
asset["selected"] = false;
}
}
clearAllSelections() {
this.selectedAssets = [];
if (this.sourceAssets.length > 0) {
for (let assetRelation of this.sourceAssets) {
for (let asset of assetRelation._embedded.assets) {
asset["selected"] = false;
}
}
}
if (this.targetAssets.length > 0) {
for (let assetRelation of this.targetAssets) {
for (let asset of assetRelation._embedded.assets) {
asset["selected"] = false;
}
}
}
}
selectRowsBetweenIndexes(assetRelation, indexes) {
indexes.sort(function (a, b) {
return a - b;
});
for (var i = indexes[0]; i <= indexes[1]; i++) {
assetRelation._embedded["assets"][i]["selected"] = true;
}
this.selectedAssets = assetRelation._embedded["assets"].filter(
(row) => row["selected"] == true
);
}
toggleRow(asset, assetRelation) {
asset["selected"] = !asset["selected"];
this.selectedAssets = assetRelation._embedded["assets"].filter(
(row) => row["selected"] == true
);
this.lastSelectedAsset = asset;
}
keydown(event: any) {
if (event.keyCode === 16) {
this.shiftIsPressed = true;
}
if (event.keyCode === 17) {
this.ctrlIsPressed = true;
}
}
keyup(event: any) {
if (event.keyCode === 16) {
this.shiftIsPressed = false;
}
if (event.keyCode === 17) {
this.ctrlIsPressed = false;
}
}
copyAssets(assetRelation) {
assetRelation["replaced"] = true;
for (let assetRelationRow of this.assetRows) {
if (assetRelationRow.identifier == assetRelation.identifier) {
assetRelationRow["targetAssetRelation"] = jQuery.extend(
true,
[],
assetRelation
);
}
}
this.addAssetRelationToChangedAssets(assetRelation);
this.refeshDnD();
}
delteAssets(assetRelation) {
assetRelation._embedded.assets = [];
assetRelation["replaced"] = true;
this.addAssetRelationToChangedAssets(assetRelation);
}
refeshDnD(): void {
let self = this;
this.zone.runOutsideAngular(() => {
setTimeout(() => {
let sourcecontainer = jQuery(
"nm-copy-assets-popup ul.source-asset-column > li"
);
let targetcontainer = jQuery(
"nm-copy-assets-popup ul.target-asset-column"
);
jQuery(targetcontainer).sortable({
revert: false,
tolerance: "touch",
placeholder: "ui-state-asset-placeholder",
cursor: "grabbing",
delay: 1,
refreshPositions: true,
helper: function (e, item) {
let selected = jQuery(this)
.closest("ul.target-asset-column")
.find(".ui-state-selected");
if (selected.length === 0) {
self.selectedAssetById(
item.context.id,
"targetAssetRelation",
jQuery(this).data("ari")
);
selected = item;
}
var container = jQuery("<div/>").attr(
"id",
"draggingContainerImage"
);
container.append(selected.clone());
item.siblings(".ui-state-selected").addClass("hidden");
return container;
},
start: function (event, ui) {
var elements = ui.item
.siblings(".selected.hidden")
.not(".ui-sortable-placeholder");
self._dragAndDropService.registerDrag(
"target-product-assets",
jQuery.extend(true, [], self.selectedAssets)
);
},
stop: function (e, ui) {
let taegetAssetRelationIdentifier = jQuery(e.target).attr(
"data-ari"
);
let dragged = self._dragAndDropService.completeDrag();
let draggedAssets = <Asset[]>dragged.data;
let prev = jQuery(ui.item[0]).prev();
let prevId = null;
if (prev.length > 0) {
prevId = jQuery(prev[0]).attr("id").trim();
}
ui.placeholder.remove();
if (ui.helper) {
ui.helper.remove();
}
ui.item.siblings(".hidden").remove();
if (!dragged) {
return;
}
if (draggedAssets[0] === undefined) {
return;
}
if (ui.item && dragged) {
ui.item.remove();
}
let assetRelationIdentifier =
draggedAssets[0]["assetRelationIdentifier"];
let titles: Set<string> = new Set<string>(
draggedAssets.map((item) => item.title)
);
for (let assetRelation of self.targetAssets) {
if (taegetAssetRelationIdentifier === assetRelation.identifier) {
//let result = Object.assign({}, assetRelation);
let assets = assetRelation._embedded["assets"].filter(
(item) => !titles.has(item.title)
);
let assetCount = assets.length;
if (
assetCount < assetRelation._embedded["assets"].length &&
dragged.source === "source-product-assets"
) {
self._notificationService.create(
self.translateService.instant(
"app.ipim-my-client-copy.asset-already-available.title"
),
self.translateService.instant(
"app.ipim-my-client-copy.asset-already-available.message"
),
"ERROR"
);
}
let index = !!prevId
? assets.findIndex((item) => item.title == prevId) + 1
: 0;
assets.splice(index, 0, ...draggedAssets);
assetRelation._embedded["assets"] = assets;
if (assetRelation._embedded["assets"].length === 0) {
assets.splice(index, 0, ...draggedAssets);
}
self.addAssetRelationToChangedAssets(assetRelation);
}
}
jQuery(".nm-attribute-list-asset.hidden").remove();
jQuery(targetcontainer).sortable("refresh");
jQuery(targetcontainer).sortable("refreshPositions");
jQuery(prev[0]).click();
jQuery("li").each(function () {
jQuery(this).removeClass("ui-state-selected");
});
self.zone.run(() => {
// self._changeDetectorRef.markForCheck();
});
},
});
let selected;
jQuery(sourcecontainer).draggable({
connectToSortable: "ul.target-asset-column",
helper: function (e) {
if (
jQuery(this)
.closest("ul.source-asset-column")
.find(".ui-state-selected").length === 0
) {
self.selectedAssetById(
e["currentTarget"]["id"],
"sourceAssetRelation",
jQuery(this).data("ari")
);
}
selected = jQuery(this)
.closest("ul.source-asset-column")
.find(".ui-state-selected");
if (selected.length === 0) {
selected = jQuery(this);
}
var container = jQuery("<div/>").attr(
"id",
"draggingContainerImage"
);
container.append(selected.clone());
return container;
},
revert: "invalid",
tolerance: "touch",
start: function (event, ui) {
self._dragAndDropService.registerDrag(
"source-product-assets",
jQuery.extend(true, [], self.selectedAssets)
);
},
});
// jQuery(sourcecontainer).draggable('instance').scssPosition = 'absolute';
jQuery("ul, li").disableSelection();
this.lastSelectedAsset = null;
this.selectedAssets = [];
}, 1);
});
// this._changeDetectorRef.markForCheck();
}
addAssetRelationToChangedAssets(assetRelation) {
this.assetRelationsReturn.push(assetRelation);
}
}
<nm-dialog
[dialogRef]="dialogRef"
class="nm-dialog"
style="max-width: 1200px; width: 1200px; min-width: 1200px"
>
<ng-container slot="title">
{{ "app.ipim-my-client-copy.placeholder.itemAssetes" | translate }}
</ng-container>
<ng-container slot="content">
<div class="nm-attribute-list">
<div *ngFor="let assetRelationRow of assetRows; let i = index">
<!-- [trackBy]="trackByFn"-->
<div class="nm-attribute-list-elements">
<div class="nm-attribute-list-description">
{{ assetRelationRow.description }}
</div>
<div
class="nm-attribute-list-value nm-attribute-list-assets asset-source-column"
>
<ul class="source-asset-column">
<li
*ngFor="
let asset of assetRelationRow['sourceAssetRelation']
?._embedded.assets;
let k = index
"
[attr.data-ari]="
assetRelationRow['sourceAssetRelation']?.identifier
"
[attr.id]="asset.title"
class="nm-attribute-list-asset"
(click)="
imageClick(k, asset, assetRelationRow['sourceAssetRelation'])
"
[class.ui-state-selected]="asset.selected === true"
>
<nm-image
[assetObj]="asset"
size="thumbnail"
[pTooltip]="asset.title"
[escape]="false"
*ngIf="
assetRelationRow['sourceAssetRelation']?.identifier ===
'pdf'
"
width="auto"
height="auto"
max-width="75px"
max-height="80px"
></nm-image>
<nm-image
[assetObj]="asset"
size="thumbnail"
*ngIf="
assetRelationRow['sourceAssetRelation']?.identifier !==
'pdf'
"
width="auto"
height="auto"
max-width="80px"
max-height="80px"
></nm-image>
</li>
</ul>
<button
class="nm-attribute-action-button"
*ngIf="
assetRelationRow['sourceAssetRelation']?._embedded?.assets
.length > 0
"
mat-icon-button
(click)="copyAssets(assetRelationRow['sourceAssetRelation'])"
>
<mat-icon color="primary">content_copy</mat-icon>
</button>
</div>
<div
class="nm-attribute-list-value nm-attribute-list-assets asset-target-column"
>
<ul
class="target-asset-column"
*ngIf="targetAssets?.length > 0"
[attr.data-ari]="
assetRelationRow['targetAssetRelation']?.identifier
"
>
<li
*ngFor="
let asset of assetRelationRow['targetAssetRelation']
?._embedded.assets;
let j = index
"
[attr.data-ari]="
assetRelationRow['targetAssetRelation']?.identifier
"
[attr.id]="asset.title"
class="nm-attribute-list-asset"
(click)="
imageClick(j, asset, assetRelationRow['targetAssetRelation'])
"
[class.ui-state-selected]="asset.selected === true"
>
<nm-image
[assetObj]="asset"
size="thumbnail"
[pTooltip]="asset.title"
[escape]="false"
*ngIf="
assetRelationRow['targetAssetRelation']?.identifier ===
'pdf'
"
width="auto"
height="auto"
max-width="75px"
max-height="80px"
></nm-image>
<nm-image
[assetObj]="asset"
size="thumbnail"
*ngIf="
assetRelationRow['targetAssetRelation']?.identifier !==
'pdf'
"
width="auto"
height="auto"
max-width="80px"
max-height="80px"
></nm-image>
</li>
</ul>
<button
class="nm-attribute-action-button"
*ngIf="
assetRelationRow['targetAssetRelation']?._embedded?.assets
.length > 0
"
mat-icon-button
(click)="delteAssets(assetRelationRow['targetAssetRelation'])"
>
<mat-icon color="primary">clear</mat-icon>
</button>
</div>
</div>
</div>
<hr />
</div>
<div
class="upload-button-container"
style="float: right; margin-bottom: 15px; margin-right: 10px"
>
<button
mat-raised-button
type="button"
class="btn-orange"
(click)="dialogRef.close(assetRelationsReturn)"
>
{{ "app.ipim-my-client-copy.apply" | translate }}
</button>
<button
mat-raised-button
type="button"
class="btn-black"
style="margin-left: 10px !important; left: 10px"
(click)="dialogRef.close(false)"
>
{{ "button.cancel" | translate }}
</button>
</div>
</ng-container>
</nm-dialog>