-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvote.html
351 lines (328 loc) · 14.1 KB
/
nvote.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>进行投票</title>
<link href="./lib/style.css" rel="stylesheet">
<link href="https://cdn.bootcss.com/bootstrap/2.3.2/css/bootstrap.min.css" rel="stylesheet">
<style>
.answer-list {
margin: 0;
}
.answer-list li{
margin-bottom: 15px;
}
.answer-list li .right-pos {
margin-left: 10px;
}
.choice, .description, .start, .end {
width: 300px;
}
.my-create-list li, .my-join-list li {
font-size: 18px;
height: 34px;
line-height: 34px;
list-style: decimal;
margin-left: 10px;
}
</style>
</head>
<body>
<div class="nav-top-wrap">
<ul class="nav-container f-cb">
<li class="f-fl logos">
<a href="./index.html">
<img class="logo-nebula" src="img/vote-logo.png" alt="" />
</a>
</li>
<li class="f-fl item"><a href="./index.html">首页</a></li>
<li class="f-fl item active"><a href="./nvote.html">发起投票</a></li>
<li class="f-fl item"><a href="./help.html">使用说明</a></li>
<li class="f-fr item"><a href="./vote.html">我的投票</a></li>
</ul>
</div>
<div class="container">
<div class="create j-createVoteFrom">
<legend>新的投票</legend>
<div class="form-horizontal">
<div class="control-group">
<label class="control-label" for="inputDescription">调研描述</label>
<div class="controls">
<input type="text" id="inputDescription" class="j-description description" placeholder="描述">
</div>
</div>
<div class="control-group">
<label class="control-label">选项</label>
<div class="controls">
<ul class="answer-list j-choicelist">
<li>
<input type="text" class="j-choice choice" placeholder="选项">
<button class="btn btn-info right-pos j-addChoice">添加</button>
</li>
<li>
<input type="text" class="j-choice choice" placeholder="选项">
<button class="btn btn-warning right-pos j-removeChoice">删除</button>
</li>
</ul>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputBlockStartHeight">投票开始区块高度</label>
<div class="controls">
<input type="text" id="inputBlockStartHeight" class="j-startHeight start" placeholder="开始区块高度">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputBlockEndHeight">投票结束区块高度</label>
<div class="controls">
<input type="text" id="inputBlockEndHeight" class="j-endHeight end" placeholder="结束区块高度">
</div>
<p style="margin-left: 180px; margin-top: 10px; color: #444;">可以在 <a href="https://explorer.nebulas.io">https://explorer.nebulas.io</a> 查看当前区块高度,每隔15s新建一个区块</p>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn btn-primary btn-large j-createVote">创建</button>
</div>
</div>
</div>
</div>
</div>
<div class="noExtension hide" id="noExtension">
<div class="tip-content">
<h2>提示</h2>
<p class="detail">为保证APP正常使用,请安装 <a target="_blank" href="https://github.com/ChengOrangeJu/WebExtensionWallet">WebExtensionWallet</a></p>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/2.3.2/js/bootstrap.min.js"></script>
<script src=lib/nebPay.js></script>
<script src=lib/nebulas.js></script>
<script src=lib/util.js></script>
<script>
var NebPay = require("nebpay"); //https://github.com/nebulasio/nebPay
var nebPay = new NebPay();
var nebulas = require("nebulas"),
Account = nebulas.Account,
Utils = nebulas.Utils,
neb = new nebulas.Neb();
neb.setRequest(new nebulas.HttpRequest("https://testnet.nebulas.io/"));
// var lists = [
// {value: '1', description: '奥巴马'},
// {value: '2', description: '克林顿'},
// {value: '3', description: '特朗普'},
// ];
// var htmlStr = '';
// for (var i=0; i<lists.length; i++) {
// htmlStr += ('<label class="radio"><input type="radio" name="optionsRadios" id="optionsRadios1" class="radio-input" value="' + lists[i].value +
// '">' + lists[i].description + '</label>');
// }
// $("#list").html(htmlStr);
$(".radio-input").attr("disabled", true)
$("#submit").addClass("disabled");
if(typeof(webExtensionWallet) === "undefined"){
$("#noExtension").removeClass("hide")
}else{
$(".radio-input").attr("disabled",false);
$("#submit").removeClass("disabled");
}
var dappAddress = "n235etkTmn7GRmgw7aA9a626sgYQBxPQXsV";
// 新的投票
$(".j-newVote").click(function() {
$(".j-newVote").hide();
$(".j-createVoteFrom").removeClass('hide');
})
// 添加选项
$('.j-addChoice').click(function(e) {
var _htmlStr = '<li><input type="text" class="j-choice choice" placeholder="选项"><button class="btn btn-warning right-pos j-removeChoice">删除</button></li>';
// $(_htmlStr).appendTo($('.j-choicelist'));
$('.j-choicelist').append(_htmlStr);
});
// 删除选项
$('.j-removeChoice').click(function(e) {
var target = e.currentTarget;
$(target).parent().remove();
});
// 创建投票
$('.j-createVote').click(function() {
var description = $('.j-description').val();
if (!description) {
alert('请输入描述');
return;
}
var answer = [];
for (var i=0; i<$('.j-choice').length; i++) {
var val = $('.j-choice').eq(i).val();
val = val.replace(/^\s*|\s*^/g, '');
if (!val) {
alert('第'+(i+1)+'个选项内容为空');
return;
}
answer.push(val);
}
if (answer.length <= 1) {
alert('至少要有两个可选项内容');
return;
}
var answers = answer.join(util.answersSplitFlag);
var startHeight = $('.j-startHeight').val();
if (!startHeight) {
alert('请填写开始区块高度');
return;
}
if (!/\d+/.test(startHeight)) {
alert('开始区块高度要为数字');
return;
}
var endHeight = $('.j-endHeight').val();
if (!endHeight) {
alert('请填写结束区块高度');
return;
}
if (!/\d+/.test(endHeight)) {
alert('结束区块高度要为数字');
return;
}
if (endHeight - startHeight <=0) {
alert('结束区块高度要大于开始区块高度');
return;
}
var to = dappAddress;
var value = "0";
var callFunction = "create";
var callArgs = "[\"" + description + "\", \"" + answers + "\", \"" + startHeight + "\", \"" + endHeight + "\"]";
nebPay.call(to, value, callFunction, callArgs, { //使用nebpay的simulateCall接口去执行get查询, 模拟执行.不发送交易,不上链
listener: function() { //指定回调函数
util.hideLoading();
}
});
util.showLoading();
});
// 取消
$('.j-cancelCreateVote').click(function() {
$(".j-newVote").show();
$(".j-createVoteFrom").addClass('hide');
});
// // 搜索功能: 查找Super-Dictionary 中有没有该词条
// $("#submit").click(function(){
// if ($("#submit").hasClass('disabled')) {
// if (!$(".tip").hasClass("hide")) {
// alert("你已经投票")
// }
// return;
// }
// var val = $('input[name="optionsRadios"]:checked').val();
// if (val === undefined) {
// alert('请选择');
// return;
// }
// var description;
// for (var i=0; i<lists.length; i++) {
// if (lists[i].value == val) {
// description = lists[i].description;
// }
// }
// var to = dappAddress;
// var value = "0";
// var callFunction = "vote";
// var callArgs = "[\"" + val + "\", \"" + description + "\"]";
// // nebPay.simulateCall(to, value, callFunction, callArgs, { //使用nebpay的simulateCall接口去执行get查询, 模拟执行.不发送交易,不上链
// // listener: cbSearch //指定回调函数
// // });
// nebPay.call(to, value, callFunction, callArgs, { //使用nebpay的simulateCall接口去执行get查询, 模拟执行.不发送交易,不上链
// listener: cbVote //指定回调函数
// });
// })
// //return of search,
// function cbVote(resp) {
// var result = resp.result
// console.log("return of rpc call: " + JSON.stringify(result))
// if (result === 'null'){
// } else {
// try {
// result = JSON.parse(result);
// } catch (err) {
// // alert("已经提交过")
// }
// }
// }
// getVoteRecord();
// function getVoteRecord() {
// var to = dappAddress;
// var value = "0";
// var callFunction = "get";
// var callArgs = "[]";
// nebPay.simulateCall(to, value, callFunction, callArgs, { //使用nebpay的call接口去调用合约,
// listener: cbGet
// });
// // nebPay.call(to, value, callFunction, callArgs, { //使用nebpay的call接口去调用合约,
// // listener: cbGet
// // });
// }
// function cbGet(resp) {
// console.log(resp);
// var result = resp.result
// console.log("return of rpc call: " + JSON.stringify(result))
// if (result === 'null'){
// } else {
// try {
// result = JSON.parse(result);
// for (var i=0; i<$('.radio-input').length; i++) {
// if ($('.radio-input').eq(i).val() == result.choice) {
// $('.radio-input').eq(i).attr('checked', true);
// $(".radio-input").attr("disabled",false);
// $("#submit").addClass("disabled");
// $(".tip").removeClass("hide");
// break;
// }
// }
// } catch (err) {
// console.log(err);
// }
// }
// }
// getAccountState("n1Yf3kX9bUbUL3yef2TZxVgghtfPKPuny2B");
// function getAccountState(address) {
// neb.api.getAccountState(address)
// .then(function (resp) {
// console.log('response', resp);
// var nas = Unit.fromBasic(resp.balance, "nas").toNumber();
// })
// .catch(function (e) {
// // this catches e thrown by nebulas.js!neb
// console.log(e);
// });
// }
// function getVoteRecord() {
// neb.api
// .call({
// from: params.from,
// to: params.to,
// value: params.value,
// nonce: params.nonce,
// gasPrice: params.gasPrice,
// gasLimit: params.gasLimit,
// contract: params.contract
// })
// .then(function (resp) {
// $("#call_test_result").text(JSON.stringify(resp));
// if (resp.execute_err && resp.execute_err !== "") {
// $("#call").attr("disabled", true);
// $("#call_result").text("");
// $(".next").removeClass("active1");
// } else {
// $("#call").attr("disabled", false);
// $("#call_result").text("");
// $(".next").removeClass("active1");
// }
// })
// .catch(function (err) {
// $("#call_test_result").text(JSON.stringify(err));
// $("#call").attr("disabled", true);
// $("#call_result").text("");
// $(".next").removeClass("active1");
// });
// }
</script>
</body>
</html>