loader.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //for closure compiler to ensure these both get exported
  2. window['loadIcons'] = loadIcons;
  3. window['loadHTML'] = loadHTML;
  4. function loadIcons(dir, assets)
  5. {
  6. dir = dir || './';
  7. //delete existing icons
  8. $('#svg-icons').remove();
  9. //add new icon holder
  10. $svg = $('<svg id="svg-icons" xmlns="http://www.w3.org/2000/svg" style="display: none"></svg>');
  11. $svg.appendTo('body');
  12. //add returned icon to svg icons container
  13. if(typeof assets === 'string')
  14. {
  15. return $.ajax(dir + assets).done(function(data, status, xhr)
  16. {
  17. $(data).each(function(item, index)
  18. {
  19. var id = $(this).attr('id');
  20. if(!id)
  21. return;
  22. $(this)
  23. .attr('id', 'icon-' + id)
  24. .appendTo($svg);
  25. });
  26. });
  27. }
  28. var addIcon = function(icon)
  29. {
  30. return function(data, status, xhr)
  31. {
  32. $(data)
  33. .find('svg')
  34. .attr('id', 'icon-' + icon)
  35. .appendTo($svg);
  36. }
  37. };
  38. var promises = [];
  39. for(var i = 0, len = assets.length; i < len; i++)
  40. promises.push($.ajax(dir + assets[i] + '.svg').done(addIcon(assets[i])));
  41. return $.when(promises);
  42. }
  43. function loadHTML(dir, assets, destination)
  44. {
  45. dir = dir || './';
  46. //add returned icon to svg icons container
  47. var addHTML = function(full_id)
  48. {
  49. return function(data, status, xhr)
  50. {
  51. $(data).appendTo(destination || 'body');
  52. }
  53. };
  54. if(typeof assets === 'string')
  55. assets = [assets];
  56. var promises = [];
  57. for(var i = 0, len = assets.length; i < len; i++)
  58. {
  59. var id = assets[i];
  60. var index = id.indexOf('-');
  61. if(index !== -1)
  62. url = dir + id.substr(0, index) + '/' + id.substr(index+1) + '.html';
  63. else
  64. url = dir + id + '.html';
  65. promises.push($.ajax(url).then(addHTML(assets[i])));
  66. }
  67. //return a promise for all the assets
  68. return $.when.apply($, promises);
  69. }