dashboard.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Contains all the logic of the user dashboard, such as create canvas, remove one or logout */
  2. $(function() {
  3. /*================================
  4. REGISTER 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. /* canvas gallery elements would be dynamically created by jQuery
  24. <div user-canvas-gallery>
  25. <!-- The canvases are added inside this div using jQurty-->
  26. <div class="canvas-gallery-item col-md-4 col-sm-6 text-center">
  27. <div class="col-md-12 test1">
  28. <h3>Canvas Title</h3>
  29. <p>created: <span>2016/09/14</span></p>
  30. </div>
  31. </div>
  32. <!-- next .canvas-gallery-item ....-->
  33. <div>
  34. */
  35. if (index < 5) {
  36. canvas_color_index = index;
  37. } else {
  38. canvas_color_index = index % 5;
  39. }
  40. 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>';
  41. //the added divs are appended to the outer gallery div .user-canvas-gallery
  42. $('.user-dashboard').find('.user-canvas-gallery').append(canvasGalleryHTML);
  43. }); // end of $.each loop for the user canvas data
  44. //if the AJAX request fails
  45. }).fail(function(jqXHR) {
  46. console.log("Error " + jqXHR.status + ' ' + jqXHR.statustext);
  47. }); // end of AJAX request t post the user's email
  48. /*=============================================
  49. Handling click on the log out button
  50. ===============================================*/
  51. $('.user-dashboard').on('click', '.dashbord-logout-btn', function(event) {
  52. var url = "logout.php";
  53. $.post(url, function(data, status) {
  54. if (data == 200) {
  55. // go to the landing page
  56. window.location.href = "../../index.html";
  57. }
  58. });
  59. });
  60. /*=============================================
  61. Handling click on each canvas in the gallery
  62. ===============================================*/
  63. $('.user-dashboard').on('click', '.canvas-gallery-item', function(event) {
  64. event.stopPropagation();
  65. //The id of the canvas that the user clicked on to load
  66. var canvas_ID = $(this).attr('id');
  67. $.post('utils.php', {
  68. canvas_ID: canvas_ID
  69. }, function(data, status) {
  70. if (data == 200) {
  71. window.location.href = "../index.php";
  72. }
  73. });
  74. // window.location.href='../index.php';
  75. }); //end of 'click' on '.canvas-gallery-item'
  76. /*=============================================
  77. Handling the click on "remove" btn for each
  78. canvas in the gallery
  79. ===============================================*/
  80. // .remove-canvas
  81. $('.user-dashboard').on('click', '.remove-canvas', function(event) {
  82. event.stopPropagation();
  83. //get the serialized canvas id for this element (given to the element as it's id attribute by the time of creation)
  84. var remove_canvas_ID = $(this).closest('.canvas-gallery-item').attr('id');
  85. $(this).closest('.canvas-gallery-item').remove();
  86. // Also tell the back end to remove this from the database
  87. var url = 'remove-canvas.php';
  88. $.post(url, {
  89. remove_canvas_ID: remove_canvas_ID
  90. }, function(data, status) {
  91. console.log("response from remove-canvas.php: --DATA: " + data + " --STATUS:" + status);
  92. }); //end of ajax post
  93. }); // end of 'click', '.remove-canvas'
  94. }); // end of dashbord.js file