Guest User

Untitled

a guest
May 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. <?php
  2.  
  3. // Controller Response
  4.  
  5. return response()->json([
  6. 'html' => [
  7. 'view' => view('ClientPanel.Modules.MenuTaskCounter')->render(),
  8. ],
  9. ]
  10. );
  11.  
  12. // View File
  13.  
  14. @if(auth()->user()->unseenTasks() !== 0)
  15. <span id="task-counter">
  16. <span class="kt-menu__link-badge">
  17. <span class="kt-badge kt-badge--success">{{ auth()->user()->unseenTasks() }}</span>
  18. </span>
  19. </span>
  20. @endif
  21.  
  22.  
  23. // JS Function
  24.  
  25. function notify(message,type = 'success') {
  26.  
  27. toastr.options = {
  28. "closeButton": false,
  29. "debug": false,
  30. "newestOnTop": false,
  31. "progressBar": false,
  32. "positionClass": "toast-bottom-left",
  33. "preventDuplicates": false,
  34. "onclick": null,
  35. "showDuration": "300",
  36. "hideDuration": "1000",
  37. "timeOut": "5000",
  38. "extendedTimeOut": "1000",
  39. "showEasing": "swing",
  40. "hideEasing": "linear",
  41. "showMethod": "fadeIn",
  42. "hideMethod": "fadeOut"
  43. };
  44.  
  45. if(type === 'success')
  46. {
  47. toastr.success(message);
  48. }
  49. else if(type === 'error')
  50. {
  51. toastr.error(message);
  52. }
  53. else if(type === 'warning')
  54. {
  55. toastr.warning(message);
  56. }
  57. }
  58.  
  59.  
  60. function post(requestURL,formElement,htmlResponseElement)
  61. {
  62. var formAccessor = $(formElement)[0];
  63. var form = new FormData(formAccessor);
  64.  
  65. var status = false;
  66.  
  67. $.ajax({
  68. headers: {
  69. 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
  70. },
  71. url: requestURL,
  72. type: 'POST',
  73. async: true,
  74. dataType: 'json',
  75. contentType: false,
  76. processData: false,
  77. data: form,
  78. success: function (response)
  79. {
  80.  
  81. if(response.notify != null)
  82. {
  83. notify(response.notify.message);
  84. }
  85.  
  86. if(response.html != null)
  87. {
  88. $(htmlResponseElement)[0].innerHTML = response.html.view;
  89. }
  90.  
  91. if(response.console != null)
  92. {
  93. console.log(response.console.message)
  94. }
  95.  
  96. if(response.refresh == true)
  97. {
  98. window.setTimeout(function(){
  99. location.reload();
  100. }, 3500 );
  101. }
  102.  
  103. if(response.status != null)
  104. {
  105. $("#ajax-response-data")[0].innerText = response.status;
  106. }
  107. },
  108. error: function (jqXhr, json, errorThrown)
  109. {
  110.  
  111. var errors = jqXhr.responseJSON;
  112.  
  113.  
  114. if(errors.message != null)
  115. {
  116. notify(errors.message,'warning');
  117. }
  118.  
  119. if(errors.errors != null)
  120. {
  121. $.each(errors['errors'], function (index, value)
  122. {
  123. notify(value[0],'error');
  124. });
  125. }
  126. }
  127. })
  128. }
  129.  
  130. // Usage ( onclick event )
  131.  
  132. var hasSeen = function (id)
  133. {
  134. post("{{ route('api.project.tasks.seen') }}","#form-id",'#task-counter');
  135. };
  136.  
  137. // Layout view with default value
  138. ............
  139. <div id='something etc' class='lol'>
  140. <span id="task-counter">
  141. <span class="kt-menu__link-badge">
  142. <span class="kt-badge kt-badge--success">{{ auth()->user()->unseenTasks() }}</span>
  143. </span>
  144. </span>
  145. </div>
  146. ................
Advertisement
Add Comment
Please, Sign In to add comment