Source js/bauplan.require.js

  1. define(function () {
  2. /**
  3. * @description ## RequireJS for Bauplan bundle
  4. * Convenience module for loading lang and template files into same require context
  5. * @module bauplan%require
  6. */
  7. var defaultLoadMethods = {
  8. processKey: function defaultLoadMethodsprocessKey (key) {
  9. return key.replace(/^[^!]+!/, "");
  10. }
  11. };
  12. var BauplanRequire = {
  13. /**
  14. * @method load
  15. * @instance
  16. * @param {object} modules
  17. * @param {function} callback
  18. */
  19. load: function BauplanRequireLoad (modules, callback) {
  20. var lmodules;
  21. var moduleKeys;
  22. if (Array.prototype.isPrototypeOf(modules)) {
  23. lmodules = modules.slice(0);
  24. } else {
  25. delete modules.originalkeys;
  26. lmodules = [];
  27. for (var modprop in modules) {
  28. lmodules.push(modprop);
  29. }
  30. moduleKeys = lmodules.slice(0);
  31. }
  32. // or did we pass options style?
  33. var keys;
  34. var options = {};
  35. if (typeof arguments[1] === "object") {
  36. options = arguments[1];
  37. callback = options.callback;
  38. keys = options.keys;
  39. }
  40. for (var meth in defaultLoadMethods) {
  41. if (!options[meth]) {
  42. options[meth] = defaultLoadMethods[meth];
  43. }
  44. }
  45. options.defaults = defaultLoadMethods;
  46. if (options.preprocessModule) {
  47. lmodules.forEach(function(value, index, array) {
  48. array[index] = options.preprocessModule(value, index, array, options);
  49. });
  50. }
  51. if (options.plugin) {
  52. lmodules.forEach(function(value, index, array) {
  53. var addplugin = true;
  54. var modvalue = modules[value];
  55. if (modvalue) {
  56. if (modvalue.match(/\.js$/)) {
  57. addplugin = false;
  58. } else if (modvalue.indexOf("//") === 0) {
  59. addplugin = false;
  60. } else if (modvalue.indexOf("!") !== -1) {
  61. addplugin = false;
  62. }
  63. }
  64. if (addplugin) {
  65. array[index] = options.plugin + "!" + value;
  66. }
  67. });
  68. }
  69. if (!keys) {
  70. keys = moduleKeys || modules.slice(0);
  71. }
  72. /*if (options.debug) {
  73. console.log("loading", lmodules, "into", keys, "using", modules);
  74. }*/
  75. this.require(lmodules, function() {
  76. //var loaded = Array.prototype.slice.call(arguments);
  77. var output = [];
  78. for (var i = 0, argslen = arguments.length; i < argslen; i++) {
  79. var key = keys[i];
  80. var value = arguments[i];
  81. var processbundle = {
  82. key: key,
  83. value: value,
  84. keys: keys
  85. };
  86. if (options.processKey) {
  87. key = options.processKey(key, processbundle, options);
  88. }
  89. if (options.processValue) {
  90. value = options.processValue(value, processbundle, options);
  91. }
  92. output.push({
  93. key: key,
  94. value: value
  95. });
  96. }
  97. if (options.process) {
  98. output = options.process(output, options);
  99. }
  100. //return output;
  101. if (callback) {
  102. callback(output);
  103. }
  104. });
  105. },
  106. /**
  107. * @method paths
  108. * @instance
  109. * @param {object} modules
  110. * @param {object} options
  111. */
  112. paths: function (modules, options) {
  113. var paths = {};
  114. var originalkeys = [];
  115. var rgxJs = /\.js$/;
  116. var suffix = options.suffix;
  117. var rgxSuffix = new RegExp("-" + suffix + "$");
  118. var extension = options.extension;
  119. var rgxExtension = new RegExp("\\." + extension + "$");
  120. for (var module in modules) {
  121. var moduleval = modules[module];
  122. if (!moduleval.match(rgxJs)) {
  123. if (!moduleval.match(rgxExtension)) {
  124. moduleval += "." + extension;
  125. }
  126. /*if (moduleval.match(/^\/\//)) {
  127. moduleval += ".js";
  128. }*/
  129. }
  130. var modulekey = module;
  131. if (!modulekey.match(rgxSuffix)) {
  132. modulekey = modulekey.replace(/\./g, "-") + "-" + suffix;
  133. }
  134. paths[modulekey] = moduleval;
  135. originalkeys.push(module);
  136. }
  137. this.require.config({
  138. paths: paths
  139. });
  140. paths.originalkeys = originalkeys;
  141. return paths;
  142. // if we want to reload a particular module file
  143. // a) require.undef(module) - not forgetting text! if needed
  144. // b) update the module's config path value
  145. // c) re-require it
  146. // also skip, if we already have the same details
  147. // wrap callback if needed
  148. //
  149. //paths = [];
  150. }
  151. };
  152. return BauplanRequire;
  153. });
  154. /*
  155. http://rockycode.com/blog/cross-domain-requirejs-text/
  156. requirejs.config({
  157. text: {
  158. useXhr: function (url, protocol, hostname, port) {
  159. //return true if you want to allow this url, given that the
  160. //text plugin thinks the request is coming from protocol,
  161. hostname, port.
  162. }
  163. }
  164. });
  165. */