mxUrlConverter.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. *
  7. * Class: mxUrlConverter
  8. *
  9. * Converts relative and absolute URLs to absolute URLs with protocol and domain.
  10. */
  11. var mxUrlConverter = function()
  12. {
  13. // Empty constructor
  14. };
  15. /**
  16. * Variable: enabled
  17. *
  18. * Specifies if the converter is enabled. Default is true.
  19. */
  20. mxUrlConverter.prototype.enabled = true;
  21. /**
  22. * Variable: baseUrl
  23. *
  24. * Specifies the base URL to be used as a prefix for relative URLs.
  25. */
  26. mxUrlConverter.prototype.baseUrl = null;
  27. /**
  28. * Variable: baseDomain
  29. *
  30. * Specifies the base domain to be used as a prefix for absolute URLs.
  31. */
  32. mxUrlConverter.prototype.baseDomain = null;
  33. /**
  34. * Function: updateBaseUrl
  35. *
  36. * Private helper function to update the base URL.
  37. */
  38. mxUrlConverter.prototype.updateBaseUrl = function()
  39. {
  40. this.baseDomain = location.protocol + '//' + location.host;
  41. this.baseUrl = this.baseDomain + location.pathname;
  42. var tmp = this.baseUrl.lastIndexOf('/');
  43. // Strips filename etc
  44. if (tmp > 0)
  45. {
  46. this.baseUrl = this.baseUrl.substring(0, tmp + 1);
  47. }
  48. };
  49. /**
  50. * Function: isEnabled
  51. *
  52. * Returns <enabled>.
  53. */
  54. mxUrlConverter.prototype.isEnabled = function()
  55. {
  56. return this.enabled;
  57. };
  58. /**
  59. * Function: setEnabled
  60. *
  61. * Sets <enabled>.
  62. */
  63. mxUrlConverter.prototype.setEnabled = function(value)
  64. {
  65. this.enabled = value;
  66. };
  67. /**
  68. * Function: getBaseUrl
  69. *
  70. * Returns <baseUrl>.
  71. */
  72. mxUrlConverter.prototype.getBaseUrl = function()
  73. {
  74. return this.baseUrl;
  75. };
  76. /**
  77. * Function: setBaseUrl
  78. *
  79. * Sets <baseUrl>.
  80. */
  81. mxUrlConverter.prototype.setBaseUrl = function(value)
  82. {
  83. this.baseUrl = value;
  84. };
  85. /**
  86. * Function: getBaseDomain
  87. *
  88. * Returns <baseDomain>.
  89. */
  90. mxUrlConverter.prototype.getBaseDomain = function()
  91. {
  92. return this.baseDomain;
  93. };
  94. /**
  95. * Function: setBaseDomain
  96. *
  97. * Sets <baseDomain>.
  98. */
  99. mxUrlConverter.prototype.setBaseDomain = function(value)
  100. {
  101. this.baseDomain = value;
  102. };
  103. /**
  104. * Function: isRelativeUrl
  105. *
  106. * Returns true if the given URL is relative.
  107. */
  108. mxUrlConverter.prototype.isRelativeUrl = function(url)
  109. {
  110. return url != null && url.substring(0, 2) != '//' && url.substring(0, 7) != 'http://' &&
  111. url.substring(0, 8) != 'https://' && url.substring(0, 10) != 'data:image' &&
  112. url.substring(0, 7) != 'file://';
  113. };
  114. /**
  115. * Function: convert
  116. *
  117. * Converts the given URL to an absolute URL with protol and domain.
  118. * Relative URLs are first converted to absolute URLs.
  119. */
  120. mxUrlConverter.prototype.convert = function(url)
  121. {
  122. if (this.isEnabled() && this.isRelativeUrl(url))
  123. {
  124. if (this.getBaseUrl() == null)
  125. {
  126. this.updateBaseUrl();
  127. }
  128. if (url.charAt(0) == '/')
  129. {
  130. url = this.getBaseDomain() + url;
  131. }
  132. else
  133. {
  134. url = this.getBaseUrl() + url;
  135. }
  136. }
  137. return url;
  138. };