PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/RDNPlus/js/rdnrefresh.js

http://joelisester-sandbox.googlecode.com/
JavaScript | 337 lines | 278 code | 46 blank | 13 comment | 62 complexity | a49d33cdcaaad8dca61bcaeb955969cd MD5 | raw file
  1. if(!currentUser) {
  2. try { currentUser = $('#nav_personal a, #nav_profile a').attr('href').replace(siteDir,'').split('/')[1].toLowerCase(); } catch(err) { }
  3. }
  4. var lastDM = readCookie('lastdm');
  5. if(!lastDM) {
  6. lastDM = 'notice-0';
  7. }
  8. $(function(){
  9. reProcess();
  10. customStyle();
  11. // Get number of new DMs and append it to the Personal link
  12. if(location.href.replace(siteDir,'').split('/')[1] == 'inbox') {
  13. lastDM = $('.messages li').filter(':first').attr('id');
  14. eraseCookie('lastdm');
  15. createCookie('lastdm', lastDM, 365);
  16. }
  17. else {
  18. var profile = $('#site_nav_local_views, #nav_profile a').filter(':first');
  19. var oldinbox = profile.find('a[href*="inbox"]');
  20. if(oldinbox.length) {
  21. oldinbox.addClass('dmcounter');
  22. }
  23. else {
  24. profile.append('<li style="float: left;"><a title="Inbox" class="dmcounter" href="' + siteDir + currentUser + '/inbox">Inbox</a></li>');
  25. }
  26. profile.find('ul.nav').css('width','auto');
  27. updateDM();
  28. }
  29. $('.rot13').live('click', function(){
  30. $(this).closest('li').find('p.entry-content').filter(':first').find('.rotd').each(function() {
  31. $(this).text(rot13($(this).text()));
  32. });
  33. });
  34. $('.hideSpoilerT, .hideUserT').live('click', function() {
  35. $(this).removeClass('hideSpoilerT hideUserT');
  36. $(this).children().removeClass('hideSpoiler hideUser');
  37. });
  38. $('.retweet').live('click', function(){
  39. var target = $(this).closest('li');
  40. var theContent = $(target).find('p.entry-content').filter(':first').text().replace(/@[a-zA-Z0-9_]+[^\.]/g, '');
  41. var theAuthor = $(target).find('.vcard.author .url').filter(':first').attr('href').split('/');
  42. theAuthor = theAuthor[theAuthor.length-1];
  43. var theTweet = 'http://twitter.com/intent/tweet?text=';
  44. theTweet += escape('#rdn RD @' + theAuthor + ' ' + theContent);
  45. window.open(theTweet);
  46. });
  47. $('.bbTools li').live('click', function() {
  48. var notice_data = $(this).closest('form').find('#notice_data-text, .notice_data-text')
  49. if(!selectedText) {
  50. selectedText = notice_data.val();
  51. }
  52. var formatElement = $(this).children()[0].nodeName.toLowerCase();
  53. selection = $(document.createElement('div'));
  54. selection.html(selectedText);
  55. selection.wrapInner('<' + formatElement + '>');
  56. notice_data.val(notice_data.val().replace(selectedText, selection.html()));
  57. });
  58. $('#notice_data-text, .notice_data-text').live('mouseup', function() {
  59. selectedText = getSelected();
  60. });
  61. });
  62. function createCookie(name,value,days) {
  63. if (days) {
  64. var date = new Date();
  65. date.setTime(date.getTime()+(days*24*60*60*1000));
  66. var expires = "; expires="+date.toGMTString();
  67. }
  68. else var expires = "";
  69. document.cookie = name+"="+value+expires+"; path=/";
  70. }
  71. function readCookie(name) {
  72. var nameEQ = name + "=";
  73. var ca = document.cookie.split(';');
  74. for(var i=0;i < ca.length;i++) {
  75. var c = ca[i];
  76. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  77. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  78. }
  79. return null;
  80. }
  81. function eraseCookie(name) {
  82. createCookie(name,"",-1);
  83. }
  84. function getSelected(){
  85. var userSelection, ta;
  86. if (window.getSelection && document.activeElement){
  87. if (document.activeElement.nodeName == "TEXTAREA" ||
  88. (document.activeElement.nodeName == "INPUT" &&
  89. document.activeElement.getAttribute("type").toLowerCase() == "text")){
  90. ta = document.activeElement;
  91. userSelection = ta.value.substring(ta.selectionStart, ta.selectionEnd);
  92. } else {
  93. userSelection = window.getSelection();
  94. }
  95. } else {
  96. // all browsers, except IE before version 9
  97. if (document.getSelection){
  98. userSelection = document.getSelection();
  99. }
  100. // IE below version 9
  101. else if (document.selection){
  102. userSelection = document.selection.createRange();
  103. }
  104. }
  105. return userSelection;
  106. }
  107. function updateDM() {
  108. $.ajax({
  109. type: 'GET',
  110. url: siteDir + currentUser + '/inbox',
  111. error: function(response) {},
  112. success: function(response) {
  113. var holder = document.createElement('div');
  114. holder.innerHTML = response;
  115. // Passing the new page to post checker
  116. var lastDMItem = $(holder).find('#' + lastDM);
  117. if(lastDMItem.length) {
  118. var newDM = lastDMItem.prevAll().length;
  119. }
  120. else {
  121. var newDM = $(holder).find('.messages li').length;
  122. }
  123. $('.dmcounter').html('Inbox (' + newDM + ')');
  124. setTimeout(updateDM, 60000);
  125. }});
  126. }
  127. function hideUsers(newPosts) {
  128. // Remove users
  129. if(USERNAMESTAGS.replace(/W+/,'') != '') {
  130. usernamesTags = USERNAMESTAGS.split(' ');
  131. $(newPosts).find(".vcard.author .nickname.fn, .vcard.author .url").each(
  132. function(){
  133. tag = $(this);
  134. $.each(usernamesTags, function() {
  135. if($(tag).text().toLowerCase() == this || ( $(tag).attr('href') && $(tag).attr('href').replace(siteDir,'').split('/')[0].toLowerCase() == this )) {
  136. var target = tag.closest('li');
  137. $.fx.off = true;
  138. target.children().addClass('hideUser');
  139. target.addClass('hideUserT'); //select LI
  140. $.fx.off = false;
  141. }
  142. })
  143. })
  144. }
  145. }
  146. function hideSpoilers(newPosts) {
  147. // Remove spoilers
  148. if(SPOILERTAGS.replace(/W+/,'') != '') {
  149. spoilerTags = SPOILERTAGS.toLowerCase().split(' ');
  150. $(newPosts).find(".tag a").each(
  151. function(){
  152. tag = $(this);
  153. $.each(spoilerTags, function() {
  154. if(this == tag.html().toLowerCase()) {
  155. var target = tag.closest('li');
  156. $.fx.off = true;
  157. target.children().addClass('hideSpoiler'); //select subelements
  158. target.addClass('hideSpoilerT'); //select LI
  159. $.fx.off = false;
  160. }
  161. });
  162. });
  163. }
  164. }
  165. /* Reprocesses the page and/or post */
  166. function reProcess(newPosts) {
  167. setTimeout(reProcess, 50);
  168. if(!newPosts) { var newPosts = $('.hentry.notice').not('.rdnrefresh_done') }
  169. hideSpoilers(newPosts);
  170. hideUsers(newPosts);
  171. highlightUsername(newPosts);
  172. highlightAny(newPosts);
  173. var notice_options = $(newPosts).find('.notice-options');
  174. if(notice_options.length) {
  175. delButton(notice_options);
  176. }
  177. $(newPosts).addClass('rdnrefresh_done');
  178. $(newPosts).find('.hentry.notice').addClass('rdnrefresh_done');
  179. }
  180. function delButton(newPosts) {
  181. $(newPosts).find('.notice_delete').each(function() {
  182. var notice_id = $(this).parent().parent().attr('id').split('-')[1];
  183. var container = document.createElement('div');
  184. var token = $(this).parent().find('.form_favor [name*="token"]').val()
  185. $(container).html(('<form action="' + siteDir + '/notice/delete" method="post" class="notice_delete" id="delete-%%%"> <fieldset> <legend>Delete this notice?</legend> <input type="hidden" value="' + token + '" id="token-%%%" name="token"> <input type="hidden" value="%%%" id="notice-d%%%" name="notice"> <input title="Delete this Notice" value="Yes" class="submit notice_delete" name="yes" id="delete-submit-%%%" /> </fieldset> </form>').replace(/%%%/g,notice_id));
  186. container.addEventListener('click', function(event) {
  187. event.preventDefault();
  188. event.stopPropagation();
  189. var form = $(this).find('form');
  190. var submit_i = form.find('.submit');
  191. var close = function(){
  192. form.find('.close').remove();
  193. form
  194. .removeClass('dialogbox')
  195. .closest('.notice-options')
  196. .removeClass('opaque');
  197. form.find('.submit_dialogbox').remove();
  198. form.find('.submit').show();
  199. return false;
  200. }
  201. var submit = submit_i.clone();
  202. submit
  203. .addClass('submit_dialogbox')
  204. .removeClass('submit');
  205. form.append(submit);
  206. submit.bind('click', function() {
  207. $.ajax({
  208. type: 'POST',
  209. dataType: 'text',
  210. url: form.attr('action'),
  211. data: form.serialize() + '&ajax=1',
  212. beforeSend: function(xhr) {
  213. close();
  214. },
  215. error: function (xhr, textStatus, errorThrown) {
  216. },
  217. success: function(data, textStatus) {
  218. form.closest('li').remove();
  219. }
  220. });
  221. return false;
  222. });
  223. submit_i.hide();
  224. form
  225. .addClass('dialogbox')
  226. .append('<button class="close">&#215;</button>')
  227. .closest('.notice-options')
  228. .addClass('opaque');
  229. form.find('button.close').click(close);
  230. }, false, true);
  231. $(this).replaceWith(container);
  232. });
  233. }
  234. /* Highlights any word the user has typed into the highlight any box. SLOW */
  235. function highlightAny(newPosts) {
  236. if(ANYHIGHLIGHTWORDS.replace(/W+/,'') != ''){
  237. var words = ANYHIGHLIGHTWORDS.split(' ');
  238. var posts = $(newPosts).find('p.entry-content');
  239. $.each(words, function() {
  240. var wordex = new RegExp('(' + this + ')', 'gi');
  241. posts.each(function(){
  242. $(this).html( $(this).html().replace(wordex, '<span class="anyHighlight">$1</span>') );
  243. });
  244. });
  245. }
  246. }
  247. /* Scans the page for the current user's name, then applys a highlight */
  248. function highlightUsername(newPosts) {
  249. var mentionCounter = 0;
  250. $(newPosts).find('.entry-content .vcard .url, .author .addressee').each(function(){
  251. if($(this).text().toLowerCase() == currentUser || $(this).attr('href').replace(siteDir,'').split('/')[0].toLowerCase() == currentUser) {
  252. $(this).addClass('userHighlight');
  253. mentionCounter++
  254. }
  255. });
  256. }
  257. /* Converts a hex string to an array RGB */
  258. function customStyle() {
  259. if(customstyle == '1') {
  260. if(logo) {$('.logo.photo').attr('src', logo)}
  261. }
  262. }
  263. //Get user selected text
  264. function getSelected(){
  265. var userSelection, ta;
  266. if (window.getSelection && document.activeElement){
  267. if (document.activeElement.nodeName == "TEXTAREA" ||
  268. (document.activeElement.nodeName == "INPUT" &&
  269. document.activeElement.getAttribute("type").toLowerCase() == "text")){
  270. ta = document.activeElement;
  271. userSelection = ta.value.substring(ta.selectionStart, ta.selectionEnd);
  272. } else {
  273. userSelection = window.getSelection();
  274. }
  275. } else {
  276. // all browsers, except IE before version 9
  277. if (document.getSelection){
  278. userSelection = document.getSelection();
  279. }
  280. // IE below version 9
  281. else if (document.selection){
  282. userSelection = document.selection.createRange();
  283. }
  284. }
  285. return userSelection;
  286. }
  287. function rot13(text){
  288. return text.replace(/[a-zA-Z]/g, function(c){
  289. return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
  290. });
  291. }