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

theme-editor.js 2.9KB

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