Browse Source

Updated upload.js (ES2015)

Tobias Reich 8 years ago
parent
commit
d4470341ff
3 changed files with 229 additions and 241 deletions
  1. 0 0
      dist/main.js
  2. 2 2
      src/scripts/sidebar.js
  3. 227 239
      src/scripts/upload.js

File diff suppressed because it is too large
+ 0 - 0
dist/main.js


+ 2 - 2
src/scripts/sidebar.js

@@ -1,6 +1,6 @@
 /**
- * @description	This module takes care of the sidebar.
- * @copyright	2015 by Tobias Reich
+ * @description This module takes care of the sidebar.
+ * @copyright   2015 by Tobias Reich
  */
 
 sidebar = {

+ 227 - 239
src/scripts/upload.js

@@ -1,6 +1,6 @@
 /**
- * @description	Takes care of every action an album can handle and execute.
- * @copyright	2015 by Tobias Reich
+ * @description Takes care of every action an album can handle and execute.
+ * @copyright   2015 by Tobias Reich
  */
 
 upload = {}
@@ -17,23 +17,21 @@ upload.show = function(title, files, callback) {
 			}
 		},
 		callback
-	});
+	})
 
 }
 
 upload.notify = function(title, text) {
 
-	var popup;
+	if (text==null||text==='') text = 'You can now manage your new photo(s).'
 
-	if (!text||text==='') text = 'You can now manage your new photo(s).';
+	if (!window.webkitNotifications) return false
 
-	if (!window.webkitNotifications) return false;
+	if (window.webkitNotifications.checkPermission()!==0) window.webkitNotifications.requestPermission()
 
-	if (window.webkitNotifications.checkPermission()!==0) window.webkitNotifications.requestPermission();
-
-	if (window.webkitNotifications.checkPermission()===0&&title) {
-		popup = window.webkitNotifications.createNotification('', title, text);
-		popup.show();
+	if (window.webkitNotifications.checkPermission()===0 && title) {
+		let popup = window.webkitNotifications.createNotification('', title, text)
+		popup.show()
 	}
 
 }
@@ -42,261 +40,258 @@ upload.start = {
 
 	local: function(files) {
 
-		var albumID	= album.getID(),
-			error 	= false,
-			warning	= false,
-			process	= function(files, file) {
+		let albumID = album.getID(),
+		    error   = false,
+		    warning = false
 
-				var formData		= new FormData(),
-					xhr				= new XMLHttpRequest(),
-					pre_progress	= 0,
-					progress		= 0,
-					finish = function() {
+		const process = function(files, file) {
 
-						window.onbeforeunload = null;
+			let formData     = new FormData(),
+				xhr          = new XMLHttpRequest(),
+				pre_progress = 0,
+				progress     = 0
 
-						$('#upload_files').val('');
+			const finish = function() {
 
-						if (error===false&&warning===false) {
+				window.onbeforeunload = null
 
-							// Success
-							basicModal.close();
-							upload.notify('Upload complete');
+				$('#upload_files').val('')
 
-						} else if (error===false&&warning===true) {
+				if (error===false && warning===false) {
 
-							// Warning
-							$('.basicModal #basicModal__action.hidden').show();
-							upload.notify('Upload complete');
+					// Success
+					basicModal.close()
+					upload.notify('Upload complete')
 
-						} else {
+				} else if (error===false && warning===true) {
 
-							// Error
-							$('.basicModal #basicModal__action.hidden').show();
-							upload.notify('Upload complete', 'Failed to upload one or more photos.');
+					// Warning
+					$('.basicModal #basicModal__action.hidden').show()
+					upload.notify('Upload complete')
 
-						}
+				} else {
 
-						albums.refresh();
+					// Error
+					$('.basicModal #basicModal__action.hidden').show()
+					upload.notify('Upload complete', 'Failed to upload one or more photos.')
 
-						if (album.getID()===false) lychee.goto('0');
-						else album.load(albumID);
+				}
 
-					};
+				albums.refresh()
 
-				// Check if file is supported
-				if (file.supported===false) {
+				if (album.getID()===false) lychee.goto('0')
+				else                       album.load(albumID)
 
-					// Skip file
-					if (file.next!==null) process(files, file.next);
-					else {
+			}
 
-						// Look for supported files
-						// If zero files are supported, hide the upload after a delay
+			// Check if file is supported
+			if (file.supported===false) {
 
-						var hasSupportedFiles = false;
+				// Skip file
+				if (file.next!==null) process(files, file.next)
+				else {
 
-						for (var i = 0; i < files.length; i++) {
+					// Look for supported files
+					// If zero files are supported, hide the upload after a delay
 
-							if (files[i].supported===true) {
-								hasSupportedFiles = true;
-								break;
-							}
+					let hasSupportedFiles = false
 
-						}
+					for (let i = 0; i < files.length; i++) {
 
-						if (hasSupportedFiles===false) setTimeout(finish, 2000);
+						if (files[i].supported===true) {
+							hasSupportedFiles = true
+							break
+						}
 
 					}
 
-					return false;
+					if (hasSupportedFiles===false) setTimeout(finish, 2000)
 
 				}
 
-				formData.append('function', 'Photo::add');
-				formData.append('albumID', albumID);
-				formData.append('tags', '');
-				formData.append(0, file);
+				return false
 
-				xhr.open('POST', api.path);
+			}
 
-				xhr.onload = function() {
+			formData.append('function', 'Photo::add')
+			formData.append('albumID', albumID)
+			formData.append('tags', '')
+			formData.append(0, file)
 
-					var wait		= false,
-						errorText	= '';
+			xhr.open('POST', api.path)
 
-					file.ready = true;
+			xhr.onload = function() {
 
-					// Set status
-					if (xhr.status===200&&xhr.responseText==='1') {
+				let wait      = false,
+				    errorText = ''
 
-						// Success
-						$('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status')
-							.html('Finished')
-							.addClass('success');
+				file.ready = true
 
-					} else {
-
-						if (xhr.responseText.substr(0, 6)==='Error:') {
+				// Set status
+				if (xhr.status===200 && xhr.responseText==='1') {
 
-							errorText = xhr.responseText.substr(6) + ' Please take a look at the console of your browser for further details.';
-							error = true;
+					// Success
+					$('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status')
+						.html('Finished')
+						.addClass('success')
 
-							// Error Status
-							$('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status')
-								.html('Failed')
-								.addClass('error');
+				} else {
 
-						} else if (xhr.responseText.substr(0, 8)==='Warning:') {
+					if (xhr.responseText.substr(0, 6)==='Error:') {
 
-							errorText = xhr.responseText.substr(8);
-							warning = true;
+						errorText = xhr.responseText.substr(6) + ' Please take a look at the console of your browser for further details.'
+						error     = true
 
-							// Warning Status
-							$('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status')
-								.html('Skipped')
-								.addClass('warning');
+						// Error Status
+						$('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status')
+							.html('Failed')
+							.addClass('error')
 
-						} else {
+					} else if (xhr.responseText.substr(0, 8)==='Warning:') {
 
-							errorText = 'Server returned an unknown response. Please take a look at the console of your browser for further details.';
-							error = true;
+						errorText = xhr.responseText.substr(8)
+						warning   = true
 
-							// Error Status
-							$('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status')
-								.html('Failed')
-								.addClass('error');
+						// Warning Status
+						$('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status')
+							.html('Skipped')
+							.addClass('warning')
 
-						}
+					} else {
 
-						$('.basicModal .rows .row:nth-child(' + (file.num+1) + ') p.notice')
-							.html(errorText)
-							.show();
+						errorText = 'Server returned an unknown response. Please take a look at the console of your browser for further details.'
+						error     = true
 
-						// Throw error
-						if (error===true) lychee.error('Upload failed. Server returned the status code ' + xhr.status + '!', xhr, xhr.responseText);
+						// Error Status
+						$('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status')
+							.html('Failed')
+							.addClass('error')
 
 					}
 
-					// Check if there are file which are not finished
-					for (var i = 0; i < files.length; i++) {
+					$('.basicModal .rows .row:nth-child(' + (file.num+1) + ') p.notice')
+						.html(errorText)
+						.show()
 
-						if (files[i].ready===false) {
-							wait = true;
-							break;
-						}
+					// Throw error
+					if (error===true) lychee.error('Upload failed. Server returned the status code ' + xhr.status + '!', xhr, xhr.responseText)
+
+				}
 
+				// Check if there are file which are not finished
+				for (let i = 0; i < files.length; i++) {
+
+					if (files[i].ready===false) {
+						wait = true
+						break
 					}
 
-					// Finish upload when all files are finished
-					if (wait===false) finish();
+				}
 
-				};
+				// Finish upload when all files are finished
+				if (wait===false) finish()
 
-				xhr.upload.onprogress = function(e) {
+			}
 
-					if (e.lengthComputable) {
+			xhr.upload.onprogress = function(e) {
 
-						// Calculate progress
-						progress = (e.loaded / e.total * 100 | 0);
+				if (e.lengthComputable!==true) return false
 
-						// Set progress when progress has changed
-						if (progress>pre_progress) {
-							$('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status').html(progress + '%');
-							pre_progress = progress;
-						}
+				// Calculate progress
+				progress = (e.loaded / e.total * 100 | 0)
 
-						if (progress>=100) {
+				// Set progress when progress has changed
+				if (progress>pre_progress) {
+					$('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status').html(progress + '%')
+					pre_progress = progress
+				}
 
-							// Scroll to the uploading file
-							var scrollPos = 0;
-							if ((file.num+1)>4) scrollPos = (file.num + 1 - 4) * 40
-							$('.basicModal .rows').scrollTop(scrollPos);
+				if (progress>=100) {
 
-							// Set status to processing
-							$('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status').html('Processing');
+					// Scroll to the uploading file
+					let scrollPos = 0
+					if ((file.num+1)>4) scrollPos = (file.num + 1 - 4) * 40
+					$('.basicModal .rows').scrollTop(scrollPos)
 
-							// Upload next file
-							if (file.next!==null) process(files, file.next);
+					// Set status to processing
+					$('.basicModal .rows .row:nth-child(' + (file.num+1) + ') .status').html('Processing')
 
-						}
+					// Upload next file
+					if (file.next!==null) process(files, file.next)
 
-					}
+				}
 
-				};
+			}
 
-				xhr.send(formData);
+			xhr.send(formData)
 
-			};
+		}
 
-		if (files.length<=0) return false;
-		if (albumID===false||visible.albums()===true) albumID = 0;
+		if (files.length<=0) return false
+		if (albumID===false || visible.albums()===true) albumID = 0
 
-		for (var i = 0; i < files.length; i++) {
+		for (let i = 0; i < files.length; i++) {
 
-			files[i].num		= i;
-			files[i].ready		= false;
-			files[i].supported	= true;
+			files[i].num       = i
+			files[i].ready     = false
+			files[i].supported = true
 
-			if (i < files.length-1)	files[i].next = files[i+1];
-			else					files[i].next = null;
+			if (i < files.length-1) files[i].next = files[i+1]
+			else                    files[i].next = null
 
 			// Check if file is supported
-			if (files[i].type!=='image/jpeg'&&files[i].type!=='image/jpg'&&files[i].type!=='image/png'&&files[i].type!=='image/gif') {
+			if (files[i].type!=='image/jpeg' && files[i].type!=='image/jpg' && files[i].type!=='image/png' && files[i].type!=='image/gif') {
 
-				files[i].ready		= true;
-				files[i].supported	= false;
+				files[i].ready     = true
+				files[i].supported = false
 
 			}
 
 		}
 
-		window.onbeforeunload = function() { return 'Lychee is currently uploading!'; };
+		window.onbeforeunload = function() { return 'Lychee is currently uploading!' }
 
 		upload.show('Uploading', files, function() {
 
 			// Upload first file
-			process(files, files[0]);
+			process(files, files[0])
 
-		});
+		})
 
 	},
 
 	url: function(url = '') {
 
-		var albumID	= album.getID(),
-			url		= (typeof url === 'string' ? url : ''),
-			action;
+		let albumID = album.getID()
 
-		if (albumID===false) albumID = 0;
+		url = (typeof url === 'string' ? url : '')
 
-		action = function(data) {
+		if (albumID===false) albumID = 0
 
-			var extension,
-				files = [];
+		const action = function(data) {
 
-			if (data.link&&data.link.length>3) {
+			let files = []
 
-				basicModal.close();
+			if (data.link && data.link.length>3) {
 
-				extension = data.link.split('.').pop();
-				if (extension!=='jpeg'&&extension!=='jpg'&&extension!=='png'&&extension!=='gif'&&extension!=='webp') {
-					loadingBar.show('error', 'File format of link not supported.');
-					return false;
+				basicModal.close()
+
+				let extension = data.link.split('.').pop()
+				if (extension!=='jpeg' && extension!=='jpg' && extension!=='png' && extension!=='gif' && extension!=='webp') {
+					loadingBar.show('error', 'File format of link not supported.')
+					return false
 				}
 
 				files[0] = {
-					name:		data.link,
-					supported:	true
+					name      : data.link,
+					supported : true
 				}
 
 				upload.show('Importing URL', files, function() {
 
-					var params;
-
-					$('.basicModal .rows .row .status').html('Importing');
+					$('.basicModal .rows .row .status').html('Importing')
 
-					params = {
+					let params = {
 						url: data.link,
 						albumID
 					}
@@ -309,41 +304,41 @@ upload.start = {
 
 							$('.basicModal .rows .row p.notice')
 								.html('The import has been finished, but returned warnings or errors. Please take a look at the log (Settings -> Show Log) for further details.')
-								.show();
+								.show()
 
 							$('.basicModal .rows .row .status')
 								.html('Finished')
-								.addClass('warning');
+								.addClass('warning')
 
 							// Show close button
-							$('.basicModal #basicModal__action.hidden').show();
+							$('.basicModal #basicModal__action.hidden').show()
 
 							// Log error
-							lychee.error(null, params, data);
+							lychee.error(null, params, data)
 
 						} else {
 
-							basicModal.close();
+							basicModal.close()
 
 						}
 
-						upload.notify('Import complete');
+						upload.notify('Import complete')
 
-						albums.refresh();
+						albums.refresh()
 
-						if (album.getID()===false) lychee.goto('0');
-						else album.load(albumID);
+						if (album.getID()===false) lychee.goto('0')
+						else                       album.load(albumID)
 
-					});
+					})
 
-				});
+				})
 
-			} else basicModal.error('link');
+			} else basicModal.error('link')
 
 		}
 
 		basicModal.show({
-			body: "<p>Please enter the direct link to a photo to import it: <input class='text' name='link' type='text' placeholder='http://' value='" + url + "'></p>",
+			body: `<p>Please enter the direct link to a photo to import it: <input class='text' name='link' type='text' placeholder='http://' value='${ url }'></p>`,
 			buttons: {
 				action: {
 					title: 'Import',
@@ -354,53 +349,49 @@ upload.start = {
 					fn: basicModal.close
 				}
 			}
-		});
+		})
 
 	},
 
 	server: function() {
 
-		var albumID = album.getID(),
-			action;
+		let albumID = album.getID()
+		if (albumID===false) albumID = 0
 
-		if (albumID===false) albumID = 0;
+		const action = function(data) {
 
-		action = function(data) {
-
-			var files = [];
+			let files = []
 
 			files[0] = {
-				name:		data.path,
-				supported:	true
-			};
+				name      : data.path,
+				supported : true
+			}
 
 			upload.show('Importing from server', files, function() {
 
-				var params;
-
-				$('.basicModal .rows .row .status').html('Importing');
+				$('.basicModal .rows .row .status').html('Importing')
 
-				params = {
+				let params = {
 					albumID,
 					path: data.path
 				}
 
 				api.post('Import::server', params, function(data) {
 
-					albums.refresh();
-					upload.notify('Import complete');
+					albums.refresh()
+					upload.notify('Import complete')
 
 					if (data==='Notice: Import only contained albums!') {
 
 						// No error, but the folder only contained albums
 
 						// Go back to the album overview to show the imported albums
-						if (visible.albums()) lychee.load();
-						else lychee.goto('');
+						if (visible.albums()) lychee.load()
+						else                  lychee.goto('')
 
-						basicModal.close();
+						basicModal.close()
 
-						return true;
+						return true
 
 					} else if (data==='Warning: Folder empty or no readable files to process!') {
 
@@ -408,14 +399,14 @@ upload.start = {
 
 						$('.basicModal .rows .row p.notice')
 							.html('Folder empty or no readable files to process. Please take a look at the log (Settings -> Show Log) for further details.')
-							.show();
+							.show()
 
 						$('.basicModal .rows .row .status')
 							.html('Failed')
-							.addClass('error');
+							.addClass('error')
 
 						// Log error
-						lychee.error('Could not start import because the folder was empty!', params, data);
+						lychee.error('Could not start import because the folder was empty!', params, data)
 
 					} else if (data!==true) {
 
@@ -423,37 +414,37 @@ upload.start = {
 
 						$('.basicModal .rows .row p.notice')
 							.html('The import has been finished, but returned warnings or errors. Please take a look at the log (Settings -> Show Log) for further details.')
-							.show();
+							.show()
 
 						$('.basicModal .rows .row .status')
 							.html('Finished')
-							.addClass('warning');
+							.addClass('warning')
 
 						// Log error
-						lychee.error(null, params, data);
+						lychee.error(null, params, data)
 
 					} else {
 
 						// No error, everything worked fine
 
-						basicModal.close();
+						basicModal.close()
 
 					}
 
-					if (album.getID()===false) lychee.goto('0');
-					else album.load(albumID);
+					if (album.getID()===false) lychee.goto('0')
+					else                       album.load(albumID)
 
 					// Show close button
-					$('.basicModal #basicModal__action.hidden').show();
+					$('.basicModal #basicModal__action.hidden').show()
 
-				});
+				})
 
-			});
+			})
 
 		}
 
 		basicModal.show({
-			body: "<p>This action will import all photos, folders and sub-folders which are located in the following directory. The <b>original files will be deleted</b> after the import when possible. <input class='text' name='path' type='text' maxlength='100' placeholder='Absolute path to directory' value='" + lychee.location + "uploads/import/'></p>",
+			body: `<p>This action will import all photos, folders and sub-folders which are located in the following directory. The <b>original files will be deleted</b> after the import when possible. <input class='text' name='path' type='text' maxlength='100' placeholder='Absolute path to directory' value='${ lychee.location }uploads/import/'></p>`,
 			buttons: {
 				action: {
 					title: 'Import',
@@ -464,41 +455,38 @@ upload.start = {
 					fn: basicModal.close
 				}
 			}
-		});
+		})
 
 	},
 
 	dropbox: function() {
 
-		var albumID = album.getID(),
-			links = '',
-			success;
+		let albumID = album.getID()
+		if (albumID===false) albumID = 0
 
-		if (albumID===false) albumID = 0;
+		const success = function(files) {
 
-		success = function(files) {
+			let links = ''
 
-			for (var i = 0; i < files.length; i++) {
+			for (let i = 0; i < files.length; i++) {
 
-				links += files[i].link + ',';
+				links += files[i].link + ','
 
 				files[i] = {
-					name:		files[i].link,
-					supported:	true
-				};
+					name      : files[i].link,
+					supported : true
+				}
 
 			}
 
 			// Remove last comma
-			links = links.substr(0, links.length-1);
+			links = links.substr(0, links.length-1)
 
 			upload.show('Importing from Dropbox', files, function() {
 
-				var params;
-
-				$('.basicModal .rows .row .status').html('Importing');
+				$('.basicModal .rows .row .status').html('Importing')
 
-				params = {
+				let params = {
 					url: links,
 					albumID
 				}
@@ -511,34 +499,34 @@ upload.start = {
 
 						$('.basicModal .rows .row p.notice')
 							.html('The import has been finished, but returned warnings or errors. Please take a look at the log (Settings -> Show Log) for further details.')
-							.show();
+							.show()
 
 						$('.basicModal .rows .row .status')
 							.html('Finished')
-							.addClass('warning');
+							.addClass('warning')
 
 						// Show close button
-						$('.basicModal #basicModal__action.hidden').show();
+						$('.basicModal #basicModal__action.hidden').show()
 
 						// Log error
-						lychee.error(null, params, data);
+						lychee.error(null, params, data)
 
 					} else {
 
-						basicModal.close();
+						basicModal.close()
 
 					}
 
-					upload.notify('Import complete');
+					upload.notify('Import complete')
 
-					albums.refresh();
+					albums.refresh()
 
-					if (album.getID()===false) lychee.goto('0');
-					else album.load(albumID);
+					if (album.getID()===false) lychee.goto('0')
+					else                       album.load(albumID)
 
-				});
+				})
 
-			});
+			})
 
 		}
 
@@ -547,8 +535,8 @@ upload.start = {
 				linkType: 'direct',
 				multiselect: true,
 				success
-			});
-		});
+			})
+		})
 
 	}
 

Some files were not shown because too many files changed in this diff