Turn audio into a shareable video. forked from nypublicradio/audiogram

theme-editor.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. var d3 = require("d3"),
  2. $ = require("jquery"),
  3. preview = require("./preview.js");
  4. function _initialize() {
  5. d3.select("#btn-new-theme").on("click", uploadTheme);
  6. d3.select("#input-new-theme").on("change", updateNewThemeFile).each(updateNewThemeFile);
  7. d3.select("#input-new-caption").on("change keyup", updateNewCaption).each(updateNewCaption);
  8. d3.select("#btn-delete-theme").on("click", deleteTheme);
  9. d3.select("#chkNoPattern").on("change", setNoPattern);
  10. }
  11. function setClass(cl, msg) {
  12. d3.select("body").attr("class", cl || null);
  13. d3.select("#error").text(msg || "");
  14. }
  15. function uploadTheme() {
  16. var formData = new FormData();
  17. var file = preview.newTheme();
  18. formData.append("newTheme", file);
  19. var newCaption = preview.newCaption();
  20. formData.append("newCaption", newCaption);
  21. var img = new Image();
  22. img.onload = function() {
  23. var sizes = {
  24. width: this.width,
  25. height: this.height
  26. };
  27. URL.revokeObjectURL(this.src);
  28. var subtitleLeft, subtitleRight;
  29. if (sizes.width > sizes.height) {
  30. if (sizes.width > 1280) {
  31. sizes.width = 1280;
  32. }
  33. if (sizes.height > 720) {
  34. sizes.height = 720;
  35. }
  36. } else {
  37. if (sizes.height > 1280) {
  38. sizes.height = 1280;
  39. }
  40. if (sizes.width > 720) {
  41. sizes.width = 720;
  42. subtitleLeft = sizes.width/2;
  43. subtitleRight = sizes.width-20;
  44. formData.append("newSubtitleLeft", subtitleLeft);
  45. formData.append("newSubtitleRight", subtitleRight);
  46. }
  47. }
  48. formData.append("newWidth", sizes.width);
  49. formData.append("newHeight", sizes.height);
  50. $.ajax({
  51. url: "/theme/upload",
  52. type: "POST",
  53. data: formData,
  54. dataType: "json",
  55. contentType: false,
  56. cache: false,
  57. processData: false,
  58. success: function () {
  59. d3.json("/settings/themes.json", function(err, themes){
  60. var errorMessage;
  61. // Themes are missing or invalid
  62. if (err || !d3.keys(themes).filter(function(d){ return d !== "default"; }).length) {
  63. if (err instanceof SyntaxError) {
  64. errorMessage = "Error in settings/themes.json:<br/><code>" + err.toString() + "</code>";
  65. } else if (err instanceof ProgressEvent) {
  66. errorMessage = "Error: no settings/themes.json.";
  67. } else if (err) {
  68. errorMessage = "Error: couldn't load settings/themes.json.";
  69. } else {
  70. errorMessage = "No themes found in settings/themes.json.";
  71. }
  72. d3.select("#loading-bars").remove();
  73. d3.select("#loading-message").html(errorMessage);
  74. if (err) {
  75. throw err;
  76. }
  77. return;
  78. }
  79. location.reload();
  80. });
  81. },
  82. error: function (error) {
  83. console.log('error', error);
  84. }
  85. });
  86. }
  87. var objectURL = URL.createObjectURL(file);
  88. img.src = objectURL;
  89. }
  90. function updateNewThemeFile() {
  91. if (!this.files || !this.files[0]) {
  92. preview.newTheme(null);
  93. setClass(null);
  94. return true;
  95. }
  96. newTheme = this.files[0];
  97. preview.loadNewTheme(newTheme, function (err) {
  98. if (err) {
  99. setClass("error", "Error updating new theme file");
  100. } else {
  101. setClass(null);
  102. }
  103. });
  104. }
  105. function updateNewCaption() {
  106. preview.newCaption(this.value);
  107. }
  108. function deleteTheme() {
  109. if(!confirm($("#btn-delete-theme").data("confirm"))){
  110. d3.event.stopImmediatePropagation();
  111. d3.event.preventDefault();
  112. return;
  113. }
  114. var theme = d3.select("#input-theme").property("value");
  115. $.ajax({
  116. url: "/theme/delete",
  117. type: "POST",
  118. data: JSON.stringify({theme: theme}),
  119. dataType: "json",
  120. contentType: "application/json",
  121. cache: false,
  122. success: function () {
  123. location.reload();
  124. },
  125. error: function (error) {
  126. console.log('error', error);
  127. }
  128. });
  129. }
  130. function saveTheme() {
  131. $.ajax({
  132. url: "/theme/save",
  133. type: "POST",
  134. data: JSON.stringify({theme: theme}),
  135. dataType: "json",
  136. contentType: "application/json",
  137. cache: false,
  138. success: function () {
  139. preview.theme(theme);
  140. },
  141. error: function (error) {
  142. console.log('error', error);
  143. }
  144. });
  145. }
  146. function setNoPattern() {
  147. if (!theme) {
  148. return;
  149. }
  150. const checked = d3.select("#chkNoPattern").property("checked");
  151. if (!theme.noPattern) {
  152. theme['noPattern'] = checked;
  153. } else {
  154. theme.noPattern = checked;
  155. }
  156. saveTheme();
  157. }
  158. module.exports = {
  159. initialize: _initialize
  160. };