myScript.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. $(document).ready(function () {
  2. // --------------------------------------
  3. // Async data loading
  4. function asyncSend(tUri, sObj, isTxtResp, sData= {}){
  5. var res = -1;
  6. $.ajax({
  7. method: "GET",
  8. url: tUri,
  9. data: sData,
  10. }).done(function (data) {
  11. try {
  12. if (isTxtResp) {
  13. sObj.innerText = data;
  14. } else {
  15. //console.log(data);
  16. sObj.removeClass('disabled');
  17. window.location = panelRoute;
  18. }
  19. }catch (e) {
  20. console.log(data);
  21. }
  22. //console.log(data);
  23. }).fail(function (jqXHR, textStatus) {
  24. console.error(textStatus);
  25. });
  26. return res;
  27. }
  28. // Password GenSimple
  29. function randDiapInt(min, max){
  30. return Math.round(min + Math.random() * (max - min));
  31. }
  32. $('#passgen').click(function () {
  33. var pass = "";
  34. for (var i=0; i<8; i++){
  35. pass += String.fromCharCode(randDiapInt(48,122) )
  36. }
  37. $('#password').attr("type","text");
  38. $('#password-confirm').attr("type","text");
  39. $('#password').val(pass);
  40. $('#flbl').text(pass);
  41. $('#password-confirm').val(pass);
  42. });
  43. // Loop palyback - WIP
  44. $('.repeat_btn').click(function () {
  45. $(this).toggleClass('active');
  46. var audio = $(this).attr('audio_id');
  47. $('#audio_object_'+audio).attr("loop", !$('#audio_object_'+audio).attr("loop"));
  48. });
  49. // "Rename track > save" button.
  50. $('.btn_rename').click(function () {
  51. var data = $(this).parent().prev().val();
  52. var myRoute = this.getAttribute('route');
  53. asyncSend(myRoute, null, true, {'newname':data});
  54. })
  55. // "Scan files" button
  56. $('#fScan').click(function () {
  57. $(this).addClass('disabled');
  58. asyncSend(scanRoute, $(this), false);
  59. });
  60. // --------------------------------------
  61. // My Player logic
  62. var playTimer = null;
  63. function btnIconSwitch(obj, dir) {
  64. if (dir) {
  65. obj.classList.remove('fa-play');
  66. obj.classList.add('fa-pause');
  67. } else {
  68. obj.classList.remove('fa-pause');
  69. obj.classList.add('fa-play');
  70. }
  71. }
  72. function secToHMS(sec) {
  73. var date = new Date(0);
  74. date.setSeconds(Math.round(sec));
  75. return date.toISOString().substr(11,8);
  76. }
  77. function playTimePos(audioID) {
  78. $("#curTime_"+audioID).text(
  79. secToHMS(
  80. document.getElementById('audio_object_' + audioID).currentTime
  81. )
  82. );
  83. }
  84. // Simple player dynamics
  85. var players = document.getElementsByClassName('play_btn');
  86. for (var i = 0; i < players.length; i++) {
  87. players[i].onclick = function () {
  88. // Finding UI elements
  89. var audioID = this.parentNode.getAttribute('audio_id');
  90. var audio = document.getElementById('audio_object_' + audioID);
  91. var btn = document.getElementById('play_ctrl_' + audioID);
  92. var playBadge = document.getElementById('play_num_' + audioID);
  93. // Handling Events
  94. audio.onended = function(){
  95. btnIconSwitch(btn, false);
  96. }
  97. if (audio.paused) {
  98. audio.play();
  99. playTimer = setInterval(function(){playTimePos(audioID)}, 500);
  100. btnIconSwitch(btn, true);
  101. // Play event
  102. var myRoute = this.getAttribute('route');
  103. var asRes = asyncSend(myRoute, playBadge, true);
  104. } else {
  105. audio.pause();
  106. clearInterval(playTimer);
  107. btnIconSwitch(btn, false);
  108. }
  109. }
  110. }
  111. // Set step time
  112. $('.dropdown-item').click(function () {
  113. var audioID = $(this).parent().attr("audio_id");
  114. $(this).siblings().each(function () {
  115. $(this).removeClass("active");
  116. });
  117. $(this).addClass("active");
  118. $("#audio_object_"+audioID).attr("step", $(this).attr("vsec"))
  119. });
  120. // Seek buttons handlers
  121. $(".s_bkw,.s_fwd").click(function () {
  122. var audioID = $(this).parent().attr("audio_id");
  123. var audioObj = document.getElementById("audio_object_" + audioID);
  124. if(Boolean($(this).attr("direction"))) {
  125. audioObj.currentTime += Number(audioObj.getAttribute("step"));
  126. } else {
  127. audioObj.currentTime -= Number(audioObj.getAttribute("step"));
  128. }
  129. })
  130. // Prescan Durations
  131. function loadedAudio(){
  132. document.getElementById("trkTime_" + audioID).innerText = secToHMS(audio.duration);
  133. }
  134. var audios = document.getElementsByClassName('audio');
  135. for (var i = 0; i < audios.length; i++) {
  136. var audioID = audios[i].getAttribute('audio_id');
  137. var audio = document.getElementById('audio_object_' + audioID);
  138. audio.addEventListener("loadeddata", loadedAudio());
  139. }
  140. // Downloads dynamics
  141. var downs = document.getElementsByClassName('dw_btn');
  142. for (var i = 0; i < downs.length; i++) {
  143. downs[i].onclick = function () {
  144. var audioID = this.getAttribute('audio_id');
  145. var dwBadge = document.getElementById('dw_num_' + audioID)
  146. var myRoute = this.getAttribute('route');
  147. asyncSend(myRoute, dwBadge, true); // download event
  148. }
  149. }
  150. });