not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.4 KiB

  1. define(function () {
  2. 'use strict';
  3. var icon = {};
  4. function changeColors(iconData, fillColor, strokeColor) {
  5. var re;
  6. if (fillColor) {
  7. re = /(<!ENTITY fill_color ")(.*)(">)/;
  8. iconData = iconData.replace(re, "$1" + fillColor + "$3");
  9. }
  10. if (strokeColor) {
  11. re = /(<!ENTITY stroke_color ")(.*)(">)/;
  12. iconData = iconData.replace(re, "$1" + strokeColor + "$3");
  13. }
  14. return iconData;
  15. }
  16. icon.load = function (iconInfo, callback) {
  17. var source;
  18. var dataHeader = "data:image/svg+xml,";
  19. if ("uri" in iconInfo) {
  20. source = iconInfo.uri;
  21. } else if ("name" in iconInfo) {
  22. source = "lib/graphics/icons/" + iconInfo.name + ".svg";
  23. }
  24. var fillColor = iconInfo.fillColor;
  25. var strokeColor = iconInfo.strokeColor;
  26. // If source is already a data uri, read it instead of doing
  27. // the XMLHttpRequest
  28. if (source.substring(0, 4) == 'data') {
  29. var iconData = decodeURIComponent(source.slice(dataHeader.length));
  30. var newData = changeColors(iconData, fillColor, strokeColor);
  31. callback(dataHeader + encodeURI(newData));
  32. return;
  33. }
  34. var client = new XMLHttpRequest();
  35. client.onload = function () {
  36. var iconData = this.responseText;
  37. var newData = changeColors(iconData, fillColor, strokeColor);
  38. callback(dataHeader + encodeURIComponent(newData));
  39. };
  40. client.open("GET", source);
  41. client.send();
  42. };
  43. function getBackgroundURL(elem) {
  44. var style = elem.currentStyle || window.getComputedStyle(elem, '');
  45. // Remove prefix 'url(' and suffix ')' before return
  46. var res = style.backgroundImage.slice(4, -1);
  47. var last = res.length-1;
  48. if (res[0] == '"' && res[last] == '"') {
  49. res = res.slice(1, last);
  50. }
  51. return res;
  52. }
  53. function setBackgroundURL(elem, url) {
  54. elem.style.backgroundImage = "url('" + url + "')";
  55. }
  56. icon.colorize = function (elem, colors, callback) {
  57. var iconInfo = {
  58. "uri": getBackgroundURL(elem),
  59. "strokeColor": colors.stroke,
  60. "fillColor": colors.fill
  61. };
  62. icon.load(iconInfo, function (url) {
  63. setBackgroundURL(elem, url);
  64. if (callback) {
  65. callback();
  66. }
  67. });
  68. };
  69. return icon;
  70. });