| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  | import {UIElement} from "./UIElement"; | 
					
						
							|  |  |  | import $ from "jquery" | 
					
						
							| 
									
										
										
										
											2020-07-30 09:59:30 +02:00
										 |  |  | import {UserDetails} from "../Logic/Osm/OsmConnection"; | 
					
						
							| 
									
										
										
										
											2020-07-20 15:54:50 +02:00
										 |  |  | import {DropDown} from "./Input/DropDown"; | 
					
						
							| 
									
										
										
										
											2020-06-29 03:16:34 +02:00
										 |  |  | import {VariableUiElement} from "./Base/VariableUIElement"; | 
					
						
							| 
									
										
										
										
											2020-07-21 00:07:04 +02:00
										 |  |  | import Translations from "./i18n/Translations"; | 
					
						
							| 
									
										
										
										
											2020-07-30 11:30:04 +02:00
										 |  |  | import {fail} from "assert"; | 
					
						
							|  |  |  | import Combine from "./Base/Combine"; | 
					
						
							|  |  |  | import {VerticalCombine} from "./Base/VerticalCombine"; | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  | import {State} from "../State"; | 
					
						
							| 
									
										
										
										
											2020-08-17 17:23:15 +02:00
										 |  |  | import {UIEventSource} from "../Logic/UIEventSource"; | 
					
						
							|  |  |  | import {Imgur} from "../Logic/Web/Imgur"; | 
					
						
							| 
									
										
										
										
											2020-08-25 00:10:48 +02:00
										 |  |  | import {SubtleButton} from "./Base/SubtleButton"; | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2020-07-21 22:50:54 +02:00
										 |  |  |     private _didFail: UIEventSource<boolean> = new UIEventSource<boolean>(false); | 
					
						
							| 
									
										
										
										
											2020-07-30 11:30:04 +02:00
										 |  |  |     private _allDone: UIEventSource<boolean> = new UIEventSource<boolean>(false); | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |     private _uploadOptions: (license: string) => { title: string; description: string; handleURL: (url: string) => void; allDone: (() => void) }; | 
					
						
							| 
									
										
										
										
											2020-08-25 00:10:48 +02:00
										 |  |  |     private _connectButton : UIElement; | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2020-06-27 03:06:51 +02:00
										 |  |  |     constructor( | 
					
						
							| 
									
										
										
										
											2020-07-21 22:50:54 +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
										 |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |         super(State.state.osmConnection.userDetails); | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |         this._uploadOptions = uploadOptions; | 
					
						
							|  |  |  |         this.ListenTo(this._isUploading); | 
					
						
							| 
									
										
										
										
											2020-07-21 22:50:54 +02:00
										 |  |  |         this.ListenTo(this._didFail); | 
					
						
							| 
									
										
										
										
											2020-07-30 11:30:04 +02:00
										 |  |  |         this.ListenTo(this._allDone); | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-21 01:13:51 +02:00
										 |  |  |         const licensePicker = new DropDown(Translations.t.image.willBePublished, | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |             [ | 
					
						
							| 
									
										
										
										
											2020-07-21 01:13:51 +02:00
										 |  |  |                 {value: "CC0", shown: Translations.t.image.cco}, | 
					
						
							|  |  |  |                 {value: "CC-BY-SA 4.0", shown: Translations.t.image.ccbs}, | 
					
						
							|  |  |  |                 {value: "CC-BY 4.0", shown: Translations.t.image.ccb} | 
					
						
							| 
									
										
										
										
											2020-07-01 17:38:48 +02:00
										 |  |  |             ], | 
					
						
							|  |  |  |             preferedLicense | 
					
						
							| 
									
										
										
										
											2020-08-25 00:10:48 +02:00
										 |  |  |         ); const t = Translations.t.image; | 
					
						
							|  |  |  |         | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |         this._licensePicker = licensePicker; | 
					
						
							| 
									
										
										
										
											2020-07-20 21:39:07 +02:00
										 |  |  |         this._selectedLicence = licensePicker.GetValue(); | 
					
						
							| 
									
										
										
										
											2020-08-25 00:10:48 +02:00
										 |  |  |         this._connectButton = new Combine([ t.pleaseLogin]) | 
					
						
							|  |  |  |             .onClick(() => State.state.osmConnection.AttemptLogin()) | 
					
						
							|  |  |  |             .SetClass("login-button-friendly");     | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-20 21:39:07 +02:00
										 |  |  |     InnerRender(): string { | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-30 11:30:04 +02:00
										 |  |  |         const t = Translations.t.image; | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |         if (State.state.osmConnection.userDetails === undefined) { | 
					
						
							| 
									
										
										
										
											2020-07-30 11:30:04 +02:00
										 |  |  |             return ""; // No user details -> logging in is probably disabled or smthing
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |         if (!State.state.osmConnection.userDetails.data.loggedIn) { | 
					
						
							| 
									
										
										
										
											2020-08-25 00:10:48 +02:00
										 |  |  |             return this._connectButton.Render(); | 
					
						
							| 
									
										
										
										
											2020-06-27 03:06:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-20 17:30:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-30 11:30:04 +02:00
										 |  |  |         let currentState: UIElement[] = []; | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |         if (this._isUploading.data == 1) { | 
					
						
							| 
									
										
										
										
											2020-07-30 11:30:04 +02:00
										 |  |  |             currentState.push(t.uploadingPicture); | 
					
						
							|  |  |  |         } else if (this._isUploading.data > 0) { | 
					
						
							|  |  |  |             currentState.push(t.uploadingMultiple.Subs({count: this._isUploading.data})); | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-30 11:30:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (this._didFail.data) { | 
					
						
							|  |  |  |             currentState.push(t.uploadFailed); | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-30 11:30:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (this._allDone.data) { | 
					
						
							|  |  |  |             currentState.push(t.uploadDone) | 
					
						
							| 
									
										
										
										
											2020-07-21 22:50:54 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |         let currentStateHtml = ""; | 
					
						
							|  |  |  |         if (currentState.length > 0) { | 
					
						
							| 
									
										
										
										
											2020-08-22 14:59:52 +02:00
										 |  |  |             currentStateHtml = new Combine(currentState).Render(); | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |             if (!this._allDone.data) { | 
					
						
							|  |  |  |                 currentStateHtml = "<span class='alert'>" + | 
					
						
							|  |  |  |                     currentStateHtml + | 
					
						
							|  |  |  |                     "</span>"; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-21 01:13:51 +02:00
										 |  |  |             `<span class='imageflow-add-picture'>${Translations.t.image.addPicture.R()}</span>` + | 
					
						
							| 
									
										
										
										
											2020-07-21 22:50:54 +02:00
										 |  |  |             "<div class='break'></div>" + | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |             "</div>" + | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |             currentStateHtml + | 
					
						
							| 
									
										
										
										
											2020-07-29 09:57:24 +02:00
										 |  |  |             Translations.t.image.respectPrivacy.Render() + "<br/>" + | 
					
						
							| 
									
										
										
										
											2020-07-20 17:30:02 +02:00
										 |  |  |             this._licensePicker.Render() + "<br/>" + | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |             "</label>" + | 
					
						
							| 
									
										
										
										
											2020-07-27 13:04:38 +02:00
										 |  |  |             "<form id='fileselector-form-" + this.id + "'>" + | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |             "<input id='fileselector-" + this.id + "' " + | 
					
						
							|  |  |  |             "type='file' " + | 
					
						
							|  |  |  |             "class='imageflow-file-input' " + | 
					
						
							|  |  |  |             "accept='image/*' name='picField' size='24' multiple='multiple' alt=''" + | 
					
						
							|  |  |  |             "/>" + | 
					
						
							| 
									
										
										
										
											2020-07-27 13:04:38 +02:00
										 |  |  |             "</form>" + | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |             "</div>" | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |             ; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     InnerUpdate(htmlElement: HTMLElement) { | 
					
						
							|  |  |  |         super.InnerUpdate(htmlElement); | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |         const user = State.state.osmConnection.userDetails.data; | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-27 13:04:38 +02:00
										 |  |  |         this._licensePicker.Update() | 
					
						
							|  |  |  |         const form = document.getElementById('fileselector-form-' + this.id) as HTMLFormElement | 
					
						
							|  |  |  |         const selector = document.getElementById('fileselector-' + this.id) | 
					
						
							|  |  |  |         const self = this | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         function submitHandler() { | 
					
						
							|  |  |  |             const files = $(selector).prop('files'); | 
					
						
							|  |  |  |             self._isUploading.setData(files.length); | 
					
						
							| 
									
										
										
										
											2020-07-30 11:30:04 +02:00
										 |  |  |             self._allDone.setData(false); | 
					
						
							| 
									
										
										
										
											2020-07-27 13:04:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             const opts = self._uploadOptions(self._selectedLicence.data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             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 () { | 
					
						
							| 
									
										
										
										
											2020-07-30 11:30:04 +02:00
										 |  |  |                     console.log("All uploads completed"); | 
					
						
							|  |  |  |                     self._allDone.setData(true); | 
					
						
							| 
									
										
										
										
											2020-07-27 13:04:38 +02:00
										 |  |  |                     opts.allDone(); | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |                 function(failReason) { | 
					
						
							| 
									
										
										
										
											2020-07-30 11:30:04 +02:00
										 |  |  |                     console.log("Upload failed due to ", failReason) | 
					
						
							|  |  |  |                     // No need to call something from the options -> we handle this here
 | 
					
						
							|  |  |  |                     self._didFail.setData(true); | 
					
						
							|  |  |  |                     self._isUploading.data--; | 
					
						
							|  |  |  |                     self._isUploading.ping(); | 
					
						
							| 
									
										
										
										
											2020-07-27 13:04:38 +02:00
										 |  |  |                 },0 | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-27 13:04:38 +02:00
										 |  |  |         if (selector != null && form != null) { | 
					
						
							| 
									
										
										
										
											2020-06-28 00:06:23 +02:00
										 |  |  |             selector.onchange = function () { | 
					
						
							| 
									
										
										
										
											2020-07-27 13:04:38 +02:00
										 |  |  |                 submitHandler() | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-07-27 13:04:38 +02:00
										 |  |  |             form.addEventListener('submit', e => { | 
					
						
							|  |  |  |                 console.log(e) | 
					
						
							|  |  |  |                 alert('wait') | 
					
						
							|  |  |  |                 e.preventDefault() | 
					
						
							|  |  |  |                 submitHandler() | 
					
						
							|  |  |  |             }) | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |