BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

comments.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. var username;
  2. var postid;
  3. var url = { path: window.location.pathname.split('/').reverse()[0].split('.')[0] };
  4. /**
  5. * QuoteJump()
  6. */
  7. function QuoteJump(event, post) {
  8. var button = event.button;
  9. var url, pattern;
  10. if (isNaN(post.charAt(0))) {
  11. switch (post.charAt(0)) {
  12. case 'a': // artist comment
  13. url = 'artist';
  14. break;
  15. case 't': // torrent comment
  16. url = 'torrents';
  17. break;
  18. case 'c': // collage comment
  19. url = 'collages';
  20. break;
  21. case 'r': // request comment
  22. url = 'requests';
  23. break;
  24. default:
  25. return;
  26. }
  27. pattern = new RegExp(url + '\.php');
  28. post = post.substr(1);
  29. url = 'comments.php?action=jump&postid=' + post;
  30. } else {
  31. // forum post
  32. url = 'forums.php?action=viewthread&postid=' + post;
  33. pattern = /forums\.php/;
  34. }
  35. var hash = "#post" + post;
  36. if (button == 0) {
  37. if ($(hash).raw() != null && location.href.match(pattern)) {
  38. window.location.hash = hash;
  39. } else {
  40. window.open(url, '_self');
  41. }
  42. } else if (button == 1) {
  43. window.open(url, '_window');
  44. }
  45. }
  46. /**
  47. * Quote()
  48. */
  49. var original_post;
  50. function Quote(post, user, link = false) {
  51. username = user;
  52. postid = post;
  53. // check if reply_box element exists and that user is in the forums
  54. if (!$('#reply_box').length && url.path == "forums") {
  55. if ($("#quote_" + postid).text() == "Quote") {
  56. original_post = $("#content" + postid).html();
  57. $("#quote_" + postid).text("Unquote");
  58. $.ajax({
  59. type: "POST",
  60. url: "api.php?action=raw_bbcode",
  61. dataType: "json",
  62. data: {
  63. "postid": postid
  64. }
  65. }).done(function (response) {
  66. $("#content" + postid).html(response['response']['body']);
  67. var range = document.createRange();
  68. range.selectNodeContents($("#content" + postid).get(0));
  69. var sel = window.getSelection();
  70. sel.removeAllRanges();
  71. sel.addRange(range);
  72. });
  73. } else {
  74. document.getSelection().removeAllRanges();
  75. $("#content" + postid).html(original_post);
  76. $("#quote_" + postid).text("Quote");
  77. }
  78. } else {
  79. var target = '';
  80. var requrl = '';
  81. if (url.path == "inbox") {
  82. requrl = 'inbox.php?action=get_post&post=' + post;
  83. } else {
  84. requrl = 'comments.php?action=get&postid=' + post;
  85. }
  86. if (link == true) {
  87. if (url.path == "artist") {
  88. // artist comment
  89. target = 'a';
  90. } else if (url.path == "torrents") {
  91. // torrent comment
  92. target = 't';
  93. } else if (url.path == "collages") {
  94. // collage comment
  95. target = 'c';
  96. } else if (url.path == "requests") {
  97. // request comment
  98. target = 'r';
  99. } else {
  100. // forum post
  101. requrl = 'forums.php?action=get_post&post=' + post;
  102. }
  103. target += post;
  104. }
  105. ajax.get(requrl, function (response) {
  106. let postarea = ($('textarea').raw().name == 'body') ? $('textarea').raw() : $('#quickpost').raw();
  107. if (postarea.value !== '') {
  108. postarea.value += "\n\n";
  109. }
  110. // Markdown quoting partial fix
  111. // Only works with the plain editor
  112. postarea.value += "> " + "[" + username + " wrote]" + "(#post" + postid + "):\n> ";
  113. var string = html_entity_decode(response);
  114. var replace = string.replace(/(?:\r\n|\r|\n)/g, "\n> ");
  115. postarea.value += replace;
  116. console.log(postarea.value);
  117. // Original BBcode quote string
  118. //postarea.value += "[quote=" + username + (link == true ? "|" + target : "") + "]" + html_entity_decode(response) + "[/quote]";
  119. resize('quickpost');
  120. });
  121. }
  122. }
  123. /**
  124. * Edit_Form()
  125. */
  126. function Edit_Form(post, key) {
  127. postid = post;
  128. var boxWidth, postuserid, pmbox, inputname;
  129. // If no edit is already going underway or a previous edit was finished, make the necessary dom changes.
  130. if (!$('#editbox' + postid).length || $('#editbox' + postid + '.hidden').length) {
  131. $('#reply_box').ghide();
  132. boxWidth = (location.href.match(/torrents\.php/) || location.href.match(/artist\.php/)) ? "50" : "80";
  133. postuserid = $('#post' + postid + ' strong a').attr('href').split('=')[1];
  134. pmbox = (postuserid != userid) ? '<span id="pmbox' + postid + '"><label>PM user on edit? <input type="checkbox" name="pm" value="1" /></label></span>' : '';
  135. inputname = (location.href.match(/forums\.php/)) ? "post" : "postid";
  136. $('#bar' + postid).raw().cancel = $('#content' + postid).raw().innerHTML;
  137. $('#bar' + postid).raw().oldbar = $('#bar' + postid).raw().innerHTML;
  138. $('#content' + postid).raw().innerHTML = "<div id=\"preview" + postid + "\"></div><form id=\"form" + postid + "\" method=\"post\" action=\"\">" + pmbox + "<input type=\"hidden\" name=\"auth\" value=\"" + authkey + "\" /><input type=\"hidden\" name=\"key\" value=\"" + key + "\" /><input type=\"hidden\" name=\"" + inputname + "\" value=\"" + postid + "\" /><textarea id=\"editbox" + postid + "\" onkeyup=\"resize('editbox" + postid + "');\" name=\"body\" cols=\"" + boxWidth + "\" rows=\"10\"></textarea></form>";
  139. $('#bar' + postid).raw().innerHTML = '<input type="button" value="Preview" onclick="Preview_Edit(' + postid + ');" /><input type="button" value="Post" onclick="Save_Edit(' + postid + ')" /><input type="button" value="Cancel" onclick="Cancel_Edit(' + postid + ');" />';
  140. }
  141. /**
  142. * If it's the initial edit, fetch the post content to be edited.
  143. * If editing is already underway and edit is pressed again, reset the post
  144. * (keeps current functionality, move into brackets to stop from happening).
  145. */
  146. var post_endpoint = (location.href.match(/forums\.php/)) ? '?action=get_post&post=' : 'comments.php?action=get&postid=';
  147. ajax.get(post_endpoint + postid, function (response) {
  148. $('#editbox' + postid).raw().value = html_entity_decode(response);
  149. resize('editbox' + postid);
  150. //BBEditor($('#editbox' + postid).raw());
  151. var easyMDE = new easyMDE({
  152. element: $('#editbox' + postid).raw()
  153. });
  154. });
  155. }
  156. /**
  157. * Cancel_Edit()
  158. */
  159. function Cancel_Edit(postid) {
  160. var answer = confirm("Are you sure you want to cancel?");
  161. if (answer) {
  162. $('#reply_box').gshow();
  163. $('#bar' + postid).raw().innerHTML = $('#bar' + postid).raw().oldbar;
  164. $('#content' + postid).raw().innerHTML = $('#bar' + postid).raw().cancel;
  165. }
  166. }
  167. /**
  168. * Preview_Edit()
  169. */
  170. function Preview_Edit(postid) {
  171. $('#bar' + postid).raw().innerHTML = "<input type=\"button\" value=\"Editor\" onclick=\"Cancel_Preview(" + postid + ");\" /><input type=\"button\" value=\"Post\" onclick=\"Save_Edit(" + postid + ")\" /><input type=\"button\" value=\"Cancel\" onclick=\"Cancel_Edit(" + postid + ");\" />";
  172. ajax.post("api.php?action=preview", "form" + postid, function (response) {
  173. $('#preview' + postid).raw().innerHTML = response;
  174. $('#editbox' + postid).ghide();
  175. if ($('#editbox' + postid).raw().previousSibling.classList.contains('bbcode_bar')) $($('#editbox' + postid).raw().previousSibling).ghide();
  176. });
  177. }
  178. /**
  179. * Cancel_Preview()
  180. */
  181. function Cancel_Preview(postid) {
  182. $('#bar' + postid).raw().innerHTML = "<input type=\"button\" value=\"Preview\" onclick=\"Preview_Edit(" + postid + ");\" /><input type=\"button\" value=\"Post\" onclick=\"Save_Edit(" + postid + ")\" /><input type=\"button\" value=\"Cancel\" onclick=\"Cancel_Edit(" + postid + ");\" />";
  183. $('#preview' + postid).raw().innerHTML = "";
  184. $('#editbox' + postid).gshow();
  185. if ($('#editbox' + postid).raw().previousSibling.classList.contains('bbcode_bar')) $($('#editbox' + postid).raw().previousSibling).gshow();
  186. }
  187. /**
  188. * Save_Edit()
  189. */
  190. function Save_Edit(postid) {
  191. $('#reply_box').gshow();
  192. if (location.href.match(/forums\.php/)) {
  193. ajax.post("forums.php?action=takeedit", "form" + postid, function (response) {
  194. $('#bar' + postid).raw().innerHTML = "<a href=\"reports.php?action=report&amp;type=post&amp;id=" + postid + "\" class=\"brackets\">Report</a>&nbsp;<a href=\"#\">&uarr;</a>";
  195. $('#preview' + postid).raw().innerHTML = response;
  196. $('#editbox' + postid).ghide();
  197. $('#pmbox' + postid).ghide();
  198. if ($('#editbox' + postid).raw().previousSibling.className == 'bbcode_bar') $($('#editbox' + postid).raw().previousSibling).ghide();
  199. });
  200. } else {
  201. ajax.post("comments.php?action=take_edit", "form" + postid, function (response) {
  202. $('#bar' + postid).raw().innerHTML = "";
  203. $('#preview' + postid).raw().innerHTML = response;
  204. $('#editbox' + postid).ghide();
  205. $('#pmbox' + postid).ghide();
  206. if ($('#editbox' + postid).raw().previousSibling.className == 'bbcode_bar') $($('#editbox' + postid).raw().previousSibling).ghide();
  207. });
  208. }
  209. }
  210. /**
  211. * Delete()
  212. */
  213. function Delete(post) {
  214. postid = post;
  215. if (confirm('Are you sure you wish to delete this post?') == true) {
  216. if (location.href.match(/forums\.php/)) {
  217. ajax.get("forums.php?action=delete&auth=" + authkey + "&postid=" + postid, function () {
  218. $('#post' + postid).ghide();
  219. });
  220. } else {
  221. ajax.get("comments.php?action=take_delete&auth=" + authkey + "&postid=" + postid, function () {
  222. $('#post' + postid).ghide();
  223. });
  224. }
  225. }
  226. }
  227. /**
  228. * Quick_Preview()
  229. */
  230. function Quick_Preview() {
  231. var quickreplybuttons;
  232. $('#post_preview').raw().value = "Make changes";
  233. $('#post_preview').raw().preview = true;
  234. ajax.post("api.php?action=preview", "quickpostform", function (response) {
  235. $('#quickreplypreview').gshow();
  236. $('#contentpreview').raw().innerHTML = response;
  237. $('#quickreplytext').ghide();
  238. });
  239. }
  240. /**
  241. * Quick_Edit()
  242. */
  243. function Quick_Edit() {
  244. var quickreplybuttons;
  245. $('#post_preview').raw().value = "Preview";
  246. $('#post_preview').raw().preview = false;
  247. $('#quickreplypreview').ghide();
  248. $('#quickreplytext').gshow();
  249. }
  250. /**
  251. * Newthread_Preview()
  252. */
  253. function Newthread_Preview(mode) {
  254. $('#newthreadpreviewbutton').gtoggle();
  255. $('#newthreadeditbutton').gtoggle();
  256. if (mode) { // Preview
  257. ajax.post("api.php?action=preview", "newthreadform", function (response) {
  258. $('#contentpreview').raw().innerHTML = response;
  259. });
  260. $('#newthreadtitle').raw().innerHTML = $('#title').raw().value;
  261. var pollanswers = $('#answer_block').raw();
  262. if (pollanswers && pollanswers.children.length > 4) {
  263. pollanswers = pollanswers.children;
  264. $('#pollquestion').raw().innerHTML = $('#pollquestionfield').raw().value;
  265. for (var i = 0; i < pollanswers.length; i += 2) {
  266. if (!pollanswers[i].value) {
  267. continue;
  268. }
  269. var el = document.createElement('input');
  270. el.id = 'answer_' + (i + 1);
  271. el.type = 'radio';
  272. el.name = 'vote';
  273. $('#pollanswers').raw().appendChild(el);
  274. $('#pollanswers').raw().appendChild(document.createTextNode(' '));
  275. el = document.createElement('label');
  276. el.htmlFor = 'answer_' + (i + 1);
  277. el.innerHTML = pollanswers[i].value;
  278. $('#pollanswers').raw().appendChild(el);
  279. $('#pollanswers').raw().appendChild(document.createElement('br'));
  280. }
  281. if ($('#pollanswers').raw().children.length > 4) {
  282. $('#pollpreview').gshow();
  283. }
  284. }
  285. } else { // Back to editor
  286. $('#pollpreview').ghide();
  287. $('#newthreadtitle').raw().innerHTML = 'New Topic';
  288. var pollanswers = $('#pollanswers').raw();
  289. if (pollanswers) {
  290. var el = document.createElement('div');
  291. el.id = 'pollanswers';
  292. pollanswers.parentNode.replaceChild(el, pollanswers);
  293. }
  294. }
  295. $('#newthreadtext').gtoggle();
  296. $('#newthreadpreview').gtoggle();
  297. $('#subscribediv').gtoggle();
  298. }
  299. /**
  300. * LoadEdit()
  301. */
  302. function LoadEdit(type, post, depth) {
  303. ajax.get("forums.php?action=ajax_get_edit&postid=" + post + "&depth=" + depth + "&type=" + type, function (response) {
  304. $('#content' + post).raw().innerHTML = response;
  305. }
  306. );
  307. }
  308. /**
  309. * AddPollOption()
  310. */
  311. function AddPollOption(id) {
  312. var list = $('#poll_options').raw();
  313. var item = document.createElement("li");
  314. var form = document.createElement("form");
  315. form.method = "POST";
  316. var auth = document.createElement("input");
  317. auth.type = "hidden";
  318. auth.name = "auth";
  319. auth.value = authkey;
  320. form.appendChild(auth);
  321. var action = document.createElement("input");
  322. action.type = "hidden";
  323. action.name = "action";
  324. action.value = "add_poll_option";
  325. form.appendChild(action);
  326. var threadid = document.createElement("input");
  327. threadid.type = "hidden";
  328. threadid.name = "threadid";
  329. threadid.value = id;
  330. form.appendChild(threadid);
  331. var input = document.createElement("input");
  332. input.type = "text";
  333. input.name = "new_option";
  334. input.size = "50";
  335. form.appendChild(input);
  336. var submit = document.createElement("input");
  337. submit.type = "submit";
  338. submit.id = "new_submit";
  339. submit.value = "Add";
  340. form.appendChild(submit);
  341. item.appendChild(form);
  342. list.appendChild(item);
  343. }
  344. /**
  345. * HTML5-compatible storage system
  346. * Tries to use 'oninput' event to detect text changes and sessionStorage to save it.
  347. *
  348. * new StoreText('some_textarea_id', 'some_form_id', 'some_topic_id')
  349. * The form is required to remove the stored text once it is submitted.
  350. *
  351. * Topic ID is required to retrieve the right text on the right topic
  352. */
  353. function StoreText(field, form, topic) {
  354. this.field = document.getElementById(field);
  355. this.form = document.getElementById(form);
  356. this.key = 'auto_save_temp';
  357. this.keyID = 'auto_save_temp_id';
  358. this.topic = +topic;
  359. this.load();
  360. }
  361. StoreText.prototype = {
  362. constructor: StoreText,
  363. load: function () {
  364. if (this.enabled() && this.valid()) {
  365. this.retrieve();
  366. this.autosave();
  367. this.clearForm();
  368. }
  369. },
  370. valid: function () {
  371. return this.field && this.form && !isNaN(this.topic);
  372. },
  373. enabled: function () {
  374. return window.sessionStorage && typeof window.sessionStorage === 'object';
  375. },
  376. retrieve: function () {
  377. var r = sessionStorage.getItem(this.key);
  378. if (this.topic === +sessionStorage.getItem(this.keyID) && r) {
  379. this.field.value = r;
  380. }
  381. },
  382. remove: function () {
  383. sessionStorage.removeItem(this.keyID);
  384. sessionStorage.removeItem(this.key);
  385. },
  386. save: function () {
  387. sessionStorage.setItem(this.keyID, this.topic);
  388. sessionStorage.setItem(this.key, this.field.value);
  389. },
  390. autosave: function () {
  391. $(this.field).on(this.getInputEvent(), $.proxy(this.save, this));
  392. },
  393. getInputEvent: function () {
  394. var e;
  395. if ('oninput' in this.field) {
  396. e = 'input';
  397. } else if (document.body.addEventListener) {
  398. e = 'change keyup paste cut';
  399. } else {
  400. e = 'propertychange';
  401. }
  402. return e;
  403. },
  404. clearForm: function () {
  405. $(this.form).submit($.proxy(this.remove, this));
  406. }
  407. };
  408. /**
  409. * Do the needful :^)
  410. */
  411. $(document).ready(function () {
  412. var fadeSpeed = 0
  413. var avatars = []
  414. $(".double_avatar").each(function () {
  415. if ($(this).data("gazelle-second-avatar")) {
  416. var secondAvatar = $(this).data("gazelle-second-avatar")
  417. var originalAvatar = $(this).attr("src")
  418. if (!avatars.includes(secondAvatar)) {
  419. avatars.push(secondAvatar)
  420. image = new Image()
  421. image.src = secondAvatar
  422. }
  423. var that = $(this)
  424. $($(this).raw().parentNode.parentNode).hover(
  425. function () {
  426. that.attr("src", secondAvatar)
  427. },
  428. function () {
  429. that.attr("src", originalAvatar)
  430. }
  431. );
  432. $(this).one("load", function () {
  433. var par = $(this).parents('.avatar')
  434. if (par.height()) {
  435. par.raw().style.height = par.height() + 'px'
  436. }
  437. })
  438. if (this.complete) $(this).load()
  439. }
  440. })
  441. })
  442. document.querySelectorAll('[data-quote-jump]').forEach(function (el) {
  443. el.addEventListener('click', function (event) {
  444. QuoteJump(event, el.attributes['data-quote-jump'].value)
  445. })
  446. })
  447. if ($('[data-autosave-text]').raw()) {
  448. var el = $('[data-autosave-text]').raw()
  449. var storedTempTextarea = new StoreText(el.attributes['data-autosave-text'].value, el.id, $('[data-autosave-id]').raw().attributes['value'].value);
  450. }