/** * This model is used by admin/analyse_album.blade.php, to analyse all images. * @constructor */ function AnalyseAlbumViewModel() { this.el = '#analyse-album'; this.data = { imagesFailed: [], imagesToAnalyse: [], imagesInProgress: [], imagesRecentlyCompleted: [], numberSuccessful: 0, numberFailed: 0 }; this.computed = { failedPercentage: function () { var result = 0; if (this.numberTotal > 0) { result = (this.numberFailed / this.numberTotal) * 100; } return result.toFixed(2) + '%'; }, isCompleted: function () { return this.numberTotal > 0 && (this.numberSuccessful + this.numberFailed >= this.numberTotal); }, latestCompletedImages: function() { var startIndex = this.imagesRecentlyCompleted.length - 3 < 0 ? 0 : this.imagesRecentlyCompleted.length - 3; var endIndex = startIndex + 3; return this.imagesRecentlyCompleted.slice(startIndex, endIndex); }, numberTotal: function () { return this.imagesToAnalyse.length; }, successfulPercentage: function () { var result = 0; if (this.numberTotal > 0) { result = (this.numberSuccessful / this.numberTotal) * 100; } return result.toFixed(2) + '%'; } }; this.methods = { // This method is called when an image is added to the array, automatically issue it for analysis // item is an instance of AnalyseImageViewModel analyseImage: function (item) { var self = this; this.imagesToAnalyse.push(item); $.ajax( item.url, { beforeSend: function() { self.imagesInProgress.push(item); }, dataType: 'json', error: function (xhr, textStatus, errorThrown) { self.numberFailed++; 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++; item.isSuccessful = true; item.isPending = false; // Push into our "recently completed" array self.imagesRecentlyCompleted.push(item); var indexToRemove = self.imagesInProgress.indexOf(item); if (indexToRemove > -1) { self.imagesInProgress.splice(indexToRemove, 1); } // Remove it again after a few seconds /*window.setTimeout(function() { var indexToRemove = self.imagesRecentlyCompleted.indexOf(item); if (indexToRemove > -1) { self.imagesRecentlyCompleted.splice(indexToRemove, 1); } }, 2000);*/ } else { self.numberFailed++; self.imagesFailed.push({ 'name': item.name, 'reason': data.message }); item.isSuccessful = false; item.isPending = false; } } } ); } }; } /** * This model is used by admin/analyse_album.blade.php, as a sub-model of AnalyseAlbumViewModel. * @param image_info Array of information about the image * @constructor */ function AnalyseImageViewModel(image_info) { this.isPending = true; this.isSuccessful = false; this.name = image_info.name; this.photoID = image_info.photo_id; this.url = image_info.url; } /** * 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) { this.el = '#photos-tab'; this.data = { albums: [], bulkModifyMethod: '', isSubmitting: false, photoIDs: [], photoIDsAvailable: [], selectAllInAlbum: 0 }; // 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');*/ this.methods = { // Called when the Apply button on the "bulk apply selected actions" form is clicked bulkModifySelected: function (e) { if (this.isSubmitting) { return true; } var self = this; var bulk_form = $(e.target).closest('form'); if (this.bulkModifyMethod === 'change_album') { // Prompt for the new album to move to this.promptForNewAlbum(function (dialog) { var album_id = $('select', dialog).val(); $('input[name="new-album-id"]', bulk_form).val(album_id); self.isSubmitting = true; $('button[name="bulk-apply"]', bulk_form).click(); _bt_showLoadingModal(); }); e.preventDefault(); return false; } else if (this.bulkModifyMethod === 'delete') { // Prompt for a confirmation - are you sure?! bootbox.dialog({ message: language.delete_bulk_confirm_message, title: language.delete_bulk_confirm_title, buttons: { cancel: { label: language.action_cancel, className: "btn-secondary" }, confirm: { label: language.action_delete, className: "btn-danger", callback: function () { self.isSubmitting = true; $('button[name="bulk-apply"]', bulk_form).click(); _bt_showLoadingModal(); } } } }); e.preventDefault(); return false; } // All other methods submit the form as normal return true; }, changeAlbum: function (e) { this.selectPhotoSingle(e.target); var photo_id = this.photoIDs[0]; this.photoIDs = []; this.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(); }); //_bt_showLoadingModal(); }); e.preventDefault(); return false; }, deletePhoto: function (e) { var self = this; this.selectPhotoSingle(e.target); var photo_id = self.photoIDs[0]; this.photoIDs = []; bootbox.dialog({ message: language.delete_confirm_message, title: language.delete_confirm_title, buttons: { cancel: { label: language.action_cancel, className: "btn-secondary" }, 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(); }); } } } }); e.preventDefault(); return false; }, flip: function (horizontal, vertical, parent) { var url = urls.flip_photo; url = url.replace('/0/', '/' + this.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(); }); this.photoIDs = []; }, flipBoth: function (e) { this.selectPhotoSingle(e.target); this.flip(true, true, $(e.target).closest('.photo')); e.preventDefault(); return false; }, flipHorizontal: function (e) { this.selectPhotoSingle(e.target); this.flip(true, false, $(e.target).closest('.photo')); e.preventDefault(); return false; }, flipVertical: function (e) { this.selectPhotoSingle(e.target); this.flip(false, true, $(e.target).closest('.photo')); e.preventDefault(); return false; }, isPhotoSelected: function(photoID) { if (this.photoIDs.indexOf(photoID) > -1) { return 'checked'; } return ''; }, promptForNewAlbum: function (callback_on_selected) { var albums = this.albums; var select = $('