-
Notifications
You must be signed in to change notification settings - Fork 0
/
Path.js
57 lines (46 loc) · 1.45 KB
/
Path.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
/* eslint-env amd */
'use strict';
(function () {
/* work in browser and webworker */
var global = Function('return this')() // eslint-disable-line
if (typeof global.Artnum === 'undefined') {
global.Artnum = {}
}
global.Artnum.Path = (function () {
var my = {}
/* return origin directory */
my.o = function () {
var o = global.location.pathname.split('/')
for (var i = 0; i < o.length; i++) {
if (o[i]) { break }
}
return '/' + o[i]
}
/* broadcast channel name */
my.bcname = function (id) {
var origin = this.o()
return String(origin + '/' + id).replace('//', '/')
}
/* build url for fetch relative to first level dir, return an URL object */
my.url = function (str, options = {}) {
var dir = this.o()
var url = new URL(global.location.origin + '/' + dir + '/' + str)
if (typeof options.params === 'object') {
for (var param in options.params) {
if (Array.isArray(options.params[param])) {
for (var i = 0; i < options.params[param].length; i++) {
url.searchParams.append(param + '[]', options.params[param][i])
}
} else {
url.searchParams.set(param, options.params[param])
}
}
}
return url
}
return my
}())
if (typeof define === 'function' && define.amd) {
define(['artnum/Path'], function () { return global.Artnum.Path })
}
}())