imgReplacement.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //https://stackoverflow.com/questions/35000435/how-can-i-put-all-the-url-parameters-in-array-of-objects-using-jquery/35000946
  2. function getParams(url) {
  3. var queryString = url.substring(url.indexOf('?') + 1);
  4. var paramsArr = queryString.split('&');
  5. var params = [];
  6. for (var i = 0, len = paramsArr.length; i < len; i++) {
  7. var keyValuePair = paramsArr[i].split('=');
  8. params.push({
  9. name: keyValuePair[0],
  10. value: keyValuePair[1]
  11. });
  12. }
  13. return params;
  14. }
  15. $(function() {
  16. $('#urls').on("submit",function(e) {
  17. e.preventDefault(); // cancel the actual submit
  18. loadImage($('#url').val(), $('#replacement').val())
  19. });
  20. });
  21. function loadImage(url, replacement){
  22. //https://centurio.work/customers/evva/was/server/8/23/images/0/de-at/
  23. //https://centurio.work/customers/evva/was/server/8/23/replacement
  24. //https://stackoverflow.com/questions/27775579/how-to-fill-d3-svg-with-image-instead-of-colour-with-fill
  25. //Embedd image so svg is accessible
  26. //https://stackoverflow.com/questions/12975929/how-to-use-svg-file-for-image-source-in-d3
  27. d3.svg(url).then(function(xml) {
  28. d3.select("#svgEmbed").node().appendChild(xml.documentElement);
  29. var dict = {};
  30. //get All Text and if Available Rect before
  31. d3.select("#svgEmbed").selectAll("text").each(function(d,i){
  32. dict[d3.select(this).text()] = d3.select(this.previousSibling).node().getBBox()
  33. });
  34. var urlparameters = getParams(window.location.search)
  35. //replace text within image
  36. for (const [key, value] of urlparameters.entries()) {
  37. d3.select("#svgEmbed").selectAll("text").each(function(d,i){
  38. if(d3.select(this).text() == value.name){
  39. d3.select(this).text(value.value);
  40. }
  41. });
  42. }
  43. //load image Replacements
  44. $.ajax({
  45. type: "GET",
  46. url: replacement,
  47. dataType: "xml",
  48. success: function(xml) {
  49. $(xml).find('item').each(function(index){
  50. var extension = $(this).find('url').text().split('.').pop();
  51. if(extension == "mp4"){
  52. //https://stackoverflow.com/questions/38437834/append-foreignobject-containing-some-html-inside-an-svg-element
  53. var video = d3.select("#svgEmbed").append("foreignObject")
  54. video
  55. .attr("x", dict[$(this).find('abbreviation').text()].x)
  56. .attr("y", dict[$(this).find('abbreviation').text()].y)
  57. .attr("width", dict[$(this).find('abbreviation').text()].width)
  58. .attr("height", dict[$(this).find('abbreviation').text()].height)
  59. .append("xhtml:video")
  60. .attr("width", dict[$(this).find('abbreviation').text()].width)
  61. .attr("height", dict[$(this).find('abbreviation').text()].height)
  62. .attr("controls", "")
  63. .append("xhtml:source")
  64. .attr("type", "video/mp4")
  65. .attr("src", $(this).find('url').text());
  66. }
  67. else{
  68. d3.select("#svgEmbed").append("svg:image")
  69. .attr('x', dict[$(this).find('abbreviation').text()].x)
  70. .attr('y', dict[$(this).find('abbreviation').text()].y)
  71. .attr('width', dict[$(this).find('abbreviation').text()].width)
  72. .attr('height', dict[$(this).find('abbreviation').text()].height)
  73. .attr("xlink:href", $(this).find('url').text())
  74. }
  75. });
  76. }
  77. });
  78. });
  79. }