Oppaitime'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 15KB

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