mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 21:25:42 +00:00 
			
		
		
		
	Remove custom javascript from auth pages (#3250)
* Remove custom javascript from auth pages - Unauthorized user cannot load these scripts - Simply throws console errors * Split basic "show message" function out into new js file * Split more generic functions out into new .js file * javascript linting fix
This commit is contained in:
		| @@ -16,6 +16,7 @@ | |||||||
|     sanitizeData, |     sanitizeData, | ||||||
| */ | */ | ||||||
|  |  | ||||||
|  |  | ||||||
| function attachClipboard(selector, containerselector, textElement) { | function attachClipboard(selector, containerselector, textElement) { | ||||||
|     // set container |     // set container | ||||||
|     if (containerselector) { |     if (containerselector) { | ||||||
|   | |||||||
							
								
								
									
										128
									
								
								InvenTree/InvenTree/static/script/inventree/message.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										128
									
								
								InvenTree/InvenTree/static/script/inventree/message.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,128 @@ | |||||||
|  | /* exported | ||||||
|  |     showMessage, | ||||||
|  |     showAlertOrCache, | ||||||
|  |     showCachedAlerts, | ||||||
|  | */ | ||||||
|  |  | ||||||
|  | /* | ||||||
|  |  * Display an alert message at the top of the screen. | ||||||
|  |  * The message will contain a "close" button, | ||||||
|  |  * and also dismiss automatically after a certain amount of time. | ||||||
|  |  * | ||||||
|  |  * arguments: | ||||||
|  |  * - message: Text / HTML content to display | ||||||
|  |  * | ||||||
|  |  * options: | ||||||
|  |  * - style: alert style e.g. 'success' / 'warning' | ||||||
|  |  * - timeout: Time (in milliseconds) after which the message will be dismissed | ||||||
|  |  */ | ||||||
|  | function showMessage(message, options={}) { | ||||||
|  |  | ||||||
|  |     var style = options.style || 'info'; | ||||||
|  |  | ||||||
|  |     var timeout = options.timeout || 5000; | ||||||
|  |  | ||||||
|  |     var target = options.target || $('#alerts'); | ||||||
|  |  | ||||||
|  |     var details = ''; | ||||||
|  |  | ||||||
|  |     if (options.details) { | ||||||
|  |         details = `<p><small>${options.details}</p></small>`; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // Hacky function to get the next available ID | ||||||
|  |     var id = 1; | ||||||
|  |  | ||||||
|  |     while ($(`#alert-${id}`).exists()) { | ||||||
|  |         id++; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     var icon = ''; | ||||||
|  |  | ||||||
|  |     if (options.icon) { | ||||||
|  |         icon = `<span class='${options.icon}'></span>`; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // Construct the alert | ||||||
|  |     var html = ` | ||||||
|  |     <div id='alert-${id}' class='alert alert-${style} alert-dismissible fade show' role='alert'> | ||||||
|  |         ${icon} | ||||||
|  |         <b>${message}</b> | ||||||
|  |         ${details} | ||||||
|  |         <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> | ||||||
|  |     </div> | ||||||
|  |     `; | ||||||
|  |  | ||||||
|  |     target.append(html); | ||||||
|  |  | ||||||
|  |     // Remove the alert automatically after a specified period of time | ||||||
|  |     $(`#alert-${id}`).delay(timeout).slideUp(200, function() { | ||||||
|  |         $(this).alert(close); | ||||||
|  |     }); | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | /* | ||||||
|  |  * Add a cached alert message to sesion storage | ||||||
|  |  */ | ||||||
|  | function addCachedAlert(message, options={}) { | ||||||
|  |  | ||||||
|  |     var alerts = sessionStorage.getItem('inventree-alerts'); | ||||||
|  |  | ||||||
|  |     if (alerts) { | ||||||
|  |         alerts = JSON.parse(alerts); | ||||||
|  |     } else { | ||||||
|  |         alerts = []; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     alerts.push({ | ||||||
|  |         message: message, | ||||||
|  |         style: options.style || 'success', | ||||||
|  |         icon: options.icon, | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     sessionStorage.setItem('inventree-alerts', JSON.stringify(alerts)); | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | /* | ||||||
|  |  * Remove all cached alert messages | ||||||
|  |  */ | ||||||
|  | function clearCachedAlerts() { | ||||||
|  |     sessionStorage.removeItem('inventree-alerts'); | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | /* | ||||||
|  |  * Display an alert, or cache to display on reload | ||||||
|  |  */ | ||||||
|  | function showAlertOrCache(message, cache, options={}) { | ||||||
|  |  | ||||||
|  |     if (cache) { | ||||||
|  |         addCachedAlert(message, options); | ||||||
|  |     } else { | ||||||
|  |         showMessage(message, options); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | /* | ||||||
|  |  * Display cached alert messages when loading a page | ||||||
|  |  */ | ||||||
|  | function showCachedAlerts() { | ||||||
|  |  | ||||||
|  |     var alerts = JSON.parse(sessionStorage.getItem('inventree-alerts')) || []; | ||||||
|  |  | ||||||
|  |     alerts.forEach(function(alert) { | ||||||
|  |  | ||||||
|  |         showMessage( | ||||||
|  |             alert.message, | ||||||
|  |             { | ||||||
|  |                 style: alert.style || 'success', | ||||||
|  |                 icon: alert.icon, | ||||||
|  |             } | ||||||
|  |         ); | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     clearCachedAlerts(); | ||||||
|  | } | ||||||
| @@ -89,27 +89,20 @@ | |||||||
| <!-- general JS --> | <!-- general JS --> | ||||||
| {% include "third_party_js.html" %} | {% include "third_party_js.html" %} | ||||||
|  |  | ||||||
| <script type='text/javascript' src="{% static 'script/inventree/inventree.js' %}"></script> | <script type='text/javascript' src='{% static "script/inventree/message.js" %}'></script> | ||||||
| <script type='text/javascript' src="{% i18n_static 'notification.js' %}"></script> |  | ||||||
|  |  | ||||||
|  |  | ||||||
| <script type='text/javascript'> | <script type='text/javascript'> | ||||||
|  |  | ||||||
| $(document).ready(function () { | $(document).ready(function () { | ||||||
|     // notifications |  | ||||||
|     {% if messages %} |     {% if messages %} | ||||||
|     {% for message in messages %} |     {% for message in messages %} | ||||||
|     showAlertOrCache( |         showMessage(messsage); | ||||||
|         '{{ message }}', |  | ||||||
|         true, |  | ||||||
|         { |  | ||||||
|             style: 'info', |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
|     {% endfor %} |     {% endfor %} | ||||||
|     {% endif %} |     {% endif %} | ||||||
|  |  | ||||||
|     inventreeDocReady(); |     showCachedAlerts(); | ||||||
|  |  | ||||||
| }); | }); | ||||||
|  |  | ||||||
| </script> | </script> | ||||||
|   | |||||||
| @@ -141,6 +141,7 @@ | |||||||
|  |  | ||||||
| <!-- general JS --> | <!-- general JS --> | ||||||
| <script defer type='text/javascript' src="{% static 'script/inventree/inventree.js' %}"></script> | <script defer type='text/javascript' src="{% static 'script/inventree/inventree.js' %}"></script> | ||||||
|  | <script defer type='text/javascript' src="{% static 'script/inventree/message.js' %}"></script> | ||||||
|  |  | ||||||
| <!-- dynamic javascript templates --> | <!-- dynamic javascript templates --> | ||||||
| <script defer type='text/javascript' src="{% url 'calendar.js' %}"></script> | <script defer type='text/javascript' src="{% url 'calendar.js' %}"></script> | ||||||
|   | |||||||
| @@ -2,8 +2,6 @@ | |||||||
|  |  | ||||||
| /* exported | /* exported | ||||||
|     loadNotificationTable, |     loadNotificationTable, | ||||||
|     showAlertOrCache, |  | ||||||
|     showCachedAlerts, |  | ||||||
|     startNotificationWatcher, |     startNotificationWatcher, | ||||||
|     stopNotificationWatcher, |     stopNotificationWatcher, | ||||||
|     openNotificationPanel, |     openNotificationPanel, | ||||||
| @@ -100,128 +98,6 @@ function loadNotificationTable(table, options={}, enableDelete=false) { | |||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| /* |  | ||||||
|  * Add a cached alert message to sesion storage |  | ||||||
|  */ |  | ||||||
| function addCachedAlert(message, options={}) { |  | ||||||
|  |  | ||||||
|     var alerts = sessionStorage.getItem('inventree-alerts'); |  | ||||||
|  |  | ||||||
|     if (alerts) { |  | ||||||
|         alerts = JSON.parse(alerts); |  | ||||||
|     } else { |  | ||||||
|         alerts = []; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     alerts.push({ |  | ||||||
|         message: message, |  | ||||||
|         style: options.style || 'success', |  | ||||||
|         icon: options.icon, |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     sessionStorage.setItem('inventree-alerts', JSON.stringify(alerts)); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  |  | ||||||
| /* |  | ||||||
|  * Remove all cached alert messages |  | ||||||
|  */ |  | ||||||
| function clearCachedAlerts() { |  | ||||||
|     sessionStorage.removeItem('inventree-alerts'); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  |  | ||||||
| /* |  | ||||||
|  * Display an alert, or cache to display on reload |  | ||||||
|  */ |  | ||||||
| function showAlertOrCache(message, cache, options={}) { |  | ||||||
|  |  | ||||||
|     if (cache) { |  | ||||||
|         addCachedAlert(message, options); |  | ||||||
|     } else { |  | ||||||
|         showMessage(message, options); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  |  | ||||||
| /* |  | ||||||
|  * Display cached alert messages when loading a page |  | ||||||
|  */ |  | ||||||
| function showCachedAlerts() { |  | ||||||
|  |  | ||||||
|     var alerts = JSON.parse(sessionStorage.getItem('inventree-alerts')) || []; |  | ||||||
|  |  | ||||||
|     alerts.forEach(function(alert) { |  | ||||||
|         showMessage( |  | ||||||
|             alert.message, |  | ||||||
|             { |  | ||||||
|                 style: alert.style || 'success', |  | ||||||
|                 icon: alert.icon, |  | ||||||
|             } |  | ||||||
|         ); |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     clearCachedAlerts(); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  |  | ||||||
| /* |  | ||||||
|  * Display an alert message at the top of the screen. |  | ||||||
|  * The message will contain a "close" button, |  | ||||||
|  * and also dismiss automatically after a certain amount of time. |  | ||||||
|  * |  | ||||||
|  * arguments: |  | ||||||
|  * - message: Text / HTML content to display |  | ||||||
|  * |  | ||||||
|  * options: |  | ||||||
|  * - style: alert style e.g. 'success' / 'warning' |  | ||||||
|  * - timeout: Time (in milliseconds) after which the message will be dismissed |  | ||||||
|  */ |  | ||||||
| function showMessage(message, options={}) { |  | ||||||
|  |  | ||||||
|     var style = options.style || 'info'; |  | ||||||
|  |  | ||||||
|     var timeout = options.timeout || 5000; |  | ||||||
|  |  | ||||||
|     var target = options.target || $('#alerts'); |  | ||||||
|  |  | ||||||
|     var details = ''; |  | ||||||
|  |  | ||||||
|     if (options.details) { |  | ||||||
|         details = `<p><small>${options.details}</p></small>`; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     // Hacky function to get the next available ID |  | ||||||
|     var id = 1; |  | ||||||
|  |  | ||||||
|     while ($(`#alert-${id}`).exists()) { |  | ||||||
|         id++; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     var icon = ''; |  | ||||||
|  |  | ||||||
|     if (options.icon) { |  | ||||||
|         icon = `<span class='${options.icon}'></span>`; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     // Construct the alert |  | ||||||
|     var html = ` |  | ||||||
|     <div id='alert-${id}' class='alert alert-${style} alert-dismissible fade show' role='alert'> |  | ||||||
|         ${icon} |  | ||||||
|         <b>${message}</b> |  | ||||||
|         ${details} |  | ||||||
|         <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> |  | ||||||
|     </div> |  | ||||||
|     `; |  | ||||||
|  |  | ||||||
|     target.append(html); |  | ||||||
|  |  | ||||||
|     // Remove the alert automatically after a specified period of time |  | ||||||
|     $(`#alert-${id}`).delay(timeout).slideUp(200, function() { |  | ||||||
|         $(this).alert(close); |  | ||||||
|     }); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| var notificationWatcher = null; // reference for the notificationWatcher | var notificationWatcher = null; // reference for the notificationWatcher | ||||||
| /** | /** | ||||||
|  * start the regular notification checks |  * start the regular notification checks | ||||||
|   | |||||||
| @@ -70,6 +70,7 @@ | |||||||
|  |  | ||||||
| <!-- general JS --> | <!-- general JS --> | ||||||
| <script type='text/javascript' src="{% static 'script/inventree/inventree.js' %}"></script> | <script type='text/javascript' src="{% static 'script/inventree/inventree.js' %}"></script> | ||||||
|  | <script type='text/javascript' src="{% static 'script/inventree/message.js' %}"></script> | ||||||
| <script type='text/javascript' src="{% i18n_static 'notification.js' %}"></script> | <script type='text/javascript' src="{% i18n_static 'notification.js' %}"></script> | ||||||
| {% block body_scripts_inventree %} | {% block body_scripts_inventree %} | ||||||
| {% endblock %} | {% endblock %} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user