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