mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 21:25:42 +00:00 
			
		
		
		
	Adds a "persist" option for modal forms
This commit is contained in:
		| @@ -542,6 +542,11 @@ function constructFormBody(fields, options) { | ||||
|         insertConfirmButton(options); | ||||
|     } | ||||
|  | ||||
|     // Insert "persist" button (if required) | ||||
|     if (options.persist) { | ||||
|         insertPersistButton(options); | ||||
|     } | ||||
|  | ||||
|     // Display the modal | ||||
|     $(modal).modal('show'); | ||||
|  | ||||
| @@ -616,6 +621,22 @@ function insertConfirmButton(options) { | ||||
| } | ||||
|  | ||||
|  | ||||
| /* Add a checkbox to select if the modal will stay open after success */ | ||||
| function insertPersistButton(options) { | ||||
|  | ||||
|     var message = options.persistMessage || '{% trans "Keep this form open" %}'; | ||||
|  | ||||
|     var html = ` | ||||
|     <div class="form-check form-switch"> | ||||
|         <input class="form-check-input" type="checkbox" id="modal-persist"> | ||||
|         <label class="form-check-label" for="modal-persist">${message}</label> | ||||
|     </div> | ||||
|     `; | ||||
|  | ||||
|     $(options.modal).find('#modal-footer-buttons').append(html); | ||||
| } | ||||
|  | ||||
|  | ||||
| /* | ||||
|  * Extract all specified form values as a single object | ||||
|  */ | ||||
| @@ -938,13 +959,23 @@ function handleFormSuccess(response, options) { | ||||
|     // Should we show alerts immediately or cache them? | ||||
|     var cache = (options.follow && response.url) || options.redirect || options.reload; | ||||
|  | ||||
|     if (options.reloadFormAfterSuccess) { | ||||
|     // Should the form "persist"? | ||||
|     var persist = false; | ||||
|  | ||||
|     if (options.persist && options.modal) { | ||||
|         // Determine if this form should "persist", or be dismissed? | ||||
|         var chk = $(options.modal).find('#modal-persist'); | ||||
|  | ||||
|         persist = chk.exists() && chk.prop('checked'); | ||||
|     } | ||||
|  | ||||
|     if (persist) { | ||||
|         cache = false; | ||||
|     } | ||||
|  | ||||
|     var msg_target = null; | ||||
|  | ||||
|     if (options.modal && options.reloadFormAfterSuccess) { | ||||
|     if (persist) { | ||||
|         // If the modal is persistant, the target for any messages should be the modal! | ||||
|         msg_target = $(options.modal).find('#pre-form-content'); | ||||
|     } | ||||
| @@ -971,13 +1002,8 @@ function handleFormSuccess(response, options) { | ||||
|     if (response && response.danger) { | ||||
|         showAlertOrCache(response.danger, cache, {style: 'danger'}); | ||||
|     } | ||||
|      | ||||
|     if (options.onSuccess) { | ||||
|         // Callback function | ||||
|         options.onSuccess(response, options); | ||||
|     } | ||||
|  | ||||
|     if (options.reloadFormAfterSuccess) { | ||||
|     if (persist) { | ||||
|         // Instead of closing the form and going somewhere else, | ||||
|         // reload (empty) the form so the user can input more data | ||||
|          | ||||
| @@ -997,6 +1023,11 @@ function handleFormSuccess(response, options) { | ||||
|             $(options.modal).modal('hide'); | ||||
|         } | ||||
|  | ||||
|         if (options.onSuccess) { | ||||
|             // Callback function | ||||
|             options.onSuccess(response, options); | ||||
|         } | ||||
|  | ||||
|         if (options.follow && response.url) { | ||||
|             // Follow the returned URL | ||||
|             window.location.href = response.url; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user