dashboard.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Contains all the logic of the user dashboard, such as create canvas, remove one or logout */
  2. $(function() {
  3. /*================================
  4. REGISTERED USER HAS LOGGED IN:
  5. Load their canvases
  6. ================================*/
  7. // "loggedin_user_email" is the email of the user who's logged in. It is retrieved from the php script on top of the in the dashbord.php file and stored as a js variable in the js script the in the header of the dashboard.php file
  8. /* ---------------------------------------------------------
  9. An AJAX request to post the user's email to the server to
  10. load their saved canvases
  11. -----------------------------------------------------------*/
  12. /*If the user has no canvases it returns []. This is an example of what two canvases returned:
  13. [{"canvas_id":"6RnpIt9d9W","user_id":"hello@arturocalvo.com","canvas_name":"Test ARTURO 2","canvas_date":"2016-07-16"},{"canvas_id":"E4YoiRJgSB","user_id":"hello@arturocalvo.com","canvas_name":"Test ARTURO 1","canvas_date":"2016-07-16"}]*/
  14. var url = "load_user_canvases.php";
  15. $.post(url, {
  16. loggedin_user_email: loggedin_user_email
  17. }, function(data, status) {
  18. //the data is a string holding array of json
  19. //concert the string to array of json to be able to loop it
  20. var canvasArray = jQuery.parseJSON(data);
  21. var canvas_color_index; //the color of the canvas in the gallery ( is designed in dashbord.css, and assiged here with jQuery)
  22. $.each(canvasArray, function(index, canvasItem) {
  23. /*
  24. canvas gallery elements would be dynamically created by jQuery
  25. <div user-canvas-gallery>
  26. <!-- The canvases are added inside this div using jQurty-->
  27. <div class="canvas-gallery-item col-md-4 col-sm-6 text-center">
  28. <div class="col-md-12 test1">
  29. <h3>Canvas Title</h3>
  30. <p>created: <span>2016/09/14</span></p>
  31. </div>
  32. </div>
  33. <!-- next .canvas-gallery-item ....-->
  34. <div>
  35. */
  36. if (index < 5) {
  37. canvas_color_index = index;
  38. }
  39. else {
  40. canvas_color_index = index % 5;
  41. }
  42. var canvasGalleryHTML = '<div class="canvas-gallery-item col-md-4 col-sm-6 text-center" id="' + canvasItem.canvas_id + '"><div class="col-md-12 color' + canvas_color_index + '"><h4>Canvas Title:</h4><h3>' + canvasItem.canvas_name + '</h3><p>Created:</p><p>' + canvasItem.canvas_date + '</p></div><button type="button" class="remove-canvas"><span class="glyphicon glyphicon-remove-circle" aria-hidden="true"></span>Remove</button></div>';
  43. //the added divs are appended to the outer gallery div .user-canvas-gallery
  44. $('.user-dashboard').find('.user-canvas-gallery').append(canvasGalleryHTML);
  45. }); // End of $.each loop for the user canvas data
  46. // If the AJAX request fails
  47. }).fail(function(jqXHR) {
  48. console.log("Error " + jqXHR.status + ' ' + jqXHR.statustext);
  49. }); // End of AJAX request t post the user's email
  50. /* ---------------------------------------------------------
  51. An AJAX request to load canvases shared with the user
  52. -----------------------------------------------------------*/
  53. /*If the user has no canvases it returns []. This is an example of what two canvases returned:
  54. [{"canvas_id":"6RnpIt9d9W","user_id":"hello@arturocalvo.com","canvas_name":"Test ARTURO 2","canvas_date":"2016-07-16"},{"canvas_id":"E4YoiRJgSB","user_id":"hello@arturocalvo.com","canvas_name":"Test ARTURO 1","canvas_date":"2016-07-16"}]*/
  55. var url = "load_other_canvases.php";
  56. $.post(url, {
  57. loggedin_user_email: loggedin_user_email
  58. }, function(data, status) {
  59. //the data is a string holding array of json
  60. //concert the string to array of json to be able to loop it
  61. var canvasArray = jQuery.parseJSON(data);
  62. var canvas_color_index; //the color of the canvas in the gallery ( is designed in dashbord.css, and assiged here with jQuery)
  63. $.each(canvasArray, function(index, canvasItem) {
  64. /*
  65. canvas gallery elements would be dynamically created by jQuery
  66. <div user-canvas-gallery>
  67. <!-- The canvases are added inside this div using jQurty-->
  68. <div class="canvas-gallery-item col-md-4 col-sm-6 text-center">
  69. <div class="col-md-12 test1">
  70. <h3>Canvas Title</h3>
  71. <p>created: <span>2016/09/14</span></p>
  72. </div>
  73. </div>
  74. <!-- next .canvas-gallery-item ....-->
  75. <div>
  76. */
  77. if (index < 5) {
  78. canvas_color_index = index;
  79. }
  80. else {
  81. canvas_color_index = index % 5;
  82. }
  83. var canvasGalleryHTML = '<div class="canvas-gallery-item col-md-4 col-sm-6 text-center" id="' + canvasItem.canvas_id + '"><div class="col-md-12 color' + canvas_color_index + '"><h4>Canvas Title:</h4><h3>' + canvasItem.canvas_name + '</h3><p>Owner:</p><p>' + canvasItem.owner + '</p></div></div>';
  84. //the added divs are appended to the outer gallery div .user-canvas-gallery
  85. $('.user-dashboard').find('.shared-canvases-gallery').append(canvasGalleryHTML);
  86. }); // End of $.each loop for the user canvas data
  87. // If the AJAX request fails
  88. }).fail(function(jqXHR) {
  89. console.log("Error " + jqXHR.status + ' ' + jqXHR.statustext);
  90. }); // End of AJAX request t post the user's email
  91. /*=============================================
  92. Handling click on the log out button
  93. ===============================================*/
  94. $('.user-dashboard').on('click', '.dashbord-logout-btn', function(event) {
  95. var url = "logout.php";
  96. $.post(url, function(data, status) {
  97. if (data == 200) {
  98. // go to the landing page
  99. window.location.href = "../../index.html";
  100. }
  101. });
  102. });
  103. /*=============================================
  104. Handling click on each canvas in the gallery
  105. ===============================================*/
  106. $('.user-dashboard').on('click', '.canvas-gallery-item', function(event) {
  107. event.stopPropagation();
  108. //The id of the canvas that the user clicked on to load
  109. var canvas_ID = $(this).attr('id');
  110. $.post('utils.php', {
  111. canvas_ID: canvas_ID
  112. }, function(data, status) {
  113. if (data == 200) {
  114. window.location.href = "../index.php";
  115. }
  116. });
  117. // window.location.href='../index.php';
  118. }); // End of 'click' on '.canvas-gallery-item'
  119. /*=============================================
  120. Handling the click on "remove" btn for each
  121. canvas in the gallery
  122. ===============================================*/
  123. // .remove-canvas
  124. $('.user-dashboard').on('click', '.remove-canvas', function(event) {
  125. event.stopPropagation();
  126. // Get the serialized canvas id for this element (given to the element as it's id attribute by the time of creation)
  127. var remove_canvas_ID = $(this).closest('.canvas-gallery-item').attr('id');
  128. $(this).closest('.canvas-gallery-item').remove();
  129. // Also tell the back end to remove this from the database
  130. var url = 'remove-canvas.php';
  131. $.post(url, {
  132. remove_canvas_ID: remove_canvas_ID
  133. }, function(data, status) {
  134. console.log("response from remove-canvas.php: --DATA: " + data + " --STATUS:" + status);
  135. }); // End of ajax post
  136. }); // End of 'click', '.remove-canvas'
  137. }); // End of dashbord.js file