Advertisement
6jarjar6

http://ifunny.mobi/public/js/desktop.js?v=30

Jan 1st, 2014
1,574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var app = {
  2.     sbСontainer: null,
  3.     content: null,
  4.     preloader: new Image(),
  5.     messageContainer: null,
  6.     footer: null,
  7.  
  8.     currentState: null,// 1 - featured, 2 - popular, 3 - collective
  9.     currentImgIndex: null,
  10.     imagesViews: {},
  11.     imagesIds: [],
  12.     excludedIds: [],
  13.     imagesUrls: [],
  14.     serverGetImagesProccessing: false,
  15.     firstPageCid: null,
  16.     isPreloaderShow: false,
  17.     isDebug: true,
  18.  
  19.     init: function (state, imageId, isDebug) {
  20.         this.currentState = state;
  21.         this.isDebug = isDebug;
  22.  
  23.         this.sbСontainer = $('.gallery-social div.widgets');
  24.         this.content     = $('#content-container');
  25.         this.footer      = $('.footer');
  26.         this.messageContainer     = $('#message-container');
  27.         this.preloader.src = '/public/img/desktop/preloader_min.gif';
  28.  
  29.         //Голосование
  30.         this.content.on('click', 'a.vote', function (e) {
  31.             e.preventDefault();
  32.  
  33.             var isLike = 1, unVote = 0, isReverce = 0;
  34.             var cid = app.content.find('.content-table').attr('data-cid');
  35.  
  36.             if ($(this).hasClass('dislike')) {
  37.                 isLike = 0;
  38.             }
  39.  
  40.             if ($(this).hasClass('active')) {
  41.                 unVote = 1;
  42.             } else {
  43.                 if (isLike) {
  44.                     if (app.content.find('.gallery-left a.vote.dislike').hasClass('active')) {
  45.                         isReverce = 1;
  46.                     }
  47.                 } else {
  48.                     if (app.content.find('.gallery-right a.vote.like').hasClass('active')) {
  49.                         isReverce = 1;
  50.                     }
  51.                 }
  52.             }
  53.  
  54.             $(this).toggleClass('active');
  55.  
  56.             if (isLike) {
  57.                 app.content.find('.gallery-left a.vote.dislike').removeClass('active');
  58.  
  59.                 app.msg.console((unVote?'un':'')+'like '+cid);
  60.             } else {
  61.                 app.content.find('.gallery-right a.vote.like').removeClass('active');
  62.  
  63.                 app.msg.console((unVote?'un':'')+'dislike '+cid);
  64.             }
  65.  
  66.             app.server.vote(cid, isLike, unVote, isReverce);
  67.         });
  68.  
  69.         // ссылки влево и вправо
  70.         this.content.on('click', '.gallery-left a.arrow, .gallery-right a.arrow', function (e) {
  71.             app.pushHistory(e, $(this));
  72.  
  73.             $(this).toggleClass('active');
  74.  
  75.             if ($(this).closest('.gallery-left').length != 0) {
  76.                 app.showPrev();
  77.             } else {
  78.                 app.showNext();
  79.             }
  80.         });
  81.  
  82.         this.content.on('click', '.image-margin', function (e) {
  83.             app.pushHistory(e, $(this).find('a.arrow'));
  84.             $(this).toggleClass('active');
  85.             app.showNext();
  86.         });
  87.  
  88.         // ссылки потоков
  89.         this.content.on('click', '.feeds .links a', function (e) {
  90.             app.pushHistory(e, $(this));
  91.             app.changeFeed($(this).attr('data-feed'));
  92.         });
  93.  
  94.         // вешаем событие на popstate которое срабатывает при нажатии back/forward в браузере
  95.         $(window).bind("popstate", function (e) {
  96.             app.msg.console('popstate ');
  97.             var index = 0;
  98.             if (e.originalEvent.state != null) {
  99.                 app.msg.console(e.originalEvent.state);
  100.                 var historyCid = app.utils.getCidFromUrl(e.originalEvent.state.url);
  101.                 app.firstPageCid = e.originalEvent.state.cid;
  102.  
  103.                 if (historyCid != null) {
  104.                     if (app.imagesViews[historyCid] != undefined) {
  105.                         app.showView(historyCid);
  106.                     } else {
  107.                         app.server.getImage(app.currentState, historyCid, true);
  108.                     }
  109.                 }
  110.             } else {
  111.                 if (app.firstPageCid != null) {
  112.                     if (app.imagesViews[app.firstPageCid] != undefined) {
  113.                         app.showView(app.firstPageCid);
  114.                     } else {
  115.                         app.server.getImage(app.currentState, app.firstPageCid, true);
  116.                     }
  117.                 }
  118.             }
  119.  
  120.         });
  121.  
  122.         //переход по стрелкам влево/вправо
  123.         $(document).keyup(app.onKeyUp);
  124.  
  125.         //сдвигание боковых элементов на больших картинках
  126.         $(window).scroll(function () {
  127.             app.utils.checkButtonsFloating.call(this);
  128.         });
  129.  
  130.         //Загрузка первого контента
  131.         app.server.getImages(this.currentState, app.constants.NUM_LOAD_IMAGES_FIRST, [], imageId);
  132.  
  133.         var footerTop = app.footer.offset().top;
  134.         app.utils.calcImageMarginTop(app.content.find('.gallery-center'), footerTop);
  135.  
  136.     }, //init
  137.  
  138.     onKeyUp: function(e) {
  139.         var keyCode = e.keyCode ? e.keyCode : e.which;
  140.         var view = app.utils.getViewByIndex(app.currentImgIndex);
  141.  
  142.         if (keyCode == 37 && view != null && !view.find('.gallery-left a.arrow').hasClass('visibility_hidden')) {
  143.             app.showPrev();
  144.         } else if (keyCode == 39) {
  145.             app.showNext();
  146.         }
  147.     },
  148.  
  149.     pushHistory: function(e, link) {
  150.         e.preventDefault();
  151.  
  152.         var state = {
  153.             url: link.attr("href"),
  154.             title: null,
  155.             cid: this.content.find('.content-table').attr('data-cid')
  156.         };
  157.         //app.msg.console('pushHistory ' + JSON.stringify(state));
  158.         state.url = state.url.replace(new RegExp(".*//[a-z0-9\.]+/",'g'), document.location.origin + "/");
  159.  
  160.         // заносим ссылку в историю
  161.         history.pushState(state, state.title, state.url);
  162.     },
  163.  
  164.     showNext: function() {
  165.         this.showImage(app.currentImgIndex + 1);
  166.     },
  167.  
  168.     showPrev: function() {
  169.         this.showImage(app.currentImgIndex - 1);
  170.     },
  171.  
  172.     /**
  173.      * @param state
  174.      */
  175.     changeFeed: function(state) {
  176.         this.currentState = state;
  177.         //Перерисовка кнопок потоков до загрузки контента ленты
  178.         var feedLinks = this.content.find(".feeds .links a");
  179.         $.each(feedLinks, function(index, link) {
  180.             var lnk = $(link);
  181.             if (lnk.attr('data-feed') != state) {
  182.                 lnk.removeClass('active');
  183.             } else {
  184.                 if (!lnk.hasClass('active')) {
  185.                     lnk.addClass('active');
  186.                 }
  187.             }
  188.         });
  189.  
  190.         app.utils.clearCache();
  191.         app.server.getImages(this.currentState, app.constants.NUM_LOAD_IMAGES_FIRST, this.excludedIds, null);
  192.     },
  193.  
  194.     showImage: function(showIndex) {
  195.         app.msg.console('showImage fired: '+showIndex);
  196.         if (this.imagesIds.length == 0) {
  197.             return;
  198.         }
  199.  
  200.         if (showIndex < 0) {
  201.             showIndex = this.imagesIds.length - 1;
  202.         } else if (showIndex >= this.imagesIds.length) {
  203.             if (this.serverGetImagesProccessing) {
  204.                 if (this.currentImgIndex < this.imagesIds.length) {
  205.                     this.currentImgIndex = showIndex;
  206.                 }
  207.                 return;
  208.             }
  209.             showIndex = 0;
  210.         }
  211.  
  212.         this.currentImgIndex = showIndex;
  213.  
  214.         this._mainImgShowPicture(this.currentImgIndex);
  215.  
  216.         if ((this.currentImgIndex >= (this.imagesIds.length - 3)) && !this.serverGetImagesProccessing) {
  217.             app.server.getImages(this.currentState, app.constants.NUM_LOAD_IMAGES_AFTER, this.excludedIds, null);
  218.         }
  219.     },
  220.  
  221.  
  222.     _mainImgShowPicture: function(index) {
  223.         var view = app.utils.getViewByIndex(index);
  224.  
  225.         if (view) {
  226.             if (index == 0) {
  227.                 view.find('.gallery-left a.arrow').addClass('visibility_hidden');
  228.             } else {
  229.                 view.find('.gallery-left a.arrow').removeClass('visibility_hidden');
  230.             }
  231.  
  232.             view.find('.gallery-left a.arrow, .gallery-right a.arrow').removeClass('active');
  233.  
  234.             //установка ссылок на следующий и предыдущий контенты на границе стыковки разных пачек, полученных от сервера
  235.             if (this.imagesUrls[index+1] != undefined) {
  236.                 view.find('.gallery-right a.arrow').attr('href', this.imagesUrls[index+1]);
  237.                 view.find('.gallery-center a.arrow').attr('href', this.imagesUrls[index+1]);
  238.  
  239.                 var viewNext = app.utils.getViewByIndex(index+1);
  240.                 if (viewNext) {
  241.                     viewNext.find('.gallery-left a.arrow').attr('href', this.imagesUrls[index]);
  242.                 }
  243.             }
  244.  
  245.             if (this.imagesUrls[index-1] != undefined) {
  246.                 view.find('.gallery-left a.arrow').attr('href', this.imagesUrls[index-1]);
  247.  
  248.                 var viewPrev = app.utils.getViewByIndex(index-1);
  249.                 if (viewPrev) {
  250.                     viewPrev.find('.gallery-right a.arrow').attr('href', this.imagesUrls[index]);
  251.                     viewPrev.find('.gallery-center a.arrow').attr('href', this.imagesUrls[index]);
  252.                 }
  253.             }
  254.  
  255.             app.content.empty();
  256.             $.when(
  257.                     app.content.append(view)
  258.                 ).then(
  259.                     app._afterAppendActions(view)
  260.                 );
  261.             app.isPreloaderShow = false;
  262.         }
  263.     },
  264.  
  265.     showView: function(cid) {
  266.         if (this.imagesViews[cid] != undefined) {
  267.             var view = this.imagesViews[cid];
  268.             app.content.empty();
  269.             var footerTop = app.footer.offset().top;
  270.             $.when(
  271.                     app.content.append(view)
  272.                 ).then(
  273.                     app._afterAppendActions(view, footerTop)
  274.                 );
  275.             this.currentImgIndex = app.utils.getCidIndex(cid);
  276.         } else {
  277.             app.msg.console('showView - nothing to show with cid: ' + cid);
  278.         }
  279.     },
  280.  
  281.     _afterAppendActions: function(view, footerTop) {
  282.         var gc = view.find('.gallery-center');
  283.         if (gc.find('a').length) {
  284.             app.utils.setPageMetaInformation(gc);
  285.         } else {
  286.             app.utils.setPageMetaInformation(gc);
  287.         }
  288.         var img = gc.find('img');
  289.         if (img.attr('data-margin') == 1) {
  290.             img.css({'margin': app.utils.calcImageMarginBottom(img.attr('src'))});
  291.         }
  292.  
  293.         if (gc.attr('data-abused') != 1) {
  294.             app.utils.calcImageMarginTop(gc, footerTop);
  295.         } else {
  296.             gc.css('margin-top', 0);
  297.         }
  298.  
  299.         app.socialButtons.redraw();
  300.  
  301.         app.utils.checkButtonsFloating();
  302.     }
  303. };
  304.  
  305. app.socialButtons = {
  306.     init: function() {
  307.         //Facebook
  308.         app.sbСontainer.find('div.sb.display_none').each(function(index, elem) {
  309.             var el = $(elem);
  310.             if (!el.hasClass('rendered')) {
  311.                 FB.XFBML.parse(elem);
  312.                 $(el).addClass('rendered');
  313.             }
  314.         });
  315.  
  316.         //Twitter
  317.         twttr.widgets.load();
  318.     },
  319.  
  320.     vkCounter: 1,
  321.  
  322.     add: function(cid, url, meta, image) {
  323.         var description = meta.tizer;
  324.         var title = meta.title;
  325.  
  326.         var sbContent = $(app.constants.sbElem);
  327.         sbContent.append("<div id='vk_" + app.socialButtons.vkCounter + "' class='vk'></div>");
  328.  
  329.         var vkButton = app.socialButtons.addVK(url, app.socialButtons.vkCounter++, title, description, image);
  330.         var twButton = app.socialButtons.addTwitter(url, title);
  331.         var fbButton = app.socialButtons.addFacebook(url);
  332.  
  333.         sbContent.append(vkButton).append(fbButton).append(twButton);
  334.  
  335.         sbContent.attr('data-cid', cid);
  336.         app.sbСontainer.append(sbContent);
  337.     },
  338.  
  339.     addVK: function(url, id, title, description, image){
  340.         if(PROJECT_LANG == "ru") {
  341.             VK.Widgets.Like("vk_"+id, {
  342.                 type: "mini",
  343.                 pageUrl: url,
  344.                 pageTitle: title,
  345.                 //pageDescription: description,
  346.                 pageDescription: 'Нажмите, чтобы посмотреть прикол',
  347.                 pageImage: image.attr('src')
  348.             });
  349.         }
  350.     },
  351.  
  352.     addTwitter: function(url, text){
  353.         return '<div class="tw"><a href="https://twitter.com/share" class="twitter-share-button" data-lang="'+PROJECT_LANG+'" data-url="'+url+'" data-count="horizontal" data-text="'+text+'" data-counturl="'+url+'">Tweet</a></div></div>';
  354.     },
  355.  
  356.     addFacebook: function(url){
  357.         return '<div class="fb-like" data-href="'+url+'" data-send="false" data-layout="button_count" data-show-faces="false"></div>';
  358.     },
  359.  
  360.     remove: function(cid) {
  361.         app.sbСontainer.find('div.sb').each(function(index, elem){
  362.             if ($(elem).attr('data-cid') == cid) {
  363.                 $(elem).remove();
  364.             }
  365.         });
  366.     },
  367.  
  368.     redraw: function() {
  369.         app.sbСontainer.find('div.sb').each(function(index, elem){
  370.             var el = $(elem);
  371.             if (el.hasClass('display_block')) {
  372.                 el.toggleClass('display_block display_none');
  373.             }
  374.         });
  375.  
  376.         var fbCss = {width: '101px', height: '20px'};
  377.         var cid = app.utils.getCidByIndex(app.currentImgIndex);
  378.         if (cid) {
  379.             var sbCurrent = app.sbСontainer.find('.sb[data-cid='+cid+']');
  380.             sbCurrent.toggleClass('display_none display_block');
  381.             sbCurrent.find('div.fb-like > span').css(fbCss);
  382.             sbCurrent.find('div.fb-like > span > iframe').css(fbCss);
  383.         }
  384.     },
  385.  
  386.     clearContainer: function(){
  387.         app.sbСontainer.empty();
  388.     }
  389. };
  390.  
  391. app.utils = {
  392.     clearCache: function() {
  393.         this.showPreloder();
  394.  
  395.         app.imagesIds = [];
  396.         app.imagesUrls = [];
  397.         app.excludedIds = [];
  398.         app.imagesViews = {};
  399.         app.currentImgIndex = null;
  400.         app.socialButtons.clearContainer();
  401.     },
  402.  
  403.     showPreloder: function() {
  404.         var gc = app.content.find('.gallery-center');
  405.         gc.attr('data-height', 60)
  406.             .css('margin-top', 0)
  407.             .find('img').attr('src', app.preloader.src)
  408.             .css('margin', '0 auto');
  409.  
  410.         app.content.find('.gallery-right', '.gallery-left').empty();
  411.         app.isPreloaderShow = true;
  412.         app.utils.calcImageMarginTop(gc);
  413.     },
  414.  
  415.     getCidFromUrl: function(url) {
  416.         var list = url.split('/');
  417.         var cid = list.pop();
  418.         if (cid != 'f' && cid != 'p' && cid != 'c' && cid != '') {
  419.             return cid;
  420.         } else {
  421.             return null;
  422.         }
  423.     },
  424.  
  425.     pushAndTrim: function(data) {
  426.         app.msg.console('pushAndTrim');
  427.         for (var i = 0; i < data.cids.length; i++) {
  428.             var cid = data.cids[i];
  429.             app.imagesIds.push(cid);
  430.             app.imagesViews[cid] = $(data.views[i]);
  431.             app.imagesUrls.push(data.urls[i]);
  432.  
  433.             var gc = app.imagesViews[cid].find('.gallery-center');
  434.             var image = gc.find('img').eq(0);
  435.             var tt = app.utils.getPageTitleTizer(gc);
  436.  
  437.             app.socialButtons.add(cid, gc.attr('data-link'), tt, image);
  438.  
  439.             if (app.imagesIds.length > app.constants.CACHE_LIMIT) {
  440.                 var shiftedCid = app.imagesIds.shift();
  441.                 app.imagesUrls.shift();
  442.                 app.socialButtons.remove(shiftedCid);
  443.                 app.currentImgIndex--;
  444.             }
  445.         }
  446.  
  447.         app.socialButtons.init();
  448.  
  449.         this.rebuildViews();
  450.     },
  451.  
  452.     unshiftAndTrim: function(data) {
  453.         app.msg.console('unshiftAndTrim');
  454.         for (var i = 0; i < data.cids.length; i++) {
  455.             var cid = data.cids[i];
  456.             app.imagesIds.unshift(cid);
  457.             app.imagesViews[cid] = $(data.views[i]);
  458.             app.imagesUrls.unshift(data.urls[i]);
  459.             app.currentImgIndex--;
  460.  
  461.             var gc = app.imagesViews[cid].find('.gallery-center');
  462.             var image = gc.find('img').eq(0);
  463.             var tt = app.utils.getPageTitleTizer(gc);
  464.  
  465.             app.socialButtons.add(cid, gc.attr('data-link'), tt, image);
  466.  
  467.             if (app.imagesIds.length > app.constants.CACHE_LIMIT) {
  468.                 var popedCid = app.imagesIds.pop();
  469.                 app.imagesUrls.pop();
  470.                 app.socialButtons.remove(popedCid);
  471.             }
  472.         }
  473.  
  474.         app.socialButtons.init();
  475.  
  476.         this.rebuildViews();
  477.     },
  478.  
  479.     rebuildViews: function() {
  480.         for (var cid in app.imagesViews) {
  481.             var isOrphan = true;
  482.             for (var i = 0; i < app.imagesIds.length; i++) {
  483.                 if (cid == app.imagesIds[i]) {
  484.                     isOrphan = false;
  485.                     break;
  486.                 }
  487.             }
  488.  
  489.             //удаляем сироту
  490.             if (isOrphan) {
  491.                 delete app.imagesViews[cid];
  492.             }
  493.         }
  494.     },
  495.  
  496.     getCidIndex: function(cid) {
  497.         var index = 0;
  498.         for(var i = 0; i < app.imagesIds.length; i++) {
  499.             if (app.imagesIds[i] == cid) {
  500.                 index = i;
  501.                 break;
  502.             }
  503.         }
  504.         return index;
  505.     },
  506.  
  507.     getUrlIndex: function(url) {
  508.         var index = 0;
  509.         for(var i = 0; i < app.imagesUrls.length; i++) {
  510.             if (app.imagesUrls[i] == url) {
  511.                 index = i;
  512.                 break;
  513.             }
  514.         }
  515.         return index;
  516.     },
  517.  
  518.     getViewByIndex: function(index){
  519.         var cid = this.getCidByIndex(index);
  520.         if (cid != null) {
  521.             if (app.imagesViews[cid] != undefined) {
  522.                 return app.imagesViews[cid];
  523.             }
  524.         }
  525.         return null;
  526.     },
  527.  
  528.     getCidByIndex: function(index){
  529.         if (app.imagesIds[index] != undefined) {
  530.             return app.imagesIds[index];
  531.         }
  532.         return null;
  533.     },
  534.  
  535.     checkButtonsFloating: function() {
  536.         var gs = app.sbСontainer.offset().top;
  537.         var top = $(window).scrollTop();
  538.         var offset = (top + 650);
  539.         var windowHeight = $(window).height();
  540.         var docHeight = $(document).height();
  541.  
  542.         if ((top + windowHeight) <= docHeight) {
  543.             if (offset > gs && top > 0) {
  544.                 app.content.find('.gallery-right a.arrow, .gallery-left a.arrow').css('top', (415 - (offset - gs))+'px');
  545.                 app.content.find('.gallery-right a.vote, .gallery-left a.vote').css('top', (500 - (offset - gs)));
  546.                 app.content.find('.gallery-right div.likes-tooltip').css('top', (588 - (offset - gs)));
  547.             } else {
  548.                 app.content.find('.gallery-right a.arrow, .gallery-left a.arrow').css('top', 415);
  549.                 app.content.find('.gallery-right a.vote, .gallery-left a.vote').css('top', 500);
  550.                 app.content.find('.gallery-right div.likes-tooltip').css('top', 588);
  551.             }
  552.         }
  553.     },
  554.  
  555.     setPageMetaInformation: function(gc){
  556.  
  557.         var isImage = false, isAbused = false;
  558.         if (gc.find('a').length) {
  559.             isImage = true;
  560.  
  561.         }
  562.  
  563.         if (gc.attr('data-abused') == 1) {
  564.             isAbused = true;
  565.         }
  566.  
  567.         var tt = app.utils.getPageTitleTizer(gc);
  568.         var title = tt.title;
  569.         var tizer = tt.tizer;
  570.  
  571.         //Удаление старой мета инфы
  572.         $('head meta[property^="og:"]')
  573.             .add('head meta[name^="og:"]')
  574.             .add('head meta[property^="twitter:"]')
  575.             .add('head meta[name^="twitter:"]')
  576.             .add('head link[rel="image_src"]')
  577.             .remove();
  578.  
  579.         //общая мета инфа
  580.         $('head title').text(title);
  581.         $('head meta[name="description"]').attr('content', tizer);
  582.         $('head').append('<meta property="og:title" content="'+title+'"/>')
  583.             .append('<meta name="og:description" content="'+tizer+'"/>')
  584.             .append('<meta name="og:site_name" content="'+app.utils.getProjectName()+'"/>')
  585.             .append('<meta name="og:url" content="'+gc.attr('data-link')+'"/>');
  586.  
  587.         if (!isAbused) {
  588.             $('head').append('<meta name="twitter:card" content="'+(!isImage?'player':'photo')+'" rr="'+isAbused+'"/>')
  589.                 .append('<meta name="twitter:site" content="@ifunny"/>')
  590.                 .append('<meta name="twitter:title" content="'+tizer+'"/>')
  591.                 .append('<meta name="twitter:description" content="'+(!isImage?'Click to see the video...':'Click to see the pic...')+'"/>');
  592.  
  593.             if (isImage) {
  594.                 var src = gc.find('img').attr('src');
  595.                 $('head').append('<meta name="twitter:image" content="'+src+'"/>')
  596.                     .append('<meta name="twitter:image:width" content="'+gc.attr('data-width')+'"/>')
  597.                     .append('<meta name="twitter:" content="'+gc.attr('data-height')+'"/>')
  598.                     .append('<meta name="og:type" content="article"/>')
  599.                     .append('<meta name="og:image" content="'+src+'"/>')
  600.                     .append('<link rel="image_src" href="'+src+'"/>');
  601.             } else {
  602.                 $('head').append('<meta name="twitter:player" content="'+gc.find('iframe').attr('src')+'"/>')
  603.                     .append('<meta name="twitter:player:width" content="600"/>')
  604.                     .append('<meta name="twitter:player:height" content="480"/>');
  605.  
  606.                 $('head').append('<meta name="og:video" content="http://www.youtube.com/v/'+gc.find('iframe').attr('data-vid')+'?version=3&amp;autohide=1"/>')
  607.                     .append('<meta name="og:video:type" content="application/x-shockwave-flash"/>')
  608.                     .append('<meta name="og:video:width" content="600"/>')
  609.                     .append('<meta name="og:video:height" content="480"/>')
  610.                     .append('<meta name="og:type" content="video"/>')
  611.                     .append('<meta name="og:image" content="'+gc.find('iframe').attr('data-src')+'"/>')
  612.                     .append('<link rel="image_src" href="'+gc.find('iframe').attr('data-src')+'"/>');
  613.             }
  614.         }
  615.     },
  616.  
  617.     getPageTitleTizer: function(gc) {
  618.         var tizer = '';
  619.         var title = '';
  620.         if (gc.find('a').length) {
  621.             tizer = gc.find('a').attr('title');
  622.         } else {
  623.             tizer = gc.find('iframe').attr('title');
  624.         }
  625.  
  626.         if (tizer) {
  627.             title = tizer + ' / ' + app.utils.getProjectName();
  628.         } else {
  629.             title = app.utils.getProjectName();
  630.         }
  631.  
  632.         return {'title': title, 'tizer': tizer};
  633.     },
  634.  
  635.     getProjectName: function() {
  636.         return (PROJECT_LANG == 'ru') ? 'АйДаПрикол :)' : 'iFunny :)';
  637.     },
  638.  
  639.     calcImageMarginBottom: function(src) {
  640.         var margin = '0 auto';
  641.         var img = new Image();
  642.         img.src = src;
  643.         var w = img.width;
  644.         var h = img.height;
  645.         var watermarkHeight = 20;
  646.  
  647.         var m = watermarkHeight;
  648.         if (w > 600) {
  649.             var rate = w / 600;
  650.  
  651.             var imgHeight = Math.ceil(h / rate);
  652.             m = Math.round(imgHeight / h * watermarkHeight);
  653.         }
  654.  
  655.         return margin += ' -'+m+'px';
  656.     },
  657.  
  658.     calcImageMarginTop: function(gc, footerTop) {
  659.         footerTop = footerTop || app.footer.offset().top;
  660.  
  661.         var contentHeight = (!gc.find('iframe').length) ? parseInt(gc.attr('data-height')) : 480;
  662.  
  663.         var feedOffset = 90 + 80;
  664.         var contentTop = app.content.find('.feeds').offset().top + 55;
  665.         var contentAreaHeight = footerTop - contentTop;
  666.         app.msg.console('contentTop: '+contentTop);
  667.         app.msg.console('footerTop: '+footerTop);
  668.         app.msg.console('contentAreaHeight: '+contentAreaHeight+', contentHeight: '+contentHeight);
  669.  
  670.         var marginTop = 0;
  671.         if (contentHeight < contentAreaHeight) {
  672.             var marginTop = Math.round((contentAreaHeight - contentHeight) / 2);
  673.             marginTop = (marginTop > feedOffset) ? marginTop-feedOffset : 0;
  674.             app.msg.console('margin-top: '+marginTop);
  675.         }
  676.  
  677.         gc.css('margin-top', marginTop);
  678.  
  679.     }
  680. }; //utils
  681.  
  682. app.server = {
  683.     /**
  684.      * @param type
  685.      * @param count
  686.      * @param excludeIds
  687.      * @param firstImg
  688.      */
  689.     getImages: function(type, count, excludeIds, firstImg) {
  690.         app.msg.console('########## serverGetImages fired');
  691.  
  692.         var reqData = {'type': type, 'count': count, 'excludeIds': excludeIds.join(',')};
  693.         if (firstImg) {
  694.             reqData.firstImg = firstImg;
  695.         }
  696.  
  697.         app.serverGetImagesProccessing = true;
  698.         var lengthBefore = app.imagesIds.length;
  699.  
  700.         $.ajax({
  701.             url: '/server/get-content/',
  702.             type: "POST",
  703.             data: reqData,
  704.             success: function (data) {
  705.                 var isNeedShow = (app.currentImgIndex >= app.imagesIds.length);
  706.  
  707.                 app.serverGetImagesProccessing = false;
  708.  
  709.                 if (data.error) {
  710.                     app.msg.showError(data.error.text);
  711.                     return;
  712.                 }
  713.  
  714.                 for (var i = 0; i < data.cids.length; i++) {
  715.                     app.excludedIds.push(data.cids[i]);
  716.                 }
  717.  
  718.                 app.utils.pushAndTrim(data);
  719.  
  720.                 if (app.imagesIds.length == 0) {
  721.                     return;
  722.                 }
  723.  
  724.                 if (app.currentImgIndex == null) {
  725.                     app.currentImgIndex = 0;
  726.                     app.showImage(app.currentImgIndex);
  727.                 }
  728.  
  729.                 app.msg.console('########## imagesIds.len: ' +app.imagesIds.length);
  730.             },
  731.             error: function(jqXHR, textStatus, errorThrown) {
  732.                 app.msg.showError(errorThrown);
  733.             },
  734.             dataType: 'json'
  735.         });
  736.     },
  737.  
  738.     /**
  739.      * @param type
  740.      * @param firstImg
  741.      * @param isShowNewAfterAdd
  742.      */
  743.     getImage: function(type, firstImg, isShowNewAfterAdd) {
  744.         app.msg.console('******* serverGetImage fired: ' + firstImg);
  745.  
  746.         //    debugger;
  747.         isShowNewAfterAdd = isShowNewAfterAdd || false;
  748.         var reqData = {'type': type, 'count': 0, 'excludeIds': [], firstImg: firstImg};
  749.  
  750.         app.serverGetImagesProccessing = true;
  751.         var lengthBefore = app.imagesIds.length;
  752.         $.ajax({
  753.             url: '/server/get-content/',
  754.             type: "POST",
  755.             data: reqData,
  756.             success: function (data) {
  757.                 app.serverGetImagesProccessing = false;
  758.  
  759.                 if (data.error) {
  760.                     app.msg.showError(data.error.text);
  761.                     return;
  762.                 }
  763.  
  764.                 app.utils.unshiftAndTrim(data);
  765.  
  766.                 if (isShowNewAfterAdd) {
  767.                     app.currentImgIndex = null;
  768.                     app.showImage(0);
  769.                 }
  770.  
  771.                 app.msg.console('******* imagesIds.len: ' +app.imagesIds.length);
  772.             },
  773.             error: function(jqXHR, textStatus, errorThrown) {
  774.                 app.msg.showError(errorThrown);
  775.             },
  776.             dataType: 'json'
  777.         });
  778.     },
  779.  
  780.     /**
  781.      * @param cid
  782.      * @param isLike
  783.      * @param unVote
  784.      * @param isReverce
  785.      */
  786.     vote: function(cid, isLike, unVote, isReverce) {
  787.         $.ajax({
  788.             url: '/server/vote/',
  789.             type: "POST",
  790.             data: {cid: cid, isLike: isLike, unVote: unVote, isReverce: isReverce},
  791.             success: function (data) {
  792.                 //app.content.find('div.likes-tooltip div.vc').html(data);
  793.             },
  794.             error: function(jqXHR, textStatus, errorThrown) {
  795.                 app.msg.showError(errorThrown);
  796.             },
  797.             cache: false,
  798.             async: false
  799.         });
  800.  
  801.         var inc = unVote ? -1 : 1;
  802.  
  803.         var vc = app.content.find('div.likes-tooltip div.vc');
  804.         vc.html(parseInt(vc.text()) + inc);
  805.     }
  806. };
  807.  
  808. app.msg = {
  809.     showError: function(text){
  810.         if (app.messageContainer.is(':visible').length > 0) {
  811.             hideMessageContainer(0);
  812.         }
  813.         app.messageContainer.html('<strong>Error!</strong> ' + text).fadeIn(400);
  814.         setTimeout(app.msg.hideError, 3000);
  815.         if (app.isPreloaderShow || true) {
  816.             app.content.find('.gallery-center .image-margin').empty().append(
  817.                 $('<a href="#">Refresh page</a>').click(function(e){
  818.                     e.preventDefault();
  819.                     document.location = document.location;
  820.                 })
  821.             ).toggleClass('image-margin refresh-page');
  822.         }
  823.     },
  824.  
  825.     hideError: function(duration) {
  826.         duration = duration || 400;
  827.         app.messageContainer.fadeOut(duration);
  828.     },
  829.  
  830.     console: function(text) {
  831.         if (app.isDebug) {
  832.             console.log(text);
  833.         }
  834.     }
  835. };
  836.  
  837. app.constants = {
  838.     CACHE_LIMIT: 9,
  839.     NUM_LOAD_IMAGES_FIRST: 6,
  840.     NUM_LOAD_IMAGES_AFTER: 3,
  841.     sbElem: '<div class="sb display_none" data-cid=""></div>'
  842. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement