This repository has been archived by the owner on Feb 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathfileUtils.js
80 lines (75 loc) · 2.39 KB
/
fileUtils.js
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
/*******************************************************************************
* @license
* Copyright (c) 2009, 2012 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors: IBM Corporation - initial API and implementation
*******************************************************************************/
/*eslint-env browser, amd*/
/*global URL*/
define(['require', 'orion/URL-shim'], function(require) {
var tryParentRelative = true;
function makeParentRelative(location) {
var link = document.createElement('a'); //$NON-NLS-0$
link.href = location;
location = link.href;
if (tryParentRelative) {
try {
if (window.location.host === parent.location.host && window.location.protocol === parent.location.protocol) {
return location.substring(parent.location.href.indexOf(parent.location.host) + parent.location.host.length);
} else {
tryParentRelative = false;
}
} catch (e) {
tryParentRelative = false;
}
}
return location;
}
/**
* This class contains static utility methods. It is not intended to be instantiated.
* @class This class contains static utility methods.
* @name orion.fileUtils
*/
function makeRelative(location) {
if (!location) {
return location;
}
var hostName = window.location.protocol + "//" + window.location.host; //$NON-NLS-0$
if (location.indexOf(hostName) === 0) {
return location.substring(hostName.length);
}
return location;
}
//cache this
var _workspaceUrlHref;
if(!require.toUrl){
_workspaceUrlHref =(new URL("/", window.location.href)).href;
} else {
_workspaceUrlHref =(new URL(require.toUrl("workspace"), window.location.href)).href;
}
/**
* Determines if the path represents the workspace root
* @name orion.util#isAtRoot
* @function
*/
function isAtRoot(path) {
if (!path) {
return false;
}
if (path === "/workspace") {
return true; // sad but true
}
var pathUrl = new URL(path, window.location.href);
return pathUrl.href.indexOf(_workspaceUrlHref) === 0; //$NON-NLS-0$
}
//return module exports
return {
makeParentRelative: makeParentRelative,
makeRelative: makeRelative,
isAtRoot: isAtRoot
};
});