Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
13df7a85
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
13df7a85
编写于
12月 07, 2017
作者:
P
Phil Hughes
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Moved URL utility methods into es modules
上级
12d33b88
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
58 addition
and
69 deletion
+58
-69
app/assets/javascripts/lib/utils/url_utility.js
app/assets/javascripts/lib/utils/url_utility.js
+58
-69
未找到文件。
app/assets/javascripts/lib/utils/url_utility.js
浏览文件 @
13df7a85
/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, no-param-reassign, no-cond-assign, one-var, one-var-declaration-per-line, no-void, guard-for-in, no-restricted-syntax, prefer-template, quotes, max-len */
var
base
;
var
w
=
window
;
if
(
w
.
gl
==
null
)
{
w
.
gl
=
{};
}
if
((
base
=
w
.
gl
).
utils
==
null
)
{
base
.
utils
=
{};
}
// Returns an array containing the value(s) of the
// of the key passed as an argument
w
.
gl
.
utils
.
getParameterValues
=
function
(
sParam
)
{
var
i
,
sPageURL
,
sParameterName
,
sURLVariables
,
values
;
sPageURL
=
decodeURIComponent
(
window
.
location
.
search
.
substring
(
1
));
sURLVariables
=
sPageURL
.
split
(
'
&
'
);
sParameterName
=
void
0
;
values
=
[];
i
=
0
;
while
(
i
<
sURLVariables
.
length
)
{
sParameterName
=
sURLVariables
[
i
].
split
(
'
=
'
);
export
function
getParameterValues
(
sParam
)
{
const
sPageURL
=
decodeURIComponent
(
window
.
location
.
search
.
substring
(
1
));
return
sPageURL
.
split
(
'
&
'
).
reduce
((
acc
,
urlParam
)
=>
{
const
sParameterName
=
urlParam
.
split
(
'
=
'
);
if
(
sParameterName
[
0
]
===
sParam
)
{
values
.
push
(
sParameterName
[
1
].
replace
(
/
\+
/g
,
'
'
));
acc
.
push
(
sParameterName
[
1
].
replace
(
/
\+
/g
,
'
'
));
}
i
+=
1
;
}
return
values
;
};
return
acc
;
},
[]);
}
// @param {Object} params - url keys and value to merge
// @param {String} url
w
.
gl
.
utils
.
mergeUrlParams
=
function
(
params
,
url
)
{
var
lastChar
,
newUrl
,
paramName
,
paramValue
,
pattern
;
newUrl
=
decodeURIComponent
(
url
)
;
for
(
paramName
in
params
)
{
paramValue
=
params
[
paramName
]
;
pattern
=
new
RegExp
(
"
\\
b(
"
+
paramName
+
"
=).*?(&|$)
"
);
if
(
paramValue
==
null
)
{
newUrl
=
newUrl
.
replace
(
pattern
,
''
);
export
function
mergeUrlParams
(
params
,
url
)
{
let
newUrl
=
Object
.
keys
(
params
).
reduce
((
accParam
,
paramName
)
=>
{
const
paramValue
=
params
[
paramName
]
;
const
pattern
=
new
RegExp
(
`\\b(
${
paramName
}
=).*?(&|$)`
);
let
acc
=
accParam
;
if
(
paramValue
==
=
null
)
{
acc
=
acc
.
replace
(
pattern
,
''
);
}
else
if
(
url
.
search
(
pattern
)
!==
-
1
)
{
newUrl
=
newUrl
.
replace
(
pattern
,
"
$1
"
+
paramValue
+
"
$2
"
);
acc
=
acc
.
replace
(
pattern
,
`$1
${
paramValue
}
$2`
);
}
else
{
newUrl
=
""
+
newUrl
+
(
newUrl
.
indexOf
(
'
?
'
)
>
0
?
'
&
'
:
'
?
'
)
+
paramName
+
"
=
"
+
paramValue
;
acc
=
`
${
accParam
}${
accParam
.
indexOf
(
'
?
'
)
>
0
?
'
&
'
:
'
?
'
}${
paramName
}
=
${
paramValue
}
`
;
}
}
return
acc
;
},
decodeURIComponent
(
url
));
// Remove a trailing ampersand
lastChar
=
newUrl
[
newUrl
.
length
-
1
];
const
lastChar
=
newUrl
[
newUrl
.
length
-
1
];
if
(
lastChar
===
'
&
'
)
{
newUrl
=
newUrl
.
slice
(
0
,
-
1
);
}
return
newUrl
;
};
// removes parameter query string from url. returns the modified url
w
.
gl
.
utils
.
removeParamQueryString
=
function
(
url
,
param
)
{
var
urlVariables
,
variables
;
url
=
decodeURIComponent
(
url
);
urlVariables
=
url
.
split
(
'
&
'
);
return
((
function
()
{
var
j
,
len
,
results
;
results
=
[];
for
(
j
=
0
,
len
=
urlVariables
.
length
;
j
<
len
;
j
+=
1
)
{
variables
=
urlVariables
[
j
];
if
(
variables
.
indexOf
(
param
)
===
-
1
)
{
results
.
push
(
variables
);
}
}
return
results
;
})()).
join
(
'
&
'
);
};
w
.
gl
.
utils
.
removeParams
=
(
params
)
=>
{
}
export
function
removeParamQueryString
(
url
,
param
)
{
const
decodedUrl
=
decodeURIComponent
(
url
);
const
urlVariables
=
decodedUrl
.
split
(
'
&
'
);
return
urlVariables
.
filter
(
variable
=>
variable
.
indexOf
(
param
)
===
-
1
).
join
(
'
&
'
);
}
export
function
removeParams
(
params
)
{
const
url
=
document
.
createElement
(
'
a
'
);
url
.
href
=
window
.
location
.
href
;
params
.
forEach
((
param
)
=>
{
url
.
search
=
w
.
gl
.
utils
.
removeParamQueryString
(
url
.
search
,
param
);
url
.
search
=
removeParamQueryString
(
url
.
search
,
param
);
});
return
url
.
href
;
};
w
.
gl
.
utils
.
getLocationHash
=
function
(
url
)
{
var
hashIndex
;
if
(
typeof
url
===
'
undefined
'
)
{
// Note: We can't use window.location.hash here because it's
// not consistent across browsers - Firefox will pre-decode it
url
=
window
.
location
.
href
;
}
hashIndex
=
url
.
indexOf
(
'
#
'
);
return
hashIndex
===
-
1
?
null
:
url
.
substring
(
hashIndex
+
1
);
};
}
w
.
gl
.
utils
.
refreshCurrentPage
=
()
=>
gl
.
utils
.
visitUrl
(
window
.
location
.
href
);
export
function
getLocationHash
(
url
)
{
const
hashIndex
=
window
.
location
.
href
.
indexOf
(
'
#
'
);
return
hashIndex
===
-
1
?
null
:
url
.
substring
(
hashIndex
+
1
);
}
// eslint-disable-next-line import/prefer-default-export
export
function
visitUrl
(
url
,
external
=
false
)
{
if
(
external
)
{
// Simulate `target="blank" rel="noopener noreferrer"`
...
...
@@ -100,6 +79,10 @@ export function visitUrl(url, external = false) {
}
}
export
function
refreshCurrentPage
()
{
visitUrl
(
window
.
location
.
href
);
}
export
function
redirectTo
(
url
)
{
return
window
.
location
.
assign
(
url
);
}
...
...
@@ -107,5 +90,11 @@ export function redirectTo(url) {
window
.
gl
=
window
.
gl
||
{};
window
.
gl
.
utils
=
{
...(
window
.
gl
.
utils
||
{}),
mergeUrlParams
,
getLocationHash
,
getParameterValues
,
redirectTo
,
refreshCurrentPage
,
removeParams
,
visitUrl
,
};
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录