-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathscene_toolbox.html
141 lines (120 loc) · 3.29 KB
/
scene_toolbox.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<style>
* {
margin: 0;
padding: 0;
}
html {
background-color: rgba(0, 0, 0, 0.4);
}
body {
padding: 4px;
text-align: center;
}
select, button {
display: inline-block;
cursor: pointer;
}
button {
padding: 0 6px 4px;
}
.switch-locale {
width: 77px;
}
.switch-scene {
width: 90%;
}
</style>
</head>
<body>
<select id="j-switch-scene" class="switch-scene">
<option></option>
</select>
<script>
// try to get all scenes from querystring: '/scene_toolbox.html?scenes=one,two,...'
var qs = parseQueryString(location.search);
if (qs.domain) {
document.domain = qs.domain;
}
var parent = this.top;
var scenes = window.__koa_mock_scenes || {};
var select = document.getElementById('j-switch-scene');
var queries = parseQueryString(parent.location.search);
var options = '<option value=""></option>';
// create select options
var currentScene = queries.__scene;
for (var filename in scenes) {
var opt = document.createElement('OPTION');
opt.text = scenes[filename] + '(' + filename + ')';
opt.value = filename;
if(currentScene && filename === currentScene) {
opt.selected = true;
}
select.options.add(opt);
}
// register event listener , fix ie8
if (select.addEventListener) {
select.addEventListener('change', handleSelect);
} else if(select.attachEvent) {
select.attachEvent('onchange', handleSelect);
}
// update scene
function handleSelect() {
var val = select.value;
if (val) {
queries.__scene = val;
} else {
delete queries.__scene;
}
var qs = stringifyQueryString(queries);
if (qs) {
parent.location.search = '?' + qs;
} else {
parent.location = parent.location.pathname;
}
}
function stringifyQueryString(qs) {
var s = [];
for (var k in qs) {
s.push(k + '=' + encodeURIComponent(qs[k]));
}
s = s.join('&');
return s;
}
function parseQueryString(search) {
search = search || '';
var str = search.replace(/^(\?|#)/, '');
if (!str) {
return {};
}
var ret = {};
str = decodeURIComponent(str).replace(/\+/g, ' ')
var params = str.split('&');
for (var i = 0; i < params.length; i++) {
var param = params[i];
var index = param.indexOf('=');
var key, val;
if (index === -1) {
key = param;
val = null;
} else {
key = param.substring(0, index);
val = param.substring(index + 1);
}
if (!ret.hasOwnProperty(key)) {
ret[key] = val;
} else if (Array.isArray(ret[key])) {
ret[key].push(val);
} else {
ret[key] = [ret[key], val];
}
}
return ret;
}
</script>
</body>
</html>