-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbg-test.html
More file actions
221 lines (191 loc) · 5.68 KB
/
bg-test.html
File metadata and controls
221 lines (191 loc) · 5.68 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!DOCTYPE html>
<html>
<head>
<!--Import Google Icon Font-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!--Import materialize.css-->
<!-- <link type="text/css" rel="stylesheet" href="css/materialize.css" media="screen,projection"/> -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css">
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style type="text/css">
.box {
height: 150px;
line-height: 150px;
text-align: center;
font-size: 40px;
color: white;
}
.console {
min-height: 300px;
max-height: 300px;
}
</style>
<script>
var ctx = {
blue: 0,
green: 0,
delay: 1000, //ms
test: false
}
function renderTheme(res){
if(ctx.test){
res = {color: Math.floor(Math.random() * 2) == 0 ? 'blue' : 'green'};
log('Run as test mode.');
}
// handle response
var color = (res && res.color) || 'grey';
if(!res || !res.color)
return log('ERROR [from={0}, res={1}]'.format(color, JSON.stringify(res)));
// change theme
var target = $('#theme').attr('class');
$('#theme, .target.'+ target).removeClass(target).addClass(color);
// update count
if(ctx[color] !== undefined)
ctx[color] = ctx[color] + 1;
renderRatio();
// print log
var msg = 'Change color : [from={0}, to={1}, res={2}]'.format(target, color, JSON.stringify(res));
log(msg);
};
function renderRatio(){
$('#box-blue').html(ctx.blue);
$('#box-green').html(ctx.green);
}
function log(msg){
var timestamp = moment().format('YYYY-MM-DD HH:mm:ss.SSS');
msg = "{0} :: {1}\n".format(timestamp, msg);
$('.console').prepend(msg);
}
// https://stackoverflow.com/a/4673436
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'? args[number] : match;
});
};
}
</script>
</head>
<body class="grey lighten-4 target">
<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.js"></script>
<!-- <script type="text/javascript" src="js/materialize.min.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
<!-- https://highlightjs.org/ -->
<!-- https://cdnjs.com/libraries/highlight.js/ -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/railscasts.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/bash.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<!-- https://momentjs.com/ -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
<script>
$(document).ready(function(){
$(document).on('click', '#add-blue', function(){
callback({color:'blue'});
});
$(document).on('click', '#add-green', function(){
callback({color:'green'});
});
$(document).on('click', '#btn-clear', function(){
ctx.blue = ctx.green = 0;
renderRatio();
log('Clear request.');
});
$(document).on('click', '#btn-start', function(){
function request(){
function success(res, status, xhr){
console.log(res, xhr.response);
renderTheme(res);
}
function error(xhr, status, err){
renderTheme();
}
/*
$.ajax({
url: 'https://jsonplaceholder.typicode.com/posts/1',
dataType: 'jsonp',
success: wrapper
});
*/
/*
* jquery.js@ jQuery.ajaxTransport
*
* http://json2jsonp.com/
* https://jsonp.afeld.me/
*/
$.ajax({
url: 'https://jsonplaceholder.typicode.com/posts/1',
dataType: 'jsonp',
success: success,
error: error
});
}
log('Start requet.');
ctx.loop = setInterval(request, ctx.delay);
});
$(document).on('click', '#btn-stop', function(){
if(ctx.loop){
clearInterval(ctx.loop);
ctx.loop = undefined;
log('Stop request.');
}
});
renderRatio();
});
</script>
<main class="container">
<div class="row">
<div class="col s12">
<h3 class="header">Blue-Green Test Page</h3>
<nav>
<div class="nav-wrapper grey target">
<div class="col s12">
<a href="#!" class="breadcrumb">Main</a>
<a href="#!" class="breadcrumb">Theme</a>
</div>
</div>
</nav>
</div>
</div>
<div class="row">
<div class="col s8">
<div class="card">
<div class="card-content">
<span class="card-title">Log</span>
<pre>
<code class="bash console"></code>
</pre>
</div>
<div class="card-action">
<a href="#" id="btn-start">Start</a>
<a href="#" id="btn-stop">Stop</a>
<!--
<a href="#" id="add-blue">Blue</a>
<a href="#" id="add-green">Green</a>
-->
</div>
</div>
</div>
<div class="col s4">
<div class="card">
<div class="card-content">
<span class="card-title">Ratio</span>
<div class="row">
<div class="col s6 box blue" id="box-blue"></div>
<div class="col s6 box green" id="box-green"></div>
</div>
</div>
<div class="card-action">
<a href="#" id="btn-clear">Clear</a>
</div>
</div>
</div>
</div>
</main>
<div id="theme" class="grey"></div>
</body>
</html>