dashboard.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Contains all the logic of the user dashboard, such as create canvas, remove one or logout
  2. $(function() {
  3. /* ================================================
  4. Miscellaneous
  5. ================================================= */
  6. // Prevent AJAX requests from being cached
  7. $.ajaxSetup({ cache: false });
  8. /* ================================
  9. REGISTERED USER HAS LOGGED IN:
  10. Load their canvases
  11. ================================= */
  12. // "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
  13. /* ---------------------------------------------------------
  14. An AJAX request to post the user's email to the server to
  15. load their saved canvases
  16. ---------------------------------------------------------- */
  17. /*
  18. If the user has no canvases it returns []. This is an example of what two canvases returned:
  19. [
  20. { "canvas_id": "6RnpIt9d9W", "user_id": "hello@arturocalvo.com", "canvas_name": "Test ARTURO 2", "canvas_date": "2016-07-16"},
  21. { "canvas_id": "E4YoiRJgSB", "user_id": "hello@arturocalvo.com", "canvas_name": "Test ARTURO 1", "canvas_date": "2016-07-16"}
  22. ]
  23. */
  24. var url = "load_user_canvases.php";
  25. $.post(url, {
  26. loggedin_user_email: loggedin_user_email
  27. }, function(data, status) {
  28. // The data is a string holding array of json
  29. // Concert the string to array of json to be able to loop it
  30. var canvasArray = jQuery.parseJSON(data);
  31. var canvas_color_index; // The color of the canvas in the gallery (is designed in dashbord.css, and assiged here with jQuery)
  32. $.each(canvasArray, function(index, canvasItem) {
  33. // Canvas gallery elements will be dynamically created by jQuery
  34. if (index < 5) {
  35. canvas_color_index = index;
  36. }
  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;
  41. if(canvasItem.canvas_version === "1.7") {
  42. canvasGalleryHTML += ' old-version';
  43. }
  44. canvasGalleryHTML += '"><h4>Canvas Title:</h4><h3>' + canvasItem.canvas_name + '</h3><p>Date Modified:<br />' + canvasItem.canvas_date + '</p>';
  45. if(canvasItem.canvas_version === "1.7") {
  46. canvasGalleryHTML += '<p><span class="glyphicon glyphicon-exclamation-sign"></span> Old version (1.7)</p>';
  47. }
  48. canvasGalleryHTML += '</div><button type="button" class="remove-canvas"><span class="glyphicon glyphicon-remove-circle" aria-hidden="true"></span>Remove</button></div>';
  49. // The added divs are appended to the outer gallery div .user-canvas-gallery
  50. $('.user-dashboard').find('.user-canvas-gallery').append(canvasGalleryHTML);
  51. });
  52. // If the AJAX request fails
  53. }).fail(function(jqXHR) {
  54. console.log("Error " + jqXHR.status + ' ' + jqXHR.statustext);
  55. });
  56. /* ---------------------------------------------------------
  57. An AJAX request to load shared canvases with the user
  58. ---------------------------------------------------------- */
  59. /*
  60. If the user has no canvases it returns []. This is an example of what two canvases returned:
  61. [
  62. { "canvas_id": "6RnpIt9d9W", "user_id": "hello@arturocalvo.com", "canvas_name": "Test ARTURO 2", "canvas_date": "2016-07-16"},
  63. { "canvas_id": "E4YoiRJgSB", "user_id": "hello@arturocalvo.com", "canvas_name": "Test ARTURO 1", "canvas_date": "2016-07-16"}
  64. ]
  65. */
  66. var url = "load_shared_canvases.php";
  67. $.post(url, {
  68. loggedin_user_email: loggedin_user_email
  69. }, function(data, status) {
  70. // The data is a string holding array of json
  71. // Concert the string to array of json to be able to loop it
  72. var canvasArray = jQuery.parseJSON(data);
  73. var canvas_color_index; // The color of the canvas in the gallery (is designed in dashbord.css, and assiged here with jQuery)
  74. $.each(canvasArray, function(index, canvasItem) {
  75. // Canvas gallery elements will be dynamically created by jQuery
  76. if (index < 5) {
  77. canvas_color_index = index;
  78. }
  79. else {
  80. canvas_color_index = index % 5;
  81. }
  82. 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;
  83. if(canvasItem.canvas_version === "1.7") {
  84. canvasGalleryHTML += ' old-version';
  85. }
  86. canvasGalleryHTML += '"><h4>Canvas Title:</h4><h3>' + canvasItem.canvas_name + '</h3><p>Owner:<br />' + canvasItem.user_id + '</p>';
  87. if(canvasItem.canvas_version === "1.7") {
  88. canvasGalleryHTML += '<p><span class="glyphicon glyphicon-exclamation-sign"></span> Old version (1.7)</p>';
  89. }
  90. canvasGalleryHTML += '</div></div>';
  91. // The added divs are appended to the outer gallery div .user-canvas-gallery
  92. $('.user-dashboard').find('.shared-canvases-gallery').append(canvasGalleryHTML);
  93. });
  94. // If the AJAX request fails
  95. }).fail(function(jqXHR) {
  96. console.log("Error " + jqXHR.status + ' ' + jqXHR.statustext);
  97. });
  98. /* ---------------------------------------------------------
  99. An AJAX request to load public canvases with the user
  100. ---------------------------------------------------------- */
  101. /*
  102. If the user has no canvases it returns []. This is an example of what two canvases returned:
  103. [
  104. { "canvas_id": "6RnpIt9d9W", "user_id": "hello@arturocalvo.com", "canvas_name": "Test ARTURO 2", "canvas_date": "2016-07-16"},
  105. { "canvas_id": "E4YoiRJgSB", "user_id": "hello@arturocalvo.com", "canvas_name": "Test ARTURO 1", "canvas_date": "2016-07-16"}
  106. ]
  107. */
  108. var url = "load_public_canvases.php";
  109. $.post(url, {
  110. loggedin_user_email: loggedin_user_email
  111. }, function(data, status) {
  112. // The data is a string holding array of json
  113. // Concert the string to array of json to be able to loop it
  114. var canvasArray = jQuery.parseJSON(data);
  115. var canvas_color_index; // The color of the canvas in the gallery (is designed in dashbord.css, and assiged here with jQuery)
  116. $.each(canvasArray, function(index, canvasItem) {
  117. // Canvas gallery elements will be dynamically created by jQuery
  118. if (index < 5) {
  119. canvas_color_index = index;
  120. }
  121. else {
  122. canvas_color_index = index % 5;
  123. }
  124. 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;
  125. if(canvasItem.canvas_version === "1.7") {
  126. canvasGalleryHTML += ' old-version';
  127. }
  128. canvasGalleryHTML += '"><h4>Canvas Title:</h4><h3>' + canvasItem.canvas_name + '</h3><p>Owner:<br />' + canvasItem.user_id + '</p>';
  129. if(canvasItem.canvas_version === "1.7") {
  130. canvasGalleryHTML += '<p><span class="glyphicon glyphicon-exclamation-sign"></span> Old version (1.7)</p>';
  131. }
  132. canvasGalleryHTML += '</div></div>';
  133. // The added divs are appended to the outer gallery div .user-canvas-gallery
  134. $('.user-dashboard').find('.public-canvases-gallery').append(canvasGalleryHTML);
  135. });
  136. // If the AJAX request fails
  137. }).fail(function(jqXHR) {
  138. console.log("Error " + jqXHR.status + ' ' + jqXHR.statustext);
  139. });
  140. /*=============================================
  141. Handling click on the log out button
  142. ============================================= */
  143. $('.user-dashboard').on('click', '.dashbord-logout-btn', function(event) {
  144. var url = "logout.php";
  145. $.post(url, function(data, status) {
  146. if (data == 200) {
  147. // Go to the landing page
  148. window.location.href = "../../index.html";
  149. }
  150. });
  151. });
  152. /*=============================================
  153. Handling click on each canvas in the gallery
  154. ============================================= */
  155. $(".user-dashboard").on("click", ".canvas-gallery-item", function(event) {
  156. event.stopPropagation();
  157. // The ID of the canvas that the user clicked on to load
  158. var canvas_ID = $(this).attr("id");
  159. // AJAX
  160. $.post("utils.php", {
  161. canvas_ID: canvas_ID
  162. }, function(data, status) {
  163. if(data == 200) {
  164. $.ajax({
  165. timeout: 5000,
  166. dataType: "text",
  167. type: "post",
  168. data: {
  169. "canvas_id": canvas_ID
  170. },
  171. url: "../php/check-version.php",
  172. success: function(returnData) {
  173. // Declarations
  174. var canvasVersion = returnData;
  175. // If the chosen canvas is version 1.8
  176. if(canvasVersion === "1.8") {
  177. window.location.href = "../index.php";
  178. }
  179. // If the chosen canvas is version 1.7
  180. else if(canvasVersion === "1.7") {
  181. window.location.href = "../1.7/index.php";
  182. }
  183. },
  184. error: function(xhr) {
  185. console.log(xhr.statusText);
  186. }
  187. });
  188. }
  189. });
  190. });
  191. /* ================================================
  192. If the user closes a dialog
  193. ================================================= */
  194. $("div.dialog button.close").on("click", function() {
  195. $("div#shadow").css("display", "none");
  196. $("div.dialog").css("display", "none");
  197. });
  198. /*==============================================
  199. Handling the click on "remove" btn for each canvas in the gallery
  200. ============================================== */
  201. function removeCanvas(canvas, remove_canvas_ID) {
  202. canvas.remove();
  203. // Also tell the back-end to remove this from the database
  204. var url = "remove-canvas.php";
  205. $.post(url, {
  206. remove_canvas_ID: remove_canvas_ID
  207. }, function(data, status) {
  208. console.log("response from remove-canvas.php: --DATA: " + data + " --STATUS:" + status);
  209. });
  210. }
  211. $(".user-dashboard").on("click", ".remove-canvas", function(event) {
  212. event.stopPropagation();
  213. // Declarations
  214. var canvas = $(this).closest(".canvas-gallery-item");
  215. var remove_canvas_ID = $(this).closest(".canvas-gallery-item").attr("id");
  216. // Show the Remove Canvas dialog
  217. $("div#shadow").css("display", "block");
  218. $("div#dialog-remove-canvas").css("display", "block");
  219. // If the user clicks on "Yes"
  220. $("#dialog-remove-canvas #button-yes").on("click", function() {
  221. $("div#shadow").css("display", "none");
  222. $("div#dialog-remove-canvas").css("display", "none");
  223. removeCanvas(canvas, remove_canvas_ID);
  224. });
  225. // If the user clicks on "Cancel"
  226. $("#dialog-remove-canvas #button-no").on("click", function() {
  227. $("div#shadow").css("display", "none");
  228. $("div#dialog-remove-canvas").css("display", "none");
  229. });
  230. });
  231. });