client.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  6. <meta name="description" content="">
  7. <meta name="author" content="">
  8. <link rel="icon" href="favicon.ico">
  9. <title>Reactive API client</title>
  10. <!-- Bootstrap core CSS -->
  11. <link href="dist/css/bootstrap.min.css" rel="stylesheet">
  12. <!-- Custom styles for this template -->
  13. <link href="dist/css/album.css" rel="stylesheet">
  14. </head>
  15. <body>
  16. <header>
  17. <div class="navbar navbar-dark bg-dark box-shadow">
  18. <div class="container d-flex justify-content-between">
  19. <a href="#" class="navbar-brand d-flex align-items-center">
  20. <strong>Reactive API client</strong>
  21. </a>
  22. <span class="btn btn-warning" id="clear_btn" style="float:right;top:0;">Clear</span>
  23. </div>
  24. </div>
  25. </header>
  26. <main role="main">
  27. <div class="album py-5 bg-light" style="height:40px;padding-bottom:0px !important;padding-top:25px !important;margin-top:0px;">
  28. <div class="" style="height:40px;margin-left:100px;">
  29. <div class="row" id="menu" style="max-height:40px;">
  30. <span class="btn btn-primary" id="new_doc" style="height:40px;margin-right:25px;">New document</span>
  31. <span class="btn btn-primary" id="get_elements" style="height:40px;margin-right:25px;">Retrieve All</span>
  32. <div class="input-group mb-1" style="height:40px;margin-right:25px;width:auto;">
  33. <span class="btn btn-primary" id="get_doc">Retrieve by id</span>
  34. <div class="input-group-append">
  35. <input class="input-group-text" id="get_num_doc" type="text" size="3" style="width: 80px;"> </span>
  36. </div>
  37. </div>
  38. <div class="input-group mb-1" style="height:40px;margin-right:25px;width:auto;">
  39. <span class="btn btn-primary" id="get_doc_author">Retrieve by author</span>
  40. <div class="input-group-append">
  41. <input class="input-group-text" id="get_author" type="text" style="width: 300px;"> </span>
  42. </div>
  43. </div>
  44. <div class="input-group mb-1" style="height:40px;margin-right:25px;width:auto;">
  45. <span class="btn btn-primary" id="get_doc_publisher">Retrieve by publisher</span>
  46. <div class="input-group-append">
  47. <input class="input-group-text" id="get_publisher" type="text" style="width: 300px;"> </span>
  48. </div>
  49. </div>
  50. </div>
  51. </div>
  52. </div>
  53. <div class="album py-5 bg-light">
  54. <div class="container">
  55. <div class="row" id="notification_container">
  56. </div>
  57. </div>
  58. </div>
  59. </main>
  60. <div class="modal modal-lg fade" id="new_modal" tabindex="-1" role="dialog" aria-labelledby="#new_modal" aria-hidden="true">
  61. <div class="modal-dialog" role="document">
  62. <div class="modal-content">
  63. <div class="modal-header">
  64. <h5 class="modal-title">Add a new document</h5>
  65. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  66. <span aria-hidden="true">&times;</span>
  67. </button>
  68. </div>
  69. <div class="modal-body">
  70. <form>
  71. <div class="form-group">
  72. <label>Title</label>
  73. <input type="text" class="form-control" placeholder="" id="doc_title">
  74. </div>
  75. <div class="form-group">
  76. <label>Publisher</label>
  77. <input type="email" class="form-control" placeholder="" id="doc_publisher">
  78. </div>
  79. <div class="form-group">
  80. <label>Author</label>
  81. <input type="email" class="form-control" placeholder="" id="doc_author">
  82. </div>
  83. <div class="form-group">
  84. <label>Content</label>
  85. <textarea class="form-control" rows="5" id="doc_content"></textarea>
  86. </div>
  87. </form>
  88. </div>
  89. <div class="modal-footer">
  90. <span class="btn btn-primary" onclick="send_document();" data-dismiss="modal">Send</span>
  91. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  92. </div>
  93. </div>
  94. </div>
  95. </div>
  96. <!-- Bootstrap core JavaScript
  97. ================================================== -->
  98. <!-- Placed at the end of the document so the pages load faster -->
  99. <script src="assets/js/vendor/jquery-slim.min.js"></script>
  100. <script src="assets/js/vendor/popper.min.js"></script>
  101. <script src="dist/js/bootstrap.min.js"></script>
  102. <script src="assets/js/vendor/holder.min.js"></script>
  103. <script>
  104. var base_url = 'http://127.0.0.1:8080/api/v1/news';
  105. $(document).ready(function(){
  106. $('#new_doc').click(function(){$("#new_modal").modal().show();});
  107. $('#clear_btn').click(function(){clear();});
  108. $('#get_doc').click(function(){get_document($('#get_num_doc').val())});
  109. $('#get_num_doc').keypress(function(event){
  110. var keycode = (event.keyCode ? event.keyCode : event.which);
  111. if(keycode == '13'){ get_document($('#get_num_doc').val()); }
  112. });
  113. $('#get_elements').click(function(){get_documents()});
  114. $('#get_doc_author').click(function(){filter_documents('author', $('#get_author').val())});
  115. $('#get_author').keypress(function(event){
  116. var keycode = (event.keyCode ? event.keyCode : event.which);
  117. if(keycode == '13'){ filter_documents('author', $('#get_author').val()); }
  118. });
  119. $('#get_doc_publisher').click(function(){filter_documents('publisher', $('#get_publisher').val())});
  120. $('#get_publisher').keypress(function(event){
  121. var keycode = (event.keyCode ? event.keyCode : event.which);
  122. if(keycode == '13'){ filter_documents('publisher', $('#get_publisher').val()); }
  123. });
  124. });
  125. function get_document(id){
  126. fetch(base_url+'/'+id)
  127. .then(r => r.json())
  128. .then(d => {
  129. $(assembler(d)).appendTo($('#notification_container'));
  130. })
  131. .catch(err=>{});
  132. }
  133. function filter_documents(t, x){
  134. clear();
  135. var evtSource = new EventSource(base_url+'/'+t+'/'+encodeURIComponent(x));
  136. evtSource.onmessage = function (event) {
  137. if(!event){ evtSource.close(); console.log("[I] Filter event closed");}
  138. $(assembler(JSON.parse(event.data))).appendTo($('#notification_container'));
  139. }
  140. evtSource.onerror = function(event) {
  141. evtSource.close();
  142. }
  143. }
  144. function get_documents(){
  145. clear();
  146. var evtSource = new EventSource(base_url);
  147. evtSource.onmessage = function (event) {
  148. $(assembler(JSON.parse(event.data))).appendTo($('#notification_container'));
  149. }
  150. evtSource.onerror = function(event) {
  151. evtSource.close();
  152. }
  153. }
  154. function assembler(item){
  155. return $.parseHTML(`<div class="col-md-4" style="padding:10px;" id="video_${item.id}">
  156. <div class="card mb-4 box-shadow" style="padding:10px;">
  157. <span class="card-text btn-secondary" style="width:55px;border-radius:3%;margin-right:10px;text-align:center;">${item.id}</span>
  158. <br>
  159. <strong class="card-text">${item.title}</strong>
  160. <div class="card-body">
  161. <span class="card-text"><small>${item.date_add}</small></span><br>
  162. <span class="card-text"><small>Publisher: ${item.publisher}</small></span><br>
  163. <span class="card-text"><small>Author: ${item.author}</small></span>
  164. </div>
  165. <span>${item.content}...</span>
  166. <div class="d-flex justify-content-between align-items-center">
  167. <div class="btn-group">
  168. <button type="button" class="btn btn-sm btn-outline-secondary" onclick="remove('${item.id}');">Remove</button>
  169. </div>
  170. </div>
  171. </div>
  172. </div>`);
  173. }
  174. function send_document(){
  175. const options = {
  176. method: "POST",
  177. headers: new Headers({'content-type': 'application/json'}),
  178. body: JSON.stringify({title:$('#doc_title').val(), publisher:$('#doc_publisher').val(), author:$('#doc_author').val(), content:$('#doc_content').val()})
  179. };
  180. fetch(base_url, options)
  181. .then(r => r.json())
  182. .then(d => {get_document(d.id);})
  183. .catch(err => {console.log(err);});
  184. $("#new_modal").modal().hide();
  185. }
  186. function remove(id){
  187. const options = {
  188. method: "DELETE",
  189. };
  190. fetch(base_url+'/'+id, options)
  191. .then(r => {$('#video_'+id).remove();})
  192. .catch(err => {console.log(err);});
  193. }
  194. function clear(){
  195. $('#notification_container').html('');
  196. }
  197. </script>
  198. </body>
  199. </html>