2016-10-05 05:02:47 +01:00
|
|
|
function AnalyseAlbumViewModel() {
|
2016-09-24 08:17:51 +01:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.imagesFailed = ko.observableArray();
|
|
|
|
self.imagesToAnalyse = ko.observableArray();
|
2016-10-30 18:36:34 +00:00
|
|
|
self.imagesInProgress = ko.observableArray();
|
|
|
|
self.imagesRecentlyCompleted = ko.observableArray();
|
2016-09-24 08:17:51 +01:00
|
|
|
self.numberSuccessful = ko.observable(0);
|
|
|
|
self.numberFailed = ko.observable(0);
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
self.numberTotal = ko.computed(function () {
|
2016-09-24 08:17:51 +01:00
|
|
|
return self.imagesToAnalyse().length;
|
|
|
|
});
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
self.failedPercentage = ko.computed(function () {
|
2016-09-24 08:17:51 +01:00
|
|
|
return ((self.numberFailed() / self.numberTotal()) * 100).toFixed(2) + '%';
|
|
|
|
});
|
2016-10-05 05:02:47 +01:00
|
|
|
self.successfulPercentage = ko.computed(function () {
|
2016-09-24 08:17:51 +01:00
|
|
|
return ((self.numberSuccessful() / self.numberTotal()) * 100).toFixed(2) + '%';
|
|
|
|
});
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
self.isCompleted = ko.computed(function () {
|
2016-09-24 08:17:51 +01:00
|
|
|
return self.numberTotal() > 0 && (self.numberSuccessful() + self.numberFailed() >= self.numberTotal());
|
|
|
|
});
|
|
|
|
|
|
|
|
// When an image is added to the array, automatically issue it for analysis
|
2016-10-05 05:02:47 +01:00
|
|
|
self.imagesToAnalyse.subscribe(function (changes) {
|
2016-10-30 18:36:34 +00:00
|
|
|
// We only care about additions
|
|
|
|
if (changes[0].status !== 'added')
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-24 08:17:51 +01:00
|
|
|
// changes[0].value is an instance of AnalyseImageViewModel
|
|
|
|
var item = changes[0].value;
|
|
|
|
$.ajax(
|
|
|
|
item.url(),
|
|
|
|
{
|
2016-10-30 18:36:34 +00:00
|
|
|
beforeSend: function() {
|
|
|
|
self.imagesInProgress.push(item);
|
|
|
|
},
|
2016-09-24 08:17:51 +01:00
|
|
|
dataType: 'json',
|
2016-10-05 05:02:47 +01:00
|
|
|
error: function (xhr, textStatus, errorThrown) {
|
2016-09-24 08:17:51 +01:00
|
|
|
self.numberFailed(self.numberFailed() + 1);
|
|
|
|
self.imagesFailed.push({
|
|
|
|
'name': item.name(),
|
|
|
|
'reason': textStatus
|
|
|
|
});
|
|
|
|
item.isSuccessful(false);
|
|
|
|
item.isPending(false);
|
|
|
|
},
|
|
|
|
method: 'POST',
|
2016-10-05 05:02:47 +01:00
|
|
|
success: function (data) {
|
|
|
|
if (data.is_successful) {
|
2016-09-24 08:17:51 +01:00
|
|
|
self.numberSuccessful(self.numberSuccessful() + 1);
|
|
|
|
item.isSuccessful(true);
|
|
|
|
item.isPending(false);
|
2016-10-30 18:36:34 +00:00
|
|
|
|
|
|
|
// Push into our "recently completed" array
|
|
|
|
self.imagesRecentlyCompleted.push(item);
|
|
|
|
self.imagesInProgress.remove(item);
|
|
|
|
|
|
|
|
// Remove it again after a few seconds
|
|
|
|
window.setTimeout(function() {
|
|
|
|
self.imagesRecentlyCompleted.remove(item);
|
|
|
|
}, 2000);
|
2016-09-24 08:17:51 +01:00
|
|
|
}
|
2016-10-05 05:02:47 +01:00
|
|
|
else {
|
2016-09-24 08:17:51 +01:00
|
|
|
self.numberFailed(self.numberFailed() + 1);
|
|
|
|
self.imagesFailed.push({
|
|
|
|
'name': item.name(),
|
|
|
|
'reason': data.message
|
|
|
|
});
|
|
|
|
item.isSuccessful(false);
|
|
|
|
item.isPending(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}, null, 'arrayChange');
|
|
|
|
}
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
function AnalyseImageViewModel(image_info) {
|
2016-09-24 08:17:51 +01:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.isPending = ko.observable(true);
|
|
|
|
self.isSuccessful = ko.observable(false);
|
|
|
|
self.name = ko.observable(image_info.name);
|
|
|
|
self.photoID = ko.observable(image_info.photo_id);
|
|
|
|
self.url = ko.observable(image_info.url);
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
self.iconClass = ko.computed(function () {
|
2016-10-30 18:36:34 +00:00
|
|
|
var string = 'fa fa-fw fa-';
|
2016-09-24 08:17:51 +01:00
|
|
|
|
2016-10-30 18:36:34 +00:00
|
|
|
if (self.isPending()) {
|
|
|
|
string += 'refresh';
|
|
|
|
}
|
|
|
|
else {
|
2016-09-24 08:17:51 +01:00
|
|
|
string += (self.isSuccessful() ? 'check text-success' : 'times text-danger')
|
|
|
|
}
|
2016-10-05 05:02:47 +01:00
|
|
|
|
2016-09-24 08:17:51 +01:00
|
|
|
return string;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
/**
|
|
|
|
* This model is used by admin/show_album.blade.php to handle photo changes.
|
|
|
|
* @param album_id ID of the album the photos are in
|
|
|
|
* @param language Array containing language strings
|
|
|
|
* @param urls Array containing URLs
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function EditPhotosViewModel(album_id, language, urls) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.albums = ko.observableArray();
|
|
|
|
self.bulkModifyMethod = ko.observable();
|
|
|
|
self.photoIDs = ko.observableArray();
|
2016-10-30 19:10:20 +00:00
|
|
|
self.photoIDsAvailable = ko.observableArray();
|
2016-10-28 12:59:36 +01:00
|
|
|
self.isSubmitting = false;
|
2016-10-30 19:10:20 +00:00
|
|
|
self.selectAllInAlbum = ko.observable(0);
|
2016-10-05 05:02:47 +01:00
|
|
|
|
2016-10-30 19:14:00 +00:00
|
|
|
// When a photo is un-selected, remove the "select all in album" flag as the user has overridden the selection
|
|
|
|
self.photoIDs.subscribe(function (changes)
|
|
|
|
{
|
|
|
|
if (changes[0].status !== 'deleted')
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.selectAllInAlbum(0);
|
|
|
|
}, null, 'arrayChange');
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
/* Called when the Apply button on the "bulk apply selected actions" form is clicked */
|
|
|
|
self.bulkModifySelected = function()
|
|
|
|
{
|
2016-10-28 12:59:36 +01:00
|
|
|
if (self.isSubmitting)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
var bulk_form = $('form#bulk-modify-form');
|
|
|
|
|
|
|
|
if (self.bulkModifyMethod() == 'change_album')
|
|
|
|
{
|
|
|
|
// Prompt for the new album to move to
|
|
|
|
self.promptForNewAlbum(function(dialog) {
|
|
|
|
var album_id = $('select', dialog).val();
|
2016-10-28 12:59:36 +01:00
|
|
|
$('input[name="new-album-id"]', bulk_form).val(album_id);
|
|
|
|
|
|
|
|
self.isSubmitting = true;
|
|
|
|
$('button[name="bulk-apply"]', bulk_form).click();
|
|
|
|
_bt_showLoadingModal();
|
2016-10-05 05:02:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (self.bulkModifyMethod() == 'delete')
|
|
|
|
{
|
|
|
|
// Prompt for a confirmation - are you sure?!
|
|
|
|
bootbox.dialog({
|
2016-10-30 19:10:20 +00:00
|
|
|
message: language.delete_bulk_confirm_message,
|
|
|
|
title: language.delete_bulk_confirm_title,
|
2016-10-05 05:02:47 +01:00
|
|
|
buttons: {
|
|
|
|
cancel: {
|
|
|
|
label: language.action_cancel,
|
|
|
|
className: "btn-default"
|
|
|
|
},
|
|
|
|
confirm: {
|
|
|
|
label: language.action_delete,
|
|
|
|
className: "btn-danger",
|
|
|
|
callback: function() {
|
2016-10-28 12:59:36 +01:00
|
|
|
self.isSubmitting = true;
|
|
|
|
$('button[name="bulk-apply"]', bulk_form).click();
|
|
|
|
_bt_showLoadingModal();
|
2016-10-05 05:02:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-10-05 05:35:14 +01:00
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All other methods submit the form as normal
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.changeAlbum = function()
|
|
|
|
{
|
|
|
|
self.selectPhotoSingle(this);
|
|
|
|
|
2016-10-05 05:35:14 +01:00
|
|
|
var photo_id = self.photoIDs()[0];
|
|
|
|
self.photoIDs.removeAll();
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
self.promptForNewAlbum(function(dialog) {
|
|
|
|
var album_id = $('select', dialog).val();
|
2016-10-05 05:35:14 +01:00
|
|
|
$.post(urls.move_photo.replace(/\/0$/, '/' + photo_id), { 'new_album_id': album_id }, function()
|
2016-10-05 05:02:47 +01:00
|
|
|
{
|
|
|
|
window.location.reload();
|
|
|
|
});
|
2016-10-28 12:59:36 +01:00
|
|
|
_bt_showLoadingModal();
|
2016-10-05 05:02:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.delete = function() {
|
|
|
|
self.selectPhotoSingle(this);
|
|
|
|
|
2016-10-05 05:35:14 +01:00
|
|
|
var photo_id = self.photoIDs()[0];
|
|
|
|
self.photoIDs.removeAll();
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
bootbox.dialog({
|
|
|
|
message: language.delete_confirm_message,
|
|
|
|
title: language.delete_confirm_title,
|
|
|
|
buttons: {
|
|
|
|
cancel: {
|
|
|
|
label: language.action_cancel,
|
|
|
|
className: "btn-default"
|
|
|
|
},
|
|
|
|
confirm: {
|
|
|
|
label: language.action_delete,
|
|
|
|
className: "btn-danger",
|
|
|
|
callback: function() {
|
|
|
|
var url = urls.delete_photo;
|
2016-10-05 05:35:14 +01:00
|
|
|
url = url.replace(/\/0$/, '/' + photo_id);
|
2016-10-05 05:02:47 +01:00
|
|
|
|
|
|
|
$('.loading', parent).show();
|
|
|
|
|
|
|
|
$.post(url, {'_method': 'DELETE'}, function(data)
|
|
|
|
{
|
|
|
|
window.location.reload();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2016-10-05 05:35:14 +01:00
|
|
|
self.flip = function(horizontal, vertical, parent)
|
|
|
|
{
|
|
|
|
var url = urls.flip_photo;
|
|
|
|
url = url.replace('/0/', '/' + self.photoIDs()[0] + '/');
|
|
|
|
|
|
|
|
if (horizontal)
|
|
|
|
{
|
|
|
|
url = url.replace(/\/-1\//, '/1/');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
url = url.replace(/\/-1\//, '/0/');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vertical)
|
|
|
|
{
|
|
|
|
url = url.replace(/\/-2$/, '/1');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
url = url.replace(/\/-2$/, '/0');
|
|
|
|
}
|
|
|
|
|
|
|
|
$('.loading', parent).show();
|
|
|
|
$.post(url, function()
|
|
|
|
{
|
|
|
|
var image = $('img.photo-thumbnail', parent);
|
|
|
|
var originalUrl = image.data('original-src');
|
|
|
|
image.attr('src', originalUrl + "&_=" + new Date().getTime());
|
|
|
|
|
|
|
|
$('.loading', parent).hide();
|
|
|
|
});
|
|
|
|
|
|
|
|
self.photoIDs.removeAll();
|
|
|
|
};
|
|
|
|
|
|
|
|
self.flipBoth = function() {
|
|
|
|
self.selectPhotoSingle(this);
|
|
|
|
self.flip(true, true, $(this).parents('.photo'));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.flipHorizontal = function() {
|
|
|
|
self.selectPhotoSingle(this);
|
|
|
|
self.flip(true, false, $(this).parents('.photo'));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.flipVertical = function() {
|
|
|
|
self.selectPhotoSingle(this);
|
|
|
|
self.flip(false, true, $(this).parents('.photo'));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
self.promptForNewAlbum = function(callback_on_selected)
|
|
|
|
{
|
|
|
|
var albums = self.albums();
|
|
|
|
var select = $('<select/>')
|
|
|
|
.attr('name', 'album_id')
|
|
|
|
.addClass('form-control');
|
|
|
|
|
|
|
|
for (var i = 0; i < albums.length; i++)
|
|
|
|
{
|
|
|
|
var option = $('<option/>')
|
|
|
|
.attr('value', albums[i].id)
|
|
|
|
.html(albums[i].name)
|
|
|
|
.appendTo(select);
|
|
|
|
|
|
|
|
// Pre-select the current album
|
|
|
|
if (album_id == albums[i].id)
|
|
|
|
{
|
|
|
|
option.attr('selected', 'selected');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bootbox.dialog({
|
|
|
|
message: $('<p/>').html(language.change_album_message).prop('outerHTML') + select.prop('outerHTML'),
|
|
|
|
title: language.change_album_title,
|
|
|
|
buttons:
|
|
|
|
{
|
|
|
|
cancel:
|
|
|
|
{
|
|
|
|
label: language.action_cancel,
|
|
|
|
className: 'btn-default'
|
|
|
|
},
|
|
|
|
confirm:
|
|
|
|
{
|
|
|
|
label: language.action_continue,
|
|
|
|
className: 'btn-success',
|
|
|
|
callback: function()
|
|
|
|
{
|
|
|
|
callback_on_selected(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-10-05 05:35:14 +01:00
|
|
|
self.regenerateThumbnails = function()
|
|
|
|
{
|
|
|
|
self.selectPhotoSingle(this);
|
|
|
|
var parent = $(this).parents('.photo');
|
|
|
|
|
|
|
|
var url = urls.regenerate_thumbnails;
|
|
|
|
url = url.replace(/\/0$/, '/' + self.photoIDs()[0]);
|
|
|
|
|
|
|
|
$('.loading', parent).show();
|
|
|
|
$.post(url, function()
|
|
|
|
{
|
|
|
|
var image = $('img.photo-thumbnail', parent);
|
|
|
|
var originalUrl = image.data('original-src');
|
|
|
|
image.attr('src', originalUrl + "&_=" + new Date().getTime());
|
|
|
|
|
|
|
|
$('.loading', parent).hide();
|
|
|
|
});
|
|
|
|
|
|
|
|
self.photoIDs.removeAll();
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.rotate = function(angle, parent)
|
|
|
|
{
|
|
|
|
var url = urls.rotate_photo;
|
|
|
|
url = url.replace('/0/', '/' + self.photoIDs()[0] + '/');
|
|
|
|
url = url.replace(/\/1$/, '/' + angle);
|
|
|
|
|
|
|
|
$('.loading', parent).show();
|
|
|
|
$.post(url, function()
|
|
|
|
{
|
|
|
|
var image = $('img.photo-thumbnail', parent);
|
|
|
|
var originalUrl = image.data('original-src');
|
|
|
|
image.attr('src', originalUrl + "&_=" + new Date().getTime());
|
|
|
|
|
|
|
|
$('.loading', parent).hide();
|
|
|
|
self.photoIDs.removeAll();
|
|
|
|
});
|
|
|
|
|
|
|
|
self.photoIDs.removeAll();
|
|
|
|
};
|
|
|
|
|
|
|
|
self.rotateLeft = function()
|
|
|
|
{
|
|
|
|
self.selectPhotoSingle(this);
|
|
|
|
self.rotate(90, $(this).parents('.photo'));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.rotateRight = function()
|
|
|
|
{
|
|
|
|
self.selectPhotoSingle(this);
|
|
|
|
self.rotate(270, $(this).parents('.photo'));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2016-10-30 19:10:20 +00:00
|
|
|
self.selectAll = function() {
|
|
|
|
bootbox.dialog({
|
|
|
|
title: language.select_all_choice_title,
|
|
|
|
message: language.select_all_choice_message,
|
|
|
|
buttons: {
|
|
|
|
select_all: {
|
|
|
|
label: language.select_all_choice_all_action,
|
|
|
|
className: 'btn-default',
|
|
|
|
callback: function()
|
|
|
|
{
|
|
|
|
self.selectAllInAlbum(1);
|
|
|
|
for (i = 0; i < self.photoIDsAvailable().length; i++)
|
|
|
|
{
|
|
|
|
self.photoIDs.push(self.photoIDsAvailable()[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
select_visible: {
|
|
|
|
label: language.select_all_choice_visible_action,
|
|
|
|
className: 'btn-primary',
|
|
|
|
callback: function()
|
|
|
|
{
|
|
|
|
self.selectAllInAlbum(0);
|
2016-10-30 19:14:00 +00:00
|
|
|
for (photoID in self.photoIDsAvailable())
|
2016-10-30 19:10:20 +00:00
|
|
|
{
|
2016-10-30 19:14:00 +00:00
|
|
|
self.photoIDs.push(photoID);
|
2016-10-30 19:10:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.selectNone = function() {
|
|
|
|
self.photoIDs.removeAll();
|
|
|
|
self.selectAllInAlbum(0);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
self.selectPhotoSingle = function (link_item) {
|
|
|
|
// Get the photo ID from the clicked link
|
|
|
|
var parent = $(link_item).parents('.photo');
|
|
|
|
var photo_id = $(parent).data('photo-id');
|
|
|
|
|
|
|
|
// Save the photo ID
|
|
|
|
self.photoIDs.removeAll();
|
|
|
|
self.photoIDs.push(photo_id);
|
|
|
|
|
|
|
|
// Hide the dropdown
|
|
|
|
$(link_item).dropdown('toggle');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-05 11:49:39 +01:00
|
|
|
function SlideShowViewModel(required_timeout_ms) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.current = ko.observable();
|
|
|
|
self.currentIndex = ko.observable(0);
|
|
|
|
self.images = ko.observableArray();
|
|
|
|
self.interval = null;
|
|
|
|
|
|
|
|
self.isRunning = ko.observable(false);
|
|
|
|
self.isPaused = ko.observable(false);
|
|
|
|
|
|
|
|
self.changeCurrentImage = function(photo_id)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < self.images().length; i++)
|
|
|
|
{
|
|
|
|
var this_image = self.images()[i];
|
|
|
|
if (this_image.id == photo_id)
|
|
|
|
{
|
|
|
|
self.current(this_image);
|
|
|
|
self.currentIndex(i);
|
|
|
|
window.clearInterval(self.interval);
|
|
|
|
self.interval = window.setInterval(self.rotateNextImage, required_timeout_ms);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.continueSlideshow = function() {
|
|
|
|
self.isPaused(false);
|
|
|
|
self.interval = window.setInterval(self.rotateNextImage, required_timeout_ms);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.pauseSlideshow = function() {
|
|
|
|
self.isPaused(true);
|
|
|
|
window.clearInterval(self.interval);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.rotateNextImage = function() {
|
|
|
|
var next_index = self.currentIndex() + 1;
|
|
|
|
if (next_index >= self.images().length)
|
|
|
|
{
|
|
|
|
next_index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.current(self.images()[next_index]);
|
|
|
|
self.currentIndex(next_index);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.startSlideshow = function() {
|
|
|
|
if (self.images().length <= 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.interval = window.setInterval(self.rotateNextImage, required_timeout_ms);
|
|
|
|
|
|
|
|
self.current(self.images()[0]);
|
|
|
|
self.isRunning(true);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
function StorageLocationsViewModel() {
|
2016-09-24 09:34:08 +01:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.selectedLocation = ko.observable(true);
|
|
|
|
}
|
|
|
|
|
2016-09-24 08:17:51 +01:00
|
|
|
/**
|
2016-10-05 05:02:47 +01:00
|
|
|
* This model is used by admin/show_album.blade.php to handle photo uploads.
|
2016-09-24 08:17:51 +01:00
|
|
|
* @param album_id ID of the album the photos are being uploaded to
|
2016-10-28 05:30:57 +01:00
|
|
|
* @param queue_token Unique token of the upload queue to save the photos to
|
2016-09-24 08:17:51 +01:00
|
|
|
* @param language Array containing language strings
|
|
|
|
* @param urls Array containing URLs
|
|
|
|
* @constructor
|
|
|
|
*/
|
2016-10-28 05:30:57 +01:00
|
|
|
function UploadPhotosViewModel(album_id, queue_token, language, urls) {
|
2016-09-22 07:34:18 +01:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.currentStatus = ko.observable('');
|
|
|
|
self.imagesFailed = ko.observable(0);
|
|
|
|
self.imagesUploaded = ko.observable(0);
|
|
|
|
self.imagesTotal = ko.observable(0);
|
2016-09-24 08:17:51 +01:00
|
|
|
self.isBulkUploadInProgress = ko.observable(false);
|
2016-09-22 07:34:18 +01:00
|
|
|
self.isUploadInProgress = ko.observable(false);
|
|
|
|
self.statusMessages = ko.observableArray();
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
self.failedPercentage = ko.computed(function () {
|
2016-09-22 07:34:18 +01:00
|
|
|
return ((self.imagesFailed() / self.imagesTotal()) * 100).toFixed(2) + '%';
|
|
|
|
});
|
|
|
|
|
|
|
|
// This method is called when an image is uploaded - regardless if it fails or not
|
2016-10-05 05:02:47 +01:00
|
|
|
self.onUploadCompleted = function () {
|
2016-09-22 07:34:18 +01:00
|
|
|
self.currentStatus(language.upload_status
|
2016-10-05 05:02:47 +01:00
|
|
|
.replace(':current', (self.imagesUploaded() + self.imagesFailed()))
|
|
|
|
.replace(':total', self.imagesTotal()));
|
2016-09-22 07:34:18 +01:00
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
if ((self.imagesFailed() + self.imagesUploaded()) >= self.imagesTotal()) {
|
2016-09-22 07:34:18 +01:00
|
|
|
self.isUploadInProgress(false);
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
if (self.imagesFailed() == 0 && self.imagesUploaded() > 0) {
|
2016-09-22 07:34:18 +01:00
|
|
|
window.location = urls.analyse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This method is called when an uploaded image fails
|
2016-10-05 05:02:47 +01:00
|
|
|
self.onUploadFailed = function (data, file_name) {
|
2016-09-22 07:34:18 +01:00
|
|
|
self.imagesFailed(self.imagesFailed() + 1);
|
|
|
|
self.statusMessages.push({
|
|
|
|
'message_class': 'text-danger',
|
|
|
|
'message_text': language.image_failed.replace(':file_name', file_name)
|
|
|
|
});
|
|
|
|
self.onUploadCompleted();
|
|
|
|
};
|
|
|
|
|
|
|
|
// This method is called when an uploaded image succeeds
|
2016-10-05 05:02:47 +01:00
|
|
|
self.onUploadSuccessful = function (data, file_name) {
|
|
|
|
if (data.is_successful) {
|
2016-09-22 07:34:18 +01:00
|
|
|
self.imagesUploaded(self.imagesUploaded() + 1);
|
|
|
|
// Don't add to statusMessages() array so user only sees errors
|
|
|
|
/*self.statusMessages.push({
|
2016-10-05 05:02:47 +01:00
|
|
|
'message_class': 'text-success',
|
|
|
|
'message_text': language.image_uploaded.replace(':file_name', file_name)
|
|
|
|
});*/
|
2016-09-22 07:34:18 +01:00
|
|
|
self.onUploadCompleted();
|
|
|
|
}
|
2016-10-05 05:02:47 +01:00
|
|
|
else {
|
2016-09-22 07:34:18 +01:00
|
|
|
self.onUploadFailed(data, file_name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
self.startUpload = function (number_images) {
|
2016-09-22 07:34:18 +01:00
|
|
|
self.currentStatus('');
|
|
|
|
self.statusMessages.removeAll();
|
|
|
|
self.imagesUploaded(0);
|
|
|
|
self.imagesFailed(0);
|
|
|
|
self.imagesTotal(number_images);
|
|
|
|
self.isUploadInProgress(true);
|
|
|
|
};
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
self.successfulPercentage = ko.computed(function () {
|
2016-09-22 07:34:18 +01:00
|
|
|
return ((self.imagesUploaded() / self.imagesTotal()) * 100).toFixed(2) + '%';
|
|
|
|
});
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
self.uploadFile = function uploadImageFile(formObject, imageFile) {
|
2016-09-22 07:34:18 +01:00
|
|
|
var formData = new FormData();
|
|
|
|
formData.append('album_id', album_id);
|
2016-10-28 05:30:57 +01:00
|
|
|
formData.append('queue_token', queue_token);
|
2016-09-22 07:34:18 +01:00
|
|
|
formData.append('photo[]', imageFile, imageFile.name);
|
|
|
|
|
|
|
|
$.ajax(
|
|
|
|
{
|
|
|
|
contentType: false,
|
|
|
|
data: formData,
|
2016-10-05 05:02:47 +01:00
|
|
|
error: function (data) {
|
2016-09-22 07:34:18 +01:00
|
|
|
self.onUploadFailed(data, imageFile.name);
|
|
|
|
},
|
|
|
|
method: $(formObject).attr('method'),
|
|
|
|
processData: false,
|
2016-10-05 05:02:47 +01:00
|
|
|
success: function (data) {
|
2016-09-22 07:34:18 +01:00
|
|
|
self.onUploadSuccessful(data, imageFile.name);
|
|
|
|
},
|
|
|
|
url: $(formObject).attr('action')
|
|
|
|
}
|
|
|
|
);
|
|
|
|
viewModel.isUploadInProgress(true);
|
|
|
|
};
|
|
|
|
}
|