Repository navigation
Expand file tree
/
Copy pathdataSource.js
More file actions
166 lines (153 loc) · 7 KB
/
Copy pathdataSource.js
File metadata and controls
166 lines (153 loc) · 7 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
154
155
156
157
158
159
160
161
162
163
164
165
166
/* Copyright© 2000 - 2026 SuperMap Software Co.Ltd. All rights reserved.*/
/**
* 示例数据源支持:通过 sample-data cookie 切换示例列表与示例代码的来源目录。
*
* 无 cookie: examples/<产品>/ (默认数据源)
* sample-data=en: examples/en/<产品>/ (英文数据源)
*
* 用 cookie 而非 url 参数,是因为站点内多处跳转不会带上查询串。
* 该 cookie 只读,本仓库不写入。
*
* 中英文站点(/examples/... 与 /en/examples/...)指向同一份 examples 目录,
* 因此这里全部使用相对路径,两个站点均可正常解析。
*/
var dataSource = {
//当前数据源标识,如 "en"。使用默认数据源时为空字符串
name: "",
//当前产品名,如 "leaflet"
product: "",
//取指定产品配置文件的相对路径
configUrl: null,
//按顺序向页面追加各产品的示例配置脚本
appendConfigs: null,
//计算示例 html 的地址
resolveExampleUrl: null,
//计算示例 html 所在目录
resolveExampleDir: null,
//计算缩略图所在目录
resolveThumbLocation: null,
//计算 web 目录下页面的地址
resolveWebUrl: null
};
(function (dataSource) {
var cKey = "sample-data";
//读取 cookie。只接受字母、数字与下划线,避免把非法字符拼进脚本地址
function getDataSource() {
var name = cKey + "=";
var ca = document.cookie.split(";");
var value = "";
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === " ") {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
value = c.substring(name.length, c.length);
break;
}
}
//只认 en 与 en-xx(如 en-US),其余值一律当作未设置,走默认数据源
return /^en(-[a-zA-Z0-9]+)?$/.test(value) ? value : "";
}
//从路径中取产品名。取页面所在目录名,/examples/leaflet/ 与 /en/examples/leaflet/ 均取到 leaflet
function getProduct() {
var path = window.location.pathname.split("/");
//末位是页面文件名,其前一位即当前目录
return path[path.length - 2] || "";
}
//页面是否已位于该数据源目录下,如 examples/en/leaflet/。此时无需再跳转目录
function isInDataSourceDir(name) {
var path = window.location.pathname.split("/");
return name ? path[path.length - 3] === name : false;
}
var name = getDataSource();
var product = getProduct();
//数据源目录已在当前路径上时不再叠加
var useDataSource = !!name && !isInDataSourceDir(name);
//取指定产品配置文件的相对路径。product 省略时取当前产品
function configUrl(target) {
target = target || product;
if (target === product) {
return useDataSource ? "../" + name + "/" + product + "/config.js" : "./config.js";
}
return useDataSource ? "../" + name + "/" + target + "/config.js" : "../" + target + "/config.js";
}
//按顺序向页面追加各产品的示例配置脚本。$body 为 body 的 jQuery 对象,
//others 为除当前产品外需要加载的产品名数组(供头部导航菜单使用)
function appendConfigs($body, others) {
others = others || [];
for (var i = 0; i < others.length; i++) {
$body.append("<script type='text/javascript' src='" + configUrl(others[i]) + "'><\/script>");
}
//先清空,避免当前产品的 config.js 不存在(404)时,
//页面把上一个加载成功的产品配置当成自己的渲染出来
$body.append("<script type='text/javascript'>window.exampleConfig = {};<\/script>");
//当前产品放最后,保证页面渲染用的 exampleConfig 是当前产品的配置
$body.append("<script type='text/javascript' src='" + configUrl() + "'><\/script>");
}
//消解路径中的 "." 与 "..",避免拼出 /examples/leaflet/../en/leaflet/xx.html 这类地址
function normalizePath(path) {
var parts = path.split("/");
var stack = [];
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
if (part === "." || part === "") {
continue;
}
if (part === "..") {
stack.pop();
continue;
}
stack.push(part);
}
return "/" + stack.join("/");
}
//计算示例 html 的地址。fileName 为配置中的 fileName 字段
function resolveExampleUrl(fileName) {
//只取 pathname 推导目录,避免查询串或 hash 中的 "/" 干扰
var pathname = window.location.pathname;
var dir = pathname.substr(0, pathname.lastIndexOf("/") + 1);
var prefix = useDataSource ? "../" + name + "/" + product + "/" : "./";
return window.location.origin + normalizePath(dir + prefix + fileName + ".html");
}
//计算示例 html 所在目录(结尾带 "/")。
//预览时作为 iframe 的基准地址,使示例里的相对路径按其真实位置解析
function resolveExampleDir(fileName) {
var url = resolveExampleUrl(fileName);
return url.substr(0, url.lastIndexOf("/") + 1);
}
//计算缩略图所在目录,与示例配置取自同一数据源
function resolveThumbLocation() {
var pathname = window.location.pathname;
var dir = pathname.substr(0, pathname.lastIndexOf("/") + 1);
var prefix = useDataSource ? "../" + name + "/" + product + "/" : "./";
//结尾不带 "/",与调用方拼接 "/img/" 的写法保持一致
return window.location.origin + normalizePath(dir + prefix);
}
//计算 web 目录下页面的地址,如 404.html。
//examples 与 web 同级,需从当前页退到 examples 的上一层再进 web。
//退几层只取决于页面自身在 examples 下的深度,与数据源无关
function resolveWebUrl(page) {
var pathname = window.location.pathname;
var parts = pathname.split("/");
//去掉末位的页面文件名
parts.pop();
var index = parts.lastIndexOf("examples");
//examples 到当前目录的层级数,加 1 是为了退到 examples 之外
var levels = index > -1 ? parts.length - index : 2;
var up = "";
for (var i = 0; i < levels; i++) {
up += "../";
}
var dir = pathname.substr(0, pathname.lastIndexOf("/") + 1);
return window.location.origin + normalizePath(dir + up + "web/" + page);
}
dataSource.name = name;
dataSource.product = product;
dataSource.configUrl = configUrl;
dataSource.appendConfigs = appendConfigs;
dataSource.resolveExampleUrl = resolveExampleUrl;
dataSource.resolveExampleDir = resolveExampleDir;
dataSource.resolveThumbLocation = resolveThumbLocation;
dataSource.resolveWebUrl = resolveWebUrl;
})(dataSource);