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

theme-editor.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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("#btn-save-theme").on("click", saveTheme);
  10. d3.select("#chkNoPattern").on("change", setNoPattern);
  11. }
  12. function setClass(cl, msg) {
  13. d3.select("body").attr("class", cl || null);
  14. d3.select("#error").text(msg || "");
  15. }
  16. function uploadTheme() {
  17. var formData = new FormData();
  18. var file = preview.newTheme();
  19. formData.append("newTheme", file);
  20. var newCaption = preview.newCaption();
  21. formData.append("newCaption", newCaption);
  22. $.ajax({
  23. url: "/theme/upload",
  24. type: "POST",
  25. data: formData,
  26. dataType: "json",
  27. contentType: false,
  28. cache: false,
  29. processData: false,
  30. success: function () {
  31. d3.json("/settings/themes.json", function(err, themes){
  32. var errorMessage;
  33. // Themes are missing or invalid
  34. if (err || !d3.keys(themes).filter(function(d){ return d !== "default"; }).length) {
  35. if (err instanceof SyntaxError) {
  36. errorMessage = "Error in settings/themes.json:<br/><code>" + err.toString() + "</code>";
  37. } else if (err instanceof ProgressEvent) {
  38. errorMessage = "Error: no settings/themes.json.";
  39. } else if (err) {
  40. errorMessage = "Error: couldn't load settings/themes.json.";
  41. } else {
  42. errorMessage = "No themes found in settings/themes.json.";
  43. }
  44. d3.select("#loading-bars").remove();
  45. d3.select("#loading-message").html(errorMessage);
  46. if (err) {
  47. throw err;
  48. }
  49. return;
  50. }
  51. location.reload();
  52. });
  53. },
  54. error: function (error) {
  55. console.log('error', error);
  56. }
  57. });
  58. }
  59. function updateNewThemeFile() {
  60. if (!this.files || !this.files[0]) {
  61. preview.newTheme(null);
  62. setClass(null);
  63. return true;
  64. }
  65. newTheme = this.files[0];
  66. preview.loadNewTheme(newTheme, function (err) {
  67. if (err) {
  68. setClass("error", "Error updating new theme file");
  69. } else {
  70. setClass(null);
  71. }
  72. });
  73. }
  74. function updateNewCaption() {
  75. preview.newCaption(this.value);
  76. }
  77. function deleteTheme() {
  78. if(!confirm($("#btn-delete-theme").data("confirm"))){
  79. d3.event.stopImmediatePropagation();
  80. d3.event.preventDefault();
  81. return;
  82. }
  83. var theme = d3.select("#input-theme").property("value");
  84. $.ajax({
  85. url: "/theme/delete",
  86. type: "POST",
  87. data: JSON.stringify({theme: theme}),
  88. dataType: "json",
  89. contentType: "application/json",
  90. cache: false,
  91. success: function () {
  92. location.reload();
  93. },
  94. error: function (error) {
  95. console.log('error', error);
  96. }
  97. });
  98. }
  99. function saveTheme() {
  100. if(!confirm($("#btn-save-theme").data("confirm"))){
  101. d3.event.stopImmediatePropagation();
  102. d3.event.preventDefault();
  103. return;
  104. }
  105. $.ajax({
  106. url: "/theme/save",
  107. type: "POST",
  108. data: JSON.stringify({theme: theme}),
  109. dataType: "json",
  110. contentType: "application/json",
  111. cache: false,
  112. success: function () {
  113. preview.theme(theme);
  114. },
  115. error: function (error) {
  116. console.log('error', error);
  117. }
  118. });
  119. }
  120. function setNoPattern() {
  121. if (!theme) {
  122. return;
  123. }
  124. const checked = d3.select("#chkNoPattern").property("checked");
  125. if (!theme.noPattern) {
  126. theme['noPattern'] = checked;
  127. } else {
  128. theme.noPattern = checked;
  129. }
  130. }
  131. module.exports = {
  132. initialize: _initialize
  133. };