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.

56 lines
1.2 KiB

6 years ago
6 years ago
  1. /* global openstreetbrowserPrefix */
  2. var loadClash = {}
  3. var cache = {}
  4. function applyOptions (code, options) {
  5. var style = ''
  6. for (var k in options) {
  7. if (k !== 'size') {
  8. style += k + ':' + options[k] + ';'
  9. }
  10. }
  11. return code.replace(/<path/i, '<path style="' + style + '"')
  12. }
  13. function maki (file, options, callback) {
  14. var size = options.size || 15
  15. var m = file.match(/^(.*)-(11|15)/)
  16. if (!m) {
  17. file += '-' + size
  18. }
  19. var url = (typeof openstreetbrowserPrefix === 'undefined' ? './' : openstreetbrowserPrefix) +
  20. 'node_modules/@mapbox/maki/icons/' + file + '.svg'
  21. if (file in cache) {
  22. return callback(null, applyOptions(cache[file], options))
  23. }
  24. if (file in loadClash) {
  25. loadClash[file].push([ options, callback ])
  26. return
  27. } else {
  28. loadClash[file] = [ [ options, callback ] ]
  29. }
  30. var req = new XMLHttpRequest()
  31. req.addEventListener('load', function () {
  32. if (req.status !== 200) {
  33. loadClash[file].forEach(p => p[1](req.statusText, null))
  34. delete loadClash[file]
  35. return
  36. }
  37. cache[file] = req.responseText
  38. loadClash[file].forEach(p => p[1](null, applyOptions(cache[file], p[0])))
  39. delete loadClash[file]
  40. })
  41. req.open('GET', url)
  42. req.send()
  43. }
  44. module.exports = maki