Loader.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. (function(context){
  2. var toString = Object.prototype.toString;
  3. var isArray = Array.isArray || function(array){
  4. return toString.call(array) == '[object Array]';
  5. };
  6. var indexOf = function(array, item, from){
  7. var len = array.length;
  8. for (var i = (from < 0) ? Math.max(0, len + from) : from || 0; i < len; i++){
  9. if (array[i] === item) return i;
  10. }
  11. return -1;
  12. };
  13. var forEach = function(array, fn, bind){
  14. for (var i = 0, l = array.length; i < l; i++){
  15. if (i in array) fn.call(bind, array[i], i, array);
  16. }
  17. };
  18. context.SpecLoader = function(config, options){
  19. // initialization
  20. var preset;
  21. if (options.preset) preset = config.presets[options.preset];
  22. var setNames = [],
  23. sourceNames = [],
  24. sourceLoader = function(){},
  25. specLoader = function(){},
  26. envName = 'browser';
  27. // private methods
  28. var getDefault = function(){
  29. return config.presets[config.defaultPresets[envName]];
  30. };
  31. var getSets = function(){
  32. var requestedSets = [],
  33. sets = (preset || options).sets || getDefault().sets;
  34. forEach(sets && isArray(sets) ? sets : [sets], function(set){
  35. if (config.sets[set] && indexOf(requestedSets, set) == -1) requestedSets.push(set);
  36. });
  37. return requestedSets;
  38. };
  39. var getSource = function(){
  40. var requestedSource = [],
  41. source = (preset || options).source || getDefault().source;
  42. forEach(source && isArray(source) ? source : [source], function(src){
  43. if (config.source[src] && indexOf(requestedSource, src) == -1) requestedSource.push(src);
  44. });
  45. return requestedSource;
  46. };
  47. var loadSets = function(){
  48. forEach(setNames, function(set){
  49. specLoader(config.sets[set].files, config.sets[set].path);
  50. });
  51. };
  52. var loadSource = function(){
  53. forEach(sourceNames, function(set){
  54. sourceLoader(config.source[set].files, config.source[set].path);
  55. });
  56. };
  57. // public methods
  58. return {
  59. setSourceLoader: function(loader){
  60. sourceLoader = loader;
  61. return this;
  62. },
  63. setSpecLoader: function(load){
  64. specLoader = load;
  65. return this;
  66. },
  67. setEnvName: function(name){
  68. envName = name;
  69. return this;
  70. },
  71. run: function(){
  72. // Get the sets and source
  73. setNames = getSets();
  74. sourceNames = getSource();
  75. // Load the sets and source
  76. loadSource();
  77. loadSets();
  78. return this;
  79. },
  80. getSetNames: function(){
  81. return setNames;
  82. },
  83. getSourceNames: function(){
  84. return sourceNames;
  85. }
  86. };
  87. };
  88. })(typeof exports != 'undefined' ? exports : this);