{"version":3,"file":"../../js/llms-sl-profile.min.js","sources":["llms-sl-profile.js"],"sourcesContent":["/**\n * Student Profile JS.\n *\n * @package LifterLMS_Social_Learning/Scripts\n *\n * @since 1.0.0\n * @version 1.6.0\n */\n\n( function( $ ) {\n\t/**\n\t * UI & UX for Student Profiles.\n\t *\n\t * @since 1.0.0\n\t * @since 1.1.0 Unknown.\n\t * @since 1.6.0 Added the ability to remove an avatar or cover image through a context menu.\n\t */\n\tvar Profile = function() {\n\n\t\tvar $modal = $( '#llms-sl-upload-modal' );\n\n\t\t/**\n\t\t * Bind DOM events.\n\t\t *\n\t\t * @since 1.0.0\n\t\t * @since 1.1.0 Unknown.\n\t\t * @since 1.6.0 Change the setup and event binding of the cover image and avatar.\n\t\t *\n\t\t * @return void\n\t\t */\n\t\tfunction bind() {\n\n\t\t\t$modal.iziModal();\n\n\t\t\tvar updateCoverButton = $( '#llms-sl-upload-cover' );\n\t\t\tupdateCoverButton.attr( 'data-image-type', 'cover' );\n\t\t\tupdateCoverButton.on( 'click', onClickImageButton );\n\t\t\tupdateCoverButton.on( 'focusout', onFocusOutImageButton );\n\n\t\t\tvar updateAvatarButton = $( '#llms-sl-upload-avatar' );\n\t\t\tupdateAvatarButton.attr( 'data-image-type', 'avatar' );\n\t\t\tupdateAvatarButton.on( 'click', onClickImageButton );\n\t\t\tupdateAvatarButton.on( 'focusout', onFocusOutImageButton );\n\n\t\t\twindow.ondragover = function( e ) {\n\t\t\t\te.preventDefault();\n\t\t\t};\n\n\t\t\twindow.ondrop = onDrop;\n\n\t\t\t$( '#llms-sl-uploader-zone' ).on( 'dragover', function() {\n\t\t\t\t$( this ).addClass( 'dragover' );\n\t\t\t} ).on( 'dragout', function() {\n\t\t\t\t$( this ).removeClass( 'dragover' );\n\t\t\t} );\n\n\t\t\t$( '#llms-sl-upload-image' ).on( 'change', onUploadChange );\n\n\t\t}\n\n\t\t/**\n\t\t * Handle \"Delete Image\" button click.\n\t\t *\n\t\t * @since 1.6.0\n\t\t *\n\t\t * @param {string} type The profile image type, 'avatar'|'cover'.\n\t\t * @return void\n\t\t */\n\t\tfunction deleteImage( type ) {\n\n\t\t\tvar data = new FormData();\n\t\t\tdata.append( 'action', 'llms_sl_delete_image' );\n\t\t\tdata.append( 'nonce', $( '#llms-sl-profile-image-nonce' ).val() );\n\t\t\tdata.append( 'type', type );\n\n\t\t\trequest( data, type );\n\t\t}\n\n\t\t/**\n\t\t * Returns true if the avatar or the cover images are the default ones.\n\t\t *\n\t\t * @param {string} type The profile image type, 'avatar'|'cover'.\n\t\t * @return {boolean}\n\t\t */\n\t\tfunction isCustomImage( type ) {\n\n\t\t\tswitch ( type ) {\n\t\t\t\tcase 'avatar':\n\t\t\t\t\treturn isCustomAvatarImage();\n\t\t\t\tcase 'cover':\n\t\t\t\t\treturn isCustomCoverImage();\n\t\t\t\tdefault:\n\t\t\t\t return false;\n\t\t\t}\n\n\t\t}\n\n\t\t/**\n\t\t * Returns true if the avatar image from WordPress `get_avatar()` has been replaced by LifterLMS Social Learning.\n\t\t *\n\t\t * @since 1.6.0\n\t\t *\n\t\t * @return {boolean}\n\t\t */\n\t\tfunction isCustomAvatarImage() {\n\t\t\t// Does the image have the added 'llms-sl-custom-avatar' class?\n\t\t\treturn $( '#llms-avatar-img img' ).hasClass( 'llms-sl-custom-avatar' );\n\t\t}\n\n\t\t/**\n\t\t * Returns true if there is a cover image that obscures the default linear gradient.\n\t\t *\n\t\t * @since 1.6.0\n\t\t *\n\t\t * @return {boolean}\n\t\t */\n\t\tfunction isCustomCoverImage() {\n\t\t\t// Does the cover element contain zero images?\n\t\t\treturn $( '#llms-cover-img img' ).length > 0;\n\t\t}\n\n\t\t/**\n\t\t * Handle onClick events for the profile image by deciding whether to open the uploader modal\n\t\t * or to display the context menu.\n\t\t *\n\t\t * @since 1.6.0\n\t\t *\n\t\t * @param {jQuery.Event} jqEvent {@see https://api.jquery.com/category/events/event-object/}.\n\t\t * @return @void\n\t\t */\n\t\tfunction onClickImageButton( jqEvent ) {\n\n\t\t\tlet\n\t\t\t\t$target = $( jqEvent.currentTarget );\n\t\t\t\ttype = $target.attr( 'data-image-type' );\n\n\t\t\t// Is the image currently the default?\n\t\t\tif ( isCustomImage( type ) ) {\n\t\t\t\t// Display the context menu.\n\t\t\t\t$target.next( '.llms-sl-context-menu' ).css( 'display', 'block' );\n\t\t\t} else {\n\t\t\t\t// Display the uploader modal window.\n\t\t\t\topenUploaderModal( type );\n\t\t\t}\n\t\t}\n\n\t\t/**\n\t\t * Handle drop events to allow dragging images into uploader.\n\t\t *\n\t\t * @since 1.0.0\n\t\t *\n\t\t * @param {jQuery.Event} jqEvent {@see https://api.jquery.com/category/events/event-object/}.\n\t\t * @return void\n\t\t */\n\t\tfunction onDrop( jqEvent ) {\n\n\t\t\tjqEvent.preventDefault();\n\n\t\t\tvar $target = $( jqEvent.target );\n\n\t\t\tif ( ! $target.closest( '.llms-sl-uploader' ).length ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tupload( jqEvent.dataTransfer.files[ 0 ] );\n\n\t\t}\n\n\t\t/**\n\t\t * Handle focusout events for the profile image.\n\t\t *\n\t\t * @since 1.6.0\n\t\t *\n\t\t * @param {jQuery.Event} jqEvent {@see https://api.jquery.com/category/events/event-object/}.\n\t\t * @return void\n\t\t */\n\t\tfunction onFocusOutImageButton( jqEvent ) {\n\n\t\t\tvar type = $( jqEvent.currentTarget ).attr( 'data-image-type' );\n\t\t\t$( jqEvent.currentTarget ).next( '.llms-sl-context-menu' ).css( 'display', 'none' );\n\n\t\t\tswitch ( $( jqEvent.relatedTarget ).attr( 'id' ) ) {\n\t\t\t\tcase 'llms-sl-upload-' + type + '-image':\n\t\t\t\t\topenUploaderModal( type );\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'llms-sl-delete-' + type + '-image':\n\t\t\t\t\tdeleteImage( type );\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t/**\n\t\t * Handle upload when files are added to the file uploader.\n\t\t *\n\t\t * @since 1.0.0\n\t\t *\n\t\t * @param {jQuery.Event} jqEvent {@see https://api.jquery.com/category/events/event-object/}.\n\t\t * @return void\n\t\t */\n\t\tfunction onUploadChange( jqEvent ) {\n\n\t\t\tupload( jqEvent.target.files[ 0 ] );\n\n\t\t}\n\n\t\t/**\n\t\t * Opens the profile image uploader modal.\n\t\t *\n\t\t * @since 1.6.0\n\t\t *\n\t\t * @param {string} type The profile image type, 'avatar'|'cover'.\n\t\t * @return void\n\t\t */\n\t\tfunction openUploaderModal( type ) {\n\n\t\t\t$modal.iziModal( 'open' );\n\t\t\t$( '#llms-sl-uploader-zone' ).attr( 'data-upload-type', type );\n\t\t}\n\n\t\t/**\n\t\t * Makes an ajax request and provides error and success handlers.\n\t\t *\n\t\t * @since 1.6.0\n\t\t *\n\t\t * @param {FormData} data The data to send.\n\t\t * @param {string} type The image type, 'avatar'|'cover'.\n\t\t * @return void\n\t\t */\n\t\tfunction request( data, type ) {\n\n\t\t\tvar spinnerParent = 'opened' === $modal.iziModal( 'getState' ) ? $modal : $( '#llms-' + type + '-img' );\n\n\t\t\tLLMS.Ajax.call( {\n\t\t\t\tdata: data,\n\t\t\t\tcontentType: false,\n\t\t\t\tprocessData: false,\n\t\t\t\tbeforeSend: function() {\n\n\t\t\t\t\tspinnerParent.find( '.llms-sl-error' ).remove();\n\t\t\t\t\tLLMS.Spinner.start( spinnerParent );\n\n\t\t\t\t},\n\t\t\t\terror: function( xhr, status, error ) {\n\n\t\t\t\t\tvar msg = error;\n\n\t\t\t\t\tif ( 'object' === typeof error && error.message ) {\n\t\t\t\t\t\tmsg = error.message;\n\t\t\t\t\t}\n\t\t\t\t\tshow_error( msg, status, error );\n\t\t\t\t\tLLMS.Spinner.stop( spinnerParent );\n\n\t\t\t\t},\n\t\t\t\tsuccess: function( r ) {\n\n\t\t\t\t\tLLMS.Spinner.stop( spinnerParent );\n\t\t\t\t\tif ( r && r.success ) {\n\t\t\t\t\t\tif ( 'avatar' === type ) {\n\t\t\t\t\t\t\t$( '#llms-avatar-img' ).html( r.data.image );\n\t\t\t\t\t\t} else if ( 'cover' === type ) {\n\t\t\t\t\t\t\t$( '#llms-cover-img' ).html( r.data.image );\n\t\t\t\t\t\t}\n\t\t\t\t\t\t$modal.iziModal( 'close' );\n\t\t\t\t\t} else {\n\t\t\t\t\t\tshow_error( r.message, 'error', r );\n\t\t\t\t\t}\n\n\t\t\t\t}\n\n\t\t\t} );\n\n\t\t}\n\n\t\t/**\n\t\t * Show an uploader error message.\n\t\t *\n\t\t * @since 1.1.0\n\t\t * @since 1.1.0 Unknown.\n\t\t *\n\t\t * @param {string} msg Error message string.\n\t\t * @param {string} status Error status string.\n\t\t * @param {Object} data Additional data to log to the console.\n\t\t * @return void\n\t\t */\n\t\tfunction show_error( msg, status, data ) {\n\n\t\t\tconsole.error( status, data );\n\n\t\t\tmsg = LLMS.l10n.translate( 'Error' ) + ': ' + msg;\n\n\t\t\t$( '#llms-sl-uploader-zone' ).append( '
' + msg + '
' );\n\n\t\t}\n\n\t\t/**\n\t\t * Upload files via AJAX.\n\t\t *\n\t\t * @since 1.0.0\n\t\t * @since 1.1.0 Unknown.\n\t\t * @since 1.6.0 Allow the image to be replaced by an uploaded file with the same name.\n\t\t * Change nonce field name from 'llms-sl-upload-nonce' to 'llms-sl-profile-image-nonce'.\n\t\t *\n\t\t * @param {File} file JS DOM file transfer object.\n\t\t * @return void\n\t\t */\n\t\tfunction upload( file ) {\n\n\t\t\tvar type = $( '#llms-sl-uploader-zone' ).attr( 'data-upload-type' );\n\t\t\tvar data = new FormData();\n\n\t\t\t// Prevent IE from making a 2nd request after the `#llms-sl-upload-image` value is blanked.\n\t\t\tif ( ! file ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tdata.append( 'action', 'llms_sl_upload_image' );\n\t\t\tdata.append( 'image', file );\n\t\t\tdata.append( 'nonce', $( '#llms-sl-profile-image-nonce' ).val() );\n\t\t\tdata.append( 'type', type );\n\n\t\t\trequest( data, type );\n\n\t\t\t// Reset upload image input element value so that the image can be replaced by a file with the same name.\n\t\t\t$( '#llms-sl-upload-image' ).val( '' );\n\t\t}\n\n\t\t// Go.\n\t\tbind();\n\n\t\treturn {\n\t\t\tupload: upload,\n\t\t};\n\n\t};\n\n\twindow.llms_sl = window.llms_sl || {};\n\twindow.llms_sl.profile = new Profile();\n\n} )( jQuery );\n"],"names":["$","window","llms_sl","profile","updateCoverButton","$modal","isCustomImage","type","hasClass","length","onClickImageButton","jqEvent","let","$target","currentTarget","attr","next","css","openUploaderModal","onDrop","preventDefault","target","closest","upload","dataTransfer","files","onFocusOutImageButton","data","relatedTarget","FormData","append","val","request","onUploadChange","iziModal","spinnerParent","LLMS","Ajax","call","contentType","processData","beforeSend","find","remove","Spinner","start","error","xhr","status","msg","show_error","message","stop","success","r","html","image","console","l10n","translate","file","on","updateAvatarButton","ondragover","e","ondrop","this","addClass","removeClass","jQuery"],"mappings":"CASA,SAAYA,GAsUXC,OAAOC,QAAkBD,OAAOC,SAAW,GAC3CD,OAAOC,QAAQC,QAAU,IA/TX,WAEb,IAeKC,EAfDC,EAASL,EAAG,yBAiEhB,SAASM,EAAeC,GAEvB,OAASA,GACR,IAAK,SACJ,OAkBKP,EAAG,wBAAyBQ,SAAU,yBAjB5C,IAAK,QACJ,OA4ByC,EAApCR,EAAG,uBAAwBS,OA3BjC,QACE,QAsCJ,SAASC,EAAoBC,GAE5BC,IACCC,EAAUb,EAAGW,EAAQG,eAIjBR,EAHJC,KAAUM,EAAQE,KAAM,oBAKxBF,EAAQG,KAAM,yBAA0BC,IAAK,UAAW,SAGxDC,EAAmBX,MAYrB,SAASY,EAAQR,GAEhBA,EAAQS,iBAEMpB,EAAGW,EAAQU,QAEVC,QAAS,qBAAsBb,QAI9Cc,EAAQZ,EAAQa,aAAaC,MAAO,IAYrC,SAASC,EAAuBf,GAE/B,IA9GqBJ,EAEjBoB,EA4GApB,EAAOP,EAAGW,EAAQG,eAAgBC,KAAM,mBAG5C,OAFAf,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