Guest User

Untitled

a guest
Apr 5th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.81 KB | None | 0 0
  1. /**
  2. * Pull tabber out in to semi-stand alone module
  3. * Big thanks to netnerd01 for his pre-work on this
  4. *
  5. * Basic usage - tabbedChannels.init( dom_node_to_add_tabs_to );
  6. * and hook up tabbedChannels.proccessLine(lower_case_text, jquery_of_line_container); to each line detected by the system
  7. */
  8. var tabbedChannels = new function(){
  9. var _self = this;
  10.  
  11. // Default options
  12. this.channels = ["~","*",".","%","$","#",";","^","<3",":gov","#rpg","@"];
  13. this.mode = 'single';
  14.  
  15. // internals
  16. this.unread_counts = {};
  17. this.$el = null;
  18. this.$opt = null;
  19. this.defaultRoomClasses = '';
  20. this.channelMatchingCache = [];
  21.  
  22. //channels user is in currently
  23. this.currentRooms = 0;
  24.  
  25. // When channel is clicked, toggle it on or off
  26. this.toggle_channel = function(e){
  27. var channel = $(e.target).data("filter");
  28. if(channel===null)return; // no a channel
  29.  
  30. if(!$("#robinChatWindow").hasClass("robin-filter-" + channel)){
  31. _self.enable_channel(channel);
  32. $(e.target).addClass("selected");
  33. // clear unread counter
  34. $(e.target).find("span").text(0);
  35. _self.unread_counts[channel] = 0;
  36. }else{
  37. _self.disable_channel(channel);
  38. $(e.target).removeClass("selected");
  39. }
  40.  
  41. // scroll everything correctly
  42. _scroll_to_bottom();
  43. };
  44.  
  45. // Enable a channel
  46. this.enable_channel = function(channel_id){
  47.  
  48. // if using room type "single", deslect other rooms on change
  49. if(this.mode == "single"){
  50. this.disable_all_channels();
  51. }
  52.  
  53. $("#robinChatWindow").addClass("robin-filter robin-filter-" + channel_id);
  54. $("#robinChatWindow").attr("data-channel-key", this.channels[channel_id]);
  55. this.currentRooms++;
  56. // unselect show all
  57. _self.$el.find("span.all").removeClass("selected");
  58. };
  59.  
  60. // disable a channel
  61. this.disable_channel = function(channel_id){
  62. $("#robinChatWindow").removeClass("robin-filter-" + channel_id);
  63. this.currentRooms--;
  64.  
  65. // no rooms selcted, run "show all"
  66. if(this.currentRooms == 0) this.disable_all_channels();
  67. };
  68.  
  69. // turn all channels off
  70. this.disable_all_channels = function(e){
  71. $("#robinChatWindow").attr("class", _self.defaultRoomClasses);
  72. _self.$el.find(".robin-filters > span").removeClass("selected");
  73. this.currentRooms = 0;
  74.  
  75. _self.$el.find("span.all").addClass("selected");
  76. _scroll_to_bottom();
  77. };
  78.  
  79. // render tabs
  80. this.drawTabs = function(){
  81. html = '';
  82. for(var i in this.channels){
  83. if(typeof this.channels[i] === 'undefined') continue;
  84. html += '<span data-filter="' + i + '" data-filter-name="'+ this.channels[i] +'">' + this.channels[i] + ' (<span>0</span>)</span> ';
  85. }
  86. this.$el.find(".robin-filters").html(html);
  87. };
  88.  
  89. // Add new channel
  90. this.addChannel = function(new_channel){
  91. if(this.channels.indexOf(new_channel) === -1){
  92. this.channels.push(new_channel);
  93. this.unread_counts[this.channels.length-1] = 0;
  94. this.updateChannelMatchCache();
  95. this.saveChannelList();
  96. this.drawTabs();
  97.  
  98. // refresh everything after redraw
  99. this.disable_all_channels();
  100. }
  101. };
  102.  
  103. // remove existing channel
  104. this.removeChannel = function(channel){
  105. if(confirm("are you sure you wish to remove the " + channel + " channel?")){
  106. var idx = this.channels.indexOf(channel);
  107. delete this.channels[idx];
  108. this.updateChannelMatchCache();
  109. this.saveChannelList();
  110. this.drawTabs();
  111. // refresh everything after redraw
  112. this.disable_all_channels();
  113. }
  114. };
  115.  
  116.  
  117. // save channel list
  118. this.saveChannelList = function(){
  119. // clean array before save
  120. var channels = this.channels.filter(function (item) { return item != undefined });
  121. GM_setValue("robin-enhance-channels", channels);
  122. };
  123.  
  124. // Change chat mode
  125. this.changeChannelMode = function(e){
  126. _self.mode = $(this).data("type");
  127.  
  128. // swicth bolding
  129. $(this).parent().find("span").css("font-weight","normal");
  130. $(this).css("font-weight","bold");
  131. _self.disable_all_channels();
  132.  
  133. // Update mode setting
  134. GM_setValue("robin-enhance-mode", _self.mode);
  135. };
  136.  
  137. this.updateChannelMatchCache = function(){
  138. var order = this.channels.slice(0);
  139. order.sort(function(a, b){
  140. return b.length - a.length; // ASC -> a - b; DESC -> b - a
  141. });
  142. for(var i in order){
  143. order[i] = this.channels.indexOf(order[i]);
  144. }
  145. // sorted array of channel name indexs
  146.  
  147. this.channelMatchingCache = order;
  148. }
  149.  
  150. // Procces each chat line to create text
  151. this.proccessLine = function(text, $element){
  152. var i, idx, channel;
  153. for(i=0; i< this.channelMatchingCache.length; i++){
  154. idx = this.channelMatchingCache[i];
  155. channel = this.channels[idx];
  156.  
  157. if(typeof channel === 'undefined') continue;
  158.  
  159. if(text.indexOf(channel) === 0){
  160. $element.addClass("robin-filter-" + idx +" in-channel");
  161. this.unread_counts[idx]++;
  162. return;
  163. }
  164. }
  165. };
  166.  
  167. // If in one channel, auto add channel keys
  168. this.submit_helper = function(){
  169. if($("#robinChatWindow").hasClass("robin-filter")){
  170. // auto add channel key
  171. var channel_key = $("#robinChatWindow").attr("data-channel-key");
  172.  
  173. if($(".text-counter-input").val().indexOf("/me") === 0){
  174. $(".text-counter-input").val("/me " + channel_key + " " + $(".text-counter-input").val().substr(3));
  175. }else if($(".text-counter-input").val().indexOf("/") !== 0){
  176. // if its not a "/" command, add channel
  177. $(".text-counter-input").val(channel_key + " " + $(".text-counter-input").val());
  178. }
  179. }
  180. };
  181.  
  182. // Update everuything
  183. this.tick = function(){
  184. _self.$el.find(".robin-filters span").each(function(){
  185. if($(this).hasClass("selected")) return;
  186. $(this).find("span").text(_self.unread_counts[$(this).data("filter")]);
  187. });
  188. };
  189.  
  190. // Init tab zone
  191. this.init = function($el){
  192. // Load channels
  193. if(GM_getValue("robin-enhance-channels")){
  194. this.channels = GM_getValue("robin-enhance-channels");
  195. }
  196. if(GM_getValue("robin-enhance-mode")){
  197. this.mode = GM_getValue("robin-enhance-mode");
  198. }
  199.  
  200. // init counters
  201. for(var i in this.channels){
  202. this.unread_counts[i] = 0;
  203. }
  204.  
  205. // update channel cache
  206. this.updateChannelMatchCache();
  207.  
  208. // set up el
  209. this.$el = $el;
  210.  
  211. // Create inital markup
  212. this.$el.html("<span class='all selected'>Everything</span><span><div class='robin-filters'></div></span><span class='more'>[Options]</span>");
  213. this.$opt = $("<div class='robin-channel-add' style='display:none'><input name='add-channel'><button>Add channel</button> <span class='channel-mode'>Channel Mode: <span title='View one channel at a time' data-type='single'>Single</span> | <span title='View many channels at once' data-type='multi'>Multi</span></span></div>").insertAfter(this.$el);
  214.  
  215. // Attach events
  216. this.$el.find(".robin-filters").click(this.toggle_channel);
  217. this.$el.find("span.all").click(this.disable_all_channels);
  218. this.$el.find("span.more").click(function(){ $(".robin-channel-add").slideToggle(); });
  219. this.$el.find(".robin-filters").bind("contextmenu", function(e){
  220. e.preventDefault();
  221. e.stopPropagation();
  222. var chan_id = $(e.target).data("filter");
  223. if(chan_id===null)return; // no a channel
  224. _self.removeChannel(_self.channels[chan_id]);
  225. });
  226. // Form events
  227. this.$opt.find(".channel-mode span").click(this.changeChannelMode);
  228. this.$opt.find("button").click(function(){
  229. var new_chan = _self.$opt.find("input[name='add-channel']").val();
  230. if(new_chan != '') _self.addChannel(new_chan);
  231. _self.$opt.find("input[name='add-channel']").val('');
  232. });
  233.  
  234.  
  235. $("#robinSendMessage").submit(this.submit_helper);
  236.  
  237. // store default room class
  238. this.defaultRoomClasses = $("#robinChatWindow").attr("class");
  239.  
  240. // redraw tabs
  241. this.drawTabs();
  242.  
  243. // start ticker
  244. setInterval(this.tick, 1000);
  245. }
  246. };
Advertisement
Add Comment
Please, Sign In to add comment