HateoasClient.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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>Hateoas Videos 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>Videos API</strong>
  21. </a>
  22. </div>
  23. </div>
  24. </header>
  25. <main role="main">
  26. <div class="album py-5 bg-light">
  27. <div class="container">
  28. <div class="row" id="box_container">
  29. </div>
  30. <nav aria-label="Page navigation example">
  31. <ul class="pagination">
  32. <li class="page-item" id="btn_first" in_href="" onclick="get_video_page(this);"><span class="page-link"><<</span></li>
  33. <li class="page-item" id="btn_prev" in_href="" onclick="get_video_page(this);"><span class="page-link"><</span></li>
  34. <li class="page-item" id="btn_curr" in_href="" onclick="get_video_page(this);"><span class="page-link" href="#" id="current_page">1</span></li>
  35. <li class="page-item" id="btn_next" in_href="" onclick="get_video_page(this);"><span class="page-link">></span></li>
  36. <li class="page-item" id="btn_last" in_href="" onclick="get_video_page(this);"><span class="page-link">>></span></li>
  37. </ul>
  38. </nav>
  39. </div>
  40. </div>
  41. </main>
  42. <div class="modal modal-lg fade" id="authors_modal" tabindex="-1" role="dialog" aria-labelledby="authors_modal_label" aria-hidden="true">
  43. <div class="modal-dialog" role="document" id="authors_modal_content">
  44. </div>
  45. </div>
  46. <div class="modal modal-lgfade" id="comments_modal" tabindex="-1" role="dialog" aria-labelledby="comments_modal_label" aria-hidden="true">
  47. <div class="modal-dialog" role="document" id="comments_modal_content">
  48. </div>
  49. </div>
  50. <!-- Bootstrap core JavaScript
  51. ================================================== -->
  52. <!-- Placed at the end of the document so the pages load faster -->
  53. <script src="assets/js/vendor/jquery-slim.min.js"></script>
  54. <script src="assets/js/vendor/popper.min.js"></script>
  55. <script src="dist/js/bootstrap.min.js"></script>
  56. <script src="assets/js/vendor/holder.min.js"></script>
  57. <script>
  58. $(document).ready(function(){
  59. $.get('http://127.0.0.1:8080/api/v1/videos?page=0&size=6', function(data) {
  60. $('#box_container').html('');
  61. console.log(data._links);
  62. data._embedded.videoList.forEach((item, i) => {
  63. $(video_assembler(item)).appendTo($('#box_container'));
  64. });
  65. if('first' in data._links){$('#btn_first').attr('in_href', data._links.first.href);}
  66. if('prev' in data._links){$('#btn_prev').attr('in_href', data._links.prev.href);}
  67. if('next' in data._links){$('#btn_next').attr('in_href', data._links.next.href);}
  68. if('last' in data._links){$('#btn_last').attr('in_href', data._links.last.href);}
  69. $('#btn_self').attr('in_href', data._links.self.href);
  70. $('#current_page').html(data.page.number+1);
  71. });
  72. });
  73. function get_video_page(obj){
  74. $.get($(obj).attr('in_href'), function(data) {
  75. $('#box_container').html('');
  76. data._embedded.videoList.forEach((item, i) => {
  77. $(video_assembler(item)).appendTo($('#box_container'));
  78. });
  79. if('first' in data._links){$('#btn_first').attr('in_href', data._links.first.href);}
  80. if('prev' in data._links){$('#btn_prev').attr('in_href', data._links.prev.href);}
  81. if('next' in data._links){$('#btn_next').attr('in_href', data._links.next.href);}
  82. if('last' in data._links){$('#btn_last').attr('in_href', data._links.last.href);}
  83. $('#btn_self').attr('in_href', data._links.self.href);
  84. $('#current_page').html(data.page.number+1);
  85. });
  86. }
  87. function video_assembler(item){
  88. return $.parseHTML(`<div class="col-md-4">
  89. <div class="card mb-4 box-shadow">
  90. <img class="card-img-top" data-src="holder.js/100px225?theme=thumb&amp;bg=55595c&amp;fg=eceeef&amp;text=Thumbnail" alt="Thumbnail [100%x225]" style="height: 100px; width: 100%; display: block;" src="data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22348%22%20height%3D%22225%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20348%20225%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_18650818d5c%20text%20%7B%20fill%3A%23eceeef%3Bfont-weight%3Abold%3Bfont-family%3AArial%2C%20Helvetica%2C%20Open%20Sans%2C%20sans-serif%2C%20monospace%3Bfont-size%3A17pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_18650818d5c%22%3E%3Crect%20width%3D%22348%22%20height%3D%22225%22%20fill%3D%22%2355595c%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%22116.7265625%22%20y%3D%22120.3%22%3EThumbnail%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E" data-holder-rendered="true">
  91. <div class="card-body">
  92. <strong class="card-text">${item.title}</strong>
  93. <p class="card-text">${item.description}</p>
  94. <small class="text-muted">${item.addedDate}</small><br>
  95. <div class="d-flex justify-content-between align-items-center">
  96. <div class="btn-group">
  97. <button type="button" class="btn btn-sm btn-outline-secondary" onclick="remove_video('${item._links.self.href}');">Remove</button>
  98. <button type="button" class="btn btn-sm btn-outline-secondary" onclick="get_author('${item._links.user.href}');" data-toggle="modal" data-target="#authors_modal">Author</button>
  99. <button type="button" class="btn btn-sm btn-outline-secondary" onclick="get_comments('${item._links.all_comments.href}');" data-toggle="modal" data-target="#comments_modal">Comments</button>
  100. </div>
  101. </div>
  102. </div>
  103. </div>
  104. </div>`);
  105. }
  106. function remove_video(url){
  107. $.ajax({
  108. url: url,
  109. type: 'DELETE',
  110. success: function(d) {
  111. get_videos();
  112. }
  113. });
  114. }
  115. function get_author(url){
  116. $.get(url, function(item) {
  117. $('#authors_modal_content').html('');
  118. $($.parseHTML(`<div class="modal-content">
  119. <div class="modal-header">
  120. <h5 class="modal-title">Author info</h5>
  121. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  122. <span aria-hidden="true">&times;</span>
  123. </button>
  124. </div>
  125. <div class="modal-body">
  126. <p>${item.firstname} ${item.lastname}</p>
  127. <p>${item.email}</p>
  128. <p>${item.profession}</p>
  129. </div>
  130. <div class="modal-footer">
  131. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  132. </div>
  133. </div>`)).appendTo($('#authors_modal_content'));
  134. });
  135. }
  136. function get_comments(url){
  137. $.get(url, function(data) {
  138. var m_0 = `<div class="modal-content">
  139. <div class="modal-header">
  140. <h5 class="modal-title">Comments</h5>
  141. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  142. <span aria-hidden="true">&times;</span>
  143. </button>
  144. </div>
  145. <div class="modal-body">`;
  146. var m_1 = "";
  147. var m_2 = `</div>
  148. <div class="modal-footer">
  149. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  150. </div>
  151. </div>`;
  152. data.forEach((item, i) => {
  153. m_1 += `<p>Comment: ${item.id}</p><p><small class="text-muted">${item.addedDate}</small></p><p style="line-break:anywhere;">${item.comment}</p><br><hr><br>`;
  154. });
  155. $('#comments_modal_content').html('');
  156. $($.parseHTML(m_0+m_1+m_2)).appendTo($('#comments_modal_content'));
  157. });
  158. }
  159. </script>
  160. </body>
  161. </html>