PageRenderTime 23ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/module/cfrc/js/playsheet.js

https://gitlab.com/gramie/dotlog
JavaScript | 177 lines | 153 code | 13 blank | 11 comment | 29 complexity | 7434c40f2f44f7fa99ba077540318cbb MD5 | raw file
  1. Playsheet = function(tableID, editable) {
  2. this.table = jQuery('#' + tableID);
  3. this.startTime = getDrupalFormTime();
  4. };
  5. Playsheet.prototype.renderTableFooter = function(playsheetItems) {
  6. var totalRow = '<tr>';
  7. var totals = this.getTotals(playsheetItems);
  8. var timeCols = 2;
  9. totalRow += '<th>Spoken total:</th>';
  10. if (playsheetEditable) {
  11. totalRow += '<th>' + this.formatTime(totals.spokenDuration) + '</th>';
  12. } else {
  13. totalRow += '<th></th>';
  14. timeCols = 1;
  15. }
  16. totalRow += '<th colspan="' + timeCols + '"></th>'
  17. + '<th>' + totals.newsong + '<br />'
  18. + this.getPercent(totals.newsong, totals.allMusic) + '%' + '<br />'
  19. + 'New' + '</th>'
  20. + '<th>' + totals.cancon + '<br />'
  21. + this.getPercent(totals.cancon, totals.allMusic) + '%' + '<br />'
  22. + 'CanCon' + '</th>'
  23. + '<th>' + totals.instrumental + '<br />'
  24. + this.getPercent(totals.instrumental, totals.allMusic) + '%' + '<br />'
  25. + 'Instr' + '</th>'
  26. + '<th>' + totals.hit + '<br />'
  27. + this.getPercent(totals.hit, totals.allMusic) + '%' + '<br />'
  28. + 'Hit' + '</th>'
  29. + '<th></th>'
  30. + '<th></th>';
  31. if (playsheetEditable) {
  32. totalRow += '<th></th>';
  33. }
  34. totalRow += '</tr>';
  35. this.table.find('tfoot').html(totalRow);
  36. };
  37. Playsheet.prototype.getPercent = function(part, all) {
  38. if (all == 0) {
  39. return (0);
  40. }
  41. return (Math.floor((part/all) * 100));
  42. }
  43. Playsheet.prototype.getTotals = function(playsheetItems) {
  44. var totals = {
  45. cancon: 0,
  46. hit: 0,
  47. instrumental: 0,
  48. newsong: 0,
  49. allMusic: 0,
  50. spokenDuration: 0
  51. };
  52. for(var i = 0; i < playsheetItems.length; i++) {
  53. var item = playsheetItems[i];
  54. if (item.crtc >= 20 && item.crtc <= 39) {
  55. // This is music
  56. if (item.newsong) {
  57. totals.newsong++;
  58. }
  59. if (item.hit) {
  60. totals.hit++;
  61. }
  62. if (item.cancon) {
  63. totals.cancon++;
  64. }
  65. if (item.instrumental) {
  66. totals.instrumental++;
  67. }
  68. totals.allMusic++;
  69. } else {
  70. // This is Spoken Word
  71. totals.spokenDuration += parseInt(item.duration);
  72. }
  73. }
  74. return (totals);
  75. }
  76. Playsheet.prototype.fillPlaysheetGrid = function(data, startTime, endTime) {
  77. var tableData = '';
  78. for(var i = 0; i < data.length; i++) {
  79. // Fill the row
  80. tableData += this.formatRow(data, i);
  81. }
  82. this.table.find('tbody').html(tableData);
  83. this.renderTableFooter(data);
  84. };
  85. Playsheet.prototype.formatRow = function(data, i) {
  86. var row = data[i];
  87. var newsong = row.newsong? 'checked="1"' : '';
  88. var cancon = row.cancon? 'checked="1"' : '';
  89. var instrumental = row.instrumental? 'checked="1"' : '';
  90. var hit = row.hit? 'checked="1"' : '';
  91. var rowclass = (i % 2 == 0) ? 'even' : 'odd';
  92. var Result = '<tr class="' + rowclass + '">';
  93. var entryTime = getDrupalFormTime('start') + parseInt(row.startMinutes);
  94. Result += '<td class="entry-start-time">' + this.formatTime(entryTime) + '</td>';
  95. entryTime += parseInt(row.duration);
  96. Result += '<td class="entry-end-time">' + this.formatTime(entryTime) + '</td>'
  97. + '<td>' + row.artist + '</td>'
  98. + '<td>' + row.song + '</td>'
  99. + '<td class="check-column"><input type="checkbox" disabled="disabled" class="new-check" ' + newsong + '" /></td>'
  100. + '<td class="check-column"><input type="checkbox" disabled="disabled" class="cancon-check" ' + cancon + '" /></td>'
  101. + '<td class="check-column"><input type="checkbox" disabled="disabled" class="instrumental-check" ' + instrumental + '" /></td>'
  102. + '<td class="check-column"><input type="checkbox" disabled="disabled" class="hit-check" ' + hit + '" /></td>'
  103. + '<td><span title="' + crtcValues[row.crtc] + '">' + row.crtc + '</span></td>'
  104. + '<td>' + row.language + '</td>';
  105. if (playsheetEditable) {
  106. Result += '<td><div class="playsheet-buttons">';
  107. Result += '<button type="button" onclick="editPlaysheetEntry(' + i + ');" title="Edit entry">&#9998;</button>';
  108. Result += '<button type="button" onclick="deletePlaysheetEntry(' + i + ');" title="Delete entry"><span style="color:red;">&#10007;</span></button>';
  109. if (i > 0) {
  110. Result += '<button type="button" onclick="moveEntry(' + i + ', -1);" title="Move up one row">&#8593;</button>';
  111. } else {
  112. Result += '<button type="button" >&nbsp;</button>';
  113. }
  114. if (i < (data.length -1)) {
  115. Result += '<button type="button" onclick="moveEntry(' + i + ', +1);" title="Move down one row">&#8595;</button>';
  116. } else {
  117. Result += '<button type="button">&nbsp;</button>';
  118. }
  119. Result += '</div></td>';
  120. }
  121. Result += '</tr>';
  122. return (Result);
  123. };
  124. /**
  125. * Take a time and format it as HH:MM, with leading zeroes
  126. *
  127. * This can handle the parameter as a Date object or an integer number of minutes
  128. *
  129. * @param Date or integer time
  130. * @result string the time as a string in 24-hour time
  131. */
  132. Playsheet.prototype.formatTime = function(time) {
  133. if (time.getHours) {
  134. var hours = time.getHours();
  135. } else {
  136. var hours = Math.floor(time / 60);
  137. }
  138. if (time.getMinutes) {
  139. var mins = time.getMinutes();
  140. } else {
  141. var mins = time % 60;
  142. }
  143. return (this.padNumber(hours, 2) + ':' + this.padNumber(mins, 2));
  144. }
  145. Playsheet.prototype.padNumber = function(num, length) {
  146. var s = Array(length).join("0") + num;
  147. return (s.substr(s.length - length));
  148. };
  149. Playsheet.prototype.convertStringToMinutes = function(timestring) {
  150. if (timestring == '') {
  151. timestring = 0;
  152. }
  153. var parts = timestring.split(':');
  154. if (parts.length == 2) {
  155. var result = parseInt(parts[0]) * 60 + parseInt(parts[1]);
  156. } else {
  157. var result = parseInt(timestring, 10);
  158. }
  159. return (result);
  160. };
  161. Playsheet.prototype.enableSaveButtons = function(buttonText, enabled) {
  162. jQuery("#playsheet-edit-dialog .ui-dialog-buttonpane button:contains('Save')")
  163. .attr('disabled', !enabled);
  164. }