home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / plugins / twitter / res / infobox.js < prev    next >
Encoding:
JavaScript  |  2010-01-15  |  12.8 KB  |  371 lines

  1. var commentFieldPaddingX;
  2. var commentFieldPaddingY;
  3.  
  4. $(function(){
  5.     var t = $("<textarea/>").css({'position':'fixed', 'right':'-500px', 'bottom':'-500px'}).appendTo(document.body);
  6.     commentFieldPaddingX =  t.outerWidth() - t.width();
  7.     commentFieldPaddingY =  t.outerHeight() - t.height();
  8.     t.remove();
  9. })
  10.  
  11. var changeView = function(name) {
  12.     D.notify('infobox_hide');
  13.     D.notify('change_view', name);
  14. }
  15. swapIn(function() { window.changeView = changeView; });
  16. swapOut(function() { delete window.changeView; });
  17.  
  18. var TWITTER_INFOBOX_INPUT = true;
  19.  
  20. var transitionDefault = 200;
  21.  
  22. var fxAttrs = [
  23.     // height animations
  24.     [ "height", "marginTop", "marginBottom", "paddingTop", "paddingBottom" ],
  25.     // width animations
  26.     [ "width", "marginLeft", "marginRight", "paddingLeft", "paddingRight" ],
  27.     // opacity animations
  28.     [ "opacity" ]
  29. ];
  30.  
  31. function genFx( type, num ){
  32.     var obj = {};
  33.     jQuery.each( fxAttrs.concat.apply([], fxAttrs.slice(0,num)), function(){
  34.         obj[ this ] = type;
  35.     });
  36.     return obj;
  37. }
  38.  
  39. var TW_SLIDE_UP = genFx('hide', 1);
  40.  
  41. var tw_show_reply_box = function(self, start_text){
  42.     var me = $(self);
  43.     var txt = me.parents(".tweetContent").find(".input_area");
  44.     var name = tw_get_replyto_name(self);
  45.     var tweet_id = tw_get_tweet_id(self);
  46.  
  47.     var val_set = false;
  48.  
  49. //    var text_adjust = 0;
  50.     var last_val = txt.data('last_val') || '';
  51.     if (start_text === 'RT ') {
  52.         last_val = start_text + '@' + name + ': ' + tw_get_tweet_text(self);
  53. //        text_adjust = last_val.length;
  54.     } else if (!last_val) {
  55.         last_val = start_text + name + ' ';
  56. //        text_adjust = last_val.length;
  57.     } else {
  58.         if (last_val.substring(0,1) === '@') {
  59.             last_val = start_text + last_val.substring(1);
  60. //            text_adjust = start_text.length - '@'.length;
  61.         } else if (last_val.substring(0,2) === 'd ') {
  62.             last_val = start_text + last_val.substring(2);
  63. //            text_adjust = start_text.length - 'd '.length;
  64.         } else {
  65.             last_val = start_text + name + ' ';
  66. //            text_adjust = last_val.length;
  67.         }
  68.     }
  69.     txt.data('last_val', last_val);
  70. //    txt.data('text_adjust', text_adjust);
  71.  
  72.     if (!txt.hasClass("tw_bound")) {
  73.         txt.addClass("tw_bound")
  74.         var haveFocus = false;
  75.  
  76.         var _text_changed = function (self) {
  77.             $(self).parents('.form_wrapper').find('.char_count').text(140-self.value.length);
  78.             if (self.value.length > 140){
  79.                 $(self).addClass('input_area_over_limit');
  80.             } else {
  81.                 $(self).removeClass('input_area_over_limit');
  82.             }
  83.         }
  84.         var text_changed = function () {
  85.             return _text_changed(this);
  86.         }
  87.  
  88.         var blurFunc = function(animate) {
  89.             if (!haveFocus) {
  90.                 return true;
  91.             }
  92.             $(txt).unbind('keydown', 'esc');
  93.             $(txt).unbind('keydown', 'shift+return');
  94.             $(txt).unbind('keydown', 'return');
  95.             $(txt).unbind('keydown', text_changed);
  96.             $(txt).unbind('keyup', text_changed);
  97.             $(txt).unbind('keypress', text_changed);
  98.             $(txt).unbind('change', text_changed);
  99.             txt.data('last_val', this.value);
  100.             this.value = '';
  101.             if (animate != false){
  102.                 txt.parents('.tweetContent').find('.form_wrapper').animate(TW_SLIDE_UP, {queue:false, duration:100});
  103.                 txt.parents('.tweetContent').find('.controls').slideDown(100);
  104.             } else {
  105.                 txt.parents('.tweetContent').find('.form_wrapper').hide();
  106.                 txt.parents('.tweetContent').find('.controls').show();
  107.             }
  108.  
  109.             haveFocus = false;
  110.             return true; //blur should continue, so that we don't keep focus and possibly trigger another blur event.
  111.         }
  112.  
  113.         $(txt.parents('.form_wrapper')[0]).css('padding-right', commentFieldPaddingX);
  114.  
  115.         txt.focus( function() {
  116.             if (haveFocus) {
  117.                 return;
  118.             }
  119.             haveFocus = true;
  120. //            var text_adjust = txt.data('text_adjust');
  121. //            if (!val_set) {this.focus(); val_set=true;}
  122.             this.focus();
  123.             this.value = txt.data('last_val') || '';
  124.             _text_changed(this);
  125.             $(this).jGrow();
  126. //            if (text_adjust){
  127. //                var start = this.selectionStart;
  128. //                var end = this.selectionEnd;
  129. //                this.selectionStart = start + text_adjust;
  130. //                this.selectionEnd = end + text_adjust;
  131. //            }
  132.  
  133.             $(this).bind('keydown', 'esc', function(){ txt.blur(); return false;});
  134.             $(this).bind('keydown', 'shift+return', function(){
  135.                 var start = this.selectionStart;
  136.                 var end   = this.selectionEnd;
  137.                 this.value = this.value.substr(0, start)
  138.                     + '\n'
  139.                     + this.value.substr(end, this.value.length);
  140.                 _text_changed(this);
  141.                 this.selectionStart = start + 1;
  142.                 this.selectionEnd = start + 1;
  143.                 return false;
  144.             });
  145.             $(this).bind('keydown', 'return', function(){
  146.                 if (this.value && !this.disabled) {
  147.                     var opts = {status:this.value};
  148.                     if (this.value.substring(0,1) === '@') {
  149.                         opts['reply_id'] = tweet_id;
  150.                     }
  151.                     D.notify('send_tweet', opts);
  152.                     this.value = '';
  153.                     _text_changed(this);
  154.                     val_set=false;
  155.                     txt.blur();
  156.                 } else if (!this.value) {
  157.                     //hide_this_comment_box();
  158.                 }
  159.                 return false;
  160.             });
  161.  
  162.             $(this).bind('keydown', text_changed);
  163.             $(this).bind('keyup', text_changed);
  164.             $(this).bind('keypress', text_changed);
  165.             $(this).bind('change', text_changed);
  166.             haveFocus = true;
  167.             // When hiding the infobox, blur any active textboxes.
  168.             //TODO: fix/create the necessary functions so that things that don't need to happen don't
  169.             swapOut(function() { blurFunc(false); });
  170.             onHide(function() { blurFunc(false); });
  171.         });
  172.  
  173.         txt.blur(blurFunc);
  174.  
  175.     }
  176.     txt.parents('.tweetContent').find('.controls').animate(TW_SLIDE_UP, {queue:false, duration:100});
  177.     txt.parents('.tweetContent').find('.form_wrapper').slideDown(100, function(){
  178.         txt.focus();
  179.     });
  180. }
  181.  
  182. var tw_get_replyto_name = function(self) {return JSON.parse($(self).parents('.tweetContent').find('.sender_text').text());}
  183.  
  184. var tw_get_tweet_id = function(self) {return $(self).parents('.tweetContent').attr('id');}
  185.  
  186. var tw_get_tweet_text = function(self) {return JSON.parse($(self).parents('.tweetContent').find('.tweet_text_value').text());}
  187.  
  188. var tw_reply_click = function(){ tw_show_reply_box(this, '@'); return true;}
  189.  
  190. var tw_direct_click = function(){ tw_show_reply_box(this, 'd '); return true;}
  191.  
  192. var tw_retweet_click = function(){ tw_show_reply_box(this, 'RT '); return true;}
  193.  
  194. var tw_is_favorited = function(self){ return $(self).parents('.controls').hasClass('favorite_on'); }
  195.  
  196. var tw_trend_click = function() {
  197.     var query = JSON.parse($(this).parents('.trend').find('.trendval').text());
  198.     console.log('trend click: ' + query);
  199.     D.notify('infobox_hide');
  200.     D.notify('change_view', {
  201.         "query": query,
  202.         "save": false,
  203.         "type": "search"
  204.     });
  205.    return false;
  206. }
  207.  
  208. var tw_favorite_click = function() {
  209.     var favorited = tw_is_favorited(this);
  210.     console.log('classes: ' + $(this).parents('.controls').attr('class'));
  211.     console.log('favorited: ' + favorited);
  212.     $(this).parents('.controls').removeClass('favorite_on').removeClass('favorite_off').addClass('favorite_pending');
  213.     var self = this;
  214.     D.rpc('favorite_tweet', {tweet:{id:tw_get_tweet_id(this), favorited:favorited}},
  215.         function(){
  216.             $(self).parents('.controls').removeClass('favorite_pending').toggleClass('favorite_on', !favorited).toggleClass('favorite_off', favorited);
  217.         },
  218.         function(){
  219.             $(self).parents('.controls').removeClass('favorite_pending').toggleClass('favorite_on', favorited).toggleClass('favorite_off', !favorited);
  220.         });
  221.     return false;
  222. }
  223.  
  224. var tw_delete_click = function() {
  225.     $(this).parents('.controls').addClass('delete_pending');
  226.     var self = this;
  227.     D.rpc('delete_tweet', {tweet:{id:tw_get_tweet_id(this)}},
  228.         function(){
  229.             var tweet = $(self).parents('.tweet');
  230.             var hr = tweet.next('hr.post_divider');
  231.             tweet.remove();
  232.             hr.remove();
  233.             adjust_height();
  234.         },
  235.         function(){
  236.             $(self).parents('.controls').removeClass('delete_pending');
  237.         });
  238.     return false;
  239. }
  240.  
  241. var tw_status_update_clicked = function(){
  242.     D.notify('status_update_clicked');
  243. }
  244.  
  245. function tw_near_bottom(){
  246.     var desired = window.digsby_infobox_desired_height;
  247.     var window_inner_height = window.innerHeight;
  248.     if (!desired || desired < window_inner_height) {
  249.         desired = window_inner_height;
  250.     }
  251.     var pageYOffset = window.pageYOffset;
  252.     var offsetHeight = document.body.offsetHeight;
  253.  
  254.     ret = ( pageYOffset >= ( offsetHeight - ( desired * 2.0 ) ) );
  255.     return ret;
  256. }
  257.  
  258. var inited = false;
  259.  
  260. var scrollRequestOutstanding = false;
  261.  
  262. var myName = currentContentName;
  263.  
  264. var didRequestIds = {};
  265.  
  266.  
  267. var tw_on_scroll = function(post_table) {
  268.     if (!inited || myName !== currentContentName) {
  269.         console.log('tw_on_scroll ' + myName + ' shortcircut1: ' + currentContentName);
  270.         return;
  271.     }
  272.  
  273.     if (scrollRequestOutstanding)
  274.         return;
  275.     scrollRequestOutstanding = true;
  276.  
  277.     if (tw_Active && tw_near_bottom()) {
  278.         var rows = $(post_table).find('.tweet');
  279.         var last_post_id = $(rows[rows.length-1]).attr('id');
  280.  
  281.         // don't request past a certain ID more than once.
  282.         /* //doesn't play nice with new SocialFeed injection.
  283.         if (last_post_id in didRequestIds) {
  284.             scrollRequestOutstanding = false;
  285.             return;
  286.         }
  287.         didRequestIds[last_post_id] = true;
  288.         */
  289.  
  290.         D.rpc('next_item', {},
  291.                 function(args) {
  292.                     if (!scrollRequestOutstanding)
  293.                         return;
  294.                     if (!inited || myName !== currentContentName) {
  295.                         scrollRequestOutstanding = false;
  296.                         return;
  297.                     }
  298.                     var dateparent = $('<div></div>');
  299.                     dateparent.append(args.html);
  300.                     html = dateparent.contents().remove();
  301.                     html.remove();
  302.                     dateparent.remove();
  303.                     html.appendTo($(post_table));
  304.                     scrollRequestOutstanding = false;
  305.                     tw_on_scroll(post_table);
  306.                 },
  307.                 function(args) {
  308.                     scrollRequestOutstanding = false;
  309.                 }
  310.             );
  311.     } else {
  312.         scrollRequestOutstanding = false;
  313.     };
  314. }
  315.  
  316. var post_table = null;
  317.  
  318. function tw_do_scroll(){
  319.     tw_on_scroll(post_table);
  320. }
  321.  
  322. var tw_Active = false;
  323.  
  324. swapOut(function(){
  325.     tw_Active = false;
  326. });
  327.  
  328. swapIn(function(){
  329.     tw_Active = true;
  330.     if (!inited){
  331.         inited=true;
  332.     };
  333. });
  334.  
  335.  
  336. swapIn(function() {
  337.     $(".tw_reply_link").live("click", tw_reply_click);
  338.     $(".tw_retweet_link").live("click", tw_retweet_click);
  339.     $(".tw_direct_link").live("click", tw_direct_click);
  340.     $(".tw_delete_link").live("click", tw_delete_click);
  341.     $(".tw_favorite_link").live("click", tw_favorite_click);
  342.     $(".trendlink").live("click", tw_trend_click);
  343.     $(".status_updater").live("click", tw_status_update_clicked);
  344. });
  345.  
  346. swapOut(function() {
  347.     $(".tw_reply_link").die("click", tw_reply_click);
  348.     $(".tw_retweet_link").die("click", tw_retweet_click);
  349.     $(".tw_direct_link").die("click", tw_direct_click);
  350.     $(".tw_delete_link").die("click", tw_delete_click);
  351.     $(".tw_favorite_link").die("click", tw_favorite_click);
  352.     $(".trendlink").die("click", tw_trend_click);
  353.     $(".status_updater").die("click", tw_status_update_clicked);
  354. });
  355.  
  356. D.notify('initialize_feed'); //before binding the scroll events, though those really happen later.
  357.  
  358. swapIn(function(){
  359.     post_table = $(document).find('.tweets')[0];
  360.     setDesiredHeightCallback(tw_do_scroll);
  361.     $(window).bind('scroll',tw_do_scroll);
  362.     tw_do_scroll();
  363.     setTimeout(tw_do_scroll, 500);
  364. });
  365.  
  366. swapOut(function(){
  367.     $(window).unbind('scroll');
  368.     setDesiredHeightCallback(null);
  369. });
  370.  
  371.