| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  | import {UIElement} from "./UIElement"; | 
					
						
							|  |  |  | import {UIEventSource} from "./UIEventSource"; | 
					
						
							|  |  |  | import $ from "jquery" | 
					
						
							|  |  |  | import {Imgur} from "../Logic/Imgur"; | 
					
						
							| 
									
										
										
										
											2020-06-27 03:06:51 +02:00
										 |  |  | import {UserDetails} from "../Logic/OsmConnection"; | 
					
						
							| 
									
										
										
										
											2020-06-29 03:16:34 +02:00
										 |  |  | import {DropDownUI} from "./Base/DropDownUI"; | 
					
						
							|  |  |  | import {VariableUiElement} from "./Base/VariableUIElement"; | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export class ImageUploadFlow extends UIElement { | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |     private _licensePicker: UIElement; | 
					
						
							|  |  |  |     private _selectedLicence: UIEventSource<string>; | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |     private _isUploading: UIEventSource<number> = new UIEventSource<number>(0) | 
					
						
							|  |  |  |     private _uploadOptions: (license: string) => { title: string; description: string; handleURL: (url: string) => void; allDone: (() => void) }; | 
					
						
							| 
									
										
										
										
											2020-06-27 03:06:51 +02:00
										 |  |  |     private _userdetails: UIEventSource<UserDetails>; | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-27 03:06:51 +02:00
										 |  |  |     constructor( | 
					
						
							|  |  |  |         userInfo: UIEventSource<UserDetails>, | 
					
						
							| 
									
										
										
										
											2020-07-01 17:38:48 +02:00
										 |  |  |         preferedLicense : UIEventSource<string>, | 
					
						
							| 
									
										
										
										
											2020-06-27 03:06:51 +02:00
										 |  |  |         uploadOptions: ((license: string) => | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 title: string, | 
					
						
							|  |  |  |                 description: string, | 
					
						
							|  |  |  |                 handleURL: ((url: string) => void), | 
					
						
							|  |  |  |                 allDone: (() => void) | 
					
						
							|  |  |  |             }) | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |     ) { | 
					
						
							|  |  |  |         super(undefined); | 
					
						
							| 
									
										
										
										
											2020-06-27 03:06:51 +02:00
										 |  |  |         this._userdetails = userInfo; | 
					
						
							|  |  |  |         this.ListenTo(userInfo); | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |         this._uploadOptions = uploadOptions; | 
					
						
							|  |  |  |         this.ListenTo(this._isUploading); | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-20 17:30:02 +02:00
										 |  |  |         const licensePicker = new DropDownUI("The picture is published ", | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |             [ | 
					
						
							| 
									
										
										
										
											2020-07-20 17:30:02 +02:00
										 |  |  |                 {value: "CC0", shown: "in the public domain"}, | 
					
						
							|  |  |  |                 {value: "CC-BY-SA 4.0", shown: "with a CC-BY-SA license"}, | 
					
						
							|  |  |  |                 {value: "CC-BY 4.0", shown: "with a CC-BY license"} | 
					
						
							| 
									
										
										
										
											2020-07-01 17:38:48 +02:00
										 |  |  |             ], | 
					
						
							|  |  |  |             preferedLicense | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |         this._licensePicker = licensePicker; | 
					
						
							|  |  |  |         this._selectedLicence = licensePicker.selectedElement; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected InnerRender(): string { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-27 03:06:51 +02:00
										 |  |  |         if (!this._userdetails.data.loggedIn) { | 
					
						
							| 
									
										
										
										
											2020-07-20 17:30:02 +02:00
										 |  |  |             return "<div class='activate-osm-authentication'>Please log in to add a picture</div>"; | 
					
						
							| 
									
										
										
										
											2020-06-27 03:06:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-20 17:30:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         let uploadingMessage = ""; | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |         if (this._isUploading.data == 1) { | 
					
						
							| 
									
										
										
										
											2020-07-20 17:30:02 +02:00
										 |  |  |             uploadingMessage = "<b>Uploading a picture...</b>" | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |         if (this._isUploading.data > 0) { | 
					
						
							| 
									
										
										
										
											2020-07-20 17:30:02 +02:00
										 |  |  |             uploadingMessage = "<b>Uploading multiple pictures, " + this._isUploading.data + " left...</b>" | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |         return "" + | 
					
						
							|  |  |  |             "<div class='imageflow'>" + | 
					
						
							| 
									
										
										
										
											2020-07-20 17:30:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |             "<label for='fileselector-" + this.id + "'>" + | 
					
						
							| 
									
										
										
										
											2020-07-20 17:30:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |             "<div class='imageflow-file-input-wrapper'>" + | 
					
						
							|  |  |  |             "<img src='./assets/camera-plus.svg' alt='upload image'/> " + | 
					
						
							| 
									
										
										
										
											2020-07-20 17:30:02 +02:00
										 |  |  |             "<span class='imageflow-add-picture'>Add a picture</span>" + | 
					
						
							|  |  |  |             "<div class='break'></div>" + | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |             "</div>" + | 
					
						
							| 
									
										
										
										
											2020-07-20 17:30:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             this._licensePicker.Render() + "<br/>" + | 
					
						
							|  |  |  |             uploadingMessage + | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |             "</label>" + | 
					
						
							|  |  |  |              | 
					
						
							|  |  |  |             "<input id='fileselector-" + this.id + "' " + | 
					
						
							|  |  |  |             "type='file' " + | 
					
						
							|  |  |  |             "class='imageflow-file-input' " + | 
					
						
							|  |  |  |             "accept='image/*' name='picField' size='24' multiple='multiple' alt=''" + | 
					
						
							|  |  |  |             "/>" + | 
					
						
							|  |  |  |              | 
					
						
							|  |  |  |             "</div>" | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |             ; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     InnerUpdate(htmlElement: HTMLElement) { | 
					
						
							|  |  |  |         super.InnerUpdate(htmlElement); | 
					
						
							| 
									
										
										
										
											2020-06-27 03:06:51 +02:00
										 |  |  |         const user = this._userdetails.data; | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         htmlElement.onclick = function () { | 
					
						
							|  |  |  |             if (!user.loggedIn) { | 
					
						
							| 
									
										
										
										
											2020-06-27 03:06:51 +02:00
										 |  |  |                 user.osmConnection.AttemptLogin(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |         this._licensePicker.Update(); | 
					
						
							|  |  |  |         const selector = document.getElementById('fileselector-' + this.id); | 
					
						
							|  |  |  |         const self = this; | 
					
						
							|  |  |  |         if (selector != null) { | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |             selector.onchange = function () { | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |                 const files = $(this).get(0).files; | 
					
						
							|  |  |  |                 self._isUploading.setData(files.length); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |                 const opts = self._uploadOptions(self._selectedLicence.data); | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 Imgur.uploadMultiple(opts.title, opts.description, files, | 
					
						
							|  |  |  |                     function (url) { | 
					
						
							|  |  |  |                         console.log("File saved at", url); | 
					
						
							|  |  |  |                         self._isUploading.setData(self._isUploading.data - 1); | 
					
						
							|  |  |  |                         opts.handleURL(url); | 
					
						
							|  |  |  |                     }, | 
					
						
							|  |  |  |                     function () { | 
					
						
							|  |  |  |                         console.log("All uploads completed") | 
					
						
							|  |  |  |                         opts.allDone(); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |