var cmpFeeds = { options: { itemsPerPage: 10, reconnectInterval: 5000, animationSpeed: 1000, wsPort: 8081, }, $newItems: $([]), init: function(params) { var me = this; $.extend(me.options, params); var path = 'broadcast.motorsport.com'; if(params.appENV == 'stage' || params.appENV=='development'){ path = window.location.host; } // me.wsUrl = 'ws://' + window.location.host + ':' + me.options.wsPort + '/' + me.options.userId + '/'+ me.options.countryId +'?platform=web'; me.wsUrl = 'wss://'+path+':' + me.options.wsPort + '/' + me.options.userId + '/'+ me.options.countryId +'?platform=web'; me.establishConnection(); }, _getCurrentPage: function() { var me = this, href = window.location.href, query = href.split('?')[1], querParams = query ? query.split('&') : null, page = 1; if (querParams) { $.each(querParams, function(index, val) { if (!val) return; var valSplit = val.split('='); if (valSplit[0] !== 'p') return; page = valSplit[1]; }); } return page; }, establishConnection: function() { var me = this, ws = me.ws; if (ws) return; me.ws = ws = new WebSocket(me.wsUrl); ws.onopen = function(ev) { // console.log('socket connection open'); }; ws.onerror = function(ev) { // console.log('socket connection error => '); if (ws) { ws.close(); } }; ws.onclose = function(ev) { me.ws = ws = null; // console.log('socket has been closed'); setTimeout(function() { // console.log('reconnecting...'); me.establishConnection(); }, me.options.reconnectInterval); }; ws.onmessage = function(ev) { var msg = JSON.parse(ev.data), $newItem = $(msg.html), page = me._getCurrentPage(); me.$wrapper = $('#feeds_list_wrapper'); switch(msg.action) { case 'create': me.$newItems = $newItem.add(me.$newItems); if (!me.$wrapper.length || page != 1) { me.updateNewMessagesCount('add'); } else { me.showNewMessages(); } break; case 'update': me.updateMessage(msg.type, msg.entity_id, $newItem); break; case 'delete': me.removeMessage(msg.type, msg.entity_id); break; } }; }, updateNewMessagesCount: function(action) { var me = this, $feedBtn = $('#feed_button'), $feedCount = $feedBtn.find('> a'), count = $.cookie('feed_count') || 0, countMessage; count = parseInt(count, 10); switch (action) { case 'add': count++; break; case 'remove': count--; break; } count = count < 0 ? 0 : count; countMessage = count > 99 ? '99+' : count; if (count < 100) { $feedCount.attr('data-feed-count', ''); if (count) { $feedBtn.removeClass('hidden'); setTimeout(function() { $feedCount.attr('data-feed-count', countMessage); }, 200); $(window).trigger('resize.menusItems', 1); } else { $feedBtn.addClass('hidden'); } } $.cookie('feed_count', countMessage, { path: '/' }); /* if (!count) { // TODO langs cmpTrayAlert.show('You don\'t have new feeds', 'message', 3, null, 'feed-message'); return; } // TODO langs var $message = cmpTrayAlert.show('You have '+ (me.$newItems.length) +' new feed(s)', 'message', false, null, 'feed-message'); $message.css('cursor', 'pointer'); $message.on('click', function() { $message.remove(); if (!me.$wrapper.length) { me.$newItems = $([]); window.location.href = '/feeds'; return; } me.showNewMessages(); }); */ }, updateMessage: function(type, id, $newItem) { var me = this, speed = me.options.animationSpeed, $item = me.$wrapper.find('.item[data-id="'+ id +'"][data-type="'+ type +'"]'); if (type === 'gallery') { $item.slideUp(speed, function() { $item.remove(); }); $newItem.hide(); me.$wrapper.prepend($newItem); $newItem.slideDown(speed); } else { $item.replaceWith($newItem); } $newItem.jplDateFormat(layoutConfig.dateFormat); }, removeMessage: function(type, id) { var me = this, speed = me.options.animationSpeed, $item = me.$wrapper.find('.item[data-id="'+ id +'"][data-type="'+ type +'"]'), newItemsCountBefore = me.$newItems.length, newItemsCountAfter; me.$newItems = me.$newItems.filter(function(index, el) { var $el = $(el); return $el.is(':not([data-id="'+ id +'"][data-type="'+ type +'"])'); }); newItemsCountAfter = me.$newItems.length; if (newItemsCountBefore && !$item.length && newItemsCountAfter !== newItemsCountBefore) { me.updateNewMessagesCount('remove'); } $item.fadeOut(speed, function() { $item.remove(); }); }, showNewMessages: function() { var me = this, $items = me.$wrapper.find('.item'), speed = me.options.animationSpeed, oldItemsCount = $items.length; // newItemsCount = me.$newItems.length, // itemsCount = oldItemsCount + newItemsCount, // removeItems = itemsCount - me.options.itemsPerPage, // $removeItems; // if (removeItems > oldItemsCount) { // removeItems = oldItemsCount; // } if (!oldItemsCount) { me.$wrapper.prev('.feedsHeader').removeClass('hidden'); me.$wrapper.empty(); } me.$newItems.hide(); me.$wrapper.prepend(me.$newItems); me.$newItems.fadeIn(speed); me.$newItems.jplDateFormat(layoutConfig.dateFormat); me.$newItems = $([]); /* $removeItems = $items.slice(oldItemsCount - removeItems); $removeItems.fadeOut(speed, function() { itemsCount--; $removeItems.remove(); }); */ } };