BookStack/resources/assets/js/image-manager.js

154 lines
4.9 KiB
JavaScript
Raw Normal View History

2015-08-12 13:48:26 -04:00
2015-08-15 19:18:22 -04:00
jQuery.fn.showSuccess = function(message) {
var elem = $(this);
var success = $('<div class="text-pos" style="display:none;"><i class="zmdi zmdi-check-circle"></i>'+message+'</div>');
elem.after(success);
success.slideDown(400, function() {
setTimeout(function() {success.slideUp(400, function() {
success.remove();
})}, 2000);
});
};
jQuery.fn.showFailure = function(messageMap) {
var elem = $(this);
$.each(messageMap, function(key, messages) {
var input = elem.find('[name="'+key+'"]').last();
var fail = $('<div class="text-neg" style="display:none;"><i class="zmdi zmdi-alert-circle"></i>'+messages.join("\n")+'</div>');
input.after(fail);
fail.slideDown(400, function() {
setTimeout(function() {fail.slideUp(400, function() {
fail.remove();
})}, 2000);
});
});
};
2015-08-12 18:42:42 -04:00
(function() {
2015-08-13 17:15:56 -04:00
var ImageManager = new Vue({
2015-08-12 18:42:42 -04:00
2015-08-13 17:15:56 -04:00
el: '#image-manager',
2015-08-12 18:42:42 -04:00
2015-08-13 17:15:56 -04:00
data: {
images: [],
hasMore: false,
page: 0,
2015-08-15 06:54:46 -04:00
cClickTime: 0,
selectedImage: false
2015-08-13 17:15:56 -04:00
},
2015-08-12 18:42:42 -04:00
2015-08-13 17:15:56 -04:00
created: function() {
// Get initial images
this.fetchData(this.page);
},
2015-08-12 18:42:42 -04:00
2015-08-13 17:15:56 -04:00
ready: function() {
2015-08-12 18:42:42 -04:00
// Create dropzone
2015-08-13 17:15:56 -04:00
this.setupDropZone();
},
methods: {
fetchData: function() {
var _this = this;
$.getJSON('/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'));
2015-08-12 18:42:42 -04:00
});
2015-08-13 17:15:56 -04:00
this.on("success", function(file, data) {
_this.images.unshift(data);
$(file.previewElement).fadeOut(400, function() {
dz.removeFile(file);
});
});
}
2015-08-12 18:42:42 -04:00
});
2015-08-13 17:15:56 -04:00
},
imageClick: function(image) {
var dblClickTime = 380;
var cTime = (new Date()).getTime();
var timeDiff = cTime - this.cClickTime;
2015-08-15 19:18:22 -04:00
if(this.cClickTime !== 0 && timeDiff < dblClickTime && this.selectedImage === image) {
2015-08-13 17:15:56 -04:00
// DoubleClick
if(this.callback) {
this.callback(image);
}
this.hide();
} else {
2015-08-15 06:54:46 -04:00
this.selectedImage = (this.selectedImage===image) ? false : image;
2015-08-13 17:15:56 -04:00
}
this.cClickTime = cTime;
},
2015-08-12 13:48:26 -04:00
2015-08-15 19:18:22 -04:00
selectButtonClick: function() {
if(this.callback) {
this.callback(this.selectedImage);
}
this.hide();
},
2015-08-13 17:15:56 -04:00
show: function(callback) {
this.callback = callback;
this.$$.overlay.style.display = 'block';
},
2015-08-13 02:44:10 -04:00
2015-08-15 06:54:46 -04:00
overlayClick: function(e) {
if(e.target.className==='overlay') {
this.hide();
}
},
2015-08-13 17:15:56 -04:00
hide: function() {
this.$$.overlay.style.display = 'none';
2015-08-15 19:18:22 -04:00
},
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;
var form = $(_this.$$.imageDeleteForm);
$.ajax('/images/' + _this.selectedImage.id, {
method: 'DELETE',
data: form.serialize()
}).done(function() {
_this.images.splice(_this.images.indexOf(_this.selectedImage), 1);
_this.selectedImage = false;
$(_this.$$.imageTitle).showSuccess('Image Deleted');
})
2015-08-13 02:44:10 -04:00
}
}
2015-08-13 17:15:56 -04:00
});
2015-08-12 13:48:26 -04:00
2015-08-13 17:15:56 -04:00
window.ImageManager = ImageManager;
2015-08-12 18:42:42 -04:00
2015-08-13 17:15:56 -04:00
})();