forked from jelgblad/angular2-masonry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
masonry.ts
130 lines (100 loc) · 3.42 KB
/
masonry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
declare var require: any;
declare var imagesLoaded: any;
import {
Component,
OnInit,
OnDestroy,
Input,
Output,
ElementRef,
EventEmitter,
} from '@angular/core';
// import * as masonry from 'masonry-layout';
var masonry = require('masonry-layout');
import { MasonryOptions } from './masonry-options';
@Component({
selector: '[masonry], masonry',
template: '<ng-content></ng-content>'
})
export class AngularMasonry implements OnInit, OnDestroy {
constructor(
private _element: ElementRef
) { }
public _msnry: any;
// private _imagesLoaded = null;
// Inputs
@Input() public options: MasonryOptions;
@Input() public useImagesLoaded: Boolean = false;
// Outputs
@Output() layoutComplete: EventEmitter<any[]> = new EventEmitter<any[]>();
@Output() removeComplete: EventEmitter<any[]> = new EventEmitter<any[]>();
ngOnInit() {
///TODO: How to load imagesloaded only if this.useImagesLoaded===true?
// if (this.useImagesLoaded) {
// this._imagesLoaded = require('imagesloaded');
// }
// Create masonry options object
if (!this.options) this.options = {};
// Set default itemSelector
if (!this.options.itemSelector) {
this.options.itemSelector = '[masonry-brick], masonry-brick';
}
// Set element display to block
if (this._element.nativeElement.tagName === 'MASONRY') {
this._element.nativeElement.style.display = 'block';
}
// Initialize Masonry
this._msnry = new masonry(this._element.nativeElement, this.options);
// console.log('AngularMasonry:', 'Initialized');
// Bind to events
this._msnry.on('layoutComplete', (items: any) => {
this.layoutComplete.emit(items);
});
this._msnry.on('removeComplete', (items: any) => {
this.removeComplete.emit(items);
});
}
ngOnDestroy() {
if (this._msnry) {
this._msnry.destroy();
}
}
public layout() {
setTimeout(() => {
this._msnry.layout();
});
// console.log('AngularMasonry:', 'Layout');
}
// public add(element: HTMLElement, prepend: boolean = false) {
public add(element: HTMLElement) {
var isFirstItem = false;
// Check if first item
if(this._msnry.items.length === 0){
isFirstItem = true;
}
if (this.useImagesLoaded) {
imagesLoaded(element, (instance: any) => {
this._element.nativeElement.appendChild(element);
// Tell Masonry that a child element has been added
this._msnry.appended(element);
// layout if first item
if(isFirstItem) this.layout();
});
this._element.nativeElement.removeChild(element);
}
else {
// Tell Masonry that a child element has been added
this._msnry.appended(element);
// layout if first item
if (isFirstItem) this.layout();
}
// console.log('AngularMasonry:', 'Brick added');
}
public remove(element: HTMLElement) {
// Tell Masonry that a child element has been removed
this._msnry.remove(element);
// Layout items
this.layout();
// console.log('AngularMasonry:', 'Brick removed');
}
}