home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / plugins / linkedin / res / infobox.js < prev    next >
Encoding:
JavaScript  |  2010-07-09  |  14.2 KB  |  459 lines

  1. var transitionDefault = 200;
  2. $(add_link_class_to_a_tags);
  3. /*
  4. $(clear_previous_time_containers);
  5. $(function () {add_time_containers("li.news-item");});
  6. */
  7. $(clear_previous_date_separators);
  8. $(function () {add_date_separators(".news-item[timestamp]");});
  9.  
  10. var fxAttrs = [
  11.     // height animations
  12.     [ "height", "marginTop", "marginBottom", "paddingTop", "paddingBottom" ],
  13.     // width animations
  14.     [ "width", "marginLeft", "marginRight", "paddingLeft", "paddingRight" ],
  15.     // opacity animations
  16.     [ "opacity" ]
  17. ];
  18.  
  19. function li_clear_error(callback) {
  20.     error = $('.news-item-content').find('.error_section');
  21.     var do_clear = function () {
  22.         error.html('');
  23.         if (callback) { callback(); };
  24.     }
  25.  
  26.     if (error.css('display') === 'block') {
  27.         error.slideUp(transitionDefault / 2);
  28.     } else if (!error.css('display')) {
  29.         error.css('display', 'none');
  30.     } else {
  31.  
  32.     }
  33.  
  34.     do_clear();
  35. }
  36.  
  37. function genFx( type, num ){
  38.     var obj = {};
  39.     jQuery.each( fxAttrs.concat.apply([], fxAttrs.slice(0,num)), function(){
  40.         obj[ this ] = type;
  41.     });
  42.     return obj;
  43. }
  44.  
  45. var LI_SLIDE_UP = genFx('hide', 1);
  46. var li_commentFieldPadding;
  47.  
  48. $(function(){
  49.     var t = $("<textarea/>").css({'position':'fixed', 'right':'-500px', 'bottom':'-500px'}).appendTo(document.body);
  50.     li_commentFieldPadding =  t.outerWidth() - t.width();
  51.     t.remove();
  52. })
  53.  
  54. function li_hide_this_comment_box(self, animate) {
  55.     if (animate === undefined) {
  56.         animate = true;
  57.     }
  58.  
  59.     var me = $(self);
  60.     var nic = me.parents(".news-item-content");
  61.  
  62.     var comment_box_disabled = $(nic[0]).find('[name="comment_input"]')[0].disabled
  63.  
  64.     if (comment_box_disabled) {
  65.         return false;
  66.     }
  67.  
  68.     nic.find('.comments_section').css('-webkit-user-select', 'none');
  69.     var end = $(nic[0]).find('.comments_end');
  70.     var comment_button_section = nic.find(".comment_button_section");
  71.     var likes_comment_section = nic.find(".comments_section");
  72.  
  73.     var time = animate ? transitionDefault : 0;
  74.  
  75.     if (time) {
  76.         end.animate(LI_SLIDE_UP, {queue:false, duration:time});
  77.         offset_top = nic.offset().top;
  78.  
  79.         var pageYOffset = window.pageYOffset;
  80.         var offsetHeight = document.body.offsetHeight;
  81.         if (offset_top < pageYOffset) {
  82.             $('html, body').animate({scrollTop: offset_top},
  83.                                     {queue : false, duration : time});
  84.         }
  85.  
  86.         likes_comment_section.slideUp(time, function() {
  87.             likes_comment_section.css('-webkit-user-select', 'text');
  88.         });
  89.         comment_button_section.show();
  90.     } else {
  91.         end.hide();
  92.         comment_button_section.show();
  93.         likes_comment_section.hide();
  94.         likes_comment_section.css('-webkit-user-select', 'text');
  95.     }
  96.  
  97.     return false;
  98. }
  99.  
  100. function li_show_comment_box(self) {
  101.     var me = $(self);
  102.     li_clear_error();
  103.     var comment_link = me.parents(".news-item-content").find(".comment_button_section");
  104.     me.parents(".news-item-content").find(".comments_section").css("-webkit-user-select", "none");
  105.  
  106.     var end = me.parents(".news-item-content").find(".comments_end");
  107.     var txt;
  108.  
  109.     if (end.find(".form_wrapper").length == 0) {
  110.         end.html('<div class="form_wrapper"><textarea name="comment_input" rows="1" class="input_area expanding minor_border" /></div>');
  111.         txt = end.find("textarea");
  112.  
  113.         var haveFocus = false;
  114.         var blurFunc = function(animate) {
  115.             if (!haveFocus) {
  116.                 return true;
  117.             }
  118.  
  119.             $(txt).unbind('keydown', 'esc');
  120.             $(txt).unbind('keydown', 'shift+return');
  121.             $(txt).unbind('keydown', 'return');
  122.  
  123.             li_hide_this_comment_box(txt, animate);
  124.             li_clear_error();
  125.  
  126.             haveFocus = false;
  127.             return true;
  128.         }
  129.  
  130.         $(txt.parents('.form_wrapper')[0]).css('padding-right', li_commentFieldPadding);
  131.  
  132.         txt.focus( function () {
  133.             if (haveFocus) {
  134.                 return;
  135.             }
  136.  
  137.             haveFocus = true;
  138.  
  139.             $(this).bind('keydown', 'esc', function() {txt.blur();});
  140.             $(this).bind('keydown', 'shift+return', function (){
  141.                 var start = this.selectionStart;
  142.                 var end   = this.selectionEnd;
  143.                 this.value = this.value.substr(0, start) + '\n' + this.value.substr(end, this.value.length);
  144.                 this.selectionStart = start + 1;
  145.                 this.selectionEnd = start + 1;
  146.                 return false;
  147.             });
  148.             $(this).bind("keydown", 'return', function() {
  149.                 if (this.value && !this.disabled) {
  150.                     li_post_comment(this);
  151.                 }
  152.                 return false;
  153.             });
  154.  
  155.             swapOut(function() {blurFunc(false);});
  156.             onHide(function() {blurFunc(false);});
  157.         });
  158.         txt.blur(blurFunc);
  159.     }
  160.  
  161.     txt = end.find('textarea');
  162.     txt.parents('.news-item-content').find('.comments_section').slideDown(transitionDefault/2,
  163.     function () {
  164.         comment_link.hide();
  165.         end.slideDown(transitionDefault/2, function () {
  166.             txt[0].value = '';
  167.             txt[0].disabled = false;
  168.             txt.jGrow();
  169.             txt.focus();
  170.             me.parents(".news-item-content").find('.comments_section').css('-webkit-user-select', 'text');
  171.         });
  172.     });
  173.  
  174.     txt.focus();
  175.  
  176. }
  177.  
  178.  
  179. function li_error_text(self, text, error_obj, callback){
  180.     error = $(self).parents(".news-item").find('.error_section');
  181.     if (error_obj !== undefined && error_obj !== null) {
  182.         if (error_obj.error_msg) {
  183.             text += " "  + error_obj.error_msg;
  184.         }
  185.     }
  186.     console.log("text =  " + text);
  187.     error.html(text);
  188.     if (error.css('display') === 'none'){
  189.         error.slideDown(transitionDefault, callback);
  190.         return; // don't call callback, slideDown will do it.
  191.     } else if (!error.css('display')) {
  192.         error.css('display', 'block');
  193.     }
  194.  
  195.     if (callback){
  196.         callback();
  197.     };
  198. }
  199.  
  200. function li_comments_updater(self, slide, clear_error) {
  201.     var _inner = function(args) {
  202.         console.log("CALLBACK");
  203.         var comments_html = $($(args.comments_html)[0]);
  204.         var comment_link_html = $($(args.comment_link_html)[0]);
  205.  
  206.         comments = $(comments_html[0]).find(".comment");
  207.  
  208.         if ((comments.length) && (slide == "new")) {
  209.             new_comment = $(comments[comments.length - 1]);
  210.             new_comment.hide();
  211.         } else {
  212.             new_comment = null;
  213.         }
  214.  
  215.         comment_link_html.hide();
  216.         console.log("got new comment html, initing");
  217.         li_init(comments_html);
  218.         console.log("inited comment html");
  219.         $(self).parents('.news-item-content').find('.comments').replaceWith(comments_html);
  220.  
  221.         $($(self).parents(".news-item-content").find('.comment_button_section')[0]).replaceWith(comment_link_html);
  222.  
  223.         comment_box = $($(self).parents('.form_wrapper')[0]).find("textarea[name='comment_input']");
  224.  
  225.         if (slide == "new") {
  226.             slide_element = new_comment;
  227.         } else if (slide == "all") {
  228.             slide_element = $(self).parents(".comments_section");
  229.         }
  230.  
  231.         console.log("got slide element");
  232.         if (clear_error) {
  233.             console.log("clear error");
  234.             li_clear_error(function() {
  235.                 var after = function() {
  236.                     $($(self).parents('.form_wrapper')[0]).find('.loader_img').remove();
  237.                     comment_box[0].value = '';
  238.                     comment_box[0].disabled = false;
  239.                     comment_box.jGrow();
  240.                 };
  241.                 if (slide_element != null){
  242.                     slide_element.slideDown(transitionDefault, after);
  243.                 } else {
  244.                     after();
  245.                 }
  246.             });
  247.         }
  248.  
  249.         comment_box.focus();
  250.         console.log("done with new html");
  251.     }
  252.  
  253.     return _inner;
  254. }
  255.  
  256. var li_post_comment = function (self) {
  257.     self.disabled = true;
  258.  
  259.     var comment_box = $(self);
  260.     var comment = comment_box.attr('value');
  261.     var post_id = $(comment_box.parents(".news-item")[0]).attr("id");
  262.  
  263.     $(comment_box.parents(".form_wrapper")[0]).append(loader_img);
  264.     D.rpc('post_comment', {'comment' : comment, 'post_id' : post_id},
  265.           li_comments_updater(self, 'new', true),
  266.           function (error_obj) {
  267.               console.log("error posting comment");
  268.               li_error_text(comment_box, 'Error posting comment.', error_obj, function(){
  269.                   $(comment_box.parents(".form_wrapper")[0]).find('.loader_img').remove();
  270.                   comment_box[0].disabled = false;
  271.                   comment_box.focus();
  272.               });
  273.           }
  274.     );
  275.  
  276.     return false;
  277. }
  278.  
  279. function li_liked(like, args) {
  280.     var me = args.me;
  281.     var item_html = $(args['item_html']);
  282.     li_clear_error(function(){
  283.         li_init(item_html);
  284.         me.parents('.news-item').replaceWith(item_html);
  285.         //me.parents('.messagecell').find('.' + like + 's').replaceWith(likes_html);
  286.         //me.parents('.messagecell').find('.' + like +'_link_block').replaceWith(link_html);
  287.     });
  288. }
  289.  
  290. function li_like_button_mousedown(e) {
  291.     var like = null;
  292.     if ($(e.target).hasClass("like_button")) {
  293.         like = "like";
  294.     } else if ($(e.target).hasClass("dislike_button")) {
  295.         like = "dislike";
  296.     }
  297.  
  298.     if (like == null) {
  299.         return false;
  300.     }
  301.  
  302.     me = $(this);
  303.     me.parents('.news-item').find('.' + like + '_link_block').hide();
  304.     var post_id = $(me.parents(".news-item")[0]).attr('id');
  305.     console.log("post id = " + post_id);
  306.     D.rpc('do_' + like, {"post_id":post_id},
  307.         function(args) {
  308.             args.me = me;
  309.             return li_liked(like, args);
  310.         },
  311.         function(error_obj){
  312.             var msg = 'Error adding ' + like + '.';
  313.             li_error_text(me, msg, error_obj, function(){
  314.                 me.parents('.messagecell').find('.' + like + '_button_section, .num_' + like + 's').show();
  315.             });
  316.         }
  317.     );
  318.     return false;
  319. }
  320.  
  321. var li_comment_button_mousedown = function(){ li_show_comment_box(this); return true;}
  322.  
  323. function li_false() {
  324.     return false;
  325. }
  326.  
  327. var li_inited = null;
  328.  
  329. swapIn(function () {
  330.  
  331.    $("a[href]").each(function() {
  332.        $(this).removeAttr("target");
  333.        if ($(this).attr("href").match("^/")) {
  334.            $(this).attr("href", "http://www.linkedin.com" + $(this).attr("href"));
  335.        }
  336.     })
  337. });
  338.  
  339. var li_inited = false;
  340. var li_active = false;
  341. var li_scrollRequestOutstanding = false;
  342.  
  343. var myName = currentContentName;
  344.  
  345. D.notify('initialize_feed');
  346.  
  347. function li_near_bottom() {
  348.     return ( window.pageYOffset >= ( document.body.offsetHeight - ( window.innerHeight * 2.0 ) ) );
  349. }
  350.  
  351. function li_on_scroll() {
  352.     if (!li_inited || myName !== currentContentName){
  353.         console.log('li_on_scroll ' + myName + ' shortcircut1: ' + currentContentName);
  354.         return;
  355.     }
  356.     if (li_scrollRequestOutstanding){
  357.         return;
  358.     }
  359.     li_scrollRequestOutstanding = true;
  360.     if (!(li_active && li_near_bottom())) {
  361.         li_scrollRequestOutstanding = false;
  362.         return;
  363.     }
  364.     rows = $(".news-item");
  365.     var parent = $($(".news-item-list")[0]);
  366.     var rows = parent.find(".news-item");
  367.     last_item_id = $(rows[rows.length-1]).attr('id');
  368.  
  369.     D.rpc('next_item',
  370.         {},
  371.         function (args) {
  372.             //console.log("next item success");
  373.             if (!li_scrollRequestOutstanding)
  374.                 return;
  375.             if (!li_inited || myName !== currentContentName){
  376.                 li_scrollRequestOutstanding = false;
  377.                 return;
  378.             }
  379.             //var dateparent = $('<div></div>');
  380.             //dateparent.append(args.html);
  381.             //html = dateparent.contents().remove();
  382.             //html.remove();
  383.             //dateparent.remove();
  384.             var html = $(args.html);
  385.             html.appendTo(parent);
  386.             li_init(html);
  387.  
  388.             clear_previous_date_separators();
  389.             var rows2 = $().find(".news-item[timestamp]");
  390.             add_date_separators(rows2);
  391.  
  392.             li_scrollRequestOutstanding = false;
  393.             li_on_scroll();
  394.         },
  395.         function (args) {
  396.             console.log("next item fail: error with rpc thing:" + args);
  397.         }
  398.     );
  399. }
  400.  
  401. swapOut(function() {console.log('*** linkedin SWAP OUT'); });
  402. swapIn(function() {console.log('*** linkedin SWAP IN'); });
  403.  
  404. function li_init(tree) {
  405.     TODAY_TEXT = '';
  406.     if (!tree) {
  407.         if (li_inited) {
  408.             return;
  409.         }
  410.         li_inited = true;
  411.         tree = $();
  412.     }
  413.     clip_imgs(tree);
  414.     convert_timestamps_to_text(tree);
  415. }
  416.  
  417. var _linkedin_positioned_no_news = false;
  418.  
  419. swapIn(function () {
  420.     if (!_linkedin_positioned_no_news) {
  421.         _linkedin_positioned_no_news = true;
  422.         try {
  423.             $(".no-news-today").insertBefore($(".date-separator:not(.static,.today)")[0]);
  424.         } catch (e) {};
  425.     }
  426. });
  427.  
  428. swapIn(li_init);
  429.  
  430. swapIn(function() {
  431.     li_active = true;
  432.     //D = DCallbacks(currentContentName);
  433.     myName = currentContentName;
  434.     console.log('swapin: '+ currentContentName + '/' + myName);
  435.  
  436.     $(window).bind('scroll', li_on_scroll);
  437.     $(".comment_button").live("mousedown", li_comment_button_mousedown);
  438.     $(".like_button").live("mousedown", li_false);
  439.     $(".like_button").live("click", li_like_button_mousedown);
  440.     $(".dislike_button").live("mousedown", li_false);
  441.     $(".dislike_button").live("click", li_like_button_mousedown);
  442.  
  443.     li_on_scroll();
  444. } );
  445. swapOut(function() {
  446.     console.log('swapout: '+ currentContentName + '/' + myName);
  447.     li_active = false;
  448.     $(window).unbind('scroll');
  449.     $(".comment_button").die("mousedown", li_comment_button_mousedown);
  450.     $(".like_button").die("mousedown", li_false);
  451.     $(".like_button").die("click", li_like_button_mousedown);
  452.     $(".dislike_button").die("mousedown", li_false);
  453.     $(".dislike_button").die("click", li_like_button_mousedown);
  454. } );
  455.  
  456. swapOut(li_clear_error);
  457. onHide(li_clear_error);
  458.  
  459.