(function(){Djblets.Avatars={};"use strict";Djblets.Avatars.Settings=Backbone.Model.extend({defaults(){return{configuration:{},serviceID:null,services:{}}}});"use strict";Djblets.Avatars.ServiceSettingsFormView=Backbone.View.extend({validate(){return true}});"use strict";(function(){const[readyPromise,resolve]=Promise.withResolver();Djblets.Avatars.SettingsFormView=Backbone.View.extend({events:{"change #id_avatar_service_id":"_onServiceChanged",submit:"_onSubmit"},initialize(){console.assert(Djblets.Avatars.SettingsFormView.instance===null);Djblets.Avatars.SettingsFormView.instance=this;this._configForms=new Map;this._$config=this.$(".avatar-service-configuration");const services=this.model.get("services");this.listenTo(this.model,"change:serviceID",()=>this._showHideForms());resolve()},_onSubmit(e){const serviceID=this.model.get("serviceID");const currentForm=this._configForms.get(serviceID);if(currentForm&&!currentForm.validate()){e.preventDefault()}},renderForms(){for(const form of this._configForms.values()){form.render()}this.$("#id_avatar_service_id").change();this._showHideForms(true);return this},_showHideForms(){const services=this.model.get("services");const serviceID=this.model.get("serviceID");const currentForm=this._configForms.get(serviceID);const previousID=this.model.previous("serviceID");const previousForm=previousID?this._configForms.get(previousID):undefined;if(previousForm&&currentForm){previousForm.$el.hide();currentForm.$el.show()}else if(previousForm){previousForm.$el.hide();this._$config.hide()}else if(currentForm){currentForm.$el.show();this._$config.show()}},_onServiceChanged(e){const $target=$(e.target);this.model.set("serviceID",$target.val())}},{instance:null,addConfigForm(serviceID,formClass){Djblets.Avatars.SettingsFormView.instance._configForms.set(serviceID,new formClass({el:$(`[data-avatar-service-id="${serviceID}"]`),model:Djblets.Avatars.SettingsFormView.instance.model}))},ready:readyPromise})})();"use strict";(function(){const allowedMimeTypes=["image/png","image/jpeg","image/gif"];const ParentView=Djblets.Avatars.ServiceSettingsFormView;Djblets.Avatars.FileUploadSettingsFormView=ParentView.extend({events:{"change #id_file-upload-avatar_upload":"_onFileChanged","click .avatar-file-upload-browse":"_onBrowseClicked","click .avatar-preview":"_onBrowseClicked","dragenter .avatar-file-upload-config":"_onDragEnter","dragover .avatar-file-upload-config":"_onDragOver","dragleave .avatar-file-upload-config":"_onDragLeave","drop .avatar-file-upload-config":"_onDrop"},validate(){const file=this._$fileInput[0].files[0];if(!file){alert(gettext("You must choose a file."));return false}if(!allowedMimeTypes.some(el=>el===file.type)){alert(gettext("This wasn't a valid image file format. Please provide a PNG, JPEG, or GIF file."));return false}return true},render(){this._$box=this.$(".avatar-file-upload-config");this._$preview=this.$(".avatar-preview");this._$fileInput=this.$("#id_file-upload-avatar_upload");return this},_onBrowseClicked(e){e.preventDefault();e.stopPropagation();this.$("#avatar-file-upload-browse-label").click()},_onDragEnter(e){e.preventDefault();e.stopPropagation();if(e.target===this._$box[0]){this._$box.width(this._$box.width()).addClass("drag-hover")}},_onDragOver(e){e.preventDefault();e.stopPropagation();if(e.target===this._$box[0]){const dt=e.originalEvent.dataTransfer;if(dt){dt.dropEffect="copy"}}},_onDragLeave(e){e.preventDefault();e.stopPropagation();if(e.target===this._$box[0]){this._$box.removeClass("drag-hover").width("auto");const dt=e.originalEvent.dataTransfer;if(dt){dt.dropEffect="none"}}},_onDrop(e){e.preventDefault();e.stopPropagation();this._$box.removeClass("drag-hover");const dt=e.originalEvent.dataTransfer;const files=dt&&dt.files;if(!files||files.length===0){return}if(files.length>1){alert(gettext("You can only set one file as your avatar. Please drag and drop a single file."));return}const fileType=files[0].type;if(fileType!=="image/png"&&fileType!=="image/jpeg"&&fileType!=="image/gif"){alert(gettext("This doesn't appear to be a compatible image file for avatars. Please upload a PNG, JPEG, or GIF file."));return}try{this._$fileInput[0].files=files}catch(exc){alert(gettext("Looks like dragging to upload a file isn't going to work with your browser. Try browsing for a file instead."));return}this._setAvatarFromFile(files[0])},_onFileChanged(e){const file=e.target.files[0];if(file){this._setAvatarFromFile(file)}},_setAvatarFromFile(file){const reader=new FileReader;reader.addEventListener("load",()=>{this._$preview.empty().removeClass("avatar-preview-unset").append($("<img />").attr({src:reader.result,alt:gettext("Your new avatar")}))});reader.readAsDataURL(file)}})})()}).call(this);
