function AnalyseAlbumViewModel() { var self = this; self.imagesFailed = ko.observableArray(); self.imagesToAnalyse = ko.observableArray(); self.numberSuccessful = ko.observable(0); self.numberFailed = ko.observable(0); self.numberTotal = ko.computed(function () { return self.imagesToAnalyse().length; }); self.failedPercentage = ko.computed(function () { return ((self.numberFailed() / self.numberTotal()) * 100).toFixed(2) + '%'; }); self.successfulPercentage = ko.computed(function () { return ((self.numberSuccessful() / self.numberTotal()) * 100).toFixed(2) + '%'; }); self.isCompleted = ko.computed(function () { return self.numberTotal() > 0 && (self.numberSuccessful() + self.numberFailed() >= self.numberTotal()); }); // When an image is added to the array, automatically issue it for analysis self.imagesToAnalyse.subscribe(function (changes) { // changes[0].value is an instance of AnalyseImageViewModel var item = changes[0].value; $.ajax( item.url(), { dataType: 'json', error: function (xhr, textStatus, errorThrown) { self.numberFailed(self.numberFailed() + 1); self.imagesFailed.push({ 'name': item.name(), 'reason': textStatus }); item.isSuccessful(false); item.isPending(false); }, method: 'POST', success: function (data) { if (data.is_successful) { self.numberSuccessful(self.numberSuccessful() + 1); item.isSuccessful(true); item.isPending(false); } else { self.numberFailed(self.numberFailed() + 1); self.imagesFailed.push({ 'name': item.name(), 'reason': data.message }); item.isSuccessful(false); item.isPending(false); } } } ); }, null, 'arrayChange'); } function AnalyseImageViewModel(image_info) { 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); self.iconClass = ko.computed(function () { var string = 'fa fa-fw '; if (!self.isPending()) { string += (self.isSuccessful() ? 'check text-success' : 'times text-danger') } return string; }); } /** * 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(); /* Called when the Apply button on the "bulk apply selected actions" form is clicked */ self.bulkModifySelected = function() { 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(); $('input[name="new-album-id"]', form).val(album_id); bulk_form.submit(); }); return false; } else if (self.bulkModifyMethod() == 'delete') { // Prompt for a confirmation - are you sure?! bootbox.dialog({ message: language.delete_bulk_confirm_message.replace(':number', self.photoIDs().length), title: language.delete_bulk_confirm_title.replace(':number', self.photoIDs().length), buttons: { cancel: { label: language.action_cancel, className: "btn-default" }, confirm: { label: language.action_delete, className: "btn-danger", callback: function() { bulk_form.submit(); } } } }); return false; } // All other methods submit the form as normal return true; }; self.changeAlbum = function() { self.selectPhotoSingle(this); var photo_id = self.photoIDs()[0]; self.photoIDs.removeAll(); self.promptForNewAlbum(function(dialog) { var album_id = $('select', dialog).val(); $.post(urls.move_photo.replace(/\/0$/, '/' + photo_id), { 'new_album_id': album_id }, function() { window.location.reload(); }); }); return false; }; self.delete = function() { self.selectPhotoSingle(this); var photo_id = self.photoIDs()[0]; self.photoIDs.removeAll(); 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; url = url.replace(/\/0$/, '/' + photo_id); $('.loading', parent).show(); $.post(url, {'_method': 'DELETE'}, function(data) { window.location.reload(); }); } } } }); return false; }; 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; }; self.promptForNewAlbum = function(callback_on_selected) { var albums = self.albums(); var select = $('