File size: 2,952 Bytes
e1d589b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
define(function () {
    return {
        
        /**
         * Elements
         */
        $button: null,
        $wrapper: null,
        $content: null,

        /**
         * Variables
         */
        active: 8,
       
        /**
         * Triggers
         */
        loadPosts(event) {
            var
            self = this,
            $this = $(this);

            $this.addClass("loading");

            /**
             * Ajax
             */
            $.post(
                wordpress.ajaxurl, 
                {
                    action: 'load_older_posts',
                    data: {
                        active: Main.initLoadMorePosts.active   
                    },
                },
                function(output) {
                    window.setTimeout(function() {

                        var
                        self = Main.initLoadMorePosts;
                        
                        /* test-code */
                        console.log(output);
                        /* end-test-code */

                        // More posts found
                        if (output) 
                        {
                            self.$content.append(output);

                            self.$wrapper
                            .css('height', self.$content[0].scrollHeight)
                            .one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', () => {
                                
                                if (self.active >= 9) {
                                    self.$wrapper.addClass("active");
                                }

                                self.$wrapper.css('height', '');
                            });

                            $this.removeClass("loading");

                            self.active += 9;
                        } 

                        // No more posts to load
                        if (!output || self.active >= wordpress.posts.count) 
                        {
                            $this.addClass("disable");
                        }
                        
                    }, 1000);
                }
            );

            event.preventDefault();
        },

        /**
         * Binds
         */
        bindButton($element = Main.initLoadMorePosts.$button) {
            $element.on("click", this.loadPosts);
        },
        unbindButton($element = Main.initLoadMorePosts.$button) {
            $element.off("click", this.loadPosts);
        },

        /**
         * Start
         */
        start() {
            var self = this;
            self.$button = $('#load-older-posts__button');

            if (self.$button.length) {

                self.$wrapper = $("#load-older-posts__wrapper");
                self.$content = $("#load-older-posts__content");

                self.bindButton(self.$button);
            }
        }
        
    }
});