uoj.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. // locale
  2. uojLocaleData = {
  3. "username": {
  4. "en": "Username",
  5. "zh-cn": "用户名"
  6. },
  7. "contests::total score": {
  8. "en": "Score",
  9. "zh-cn": "总分"
  10. },
  11. "contests::n participants": {
  12. "en": function(n) {
  13. return n + " participant" + (n <= 1 ? '' : 's');
  14. },
  15. "zh-cn": function(n) {
  16. return "共 " + n + " 名参赛者";
  17. }
  18. },
  19. "click-zan::good": {
  20. "en": "Good",
  21. "zh-cn": "好评"
  22. },
  23. "click-zan::bad": {
  24. "en": "Bad",
  25. "zh-cn": "差评"
  26. },
  27. "editor::use advanced editor": {
  28. "en": "use advanced editor",
  29. "zh-cn": "使用高级编辑器"
  30. },
  31. "editor::language": {
  32. "en": "Language",
  33. "zh-cn": "语言"
  34. },
  35. "editor::browse": {
  36. "en": "Browse",
  37. "zh-cn": "浏览"
  38. },
  39. "editor::upload by editor": {
  40. "en": "Upload by editor",
  41. "zh-cn": "使用编辑器上传"
  42. },
  43. "editor::upload from local": {
  44. "en": "Upload from local",
  45. "zh-cn": "从本地文件上传"
  46. }
  47. };
  48. function uojLocale(name) {
  49. locale = $.cookie('uoj_locale');
  50. if (uojLocaleData[name] === undefined) {
  51. return '';
  52. }
  53. if (uojLocaleData[name][locale] === undefined) {
  54. locale = 'zh-cn';
  55. }
  56. val = uojLocaleData[name][locale];
  57. if (!$.isFunction(val)) {
  58. return val;
  59. } else {
  60. var args = [];
  61. for (var i = 1; i < arguments.length; i++) {
  62. args.push(arguments[i]);
  63. }
  64. return val.apply(this, args);
  65. }
  66. }
  67. // utility
  68. function strToDate(str) {
  69. var a = str.split(/[^0-9]/);
  70. return new Date(
  71. parseInt(a[0]),
  72. parseInt(a[1]) - 1,
  73. parseInt(a[2]),
  74. parseInt(a[3]),
  75. parseInt(a[4]),
  76. parseInt(a[5]),
  77. 0);
  78. }
  79. function dateToStr(date) {
  80. return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
  81. }
  82. function toFilledStr(o, f, l) {
  83. var s = o.toString();
  84. while (s.length < l) {
  85. s = f.toString() + s;
  86. }
  87. return s;
  88. }
  89. function getPenaltyTimeStr(x) {
  90. var ss = toFilledStr(x % 60, '0', 2);
  91. x = Math.floor(x / 60);
  92. var mm = toFilledStr(x % 60, '0', 2);
  93. x = Math.floor(x / 60);
  94. var hh = x.toString();
  95. return hh + ':' + mm + ':' + ss;
  96. }
  97. function htmlspecialchars(str)
  98. {
  99. var s = "";
  100. if (str.length == 0) return "";
  101. s = str.replace(/&/g, "&amp;");
  102. s = s.replace(/</g, "&lt;");
  103. s = s.replace(/>/g, "&gt;");
  104. s = s.replace(/"/g, "&quot;");
  105. return s;
  106. }
  107. function getColOfRating(rating) {
  108. if (rating < 1500) {
  109. var H = 300 - (1500 - 850) * 300 / 1650, S = 30 + (1500 - 850) * 70 / 1650, V = 50 + (1500 - 850) * 50 / 1650;
  110. if (rating < 300) rating = 300;
  111. var k = (rating - 300) / 1200;
  112. return ColorConverter.toStr(ColorConverter.toRGB(new HSV(H + (300 - H) * (1 - k), 30 + (S - 30) * k, 50 + (V - 50) * k)));
  113. }
  114. if (rating > 2500) {
  115. rating = 2500;
  116. }
  117. return ColorConverter.toStr(ColorConverter.toRGB(new HSV(300 - (rating - 850) * 300 / 1650, 30 + (rating - 850) * 70 / 1650, 50 + (rating - 850) * 50 / 1650)));
  118. }
  119. function getColOfScore(score) {
  120. if (score == 0) {
  121. return ColorConverter.toStr(ColorConverter.toRGB(new HSV(0, 100, 80)));
  122. } else if (score == 100) {
  123. return ColorConverter.toStr(ColorConverter.toRGB(new HSV(120, 100, 80)));
  124. } else {
  125. return ColorConverter.toStr(ColorConverter.toRGB(new HSV(30 + score * 60 / 100, 100, 90)));
  126. }
  127. }
  128. function getUserLink(username, rating, addSymbol, certs) {
  129. if (!username) {
  130. return '';
  131. }
  132. if (addSymbol == undefined) {
  133. addSymbol = true;
  134. }
  135. var text = username;
  136. if (username.charAt(0) == '@') {
  137. username = username.substr(1);
  138. }
  139. if (addSymbol) {
  140. if (rating >= 2500) {
  141. text += '<sup>';
  142. for (var i = 2500; i <= rating; i += 200) {
  143. text += "&equiv;"
  144. }
  145. text += "</sup>";
  146. }
  147. }
  148. if (certs) {
  149. var certcolor = certs.split ('#')[0];
  150. var certhonor = certs.split ('#')[1];
  151. }
  152. if (username != 'ryp')
  153. var html = '<a class="uoj-username" href="' + uojHome + '/user/profile/' + username + '" style="color:' + getColOfRating(rating) + '">' + text + '</a>';
  154. else
  155. var html = '<a class="uoj-username" href="' + uojHome + '/user/profile/' + username + '" style="color: black">' + text + '</a>';
  156. if (certhonor != undefined)
  157. html += '<span class="uoj-certs uoj-certs-' + certcolor + '">' + certhonor + '</span>';
  158. return html;
  159. }
  160. function getUserSpan(username, rating, addSymbol, certs) {
  161. if (!username) {
  162. return '';
  163. }
  164. if (addSymbol == undefined) {
  165. addSymbol = true;
  166. }
  167. var text = username;
  168. if (username.charAt(0) == '@') {
  169. username = username.substr(1);
  170. }
  171. if (addSymbol) {
  172. if (rating >= 2500) {
  173. text += '<sup>';
  174. for (var i = 2500; i <= rating; i += 200) {
  175. text += "&equiv;"
  176. }
  177. text += "</sup>";
  178. }
  179. }
  180. if (certs) {
  181. var certcolor = certs.split ('#')[0];
  182. var certhonor = certs.split ('#')[1];
  183. }
  184. if (username != 'ryp')
  185. var html = '<span class="uoj-username" style="color:' + getColOfRating(rating) + '">' + text + '</span>';
  186. else
  187. var html = '<span class="uoj-username" style="color: black">' + text + '</span>';
  188. if (certhonor != undefined)
  189. html += '<span class="uoj-certs uoj-certs-' + certcolor + '">' + certhonor + '</span>';
  190. return html;
  191. }
  192. function replaceWithHighlightUsername() {
  193. var username = $(this).text();
  194. var rating = $(this).data("rating");
  195. var certs = $(this).data("certs");
  196. if (isNaN(rating)) {
  197. return;
  198. }
  199. if ($(this).data("link") != 0) {
  200. $(this).replaceWith(getUserLink(username, rating, false, certs));
  201. } else {
  202. $(this).replaceWith(getUserSpan(username, rating, false, certs));
  203. }
  204. }
  205. $.fn.uoj_honor = function() {
  206. return this.each(function() {
  207. var honor = $(this).text();
  208. var rating = $(this).data("rating");
  209. var certs = $(this).data("certs");
  210. if (isNaN(rating)) {
  211. return;
  212. }
  213. if (rating >= 2500) {
  214. honor += '<sup>';
  215. for (var i = 2500; i <= rating; i += 200) {
  216. honor += "&equiv;"
  217. }
  218. honor += "</sup>";
  219. }
  220. if (certs) {
  221. var certcolor = certs.split ('#')[0];
  222. var certhonor = certs.split ('#')[1];
  223. if (certhonor != undefined)
  224. honor += '<span class="uoj-certs uoj-certs-' + certcolor + '">' + certhonor + '</span>';
  225. }
  226. $(this).css("color", getColOfRating(rating)).html(honor);
  227. });
  228. }
  229. function showErrorHelp(name, err) {
  230. if (err) {
  231. $('#div-' + name).addClass('has-error');
  232. $('#help-' + name).text(err);
  233. return false;
  234. } else {
  235. $('#div-' + name).removeClass('has-error');
  236. $('#help-' + name).text('');
  237. return true;
  238. }
  239. }
  240. function getFormErrorAndShowHelp(name, val) {
  241. var err = val($('#input-' + name).val());
  242. return showErrorHelp(name, err);
  243. }
  244. function validateSettingPassword(str) {
  245. if (str.length < 6) {
  246. return '密码长度不应小于6。';
  247. } else if (! /^[!-~]+$/.test(str)) {
  248. return '密码应只包含可见ASCII字符。';
  249. } else if (str != $('#input-confirm_password').val()) {
  250. return '两次输入的密码不一致。';
  251. } else {
  252. return '';
  253. }
  254. }
  255. function validatePassword(str) {
  256. if (str.length < 6) {
  257. return '密码长度不应小于6。';
  258. } else if (! /^[!-~]+$/.test(str)) {
  259. return '密码应只包含可见ASCII字符。';
  260. } else {
  261. return '';
  262. }
  263. }
  264. function validateEmail(str) {
  265. if (str.length > 50) {
  266. return '电子邮箱地址太长。';
  267. } else if (! /^(.+)@(.+)$/.test(str)) {
  268. return '电子邮箱地址非法。';
  269. } else {
  270. return '';
  271. }
  272. }
  273. function validateUsername(str) {
  274. if (str.length == 0) {
  275. return '用户名不能为空。';
  276. } else if (! /^[a-zA-Z0-9_]+$/.test(str)) {
  277. return '用户名应只包含大小写英文字母、数字和下划线。';
  278. } else {
  279. return '';
  280. }
  281. }
  282. function validateQQ(str) {
  283. if (str.length < 5) {
  284. return 'QQ的长度不应小于5。';
  285. } else if (str.length > 15) {
  286. return 'QQ的长度不应大于15。';
  287. } else if (/\D/.test(str)) {
  288. return 'QQ应只包含0~9的数字。';
  289. } else {
  290. return '';
  291. }
  292. }
  293. function validateMotto(str) {
  294. if (str.length > 50) {
  295. return '不能超过50字';
  296. } else {
  297. return '';
  298. }
  299. }
  300. // tags
  301. $.fn.uoj_problem_tag = function() {
  302. return this.each(function() {
  303. $(this).attr('href', uojHome + '/problems?tag=' + encodeURIComponent($(this).text()));
  304. });
  305. }
  306. $.fn.uoj_blog_tag = function() {
  307. return this.each(function() {
  308. $(this).attr('href', uojBlogUrl + '/archive?tag=' + encodeURIComponent($(this).text()));
  309. });
  310. }
  311. // click zan
  312. function click_zan(zan_id, zan_type, zan_delta, node) {
  313. var loading_node = $('<div class="text-muted">loading...</div>');
  314. $(node).replaceWith(loading_node);
  315. $.post(zan_link + '/click-zan', {
  316. id : zan_id,
  317. delta : zan_delta,
  318. type : zan_type,
  319. _token : uojCsrfToken
  320. }, function(ret) {
  321. $(loading_node).replaceWith($(ret).click_zan_block());
  322. }).fail(function() {
  323. $(loading_node).replaceWith('<div class="text-danger">failed</div>');
  324. });
  325. }
  326. $.fn.click_zan_block = function() {
  327. return this.each(function() {
  328. var id = $(this).data('id');
  329. var type = $(this).data('type');
  330. var val = parseInt($(this).data('val'));
  331. var cnt = parseInt($(this).data('cnt'));
  332. if (isNaN(cnt)) {
  333. return;
  334. }
  335. if (val == 1) {
  336. $(this).addClass('uoj-click-zan-block-cur-up');
  337. } else if (val == 0) {
  338. $(this).addClass('uoj-click-zan-block-cur-zero');
  339. } else if (val == -1) {
  340. $(this).addClass('uoj-click-zan-block-cur-down');
  341. } else {
  342. return;
  343. }
  344. if (cnt > 0) {
  345. $(this).addClass('uoj-click-zan-block-positive');
  346. } else if (cnt == 0) {
  347. $(this).addClass('uoj-click-zan-block-neutral');
  348. } else {
  349. $(this).addClass('uoj-click-zan-block-negative');
  350. }
  351. var node = this;
  352. var up_node = $('<a href="#" class="uoj-click-zan-up"><span class="glyphicon glyphicon-thumbs-up"></span>'+uojLocale('click-zan::good')+'</a>').click(function(e) {
  353. e.preventDefault();
  354. click_zan(id, type, 1, node);
  355. });
  356. var down_node = $('<a href="#" class="uoj-click-zan-down"><span class="glyphicon glyphicon-thumbs-down"></span>'+uojLocale('click-zan::bad')+'</a>').click(function(e) {
  357. e.preventDefault();
  358. click_zan(id, type, -1, node);
  359. });
  360. $(this)
  361. .append(up_node)
  362. .append(down_node)
  363. .append($('<span class="uoj-click-zan-cnt">[<strong>' + (cnt > 0 ? '+' + cnt : cnt) + '</strong>]</span>'));
  364. });
  365. }
  366. // count down
  367. function getCountdownStr(t) {
  368. var x = Math.floor(t);
  369. var ss = toFilledStr(x % 60, '0', 2);
  370. x = Math.floor(x / 60);
  371. var mm = toFilledStr(x % 60, '0', 2);
  372. x = Math.floor(x / 60);
  373. var hh = x.toString();
  374. var res = '<span style="font-size:30px">';
  375. res += '<span style="color:' + getColOfScore(Math.min(t / 10800 * 100, 100)) + '">' + hh + '</span>';
  376. res += ':';
  377. res += '<span style="color:' + getColOfScore(mm / 60 * 100) + '">' + mm + '</span>';
  378. res += ':';
  379. res += '<span style="color:' + getColOfScore(ss / 60 * 100) + '">' + ss + '</span>';
  380. res += '</span>'
  381. return res;
  382. }
  383. $.fn.countdown = function(rest, callback) {
  384. return this.each(function() {
  385. var start = new Date().getTime();
  386. var cur_rest = rest != undefined ? rest : parseInt($(this).data('rest'));
  387. var cur = this;
  388. var countdown = function() {
  389. var passed = Math.floor((new Date().getTime() - start) / 1000);
  390. if (passed >= cur_rest) {
  391. $(cur).html(getCountdownStr(0));
  392. if (callback != undefined) {
  393. callback();
  394. }
  395. } else {
  396. $(cur).html(getCountdownStr(cur_rest - passed));
  397. setTimeout(countdown, 1000);
  398. }
  399. }
  400. countdown();
  401. });
  402. };
  403. // update_judgement_status
  404. update_judgement_status_list = []
  405. function update_judgement_status_details(id) {
  406. update_judgement_status_list.push(id);
  407. };
  408. $(document).ready(function() {
  409. function update() {
  410. $.get("/submission-status-details", {
  411. get: update_judgement_status_list
  412. },
  413. function(data) {
  414. for (var i = 0; i < update_judgement_status_list.length; i++) {
  415. $("#status_details_" + update_judgement_status_list[i]).html(data[i].html);
  416. if (data[i].judged) {
  417. location.reload();
  418. }
  419. }
  420. }, 'json').always(
  421. function() {
  422. setTimeout(update, 500);
  423. }
  424. );
  425. }
  426. if (update_judgement_status_list.length > 0) {
  427. setTimeout(update, 500);
  428. }
  429. });
  430. // highlight
  431. $.fn.uoj_highlight = function() {
  432. return $(this).each(function() {
  433. $(this).find("span.uoj-username").each(replaceWithHighlightUsername);
  434. $(this).find(".uoj-honor").uoj_honor();
  435. $(this).find(".uoj-score").each(function() {
  436. var score = parseInt($(this).text());
  437. var maxscore = parseInt($(this).data('max'));
  438. if (isNaN(score)) {
  439. return;
  440. }
  441. if (isNaN(maxscore)) {
  442. $(this).css("color", getColOfScore(score));
  443. } else {
  444. $(this).css("color", getColOfScore(score / maxscore * 100));
  445. }
  446. });
  447. $(this).find(".uoj-status").each(function() {
  448. var success = parseInt($(this).data("success"));
  449. if(isNaN(success)){
  450. return;
  451. }
  452. if (success == 1) {
  453. $(this).css("color", ColorConverter.toStr(ColorConverter.toRGB(new HSV(120, 100, 80))));
  454. }
  455. else {
  456. $(this).css("color", ColorConverter.toStr(ColorConverter.toRGB(new HSV(0, 100, 100))));
  457. }
  458. });
  459. $(this).find(".uoj-problem-tag").uoj_problem_tag();
  460. $(this).find(".uoj-blog-tag").uoj_blog_tag();
  461. $(this).find(".uoj-click-zan-block").click_zan_block();
  462. $(this).find(".countdown").countdown();
  463. $(this).find(".uoj-readmore").readmore({
  464. moreLink: '<a href="#" class="text-right">more...</a>',
  465. lessLink: '<a href="#" class="text-right">close</a>',
  466. });
  467. });
  468. };
  469. $(document).ready(function() {
  470. $('body').uoj_highlight();
  471. });
  472. // contest notice
  473. function checkContestNotice(id, lastTime) {
  474. $.post('/contest/' + id.toString(), {
  475. check_notice : '',
  476. last_time : lastTime
  477. },
  478. function(data) {
  479. setTimeout(function() {
  480. checkContestNotice(id, data.time);
  481. }, 60000);
  482. if (data.msg != undefined) {
  483. var len=data.msg.length;
  484. for (var i=0;i<len;i++) alert(data.msg[i]);
  485. }
  486. },
  487. 'json'
  488. ).fail(function() {
  489. setTimeout(function() {
  490. checkContestNotice(id, lastTime);
  491. }, 60000);
  492. });
  493. }
  494. // long table
  495. $.fn.long_table = function(data, cur_page, header_row, get_row_str, config) {
  496. return this.each(function() {
  497. var table_div = this;
  498. $(table_div).html('');
  499. var page_len = config.page_len != undefined ? config.page_len : 10;
  500. if (!config.echo_full) {
  501. var n_rows = data.length;
  502. var n_pages = Math.max(Math.ceil(n_rows / page_len), 1);
  503. if (cur_page == undefined) {
  504. cur_page = 1;
  505. }
  506. if (cur_page < 1) {
  507. cur_page = 1;
  508. } else if (cur_page > n_pages) {
  509. cur_page = n_pages;
  510. }
  511. var cur_start = (cur_page - 1) * page_len;
  512. } else {
  513. var n_rows = data.length;
  514. var n_pages = 1;
  515. cur_page = 1;
  516. var cur_start = (cur_page - 1) * page_len;
  517. }
  518. var div_classes = config.div_classes != undefined ? config.div_classes : ['table-responsive'];
  519. var table_classes = config.table_classes != undefined ? config.table_classes : ['table', 'table-bordered', 'table-hover', 'table-striped', 'table-text-center'];
  520. var now_cnt = 0;
  521. var tbody = $('<tbody />')
  522. for (var i = 0; i < page_len && cur_start + i < n_rows; i++) {
  523. now_cnt++;
  524. if (config.get_row_index) {
  525. tbody.append(get_row_str(data[cur_start + i], cur_start + i));
  526. } else {
  527. tbody.append(get_row_str(data[cur_start + i]));
  528. }
  529. }
  530. if (now_cnt == 0) {
  531. tbody.append('<tr><td colspan="233">无</td></tr>');
  532. }
  533. $(table_div).append(
  534. $('<div class="' + div_classes.join(' ') + '" />').append(
  535. $('<table class="' + table_classes.join(' ') + '" />').append(
  536. $('<thead>' + header_row + '</thead>')
  537. ).append(
  538. tbody
  539. )
  540. )
  541. );
  542. if (config.print_after_table != undefined) {
  543. $(table_div).append(config.print_after_table());
  544. }
  545. var get_page_li = function(p, h) {
  546. if (p == -1) {
  547. return $('<li class="page-item"></li>').addClass('disabled').append($('<a class="page-link"></a>').append(h));
  548. }
  549. var li = $('<li class="page-item"></li>');
  550. if (p == cur_page) {
  551. li.addClass('active');
  552. }
  553. li.append(
  554. $('<a class="page-link"></a>').attr('href', '#' + table_div.id).append(h).click(function(e) {
  555. if (config.prevent_focus_on_click) {
  556. e.preventDefault();
  557. }
  558. $(table_div).long_table(data, p, header_row, get_row_str, config);
  559. })
  560. );
  561. return li;
  562. };
  563. if (n_pages > 1) {
  564. var pagination = $('<ul class="pagination top-buffer-no bot-buffer-sm justify-content-center"></ul>');
  565. if (cur_page > 1) {
  566. pagination.append(get_page_li(cur_page - 1, '<span class="glyphicon glyphicon glyphicon-backward"></span>'));
  567. } else {
  568. pagination.append(get_page_li(-1, '<span class="glyphicon glyphicon glyphicon-backward"></span>'));
  569. }
  570. var max_extend = config.max_extend != undefined ? config.max_extend : 5;
  571. for (var i = Math.max(cur_page - max_extend, 1); i <= Math.min(cur_page + max_extend, n_pages); i++) {
  572. pagination.append(get_page_li(i, i.toString()));
  573. }
  574. if (cur_page < n_pages) {
  575. pagination.append(get_page_li(cur_page + 1, '<span class="glyphicon glyphicon glyphicon-forward"></span>'));
  576. } else {
  577. pagination.append(get_page_li(-1, '<span class="glyphicon glyphicon glyphicon-forward"></span>'));
  578. }
  579. $(table_div).append($('<div class="text-center"></div>').append(pagination));
  580. }
  581. });
  582. };
  583. // code mirror
  584. function require_codemirror(config, callback) {
  585. if ($('link[href="' + uojHome + '/js/codemirror/lib/codemirror.css' + '"]').length == 0) {
  586. $('<link type="text/css" rel="stylesheet" href="' + uojHome + '/js/codemirror/lib/codemirror.css' + '" />').appendTo('head');
  587. }
  588. $LAB.script(uojHome + '/js/codemirror/lib/codemirror.js')
  589. .wait()
  590. .script(uojHome + '/js/codemirror/addon/mode/overlay.js')
  591. .script(uojHome + '/js/codemirror/addon/selection/active-line.js')
  592. .wait(callback)
  593. }
  594. function get_codemirror_mode(lang) {
  595. switch (lang) {
  596. case 'C++':
  597. case 'C++11':
  598. case 'C++14':
  599. case 'C++17':
  600. case 'C++17ubsan':
  601. return 'text/x-c++src';
  602. case 'C':
  603. return 'text/x-csrc';
  604. case 'Python2':
  605. case 'Python3':
  606. return 'text/x-python';
  607. case 'Java8':
  608. case 'Java11':
  609. return 'text/x-java';
  610. case 'Pascal':
  611. return 'text/x-pascal';
  612. case 'text':
  613. return 'text/plain';
  614. default:
  615. return 'text/plain';
  616. }
  617. };
  618. function require_codemirror_mode(mode, callback) {
  619. var name = 'none';
  620. switch (mode) {
  621. case 'text/x-c++src':
  622. case 'text/x-csrc':
  623. case 'text/x-java':
  624. name = 'clike';
  625. break;
  626. case 'text/x-python':
  627. name = 'python';
  628. break;
  629. case 'text/x-pascal':
  630. name = 'pascal';
  631. break;
  632. }
  633. if (name !== 'none') {
  634. $LAB.script(uojHome + '/js/codemirror/mode/' + name + '/' + name + '.js')
  635. .wait(callback);
  636. } else {
  637. setTimeout(callback, 0);
  638. }
  639. };
  640. // auto save
  641. function autosave_locally(interval, name, target) {
  642. if (typeof(Storage) === "undefined") {
  643. console.log('autosave_locally: Sorry! No Web Storage support..');
  644. return;
  645. }
  646. var url = window.location.href;
  647. var hp = url.indexOf('#');
  648. var uri = hp == -1 ? url : url.substr(0, hp);
  649. var full_name = name + '@' + uri;
  650. target.val(localStorage.getItem(full_name));
  651. var save = function() {
  652. localStorage.setItem(full_name, target.val());
  653. setTimeout(save, interval);
  654. };
  655. setTimeout(save, interval);
  656. }
  657. // source code form group
  658. $.fn.source_code_form_group = function(name, text, langs_options_html) {
  659. return this.each(function() {
  660. var input_language_id = 'input-' + name + '_language';
  661. var input_language_name = name + '_language';
  662. var input_upload_type_name = name + '_upload_type';
  663. var input_editor_id = 'input-' + name + '_editor';
  664. var input_editor_name = name + '_editor';
  665. var input_file_id = 'input-' + name + '_file';
  666. var input_file_name = name + '_file';
  667. var div_help_language_id = 'div-help-' + name + '_language';
  668. var div_editor_id = 'div-' + name + '_editor';
  669. var div_file_id = 'div-' + name + '_file';
  670. var help_file_id = 'help-' + name + '_file';
  671. var input_language =
  672. $('<select id="' + input_language_id + '" name="' + input_language_name + '" class="form-control input-sm"/>')
  673. .html(langs_options_html);
  674. var input_upload_type_editor = $('<input type="radio" name="' + input_upload_type_name + '" value="editor" />');
  675. var input_upload_type_file = $('<input type="radio" name="' + input_upload_type_name + '" value="file" />');
  676. var input_file = $('<input type="file" id="' + input_file_id + '" name="' + input_file_name + '" style="display: none" />');
  677. var input_file_path = $('<input class="form-control" type="text" readonly="readonly" />');
  678. var input_editor = $('<textarea class="form-control" id="' + input_editor_id + '" name="' + input_editor_name + '"></textarea>');
  679. var input_use_advanced_editor = $('<input type="checkbox">');
  680. var div_editor =
  681. $('<div id="' + div_editor_id + '" class="col-sm-12"/>')
  682. .append(input_editor)
  683. .append($('<div class="checkbox text-right" />')
  684. .append($('<label />')
  685. .append(input_use_advanced_editor)
  686. .append(' ' + uojLocale('editor::use advanced editor'))
  687. )
  688. )
  689. var div_file =
  690. $('<div id="' + div_file_id + '" class="col-sm-12"/>')
  691. .append(input_file)
  692. .append($('<div class="input-group"/>')
  693. .append(input_file_path)
  694. .append($('<span class="input-group-append"/>')
  695. .append($('<button type="button" class="btn btn-primary">'+'<span class="glyphicon glyphicon-folder-open"></span> '+uojLocale('editor::browse')+'</button>')
  696. .css('width', '100px')
  697. .click(function() {
  698. input_file.click();
  699. })
  700. )
  701. )
  702. )
  703. .append($('<span class="help-block" id="' + help_file_id + '"></span>'))
  704. var div_help_language = $('<div id="' + div_help_language_id + '" class="col-sm-12 text-warning top-buffer-sm">');
  705. var show_help_lang = function() {
  706. if ($(this).val() == 'Java8' || $(this).val() == 'Java11') {
  707. div_help_language.text('注意:Java 程序源代码中不应指定所在的 package。我们会在源代码中找到第一个被定义的类并以它的 main 函数为程序入口点。');
  708. } else {
  709. div_help_language.text('');
  710. }
  711. };
  712. var advanced_editor = null;
  713. var advanced_editor_init = function() {
  714. require_codemirror({}, function() {
  715. var mode = get_codemirror_mode(input_language.val());
  716. require_codemirror_mode(mode, function() {
  717. if (advanced_editor != null) {
  718. return;
  719. }
  720. advanced_editor = CodeMirror.fromTextArea(input_editor[0], {
  721. mode: mode,
  722. lineNumbers: true,
  723. matchBrackets: true,
  724. lineWrapping: true,
  725. styleActiveLine: true,
  726. indentUnit: 4,
  727. indentWithTabs: true,
  728. theme: 'default'
  729. });
  730. advanced_editor.on('change', function() {
  731. advanced_editor.save();
  732. });
  733. $(advanced_editor.getWrapperElement()).css('box-shadow', '0 2px 10px rgba(0,0,0,0.2)');
  734. advanced_editor.focus();
  735. });
  736. });
  737. }
  738. var save_prefer_upload_type = function(type) {
  739. $.cookie('uoj_source_code_form_group_preferred_upload_type', type, { expires: 7, path: '/' });
  740. };
  741. autosave_locally(2000, name, input_editor);
  742. var prefer_upload_type = $.cookie('uoj_source_code_form_group_preferred_upload_type');
  743. if (prefer_upload_type === null) {
  744. prefer_upload_type = 'editor';
  745. }
  746. if (prefer_upload_type == 'file') {
  747. input_upload_type_file[0].checked = true;
  748. div_editor.css('display', 'none');
  749. } else {
  750. input_upload_type_editor[0].checked = true;
  751. div_file.css('display', 'none');
  752. if (prefer_upload_type == 'advanced') {
  753. input_use_advanced_editor[0].checked = true;
  754. }
  755. }
  756. input_language.each(show_help_lang);
  757. input_language.change(show_help_lang);
  758. input_language.change(function() {
  759. if (advanced_editor != null) {
  760. var mode = get_codemirror_mode(input_language.val());
  761. require_codemirror_mode(mode, function() {
  762. if (mode != get_codemirror_mode(input_language.val())) {
  763. return;
  764. }
  765. advanced_editor.setOption('mode', mode);
  766. });
  767. }
  768. })
  769. input_upload_type_editor.click(function() {
  770. div_editor.show('fast');
  771. div_file.hide('fast');
  772. save_prefer_upload_type('editor');
  773. });
  774. input_upload_type_file.click(function() {
  775. div_file.show('fast');
  776. div_editor.hide('fast');
  777. save_prefer_upload_type('file');
  778. });
  779. input_file.change(function() {
  780. input_file_path.val(input_file.val());
  781. });
  782. input_use_advanced_editor.click(function() {
  783. if (this.checked) {
  784. advanced_editor_init();
  785. save_prefer_upload_type('advanced');
  786. } else {
  787. if (advanced_editor != null) {
  788. advanced_editor.toTextArea();
  789. advanced_editor = null;
  790. input_editor.focus();
  791. }
  792. save_prefer_upload_type('editor');
  793. }
  794. });
  795. $(this)
  796. .append($('<div class="row col-sm-12"/>')
  797. .append($('<label class="col-sm-2 control-label"><div class="text-left">' + text + '</div></label>'))
  798. .append($('<label class="col-sm-1 control-label" for="' + input_language_name + '">'+uojLocale('editor::language')+'</label>'))
  799. .append($('<div class="col-sm-2"/>')
  800. .append(input_language)
  801. )
  802. .append($('<div class="col-sm-2 offset-sm-3 radio"/>')
  803. .append($('<label/>')
  804. .append(input_upload_type_editor)
  805. .append(' '+uojLocale('editor::upload by editor'))
  806. )
  807. )
  808. .append($('<div class="col-sm-2 radio"/>')
  809. .append($('<label/>')
  810. .append(input_upload_type_file)
  811. .append(' '+uojLocale('editor::upload from local'))
  812. )
  813. ))
  814. .append(div_help_language)
  815. .append(div_editor)
  816. .append(div_file);
  817. if (prefer_upload_type == 'advanced') {
  818. var check_advanced_init = function() {
  819. if (div_editor.is(':visible')) {
  820. advanced_editor_init();
  821. } else {
  822. setTimeout(check_advanced_init, 1);
  823. }
  824. }
  825. check_advanced_init();
  826. }
  827. });
  828. }
  829. // text file form group
  830. $.fn.text_file_form_group = function(name, text) {
  831. return this.each(function() {
  832. var input_upload_type_name = name + '_upload_type';
  833. var input_editor_id = 'input-' + name + '_editor';
  834. var input_editor_name = name + '_editor';
  835. var input_file_id = 'input-' + name + '_file';
  836. var input_file_name = name + '_file';
  837. var div_editor_id = 'div-' + name + '_editor';
  838. var div_file_id = 'div-' + name + '_file';
  839. var help_file_id = 'help-' + name + '_file';
  840. var input_upload_type_editor = $('<input type="radio" name="' + input_upload_type_name + '" value="editor" />');
  841. var input_upload_type_file = $('<input type="radio" name="' + input_upload_type_name + '" value="file" />');
  842. var input_file = $('<input type="file" id="' + input_file_id + '" name="' + input_file_name + '" style="display: none" />');
  843. var input_file_path = $('<input class="form-control" type="text" readonly="readonly" />');
  844. var input_editor = $('<textarea class="form-control" id="' + input_editor_id + '" name="' + input_editor_name + '"></textarea>');
  845. var input_use_advanced_editor = $('<input type="checkbox">');
  846. var div_editor =
  847. $('<div id="' + div_editor_id + '" class="col-sm-12"/>')
  848. .append(input_editor)
  849. .append($('<div class="checkbox text-right" />')
  850. .append($('<label />')
  851. .append(input_use_advanced_editor)
  852. .append(' ' + uojLocale('editor::use advanced editor'))
  853. )
  854. )
  855. var div_file =
  856. $('<div id="' + div_file_id + '" class="col-sm-12"/>')
  857. .append(input_file)
  858. .append($('<div class="input-group"/>')
  859. .append(input_file_path)
  860. .append($('<div class="input-group-append"/>')
  861. .append($('<button type="button" class="btn btn-primary">'+'<span class="glyphicon glyphicon-folder-open"></span> '+uojLocale('editor::browse')+'</button>')
  862. .css('width', '100px')
  863. .click(function() {
  864. input_file.click();
  865. })
  866. )
  867. )
  868. )
  869. .append($('<span class="help-block" id="' + help_file_id + '"></span>'))
  870. var advanced_editor = null;
  871. var advanced_editor_init = function() {
  872. require_codemirror({}, function() {
  873. var mode = get_codemirror_mode('text');
  874. require_codemirror_mode(mode, function() {
  875. if (advanced_editor != null) {
  876. return;
  877. }
  878. advanced_editor = CodeMirror.fromTextArea(input_editor[0], {
  879. mode: mode,
  880. lineNumbers: true,
  881. matchBrackets: true,
  882. lineWrapping: true,
  883. styleActiveLine: true,
  884. indentUnit: 4,
  885. indentWithTabs: true,
  886. theme: 'default'
  887. });
  888. advanced_editor.on('change', function() {
  889. advanced_editor.save();
  890. });
  891. $(advanced_editor.getWrapperElement()).css('box-shadow', '0 2px 10px rgba(0,0,0,0.2)');
  892. advanced_editor.focus();
  893. });
  894. });
  895. }
  896. var save_prefer_upload_type = function(type) {
  897. $.cookie('uoj_text_file_form_group_preferred_upload_type', type, { expires: 7, path: '/' });
  898. };
  899. autosave_locally(2000, name, input_editor);
  900. var prefer_upload_type = $.cookie('uoj_text_file_form_group_preferred_upload_type');
  901. if (prefer_upload_type === null) {
  902. prefer_upload_type = 'editor';
  903. }
  904. if (prefer_upload_type == 'file') {
  905. input_upload_type_file[0].checked = true;
  906. div_editor.css('display', 'none');
  907. } else {
  908. input_upload_type_editor[0].checked = true;
  909. div_file.css('display', 'none');
  910. if (prefer_upload_type == 'advanced') {
  911. input_use_advanced_editor[0].checked = true;
  912. }
  913. }
  914. input_upload_type_editor.click(function() {
  915. div_editor.show('fast');
  916. div_file.hide('fast');
  917. save_prefer_upload_type('editor');
  918. });
  919. input_upload_type_file.click(function() {
  920. div_file.show('fast');
  921. div_editor.hide('fast');
  922. save_prefer_upload_type('file');
  923. });
  924. input_file.change(function() {
  925. input_file_path.val(input_file.val());
  926. });
  927. input_use_advanced_editor.click(function() {
  928. if (this.checked) {
  929. advanced_editor_init();
  930. save_prefer_upload_type('advanced');
  931. } else {
  932. if (advanced_editor != null) {
  933. advanced_editor.toTextArea();
  934. advanced_editor = null;
  935. input_editor.focus();
  936. }
  937. save_prefer_upload_type('editor');
  938. }
  939. });
  940. $(this)
  941. .append($('<div class="row"/>')
  942. .append($('<label class="col-sm-2 control-label"><div class="text-left">' + text + '</div></label>'))
  943. .append($('<div class="top-buffer-sm" />'))
  944. .append($('<div class="col-sm-2 offset-sm-6 radio"/>')
  945. .append($('<label/>')
  946. .append(input_upload_type_editor)
  947. .append(' '+uojLocale('editor::upload by editor'))
  948. )
  949. )
  950. .append($('<div class="col-sm-2 radio"/>')
  951. .append($('<label/>')
  952. .append(input_upload_type_file)
  953. .append(' '+uojLocale('editor::upload from local'))
  954. )
  955. ))
  956. .append(div_editor)
  957. .append(div_file);
  958. if (prefer_upload_type == 'advanced') {
  959. var check_advanced_init = function() {
  960. if (div_editor.is(':visible')) {
  961. advanced_editor_init();
  962. } else {
  963. setTimeout(check_advanced_init, 1);
  964. }
  965. }
  966. check_advanced_init();
  967. }
  968. });
  969. }
  970. // custom test
  971. function custom_test_onsubmit(response_text, div_result, url) {
  972. if (response_text != '') {
  973. $(div_result).html('<div class="text-danger">' + response_text + '</div>');
  974. return;
  975. }
  976. var update = function() {
  977. var can_next = true;
  978. $.get(url,
  979. function(data) {
  980. if (data.judged === undefined) {
  981. $(div_result).html('<div class="text-danger">error</div>');
  982. } else {
  983. var judge_status = $('<table class="table table-bordered table-text-center"><tr class="info">' + data.html + '</tr></table>');
  984. $(div_result).empty();
  985. $(div_result).append(judge_status);
  986. if (data.judged) {
  987. var judge_result = $(data.result);
  988. judge_result.css('display', 'none');
  989. $(div_result).append(judge_result);
  990. judge_status.hide(500);
  991. judge_result.slideDown(500);
  992. can_next = false;
  993. }
  994. }
  995. }, 'json')
  996. .always(function() {
  997. if (can_next) {
  998. setTimeout(update, 500);
  999. }
  1000. });
  1001. };
  1002. setTimeout(update, 500);
  1003. }
  1004. // comment
  1005. function showCommentReplies(id, replies) {
  1006. var toggleFormReply = function(from, text) {
  1007. if (text == undefined) {
  1008. text = '';
  1009. }
  1010. var p = '#comment-body-' + id;
  1011. var q = '#div-form-reply';
  1012. var r = '#input-reply_comment';
  1013. var t = '#input-reply_id';
  1014. if ($(q).data('from') != from) {
  1015. $(q).data('from', from);
  1016. $(q).hide('fast', function() {
  1017. $(this).appendTo(p).show('fast', function() {
  1018. $(t).val(id);
  1019. $(r).val(text).focus();
  1020. });
  1021. });
  1022. } else if ($(q).css('display') != 'none') {
  1023. $(q).appendTo(p).hide('fast');
  1024. } else {
  1025. $(q).appendTo(p).show('fast', function() {
  1026. $(t).val(id);
  1027. $(r).val(text).focus();
  1028. });
  1029. }
  1030. }
  1031. $('#reply-to-' + id).click(function(e) {
  1032. e.preventDefault();
  1033. toggleFormReply(id);
  1034. });
  1035. if (replies.length == 0) {
  1036. return;
  1037. }
  1038. $("#replies-" + id).long_table(
  1039. replies,
  1040. 1,
  1041. '<tr>' +
  1042. '<th>评论回复</th>' +
  1043. '</tr>',
  1044. function(reply) {
  1045. return $('<tr id="' + 'comment-' + reply.id + '" />').append(
  1046. $('<td />').append(
  1047. $('<div class="comtbox6">' + getUserLink(reply.poster, reply.poster_rating) + ':' + reply.content + '</div>')
  1048. ).append(
  1049. $('<ul class="text-right list-inline bot-buffer-no" />').append(
  1050. '<li>' + '<small class="text-muted">' + reply.post_time + '</small>' + '</li>'
  1051. ).append(
  1052. $('<li />').append(
  1053. $('<a href="#">回复</a>').click(function (e) {
  1054. e.preventDefault();
  1055. toggleFormReply(reply.id, '回复 @' + reply.poster + ':');
  1056. })
  1057. )
  1058. )
  1059. )
  1060. ).uoj_highlight();
  1061. }, {
  1062. table_classes: ['table', 'table-condensed'],
  1063. page_len: 5,
  1064. prevent_focus_on_click: true
  1065. }
  1066. );
  1067. }
  1068. // standings
  1069. function showStandings() {
  1070. $("#standings").long_table(
  1071. standings,
  1072. 1,
  1073. '<tr>' +
  1074. '<th style="width:5em">#</th>' +
  1075. '<th style="width:14em">'+uojLocale('username')+'</th>' +
  1076. '<th style="width:5em">'+uojLocale('contests::total score')+'</th>' +
  1077. $.map(problems, function(col, idx) {
  1078. return '<th style="width:8em;">' + '<a href="/contest/' + contest_id + '/problem/' + col + '">' + String.fromCharCode('A'.charCodeAt(0) + idx) + '</a>' + '</th>';
  1079. }).join('') +
  1080. '</tr>',
  1081. function(row) {
  1082. var col_tr = '<tr>';
  1083. col_tr += '<td>' + row[3] + '</td>';
  1084. col_tr += '<td>' + getUserLink(row[2][0], row[2][1]) + '</td>';
  1085. col_tr += '<td>' + '<div><span class="uoj-score" data-max="' + problems.length * 100 + '" style="color:' + getColOfScore(row[0] / problems.length) + '">' + row[0] + '</span></div>' + '<div>' + getPenaltyTimeStr(row[1]) + '</div></td>';
  1086. for (var i = 0; i < problems.length; i++) {
  1087. col_tr += '<td>';
  1088. col = score[row[2][0]][i];
  1089. if (col != undefined) {
  1090. col_tr += '<div><a href="/submission/' + col[2] + '" class="uoj-score" style="color:' + getColOfScore(col[0]) + '">' + col[0] + '</a></div>';
  1091. if (standings_version < 2) {
  1092. col_tr += '<div>' + getPenaltyTimeStr(col[1]) + '</div>';
  1093. } else {
  1094. if (col[0] > 0) {
  1095. col_tr += '<div>' + getPenaltyTimeStr(col[1]) + '</div>';
  1096. }
  1097. }
  1098. }
  1099. col_tr += '</td>';
  1100. }
  1101. col_tr += '</tr>';
  1102. return col_tr;
  1103. }, {
  1104. table_classes: ['table', 'table-bordered', 'table-striped', 'table-text-center', 'table-vertical-middle', 'table-condensed'],
  1105. page_len: 100,
  1106. print_after_table: function() {
  1107. return '<div class="text-right text-muted">' + uojLocale("contests::n participants", standings.length) + '</div>';
  1108. }
  1109. }
  1110. );
  1111. }