Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. /*--------------------------------------------------------------------------
  2.  
  3. ActionMenu
  4. Created by WolfKnight
  5. Additional help from lowheartrate, TheStonedTurtle, and Briglair.
  6.  
  7. --------------------------------------------------------------------------*/
  8.  
  9. $( function() {
  10. // Adds all of the correct button actions
  11. init();
  12.  
  13. // Gets the actionmenu div container
  14. var actionContainer = $( "#actionmenu" );
  15.  
  16. // Listens for NUI messages from Lua
  17. window.addEventListener( 'message', function( event ) {
  18. var item = event.data;
  19.  
  20. // Show the menu
  21. if ( item.showmenu ) {
  22. ResetMenu()
  23. actionContainer.show();
  24. }
  25.  
  26. // Hide the menu
  27. if ( item.hidemenu ) {
  28. actionContainer.hide();
  29. }
  30. } );
  31. } )
  32.  
  33. // Hides all div elements that contain a data-parent, in
  34. // other words, hide all buttons in submenus.
  35. function ResetMenu() {
  36. $( "div" ).each( function( i, obj ) {
  37. var element = $( this );
  38.  
  39. if ( element.attr( "data-parent" ) ) {
  40. element.hide();
  41. } else {
  42. element.show();
  43. }
  44. } );
  45. }
  46.  
  47. // Configures every button click to use its data-action, or data-sub
  48. // to open a submenu.
  49. function init() {
  50. // Loops through every button that has the class of "menuoption"
  51. $( ".menuoption" ).each( function( i, obj ) {
  52.  
  53. // If the button has a data-action, then we set it up so when it is
  54. // pressed, we send the data to the lua side.
  55. if ( $( this ).attr( "data-action" ) ) {
  56. $( this ).click( function() {
  57. var data = $( this ).data( "action" );
  58.  
  59. sendData( "ButtonClick", data );
  60. } )
  61. }
  62.  
  63. // If the button has a data-sub, then we set it up so when it is
  64. // pressed, we show the submenu buttons, and hide all of the others.
  65. if ( $( this ).attr( "data-sub" ) ) {
  66. $( this ).click( function() {
  67. var menu = $( this ).data( "sub" );
  68. var element = $( "#" + menu );
  69. element.show();
  70. $( this ).parent().hide();
  71. } )
  72. }
  73. } );
  74. }
  75.  
  76. // Send data to lua for processing.
  77. function sendData( name, data ) {
  78. $.post( "http://wk_actionmenu/" + name, JSON.stringify( data ), function( datab ) {
  79. if ( datab != "ok" ) {
  80. console.log( datab );
  81. }
  82. } );
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement