Project

General

Profile

1
import { $ } from '../util/index';
2

    
3
export default function (UIkit) {
4

    
5
    UIkit.component('margin', {
6

    
7
        props: {
8
            margin: String,
9
            firstColumn: Boolean
10
        },
11

    
12
        defaults: {
13
            margin: 'uk-margin-small-top',
14
            firstColumn: 'uk-first-column'
15
        },
16

    
17
        update: {
18

    
19
            read() {
20

    
21
                if (this.$el[0].offsetHeight === 0) {
22
                    this.hidden = true;
23
                    return;
24
                }
25

    
26
                this.hidden = false;
27
                this.stacks = true;
28

    
29
                var columns = this.$el.children().filter((_, el) => el.offsetHeight > 0);
30

    
31
                this.rows = [[columns.get(0)]];
32

    
33
                columns.slice(1).each((_, el) => {
34

    
35
                    var top = Math.ceil(el.offsetTop), bottom = top + el.offsetHeight;
36

    
37
                    for (var index = this.rows.length - 1; index >= 0; index--) {
38
                        var row = this.rows[index], rowTop = Math.ceil(row[0].offsetTop);
39

    
40
                        if (top >= rowTop + row[0].offsetHeight) {
41
                            this.rows.push([el]);
42
                            break;
43
                        }
44

    
45
                        if (bottom > rowTop) {
46

    
47
                            this.stacks = false;
48

    
49
                            if (el.offsetLeft < row[0].offsetLeft) {
50
                                row.unshift(el);
51
                                break;
52
                            }
53

    
54
                            row.push(el);
55
                            break;
56
                        }
57

    
58
                        if (index === 0) {
59
                            this.rows.splice(index, 0, [el]);
60
                            break;
61
                        }
62

    
63
                    }
64

    
65
                });
66

    
67
            },
68

    
69
            write() {
70

    
71
                if (this.hidden) {
72
                    return;
73
                }
74

    
75
                this.rows.forEach((row, i) =>
76
                    row.forEach((el, j) =>
77
                        $(el)
78
                            .toggleClass(this.margin, i !== 0)
79
                            .toggleClass(this.firstColumn, j === 0)
80
                    )
81
                )
82

    
83
            },
84

    
85
            events: ['load', 'resize']
86

    
87
        }
88

    
89
    });
90

    
91
}
(15-15/28)