Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Pull tabber out in to semi-stand alone module
- * Big thanks to netnerd01 for his pre-work on this
- *
- * Basic usage - tabbedChannels.init( dom_node_to_add_tabs_to );
- * and hook up tabbedChannels.proccessLine(lower_case_text, jquery_of_line_container); to each line detected by the system
- */
- var tabbedChannels = new function(){
- var _self = this;
- // Default options
- this.channels = ["~","*",".","%","$","#",";","^","<3",":gov","#rpg","@"];
- this.mode = 'single';
- // internals
- this.unread_counts = {};
- this.$el = null;
- this.$opt = null;
- this.defaultRoomClasses = '';
- this.channelMatchingCache = [];
- //channels user is in currently
- this.currentRooms = 0;
- // When channel is clicked, toggle it on or off
- this.toggle_channel = function(e){
- var channel = $(e.target).data("filter");
- if(channel===null)return; // no a channel
- if(!$("#robinChatWindow").hasClass("robin-filter-" + channel)){
- _self.enable_channel(channel);
- $(e.target).addClass("selected");
- // clear unread counter
- $(e.target).find("span").text(0);
- _self.unread_counts[channel] = 0;
- }else{
- _self.disable_channel(channel);
- $(e.target).removeClass("selected");
- }
- // scroll everything correctly
- _scroll_to_bottom();
- };
- // Enable a channel
- this.enable_channel = function(channel_id){
- // if using room type "single", deslect other rooms on change
- if(this.mode == "single"){
- this.disable_all_channels();
- }
- $("#robinChatWindow").addClass("robin-filter robin-filter-" + channel_id);
- $("#robinChatWindow").attr("data-channel-key", this.channels[channel_id]);
- this.currentRooms++;
- // unselect show all
- _self.$el.find("span.all").removeClass("selected");
- };
- // disable a channel
- this.disable_channel = function(channel_id){
- $("#robinChatWindow").removeClass("robin-filter-" + channel_id);
- this.currentRooms--;
- // no rooms selcted, run "show all"
- if(this.currentRooms == 0) this.disable_all_channels();
- };
- // turn all channels off
- this.disable_all_channels = function(e){
- $("#robinChatWindow").attr("class", _self.defaultRoomClasses);
- _self.$el.find(".robin-filters > span").removeClass("selected");
- this.currentRooms = 0;
- _self.$el.find("span.all").addClass("selected");
- _scroll_to_bottom();
- };
- // render tabs
- this.drawTabs = function(){
- html = '';
- for(var i in this.channels){
- if(typeof this.channels[i] === 'undefined') continue;
- html += '<span data-filter="' + i + '" data-filter-name="'+ this.channels[i] +'">' + this.channels[i] + ' (<span>0</span>)</span> ';
- }
- this.$el.find(".robin-filters").html(html);
- };
- // Add new channel
- this.addChannel = function(new_channel){
- if(this.channels.indexOf(new_channel) === -1){
- this.channels.push(new_channel);
- this.unread_counts[this.channels.length-1] = 0;
- this.updateChannelMatchCache();
- this.saveChannelList();
- this.drawTabs();
- // refresh everything after redraw
- this.disable_all_channels();
- }
- };
- // remove existing channel
- this.removeChannel = function(channel){
- if(confirm("are you sure you wish to remove the " + channel + " channel?")){
- var idx = this.channels.indexOf(channel);
- delete this.channels[idx];
- this.updateChannelMatchCache();
- this.saveChannelList();
- this.drawTabs();
- // refresh everything after redraw
- this.disable_all_channels();
- }
- };
- // save channel list
- this.saveChannelList = function(){
- // clean array before save
- var channels = this.channels.filter(function (item) { return item != undefined });
- GM_setValue("robin-enhance-channels", channels);
- };
- // Change chat mode
- this.changeChannelMode = function(e){
- _self.mode = $(this).data("type");
- // swicth bolding
- $(this).parent().find("span").css("font-weight","normal");
- $(this).css("font-weight","bold");
- _self.disable_all_channels();
- // Update mode setting
- GM_setValue("robin-enhance-mode", _self.mode);
- };
- this.updateChannelMatchCache = function(){
- var order = this.channels.slice(0);
- order.sort(function(a, b){
- return b.length - a.length; // ASC -> a - b; DESC -> b - a
- });
- for(var i in order){
- order[i] = this.channels.indexOf(order[i]);
- }
- // sorted array of channel name indexs
- this.channelMatchingCache = order;
- }
- // Procces each chat line to create text
- this.proccessLine = function(text, $element){
- var i, idx, channel;
- for(i=0; i< this.channelMatchingCache.length; i++){
- idx = this.channelMatchingCache[i];
- channel = this.channels[idx];
- if(typeof channel === 'undefined') continue;
- if(text.indexOf(channel) === 0){
- $element.addClass("robin-filter-" + idx +" in-channel");
- this.unread_counts[idx]++;
- return;
- }
- }
- };
- // If in one channel, auto add channel keys
- this.submit_helper = function(){
- if($("#robinChatWindow").hasClass("robin-filter")){
- // auto add channel key
- var channel_key = $("#robinChatWindow").attr("data-channel-key");
- if($(".text-counter-input").val().indexOf("/me") === 0){
- $(".text-counter-input").val("/me " + channel_key + " " + $(".text-counter-input").val().substr(3));
- }else if($(".text-counter-input").val().indexOf("/") !== 0){
- // if its not a "/" command, add channel
- $(".text-counter-input").val(channel_key + " " + $(".text-counter-input").val());
- }
- }
- };
- // Update everuything
- this.tick = function(){
- _self.$el.find(".robin-filters span").each(function(){
- if($(this).hasClass("selected")) return;
- $(this).find("span").text(_self.unread_counts[$(this).data("filter")]);
- });
- };
- // Init tab zone
- this.init = function($el){
- // Load channels
- if(GM_getValue("robin-enhance-channels")){
- this.channels = GM_getValue("robin-enhance-channels");
- }
- if(GM_getValue("robin-enhance-mode")){
- this.mode = GM_getValue("robin-enhance-mode");
- }
- // init counters
- for(var i in this.channels){
- this.unread_counts[i] = 0;
- }
- // update channel cache
- this.updateChannelMatchCache();
- // set up el
- this.$el = $el;
- // Create inital markup
- this.$el.html("<span class='all selected'>Everything</span><span><div class='robin-filters'></div></span><span class='more'>[Options]</span>");
- 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);
- // Attach events
- this.$el.find(".robin-filters").click(this.toggle_channel);
- this.$el.find("span.all").click(this.disable_all_channels);
- this.$el.find("span.more").click(function(){ $(".robin-channel-add").slideToggle(); });
- this.$el.find(".robin-filters").bind("contextmenu", function(e){
- e.preventDefault();
- e.stopPropagation();
- var chan_id = $(e.target).data("filter");
- if(chan_id===null)return; // no a channel
- _self.removeChannel(_self.channels[chan_id]);
- });
- // Form events
- this.$opt.find(".channel-mode span").click(this.changeChannelMode);
- this.$opt.find("button").click(function(){
- var new_chan = _self.$opt.find("input[name='add-channel']").val();
- if(new_chan != '') _self.addChannel(new_chan);
- _self.$opt.find("input[name='add-channel']").val('');
- });
- $("#robinSendMessage").submit(this.submit_helper);
- // store default room class
- this.defaultRoomClasses = $("#robinChatWindow").attr("class");
- // redraw tabs
- this.drawTabs();
- // start ticker
- setInterval(this.tick, 1000);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment