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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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).results() || $('#editbox' + postid + '.hidden').results()) {
  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.className == '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. }
  158. function Save_Edit(postid) {
  159. $('#reply_box').gshow();
  160. if (location.href.match(/forums\.php/)) {
  161. ajax.post("forums.php?action=takeedit","form" + postid, function (response) {
  162. $('#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>";
  163. $('#preview' + postid).raw().innerHTML = response;
  164. $('#editbox' + postid).ghide();
  165. $('#pmbox' + postid).ghide();
  166. if ($('#editbox' + postid).raw().previousSibling.className == 'bbcode_bar') $($('#editbox' + postid).raw().previousSibling).ghide();
  167. });
  168. } else {
  169. ajax.post("comments.php?action=take_edit","form" + postid, function (response) {
  170. $('#bar' + postid).raw().innerHTML = "";
  171. $('#preview' + postid).raw().innerHTML = response;
  172. $('#editbox' + postid).ghide();
  173. $('#pmbox' + postid).ghide();
  174. if ($('#editbox' + postid).raw().previousSibling.className == 'bbcode_bar') $($('#editbox' + postid).raw().previousSibling).ghide();
  175. });
  176. }
  177. }
  178. function Delete(post) {
  179. postid = post;
  180. if (confirm('Are you sure you wish to delete this post?') == true) {
  181. if (location.href.match(/forums\.php/)) {
  182. ajax.get("forums.php?action=delete&auth=" + authkey + "&postid=" + postid, function () {
  183. $('#post' + postid).ghide();
  184. });
  185. } else {
  186. ajax.get("comments.php?action=take_delete&auth=" + authkey + "&postid=" + postid, function () {
  187. $('#post' + postid).ghide();
  188. });
  189. }
  190. }
  191. }
  192. function Quick_Preview() {
  193. var quickreplybuttons;
  194. $('#post_preview').raw().value = "Make changes";
  195. $('#post_preview').raw().preview = true;
  196. ajax.post("ajax.php?action=preview", "quickpostform", function(response) {
  197. $('#quickreplypreview').gshow();
  198. $('#contentpreview').raw().innerHTML = response;
  199. $('#quickreplytext').ghide();
  200. });
  201. }
  202. function Quick_Edit() {
  203. var quickreplybuttons;
  204. $('#post_preview').raw().value = "Preview";
  205. $('#post_preview').raw().preview = false;
  206. $('#quickreplypreview').ghide();
  207. $('#quickreplytext').gshow();
  208. }
  209. function Newthread_Preview(mode) {
  210. $('#newthreadpreviewbutton').gtoggle();
  211. $('#newthreadeditbutton').gtoggle();
  212. if (mode) { // Preview
  213. ajax.post("ajax.php?action=preview", "newthreadform", function(response) {
  214. $('#contentpreview').raw().innerHTML = response;
  215. });
  216. $('#newthreadtitle').raw().innerHTML = $('#title').raw().value;
  217. var pollanswers = $('#answer_block').raw();
  218. if (pollanswers && pollanswers.children.length > 4) {
  219. pollanswers = pollanswers.children;
  220. $('#pollquestion').raw().innerHTML = $('#pollquestionfield').raw().value;
  221. for (var i = 0; i < pollanswers.length; i += 2) {
  222. if (!pollanswers[i].value) {
  223. continue;
  224. }
  225. var el = document.createElement('input');
  226. el.id = 'answer_' + (i + 1);
  227. el.type = 'radio';
  228. el.name = 'vote';
  229. $('#pollanswers').raw().appendChild(el);
  230. $('#pollanswers').raw().appendChild(document.createTextNode(' '));
  231. el = document.createElement('label');
  232. el.htmlFor = 'answer_' + (i + 1);
  233. el.innerHTML = pollanswers[i].value;
  234. $('#pollanswers').raw().appendChild(el);
  235. $('#pollanswers').raw().appendChild(document.createElement('br'));
  236. }
  237. if ($('#pollanswers').raw().children.length > 4) {
  238. $('#pollpreview').gshow();
  239. }
  240. }
  241. } else { // Back to editor
  242. $('#pollpreview').ghide();
  243. $('#newthreadtitle').raw().innerHTML = 'New Topic';
  244. var pollanswers = $('#pollanswers').raw();
  245. if (pollanswers) {
  246. var el = document.createElement('div');
  247. el.id = 'pollanswers';
  248. pollanswers.parentNode.replaceChild(el, pollanswers);
  249. }
  250. }
  251. $('#newthreadtext').gtoggle();
  252. $('#newthreadpreview').gtoggle();
  253. $('#subscribediv').gtoggle();
  254. }
  255. function LoadEdit(type, post, depth) {
  256. ajax.get("forums.php?action=ajax_get_edit&postid=" + post + "&depth=" + depth + "&type=" + type, function(response) {
  257. $('#content' + post).raw().innerHTML = response;
  258. }
  259. );
  260. }
  261. function AddPollOption(id) {
  262. var list = $('#poll_options').raw();
  263. var item = document.createElement("li");
  264. var form = document.createElement("form");
  265. form.method = "POST";
  266. var auth = document.createElement("input");
  267. auth.type = "hidden";
  268. auth.name = "auth";
  269. auth.value = authkey;
  270. form.appendChild(auth);
  271. var action = document.createElement("input");
  272. action.type = "hidden";
  273. action.name = "action";
  274. action.value = "add_poll_option";
  275. form.appendChild(action);
  276. var threadid = document.createElement("input");
  277. threadid.type = "hidden";
  278. threadid.name = "threadid";
  279. threadid.value = id;
  280. form.appendChild(threadid);
  281. var input = document.createElement("input");
  282. input.type = "text";
  283. input.name = "new_option";
  284. input.size = "50";
  285. form.appendChild(input);
  286. var submit = document.createElement("input");
  287. submit.type = "submit";
  288. submit.id = "new_submit";
  289. submit.value = "Add";
  290. form.appendChild(submit);
  291. item.appendChild(form);
  292. list.appendChild(item);
  293. }
  294. /**
  295. * HTML5-compatible storage system
  296. * Tries to use 'oninput' event to detect text changes and sessionStorage to save it.
  297. *
  298. * new StoreText('some_textarea_id', 'some_form_id', 'some_topic_id')
  299. * The form is required to remove the stored text once it is submitted.
  300. *
  301. * Topic ID is required to retrieve the right text on the right topic
  302. **/
  303. function StoreText (field, form, topic) {
  304. this.field = document.getElementById(field);
  305. this.form = document.getElementById(form);
  306. this.key = 'auto_save_temp';
  307. this.keyID = 'auto_save_temp_id';
  308. this.topic = +topic;
  309. this.load();
  310. }
  311. StoreText.prototype = {
  312. constructor : StoreText,
  313. load : function () {
  314. if (this.enabled() && this.valid()) {
  315. this.retrieve();
  316. this.autosave();
  317. this.clearForm();
  318. }
  319. },
  320. valid : function () {
  321. return this.field && this.form && !isNaN(this.topic);
  322. },
  323. enabled : function () {
  324. return window.sessionStorage && typeof window.sessionStorage === 'object';
  325. },
  326. retrieve : function () {
  327. var r = sessionStorage.getItem(this.key);
  328. if (this.topic === +sessionStorage.getItem(this.keyID) && r) {
  329. this.field.value = r;
  330. }
  331. },
  332. remove : function () {
  333. sessionStorage.removeItem(this.keyID);
  334. sessionStorage.removeItem(this.key);
  335. },
  336. save : function () {
  337. sessionStorage.setItem(this.keyID, this.topic);
  338. sessionStorage.setItem(this.key, this.field.value);
  339. },
  340. autosave : function () {
  341. $(this.field).on(this.getInputEvent(), $.proxy(this.save, this));
  342. },
  343. getInputEvent : function () {
  344. var e;
  345. if ('oninput' in this.field) {
  346. e = 'input';
  347. } else if (document.body.addEventListener) {
  348. e = 'change keyup paste cut';
  349. } else {
  350. e = 'propertychange';
  351. }
  352. return e;
  353. },
  354. clearForm : function () {
  355. $(this.form).submit($.proxy(this.remove, this));
  356. }
  357. };
  358. $(document).ready(function() {
  359. var fadeSpeed = 0
  360. var avatars = new Array()
  361. $(".double_avatar").each(function() {
  362. if ($(this).data("gazelle-second-avatar")) {
  363. var secondAvatar = $(this).data("gazelle-second-avatar")
  364. var originalAvatar = $(this).attr("src")
  365. if ($.inArray(secondAvatar, avatars) == -1) {
  366. avatars.push(secondAvatar)
  367. image = new Image()
  368. image.src = secondAvatar
  369. }
  370. var that = $(this)
  371. $($(this).raw().parentNode.parentNode).hover(
  372. function() {
  373. that.attr("src", secondAvatar)
  374. },
  375. function() {
  376. that.attr("src", originalAvatar)
  377. }
  378. );
  379. $(this).one("load", function() {
  380. var par = $(this).parents('.avatar')
  381. if (par.height()) {
  382. par.raw().style.height = par.height()+'px'
  383. }
  384. })
  385. if (this.complete) $(this).load()
  386. }
  387. })
  388. })
  389. document.querySelectorAll('[data-quote-jump]').forEach(function(el) {
  390. el.addEventListener('click', function(event) {
  391. QuoteJump(event, el.attributes['data-quote-jump'].value)
  392. })
  393. })
  394. if ($('[data-autosave-text]').raw()) {
  395. var el = $('[data-autosave-text]').raw()
  396. var storedTempTextarea = new StoreText(el.attributes['data-autosave-text'].value, el.id, $('[data-autosave-id]').raw().attributes['value'].value);
  397. }