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

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