File size: 2,456 Bytes
bd98362
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
var PictureRoom = PictureRoom || {};
PictureRoom.IndexPage = function () {
    this.init();
};
PictureRoom.IndexPage.prototype = {
    CLASS: {
        dragged: 'pictureDrop__area--isDragged',
        disabled: 'isDisabled'
    },
    init: function () {
        this.setParameter();
        this.bindEvent();
        this.loadThumbnails();
    },
    setParameter: function () {
        this.$thumbnailArea = $('.js-thumbnailArea');
        this.$thumbnailTempalte = this.$thumbnailArea.children('li').detach();

        this.$thumbnailArea.html('');
        this.$loadBtn = $('.js-loadBtn');
        this.loadCount = 16;
        this.max = this.loadCount;
        this.min = 0;
    },
    bindEvent: function () {
        this.$loadBtn.on('click', $.proxy(this.loadMore, this));
    },
    loadThumbnails: function() {
        var createThumbnail = this.createThumbnail.bind(this);
        $.ajax({
            url: 'api/file?min=' + this.min + '&max=' + this.max,
            type: 'GET',
            cache: false,
            processData: false,
            contentType: false,
            timeout: 10000
        }).done(function(data){
            if (data.result) {
                createThumbnail(data.pictures);
            } else {
                console.log('upload failed...');
            }
        }).fail(function(XMLHttpRequest, textStatus, errorThrown){
            console.log(textStatus);
            console.log('upload failed...');
        });
    },
    loadMore: function(e) {
        if (e) e.preventDefault();
        this.min += this.loadCount;
        this.max += this.loadCount;
        this.loadThumbnails();
    },
    createThumbnail: function(pictures) {
        var fragment = document.createDocumentFragment();
        for(var i in pictures) {
            var $template = this.$thumbnailTempalte.clone();
            $template.children('.js-thumbnailImage').attr('src', pictures[i].thumbnailPath);
            $template.children('.js-thumbnailTitle').text(pictures[i].name);
            $template.get(0).dataset.name = pictures[i].name;
            fragment.appendChild($template.get(0));
        }
        this.$thumbnailArea.get(0).appendChild(fragment);
    }
};
$(function () {
    new PictureRoom.IndexPage();
    new PictureRoom.ImageDisplay({
        selectorDisplayTrigger: '.js-thumbnailArea > li',
        selectorImage: 'img'
    });
});