PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/chatbox/history/js/history.js

https://gitlab.com/dali99/shimmie2-Material-Theme
JavaScript | 276 lines | 215 code | 57 blank | 4 comment | 18 complexity | 43f039d26fac946565f70d98f88dd3ad MD5 | raw file
  1. /*jshint bitwise:true, curly:true, devel:true, eqeqeq:true, evil:true, forin:false, noarg:true, noempty:true, nonew:true, undef:true, strict:false, browser:true, jquery:true */
  2. var History = function() {
  3. var self = this;
  4. var args = arguments;
  5. $(function(){
  6. self.init.apply(self, args);
  7. });
  8. };
  9. History.prototype = {
  10. animSpeed: 'normal',
  11. noPosts: '<div id="ys-post-1" class="ys-post ys-first ys-admin-post">\n<span class="ys-post-timestamp">13:37</span>\n<span class="ys-post-nickname">Yurivish:<span>\n<span class="ys-post-message">Hey, there aren\'t any posts in this log.</span>\n</div>',
  12. init: function(options) {
  13. this.prefsInfo = options.prefsInfo;
  14. this.log = options.log;
  15. this.initEvents();
  16. $('body').ScrollToAnchors({ duration: 800 });
  17. },
  18. initEvents: function() {
  19. var self = this;
  20. this.initLogEvents();
  21. // Select log
  22. $('#log').change(function() {
  23. var logIndex = $(this).find('option[@selected]').attr('rel');
  24. var pars = {
  25. p: 'yes',
  26. log: logIndex
  27. };
  28. self.ajax(function(html) {
  29. $('#ys-posts').html(html);
  30. $('#yshout').fadeIn();
  31. self.initLogEvents();
  32. }, pars, true, 'index.php');
  33. });
  34. // Clear the log
  35. $('#clear-log').click(function() {
  36. var el = this;
  37. var pars = {
  38. reqType: 'clearlog'
  39. };
  40. self.ajax(function(json) {
  41. if (json.error) {
  42. switch(json.error) {
  43. case 'admin':
  44. self.error('You\'re not an admin. Log in through the admin CP to clear the log.');
  45. el.innerHTML = 'Clear this log';
  46. return;
  47. }
  48. }
  49. $('#ys-posts').html(self.noPosts);
  50. self.initLogEvents();
  51. el.innerHTML = 'Clear this log';
  52. }, pars);
  53. this.innerHTML = 'Clearing...';
  54. return false;
  55. });
  56. // Clear all logs
  57. $('#clear-logs').click(function() {
  58. var el = this;
  59. var pars = {
  60. reqType: 'clearlogs'
  61. };
  62. self.ajax(function(json) {
  63. if (json.error) {
  64. switch(json.error) {
  65. case 'admin':
  66. el.innerHTML = 'Clear all logs';
  67. self.error('You\'re not an admin. Log in through the admin CP to clear logs.');
  68. return;
  69. }
  70. }
  71. $('#ys-posts').html(self.noPosts);
  72. self.initLogEvents();
  73. el.innerHTML = 'Clear all logs';
  74. }, pars);
  75. this.innerHTML = 'Clearing...';
  76. return false;
  77. });
  78. },
  79. initLogEvents: function() {
  80. var self = this;
  81. $('#yshout .ys-post')
  82. .find('.ys-info-link').toggle(
  83. function() { self.showInfo.apply(self, [$(this).parent().parent()[0].id, this]); return false; },
  84. function() { self.hideInfo.apply(self, [$(this).parent().parent()[0].id, this]); return false; })
  85. .end()
  86. .find('.ys-ban-link').click(
  87. function() { self.ban.apply(self, [$(this).parent().parent()[0]]); return false; })
  88. .end()
  89. .find('.ys-delete-link').click(
  90. function() { self.del.apply(self, [$(this).parent().parent()[0]]); return false; });
  91. },
  92. showInfo: function(id, el) {
  93. var jEl = $('#' + id + ' .ys-post-info');
  94. if (jEl.length === 0) { return false; }
  95. if (this.prefsInfo === 'overlay') {
  96. jEl.css('display', 'block').fadeIn(this.animSpeed);
  97. } else {
  98. jEl.slideDown(this.animSpeed);
  99. }
  100. el.innerHTML ='Close Info';
  101. return false;
  102. },
  103. hideInfo: function(id, el) {
  104. var jEl = $('#' + id + ' .ys-post-info');
  105. if (jEl.length === 0) { return false; }
  106. if (this.prefsInfo === 'overlay') {
  107. jEl.fadeOut(this.animSpeed);
  108. } else {
  109. jEl.slideUp(this.animSpeed);
  110. }
  111. el.innerHTML = 'Info';
  112. return false;
  113. },
  114. ban: function(post) {
  115. var self = this;
  116. var link = $('#' + post.id).find('.ys-ban-link')[0];
  117. switch(link.innerHTML)
  118. {
  119. case 'Ban':
  120. var pIP = $(post).find('.ys-h-ip').html();
  121. var pNickname = $(post).find('.ys-h-nickname').html();
  122. var pars = {
  123. log: self.log,
  124. reqType: 'ban',
  125. ip: pIP,
  126. nickname: pNickname
  127. };
  128. this.ajax(function(json) {
  129. if (json.error) {
  130. switch (json.error) {
  131. case 'admin':
  132. self.error('You\'re not an admin. Log in through the admin CP to ban people.');
  133. break;
  134. }
  135. return;
  136. }
  137. $('#yshout .ys-post[@rel="' + pars.ip + '"]')
  138. .addClass('ys-banned-post')
  139. .find('.ys-ban-link')
  140. .html('Unban');
  141. }, pars);
  142. link.innerHTML = 'Banning...';
  143. return false;
  144. case 'Banning...':
  145. return false;
  146. case 'Unban':
  147. var pIP = $(post).find('.ys-h-ip').html();
  148. var pars = {
  149. reqType: 'unban',
  150. ip: pIP
  151. };
  152. this.ajax(function(json) {
  153. if (json.error) {
  154. switch(json.error) {
  155. case 'admin':
  156. self.error('You\'re not an admin. Log in through the admin CP to unban people.');
  157. return;
  158. }
  159. }
  160. $('#yshout .ys-post[@rel="' + pars.ip + '"]')
  161. .removeClass('ys-banned-post')
  162. .find('.ys-ban-link')
  163. .html('Ban');
  164. }, pars);
  165. link.innerHTML = 'Unbanning...';
  166. return false;
  167. case 'Unbanning...':
  168. return false;
  169. }
  170. },
  171. del: function(post) {
  172. var self = this;
  173. var link = $('#' + post.id).find('.ys-delete-link')[0];
  174. if (link.innerHTML === 'Deleting...') { return; }
  175. var pUID = $(post).find('.ys-h-uid').html();
  176. var pars = {
  177. reqType: 'delete',
  178. uid: pUID
  179. };
  180. self.ajax(function(json) {
  181. if (json.error) {
  182. switch(json.error) {
  183. case 'admin':
  184. self.error('You\'re not an admin. Log in through the admin CP to ban people.');
  185. return;
  186. }
  187. }
  188. $(post).slideUp(self.animSpeed);
  189. }, pars);
  190. link.innerHTML = 'Deleting...';
  191. return false;
  192. },
  193. json: function(parse) {
  194. var json = eval('(' + parse + ')');
  195. return json;
  196. },
  197. ajax: function(callback, pars, html, page) {
  198. pars = jQuery.extend({
  199. reqFor: 'history',
  200. log: this.log
  201. }, pars);
  202. var self = this;
  203. if (page === null) { page = '../yshout.php'; }
  204. $.post(page, pars, function(parse) {
  205. if (parse) {
  206. if (html) {
  207. callback.apply(self, [parse]);
  208. } else {
  209. callback.apply(self, [self.json(parse)]);
  210. }
  211. } else {
  212. callback.apply(self);
  213. }
  214. });
  215. },
  216. error: function(err) {
  217. alert(err);
  218. }
  219. };