PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/04_mp3Tutorial/js.js

https://gitlab.com/edward.pattillo/phpCourse
JavaScript | 363 lines | 212 code | 110 blank | 41 comment | 29 complexity | 0e35af649f0249f7aaeb3ef64cfbc0f3 MD5 | raw file
  1. // our event listeners go inside the document ready function
  2. $(document).ready(function() {
  3. $('#mainSearch').focus();
  4. $('#delete').hide();
  5. $('#update').hide();
  6. var formData = new FormData();
  7. var isImageUploaded = 0;
  8. // --------------------------------- jQuery UI widgets -------------------------------
  9. $("#accordion").accordion({
  10. animate: 200,
  11. autoFill: true,
  12. autoHeight: true
  13. });
  14. $('#searchDate').datepicker({
  15. changeMonth: true,
  16. changeYear: true,
  17. dateFormat: 'yy-mm-dd'
  18. });
  19. var slider = document.getElementById('control');
  20. var trackArea = document.getElementById('trackArea');
  21. $("#toggle").click(function() {
  22. var isOpen = slider.classList.contains('slide-in');
  23. slider.setAttribute('class', isOpen ? 'slide-out' : 'slide-in');
  24. trackArea.setAttribute('class', isOpen ? 'trackArea-grow' : 'trackArea-shrink');
  25. });
  26. // --------------------------------- event listeners -------------------------------
  27. $('#save').click(function() {
  28. var killFlag = 0;
  29. if(!isImageUploaded) {
  30. alert("You need to upload an image.");
  31. killFlag = 1;
  32. }
  33. if($('#artistInput').val()) {
  34. formData.append('artistInput', $('#artistInput').val());
  35. } else {
  36. alert("You need to fill in the artist's name.");
  37. killFlag = 1;
  38. }
  39. if($('#albumInput').val()) {
  40. formData.append('albumInput', $('#albumInput').val());
  41. } else {
  42. alert("You need to fill in the album's name.");
  43. killFlag = 1;
  44. }
  45. if($('#yearInput').val()) {
  46. formData.append('yearInput', $('#yearInput').val());
  47. } else {
  48. alert("You need to fill in the album's release date.");
  49. killFlag = 1;
  50. }
  51. var i = 0;
  52. $.each($('.trackInputAreas :input'), function() {
  53. if($(this).val()) {
  54. var trackID = this.id;
  55. var trackName = $(this).val();
  56. formData.append(trackID, trackName);
  57. i = 1;
  58. } else {
  59. if(i === 0) {
  60. alert("You need to fill in at least one track name.");
  61. killFlag = 1;
  62. }
  63. }
  64. });
  65. if (killFlag === 0) { submitAlbumInfo(formData); }
  66. });
  67. $('#fileUpButton').click(function() {
  68. $('#uploadFile').click();
  69. });
  70. $('#uploadFile').change(function() {
  71. var imgPath = $(this)[0].value;
  72. // console.log(imgPath);
  73. var extn = imgPath.substring(imgPath.lastIndexOf('.') +1).toLowerCase();
  74. if (extn == "gif" || extn == "jpg" || extn == "jpeg" || extn == "png" || extn == "bmp") {
  75. // console.log(extn);
  76. var reader = new FileReader();
  77. reader.onload = function(e) {
  78. // get loaded image and render a thumbnail
  79. $('.imageInputAreas').html('<img id="showImage" src="" width="210" height="210" />');
  80. $('#showImage').attr('src', e.target.result);
  81. };
  82. // read the image file as a data URL
  83. reader.readAsDataURL(this.files[0]);
  84. formData.append('image', this.files[0]);
  85. isImageUploaded = 1;
  86. } else {
  87. alert("Please select an image file.");
  88. }
  89. });
  90. $("body").on("click", ".plusTrackButton", function(e) {
  91. // console.log(this.id);
  92. var trackId = this.id;
  93. $('#' + trackId).fadeOut(100);
  94. var temp = trackId.split("-");
  95. var numericID = parseInt(temp[1]);
  96. var trackHTML = '<div class="trackInputAreas">';
  97. trackHTML += '<input class="trackInput" placeholder="Track" id="trackInput-' + (numericID + 1) + '">';
  98. trackHTML += '<i class="material-icons plusTrackButton" id="plusTrackButton-' + (numericID + 1) + '">add_circle</i>';
  99. trackHTML += '</div>';
  100. $('#bottomAccordian').append(trackHTML);
  101. });
  102. $("#mainSearch").keypress(function(e) {
  103. var key = e.which;
  104. if (key == 13) {
  105. var searchString = $("#mainSearch").val();
  106. var dateType = $('#dateType option:selected').text();
  107. var searchDate = $("#searchDate").val();
  108. getTracks('search', searchString, dateType, searchDate);
  109. }
  110. });
  111. $("#searchButton").click(function() {
  112. var searchString = $("#mainSearch").val();
  113. var dateType = $('#dateType option:selected').text();
  114. var searchDate = $("#searchDate").val();
  115. getTracks('search', searchString, dateType, searchDate);
  116. });
  117. // event listener to clear the contents of the trackArea
  118. $('#clearTracks').click(function() {
  119. $('#trackArea').html('');
  120. }); // END OF $('#clearTracks').click(function() {
  121. $(document).on("click", ".track", function() {
  122. console.log(this.id);
  123. $('#ui-id-2').click();
  124. $('#delete').show();
  125. $('#update').show();
  126. $('#save').hide();
  127. var thisArtist = $('#'+this.id+' '+'.artistName').text();
  128. var thisTrack = $('#'+this.id+' '+'.trackName').text();
  129. var thisAlbum = $('#'+this.id+' '+'.trackInfo').attr('albumName');
  130. $('#artistInput').val(thisArtist);
  131. $('#trackInput-1').val(thisTrack);
  132. $('#albumInput').val(thisAlbum);
  133. $('#trackEditID').val(this.id);
  134. });
  135. $('#update').click(function() {
  136. var trackID = $('#trackEditID').val();
  137. formData.append('action', 'update');
  138. formData.append('artistInput', $('#artistInput').val());
  139. formData.append('albumInput', $('#albumInput').val());
  140. formData.append('trackInput', $('#trackInput-1').val());
  141. formData.append('trackEditID', trackID);
  142. $('#'+trackID).remove();
  143. submitAlbumInfo(formData);
  144. });
  145. $('#delete').click(function() {
  146. formData.append('action', 'delete');
  147. var trackID = $('#trackEditID').val();
  148. formData.append('trackEditID', trackID);
  149. $('#'+trackID).remove();
  150. submitAlbumInfo(formData);
  151. });
  152. }); // END OF $(document).ready(function() {
  153. // ---------------------- functions -------------------------------
  154. // this function makes an AJAX request that gets tracks by a particular artist
  155. function submitAlbumInfo(formData) {
  156. $.ajax({
  157. // specifices where the AJAX request will be sent to on the server
  158. url: "ajaxSubmit.php",
  159. // an object with the data we want to send to the receiving file on the server
  160. data: formData,
  161. processData: false,
  162. contentType: false,
  163. // type of request to send (either GET or POST)
  164. type: "POST",
  165. // type of data to expect back from the server
  166. // The available data types are text, html, xml, json, jsonp, and script.
  167. dataType: 'json'
  168. // dataType: 'text'
  169. })
  170. // .done defines what will happen when the ajax request is complete
  171. // we're going to name the response "returnData" and pass it to a function
  172. // that will present the track information
  173. .done(function(returnData) {
  174. console.log(returnData);
  175. if (returnData.length === 0) {
  176. presentNoResults();
  177. } else {
  178. $("#mainSearch").val('');
  179. // console.log(returnData);
  180. $.each(returnData, function(key, trackInfo) {
  181. presentTrack(trackInfo);
  182. });
  183. }
  184. })
  185. // .fail defines what will happen when the ajax request fails and doesn't return a JSON object
  186. // we will have it present the error message returned from PHP at the top of the page
  187. .fail(function(returnData) {
  188. // console.log(returnData);
  189. $('body').prepend(returnData.responseText);
  190. });
  191. } // END OF submitAlbumInfo
  192. // this function makes an AJAX request that gets tracks by a particular artist
  193. function getTracks(queryType, p1, p2, p3) {
  194. $.ajax({
  195. // specifices where the AJAX request will be sent to on the server
  196. url: "ajax.php",
  197. // an object with the data we want to send to the receiving file on the server
  198. data: {
  199. queryType: queryType,
  200. p1: p1,
  201. p2: p2,
  202. p3: p3
  203. },
  204. // type of request to send (either GET or POST)
  205. type: "POST",
  206. // type of data to expect back from the server
  207. // The available data types are text, html, xml, json, jsonp, and script.
  208. dataType: 'json'
  209. })
  210. // .done defines what will happen when the ajax request is complete
  211. // we're going to name the response "returnData" and pass it to a function
  212. // that will present the track information
  213. .done(function(returnData) {
  214. if (returnData.length === 0) {
  215. presentNoResults();
  216. } else {
  217. $("#mainSearch").val('');
  218. // console.log(returnData);
  219. $.each(returnData, function(key, trackInfo) {
  220. presentTrack(trackInfo);
  221. });
  222. }
  223. })
  224. // .fail defines what will happen when the ajax request fails and doesn't return a JSON object
  225. // we will have it present the error message returned from PHP at the top of the page
  226. .fail(function(returnData) {
  227. // console.log(returnData);
  228. $('body').prepend(returnData.responseText);
  229. });
  230. } // END OF getTracks()
  231. function presentNoResults() {
  232. // build a string that contains the HTML for presenting our track
  233. var trackHTML = '<div class="trackError">';
  234. trackHTML += '<div class="trackInfo">';
  235. trackHTML += 'No tracks found for ' + $("#mainSearch").val();
  236. trackHTML += '</div>';
  237. trackHTML += '</div>';
  238. // add this HTML as a child to the #trackArea div in our HTML
  239. $('#trackArea').append(trackHTML);
  240. $('.trackError').fadeOut(4000);
  241. }
  242. function presentTrack(trackInfo) {
  243. // build a string that contains the HTML for presenting our track
  244. var trackHTML = '<div class="track" id="'+trackInfo.trackID+'">';
  245. trackHTML += '<img src="albumArt/' + trackInfo.artistName + ' - ' + trackInfo.albumName + '.jpg" />';
  246. trackHTML += '<div class="trackInfo" albumName="'+trackInfo.albumName +'">';
  247. trackHTML += '<div class="artistName">'+trackInfo.artistName + '</div>';
  248. trackHTML += '<div class="trackName">'+trackInfo.trackName + '</div>';
  249. trackHTML += '</div>';
  250. trackHTML += '</div>';
  251. // add this HTML as a child to the #trackArea div in our HTML
  252. $('#trackArea').append(trackHTML);
  253. }