Requires

Provides

Assets.js

Provides methods to dynamically load JavaScript, CSS, and Image files into the document.

License:
MIT-style license
Authors:
Valerio Proietti
  1. 25
  2. 26
  3. 27
  4. 28
  5. 29
  6. 30
  7. 31
  8. 32
  9. 33
  10. 34
  11. 35
  12. 36
  13. 37
  14. 38
  15. 39
  16. 40
  17. 41
  18. 42
  19. 43
  20. 44
  21. 45
  22. 46
  23. 47
  24. 48
  25. 49
  26. 50
  27. 51
  28. 52
  29. 53
  30. 54
  31. 55
  32. 56
  33. 57
  34. 58
  35. 59
  36. 60
  37. 61
  38. 62
  39. 63
  40. 64
  41. 65
  42. 66
  43. 67
  44. 68
  45. 69
  46. 70
  47. 71
  48. 72
  49. 73
  50. 74
  51. 75
  52. 76
  53. 77
  54. 78
  55. 79
  56. 80
  57. 81
  58. 82
  59. 83
  60. 84
  61. 85
  62. 86
  63. 87
  64. 88
  65. 89
  66. 90
  67. 91
  68. 92
  69. 93
  70. 94
  71. 95
  72. 96
  73. 97
  74. 98
  75. 99
  76. 100
  77. 101
  78. 102
  79. 103
  80. 104
  81. 105
  82. 106
  83. 107
  84. 108
  85. 109
  86. 110
  87. 111
  88. 112
  89. 113
  90. 114
  91. 115
  92. 116
  93. 117
  94. 118
  95. 119
  96. 120
  97. 121
  98. 122
  99. 123
  100. 124
  101. 125
  102. 126
  103. 127
  104. 128
  105. 129
  106. 130
  107. 131
  108. 132
  109. 133
  110. 134
  111. 135
  112. 136
  113. 137
  114. 138
  115. 139
  116. 140
  117. 141
var Asset = { javascript: function(source, properties){ properties = Object.append({ onload: function(){}, document: document, check: Function.from(true) }, properties); if (properties.onLoad){ properties.onload = properties.onLoad; delete properties.onLoad; } var script = new Element('script', {src: source, type: 'text/javascript'}); var load = properties.onload.bind(script), check = properties.check, doc = properties.document; delete properties.onload; delete properties.check; delete properties.document; script.addEvents({ load: load, readystatechange: function(){ if (['loaded', 'complete'].contains(this.readyState)) load(); } }).set(properties); if ((Browser.safari || Browser.chrome) && Browser.version == 2){ var checker = (function(){ if (!Function.attempt(check)) return; clearInterval(checker); load(); }).periodical(50); } return script.inject(doc.head); }, css: function(source, properties){ properties = properties || {}; var onload = properties.onload || properties.onLoad; if (onload){ properties.events = properties.events || {}; properties.events.load = onload; delete properties.onload; delete properties.onLoad; } return new Element('link', Object.merge({ rel: 'stylesheet', media: 'screen', type: 'text/css', href: source }, properties)).inject(document.head); }, image: function(source, properties){ properties = Object.merge({ onload: function(){}, onabort: function(){}, onerror: function(){} }, properties); var image = new Image(); var element = document.id(image) || new Element('img'); ['load', 'abort', 'error'].each(function(name){ var type = 'on' + name; var cap = name.capitalize(); if (properties['on' + cap]){ properties[type] = properties['on' + cap]; delete properties['on' + cap]; } var event = properties[type]; delete properties[type]; image[type] = function(){ if (!image) return; if (!element.parentNode){ element.width = image.width; element.height = image.height; } image = image.onload = image.onabort = image.onerror = null; event.delay(1, element, element); element.fireEvent(name, element, 1); }; }); image.src = element.src = source; if (image && image.complete) image.onload.delay(1); return element.set(properties); }, images: function(sources, options){ options = Object.merge({ onComplete: function(){}, onProgress: function(){}, onError: function(){}, properties: {} }, options); sources = Array.from(sources); var images = []; var counter = 0; return new Elements(sources.map(function(source){ return Asset.image(source, Object.append(options.properties, { onload: function(){ options.onProgress.call(this, counter, sources.indexOf(source)); counter++; if (counter == sources.length) options.onComplete(); }, onerror: function(){ options.onError.call(this, counter, sources.indexOf(source)); counter++; if (counter == sources.length) options.onComplete(); } })); })); } };