@ -1,6 +1,10 @@
/* global openstreetbrowserPrefix */
var loadClash = { }
var cache = { }
var paths = {
maki : 'node_modules/@mapbox/maki/icons/ID-SIZE.svg' ,
temaki : 'node_modules/temaki/icons/ID.svg'
}
function applyOptions ( code , options ) {
var style = ''
@ -14,40 +18,41 @@ function applyOptions (code, options) {
return code . replace ( /<path/i , '<path style="' + style + '"' )
}
function maki ( file , options , callback ) {
var size = options . size || 15
function maki ( set , file , options , callback ) {
var m = file . match ( /^(.*)-(11|15)/ )
if ( ! m ) {
file += '-' + size
if ( m ) {
file = m [ 1 ]
options . size = m [ 2 ]
}
var url = ( typeof openstreetbrowserPrefix === 'undefined' ? './' : openstreetbrowserPrefix ) +
'node_modules/@mapbox/maki/icons/' + file + '.svg'
paths [ set ]
. replace ( 'ID' , file )
. replace ( 'SIZE' , options . size || 15 )
if ( file in cache ) {
return callback ( null , applyOptions ( cache [ file ] , options ) )
if ( url in cache ) {
return callback ( null , applyOptions ( cache [ url ] , options ) )
}
if ( file in loadClash ) {
loadClash [ file ] . push ( [ options , callback ] )
if ( url in loadClash ) {
loadClash [ url ] . push ( [ options , callback ] )
return
} else {
loadClash [ file ] = [ [ options , callback ] ]
loadClash [ url ] = [ [ options , callback ] ]
}
var req = new XMLHttpRequest ( )
req . addEventListener ( 'load' , function ( ) {
if ( req . status !== 200 ) {
loadClash [ file ] . forEach ( p => p [ 1 ] ( req . statusText , null ) )
delete loadClash [ file ]
loadClash [ url ] . forEach ( p => p [ 1 ] ( req . statusText , null ) )
delete loadClash [ url ]
return
}
cache [ file ] = req . responseText
cache [ url ] = req . responseText
loadClash [ file ] . forEach ( p => p [ 1 ] ( null , applyOptions ( cache [ file ] , p [ 0 ] ) ) )
delete loadClash [ file ]
loadClash [ url ] . forEach ( p => p [ 1 ] ( null , applyOptions ( cache [ url ] , p [ 0 ] ) ) )
delete loadClash [ url ]
} )
req . open ( 'GET' , url )
req . send ( )