template-module-loader.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Module that loads a user's custom module that was specified within the Publishing Template. */
  2. define(["options", "util", "require"], function (options, util, require) {
  3. /**
  4. * @type {string}
  5. *
  6. * The ID of the Publishing Template base directory as it is specified in the configuration
  7. * object passed to the RequireJS library used to load the JS modules.
  8. */
  9. var TEMPLATE_BASE_DIR_ID = "template-base-dir";
  10. /**
  11. * @type {string}
  12. *
  13. * Specifies if the custom JavaScript module specified in the Publishing Template
  14. * should be loaded or not.
  15. */
  16. var isTemplateJsModuleLoadingEnabled = options.getBoolean('webhelp.enable.template.js.module.loading');
  17. /**
  18. * @type {string}
  19. *
  20. * The path of the template's main JS module relative to the base directory of the Publishing Template.
  21. */
  22. var templateMainJsModuleRelPath = options.get('webhelp.js.module.rel.path');
  23. if (isTemplateJsModuleLoadingEnabled && templateMainJsModuleRelPath) {
  24. var templateMainJsID = getTemplateMainJsID(templateMainJsModuleRelPath);
  25. try {
  26. require(
  27. [templateMainJsID],
  28. function() {
  29. util.debug("Finished loading custom script:", templateMainJsModuleRelPath);
  30. },
  31. // Error callback
  32. function(err) {
  33. console.error("Cannot load script:", templateMainJsModuleRelPath, err);
  34. }
  35. );
  36. } catch(err) {
  37. console.error("Cannot load script:", templateMainJsModuleRelPath, err);
  38. }
  39. }
  40. /**
  41. * Computes the ID of the template's main JS module.
  42. *
  43. * @param templateMainJsModuleRelPath The path of the template's main JS module
  44. * relative to the base directory of the Publishing Template.
  45. */
  46. function getTemplateMainJsID(templateMainJsModuleRelPath) {
  47. // Remove .js extension
  48. var ext = ".js";
  49. var templateMainJsRelPathNoExt;
  50. if (templateMainJsModuleRelPath.endsWith(ext)) {
  51. templateMainJsRelPathNoExt = templateMainJsModuleRelPath.substring(0, templateMainJsModuleRelPath.length - ext.length);
  52. } else {
  53. templateMainJsRelPathNoExt = templateMainJsModuleRelPath;
  54. }
  55. return TEMPLATE_BASE_DIR_ID + "/" + templateMainJsRelPathNoExt;
  56. }
  57. });