image-manager.js
3.91 KB
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
window.ImageManager = new Vue({
el: '#image-manager',
data: {
images: [],
hasMore: false,
page: 0,
cClickTime: 0,
selectedImage: false,
dependantPages: false,
deleteForm: {}
},
created: function () {
// Get initial images
this.fetchData(this.page);
},
ready: function () {
// Create dropzone
this.setupDropZone();
},
methods: {
fetchData: function () {
var _this = this;
this.$http.get('/images/all/' + _this.page, function (data) {
_this.images = _this.images.concat(data.images);
_this.hasMore = data.hasMore;
_this.page++;
});
},
setupDropZone: function () {
var _this = this;
var dropZone = new Dropzone(_this.$$.dropZone, {
url: '/upload/image',
init: function () {
var dz = this;
this.on("sending", function (file, xhr, data) {
data.append("_token", document.querySelector('meta[name=token]').getAttribute('content'));
});
this.on("success", function (file, data) {
_this.images.unshift(data);
$(file.previewElement).fadeOut(400, function () {
dz.removeFile(file);
});
});
}
});
},
imageClick: function (image) {
var dblClickTime = 380;
var cTime = (new Date()).getTime();
var timeDiff = cTime - this.cClickTime;
if (this.cClickTime !== 0 && timeDiff < dblClickTime && this.selectedImage === image) {
// DoubleClick
if (this.callback) {
this.callback(image);
}
this.hide();
} else {
this.selectedImage = (this.selectedImage === image) ? false : image;
this.dependantPages = false;
}
this.cClickTime = cTime;
},
selectButtonClick: function () {
if (this.callback) {
this.callback(this.selectedImage);
}
this.hide();
},
show: function (callback) {
this.callback = callback;
this.$$.overlay.style.display = 'block';
},
overlayClick: function (e) {
if (e.target.className === 'overlay') {
this.hide();
}
},
hide: function () {
this.$$.overlay.style.display = 'none';
},
saveImageDetails: function (e) {
e.preventDefault();
var _this = this;
var form = $(_this.$$.imageForm);
$.ajax('/images/update/' + _this.selectedImage.id, {
method: 'PUT',
data: form.serialize()
}).done(function () {
form.showSuccess('Image name updated');
}).fail(function (jqXHR) {
form.showFailure(jqXHR.responseJSON);
})
},
deleteImage: function (e) {
e.preventDefault();
var _this = this;
_this.deleteForm.force = _this.dependantPages !== false;
$.ajax('/images/' + _this.selectedImage.id, {
method: 'DELETE',
data: _this.deleteForm
}).done(function () {
_this.images.splice(_this.images.indexOf(_this.selectedImage), 1);
_this.selectedImage = false;
$(_this.$$.imageTitle).showSuccess('Image Deleted');
}).fail(function(jqXHR, textStatus) {
// Pages failure
if(jqXHR.status === 400) {
_this.dependantPages = jqXHR.responseJSON;
}
});
}
}
});