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

theme-editor.js 4.2KB

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