2017-08-26 16:06:52 -07:00
/ *
*
* Snipe - IT Universal Modal support
*
* Enables modal dialogs to create sub - resources throughout Snipe - IT
*
* /
/ *
HOW TO USE
Create a Button looking like this :
< a href = '{{ route(' modal . user ') }}' data - toggle = "modal" data - target = "#createModal" data - dependency = "user" data - select = 'assigned_to' class = "btn btn-sm btn-default" > New < / a >
If you don 't have access to Blade commands (like {{ and }}, etc), you can hard-code a URL as the ' href '
data - toggle = "modal" - required for Bootstrap Modals
data - target = "#createModal" - fixed ID for the modal , do not change
data - dependency = "user" - which Snipe - IT model you ' re going to be creating .
2017-10-16 20:34:22 -07:00
data - select = "assigned_to" - What is the * ID * of the select - dropdown that you 're going to be adding to, if the modal-create was a success? Be on the lookout for duplicate ID' s , it will confuse this library !
2017-08-26 16:06:52 -07:00
class = "btn btn-sm btn-default" - makes it look button - ey , feel free to change : )
* /
$ ( function ( ) {
2017-10-24 19:20:00 -07:00
2017-08-26 16:06:52 -07:00
//handle modal-add-interstitial calls
var model , select ;
if ( $ ( '#createModal' ) . length == 0 ) {
$ ( 'body' ) . append ( '<div class="modal fade" id="createModal"></div><!-- /.modal -->' ) ;
}
$ ( '#createModal' ) . on ( "show.bs.modal" , function ( event ) {
var link = $ ( event . relatedTarget ) ;
model = link . data ( "dependency" ) ;
select = link . data ( "select" ) ;
$ ( '#createModal' ) . load ( link . attr ( 'href' ) , function ( ) {
//do we need to re-select2 this, after load? Probably.
$ ( '#createModal' ) . find ( 'select.select2' ) . select2 ( ) ;
} ) ;
} ) ;
$ ( '#createModal' ) . on ( 'click' , '#modal-save' , function ( ) {
var data = { } ;
2017-10-24 19:20:00 -07:00
//console.warn("We are about to SAVE!!! for model: "+model+" and select ID: "+select);
2017-08-26 16:06:52 -07:00
$ ( '.modal-body input:visible' ) . each ( function ( index , elem ) {
var bits = elem . id . split ( "-" ) ;
if ( bits [ 0 ] === "modal" ) {
data [ bits [ 1 ] ] = $ ( elem ) . val ( ) ;
}
} ) ;
//this can probably get replaced with a normal 'serialize' instead
$ ( '.modal-body select:visible' ) . each ( function ( index , elem ) {
var bits = elem . id . split ( "-" ) ;
data [ bits [ 1 ] ] = $ ( elem ) . val ( ) ;
} ) ;
data . _token = Laravel . csrfToken ;
$ . ajax ( {
type : 'POST' ,
2017-10-24 19:20:00 -07:00
url : baseUrl + "/api/v1/" + model + "s" ,
2017-08-26 16:06:52 -07:00
headers : {
"X-Requested-With" : 'XMLHttpRequest' ,
"X-CSRF-TOKEN" : $ ( 'meta[name="csrf-token"]' ) . attr ( 'content' )
} ,
data : data ,
success : function ( result ) {
2017-10-24 19:20:00 -07:00
console . dir ( result ) ;
2017-08-26 16:06:52 -07:00
if ( result . status == "error" ) {
var error _message = "" ;
for ( var field in result . messages ) {
2017-10-24 19:20:00 -07:00
error _message += "<li>Problem(s) with field <i><strong>" + field + "</strong></i>: " + result . messages [ field ] ;
console . dir ( result . messages ) ;
console . log ( 'error_messages are: ' + error _message ) ;
2017-08-26 16:06:52 -07:00
}
$ ( '#modal_error_msg' ) . html ( error _message ) . show ( ) ;
return false ;
}
var id = result . payload . id ;
var name = result . payload . name || ( result . payload . first _name + " " + result . payload . last _name ) ;
if ( ! id || ! name ) {
console . error ( "Could not find resulting name or ID from modal-create. Name: " + name + ", id: " + id ) ;
return false ;
}
$ ( '#createModal' ) . modal ( 'hide' ) ;
$ ( '#createModal' ) . html ( "" ) ;
// "select" is the original drop-down menu that someone
// clicked 'add' on to add a new 'thing'
// this code adds the newly created object to that select
var selector = document . getElementById ( select ) ;
2017-10-26 02:28:17 -07:00
console . warn ( "The selector we should've selecte dis: " + select ) ;
console . dir ( selector ) ;
2017-08-26 16:06:52 -07:00
if ( ! selector ) {
return false ;
}
2017-10-24 17:10:42 -07:00
2017-10-26 02:28:17 -07:00
console . warn ( "onChange Selector Thing should've activated? Here's the selector" ) ;
console . dir ( selector ) ;
2017-08-26 16:06:52 -07:00
selector . options [ selector . length ] = new Option ( name , id ) ;
selector . selectedIndex = selector . length - 1 ;
$ ( selector ) . trigger ( "change" ) ;
2017-10-16 20:34:22 -07:00
if ( window . fetchCustomFields ) {
2017-08-26 16:06:52 -07:00
fetchCustomFields ( ) ;
}
} ,
error : function ( result ) {
msg = result . responseJSON . messages || result . responseJSON . error ;
$ ( '#modal_error_msg' ) . html ( "Server Error: " + msg ) . show ( ) ;
}
} ) ;
} ) ;
2017-10-16 20:12:11 -07:00
} ) ;