if ( window.XDomainRequest ) { $.ajaxSetup({ cache: true }); $.ajaxTransport(function( s ) { if ( s.crossDomain && s.async ) { if ( s.timeout ) { s.xdrTimeout = s.timeout; delete s.timeout; } var xdr; return { send: function( _, complete ) { function callback( status, statusText, responses, responseHeaders ) { xdr.onload = xdr.onerror = xdr.ontimeout = $.noop; xdr = undefined; complete( status, statusText, responses, responseHeaders ); } xdr = new XDomainRequest(); xdr.onload = function() { callback( 200, "OK", { text: xdr.responseText }, "Content-Type: " + xdr.contentType ); }; xdr.onerror = function() { callback( 404, "Not Found" ); }; xdr.onprogress = function() {}; xdr.ontimeout = function() { callback( 0, "timeout" ); }; xdr.timeout = s.xdrTimeout || Number.MAX_VALUE; xdr.open( s.type, s.url ); xdr.send( ( s.hasContent && s.data ) || null ); }, abort: function() { if ( xdr ) { xdr.onerror = $.noop; xdr.abort(); } } }; } }); } cmComments = { options: { userId: null, nickname: null, extId: 0, type: '', target: '', commentsPerPage: 10, apiEndpoint: 'http://talk.calciomercato.com/api' }, init: function (options) { this.options = $.extend(this.options, options); this.renderCage(); this.loadComments('all'); }, setOptions: function (options) { this.options = $.extend(this.options, options); }, send: function (params, callback, callbackFail) { var obj = this; if (params.body == '') { alert('Inserisci un contenuto al tuo commento'); callbackFail(); } else { obj.showLoading(); var data = $.extend({ type: obj.options.type, extId: obj.options.extId, userId: obj.options.userId, nickname: obj.options.nickname }, params); if (data.facebookId) data.publishOnFacebook = 1; $.ajax({ url: obj.options.apiEndpoint+"/comments", type: "POST", data: data, crossDomain: true, dataType: 'json', success: function (data) { obj.hideLoading(); callback(data); }, error: function (error, textStatus, errorThrown) { obj.hideLoading(); var res = $.parseJSON(error.responseText); alert('Ci scusiamo per il disagio ma ci sono problemi di connessione al server. Ti preghiamo di riprovare!'); } }); } }, loadComments: function (type) { var obj = this; obj.showLoading(); var data; if (type == 'my') { data = { type: obj.options.type, extId: obj.options.extId, userId: obj.options.userId, limit: obj.options.commentsPerPage } } else { data = { type: obj.options.type, extId: obj.options.extId, limit: obj.options.commentsPerPage } } var callback = function(data, type) { return obj.renderCommentsAndReplies(data, type);} obj.getComments(data, type, callback); }, renderCommentsAndReplies: function (data, type) { var obj = this; var content = obj.renderCommentWithReplies(data.comments, data.replies); // mostra il visualizza ancora se ci sono altri commenti da visualizzare if (obj.options.commentsPerPage < parseInt(data.total)) { $(obj.options.target).find('.viewMore').show(); } else { $(obj.options.target).find('.viewMore').hide(); } // posto e mostro i commenti $(obj.options.target).find('.commentsList').html(content); $(obj.options.target).find('.totalComments').html(data.total).parent('.commentsInfo').show(); obj.hideLoading(); // evento al click numero risposte $(obj.options.target).find('.btnShowReplies').unbind('click').click(function (){ var parentId = $(this).attr('data-comment-id'); if ($(this).find('.frec').hasClass('close')) { $(this).find('.frec').removeClass('close').addClass('open'); $('#comment'+parentId).find('.replies').show(); } else { $(this).find('.frec').removeClass('open').addClass('close'); $('#comment'+parentId).find('.replies').hide(); } }); // evento al click link rispondi $(obj.options.target).find('.btnReply').unbind('click').click(function (){ $(this).parent().next().next('.commentsWriteMessage').toggle(); }); // evento al click invia risposta $(obj.options.target).find('.sendComment').unbind('click').click(function (){ var button = $(this); button.attr('disabled', 'disabled'); var params = {}; params.parentId = $(this).attr('data-comment-id'); var textareaComment = $('#comment'+params.parentId).find('.commentContent'); var successMessage = $('#comment'+params.parentId).find('.successMessage'); params.body = textareaComment.val(); obj.send(params, function (data) { textareaComment.val(''); successMessage.show(); button.removeAttr('disabled'); setTimeout(function () { successMessage.fadeOut() }, 3000); }); }); // evento al click dei bottoni voti obj.eventOnVoteClick(); // evento al click visualizza ancora $(obj.options.target).find('.viewMore').unbind('click').click(function (){ obj.options.commentsPerPage += 10; if ((obj.options.commentsPerPage + 10) > data.total) { $(this).hide(); } obj.loadComments(type); }); // evento al click dei tabs $(obj.options.target).find('.tabsBar a').unbind('click').click(function (){ var type = $(this).attr('data-type'); $(obj.options.target).find('.tabsBar a').removeClass('active'); $(this).addClass('active'); obj.loadComments(type); }); }, getComments: function (params, type, callback) { var obj = this; var data = $.extend({ isPublic: 1, parentId: null }, params); var obj = this; $.ajax({ url: obj.options.apiEndpoint+"/comments-with-replies", type: "GET", data: data, dataType: 'json', success: function (data) { callback(data, type); }, error: function (error) { console.log("Errore caricamento commenti"); //console.log(error); } }); }, showLoading: function () { var obj = this; $(obj.options.target).find('.loading').show(); }, hideLoading: function () { var obj = this; $(obj.options.target).find('.loading').hide(); }, renderCage: function() { var obj = this; var html = ''+ ''+ '
'+ '
'+ '
'+ ' commenti'+ '
'+ '
'+ '
'+ '   Visualizza ancora   '+ '
'+ '
'; $(obj.options.target).html(html); }, renderComment: function (comments, type) { var obj = this; var html = ''; $.each(comments, function (k, comment) { if (type=='reply') { html += obj.getHtmlReplies({1:comment}); } else { html += obj.getHtmlComment(comment, ""); } }); if (html == '') { if (type == 'reply') html = '

Nessuna risposta.

'; else html = '

Attualmente non ci sono commenti, scrivi tu il primo!

'; } return html; }, getFormattedDate: function (date) { var date = new Date(date); var day = date.getDate(); day = (day < 10) ? '0' + day : day; var month = date.getMonth() + 1; month = (month < 10) ? '0' + month : month; var year = date.getFullYear(); var hour = date.getHours(); hour = (hour < 10) ? '0' + hour : hour; var minute = date.getMinutes(); minute = (minute < 10) ? '0' + minute : minute; var dateFormatted = hour + ':' + minute + ' - ' + day + '/' + month + '/' + year; return dateFormatted; }, renderCommentWithReplies: function (comments, replies) { var obj = this; var html = ''; $.each(comments, function (k, comment) { var commentReplies = null; if ('undefined' !== typeof replies['comment_'+comment.id]) { commentReplies = replies['comment_'+comment.id]; } htmlReplies = obj.getHtmlReplies(commentReplies) html += obj.getHtmlComment(comment, htmlReplies); }); if (html == '') { html = '

Attualmente non ci sono commenti, scrivi tu il primo!

'; } return html; }, getHtmlReplies: function (commentReplies) { var obj = this; var htmlReplies = ""; if (commentReplies) { for (replyIndex in commentReplies) { var reply = commentReplies[replyIndex]; var dateFormatted = obj.getFormattedDate(reply.createdAt); htmlReplies += '' + '
' + '
' + '
' + '
' + '' + reply.nickname + '' + '• ' + dateFormatted + '' + '
' + '
' + reply.voteUp + '
' + '' + '
' + reply.voteDown + '
' + '' + '
' + '
' + '
' + reply.bodyCensored + '
' + '
' + '
'; } } return htmlReplies; }, getHtmlComment: function (comment, htmlReplies) { var obj = this; var repliesStr = (comment.replies == 1) ? '1 risposta' : comment.replies + ' risposte'; var replyStr = (obj.options.userId) ? '- Rispondi' : ''; var dateFormatted = obj.getFormattedDate(comment.createdAt); var html = '' + '
' + '
' + '
' + '
' + '' + comment.nickname + '' + '• ' + dateFormatted + '' + '
' + '
' + comment.voteUp + '
' + '' + '
' + comment.voteDown + '
' + '' + '
' + '
' + '
' + comment.bodyCensored + '
' + '
' + (comment.replies ? '' + ' ' + repliesStr + '' : repliesStr ) + replyStr + '
' + '
' + htmlReplies + '
' + '
' + '
Grazie per averci inviato il tuo commento. E\' stato ricevuto e verrà valutato. Se sarà ritenuto pubblicabile sarà online a breve. Il sistema non accetta pubblicazioni ripetute dello stesso commento, ti chiediamo quindi di evitare di inviarlo nuovamente.
' + '' + '
' + '' + '
' + '
' + '
' + '
'; return html; }, eventOnVoteClick: function () { var obj = this; $(obj.options.target).find('.choice .up, .choice .down').unbind('click').click(function () { var commentId = $(this).attr('data-comment-id'); var choice = $(this).attr('data-choice'); var button = $(this); obj.sendVote(commentId, choice, function () { var num = parseInt(button.prev().text()); num++; button.prev().html(num); }); }); }, sendVote: function (commentId, choice, callback) { var obj = this; obj.showLoading(); $.ajax({ url: obj.options.apiEndpoint+"/comments/"+commentId+"/votes", type: "POST", data: { choice: choice }, crossDomain: true, dataType: 'json', success: function (data) { obj.hideLoading(); callback(data); }, error: function (error, textStatus, errorThrown) { obj.hideLoading(); var res = $.parseJSON(error.responseText); if (res.errorMessage) alert(res.errorMessage); else alert('Ci scusiamo per il disagio ma ci sono problemi di connessione al server. Ti preghiamo di riprovare!'); } }); }, getTotals: function (elementsList, callback) { var dataList = elementsList.join(','); var obj = this; if (dataList.length) { $.ajax({ url: obj.options.apiEndpoint+"/comments/totals", type: "GET", data: { elements: dataList }, crossDomain: true, dataType: 'json', success: function (data) { callback(data); }, error: function (error) { console.log("Errore caricamento conteggio commenti"); //console.log(error); } }); } } } var ranking = { client: null, options: { numberOfItems: 5, showPosition: false }, init: function(cmCommunity, options) { this.client = cmCommunity; this.options.numberOfItems = typeof options.numberOfItems !== 'undefined'? options.numberOfItems : 5; this.options.showPosition = typeof options.showPosition !== 'undefined'? options.showPosition : false; }, templates: { rank: function (options, data){ return ''+ '
'+ (data.team?'
':'')+ '
'+ (options.showPosition ? '
'+data.position+'°
' : '') + '
'+ '
'+ '
'+data.votes_up+'
'+ '
'+data.votes_down+'
'+ '
'+ '
'+ ''+ ''+data.nickname+''+ ''+ '
'+ '
'+ ''+data.nickname+''+ '
'+ '
'; } }, render: function(template, data) { return this.templates[template](this.options, data); }, loadRanks: function(type, callback, callbackKO) { var endpoint = ""; if (type == 'general') { endpoint = 'users/best-commentators'; } else { endpoint = 'users/week-best-commentators'; } var tt = type; var cb = function (result) { return callback(tt, result); }; this.client.apiRequest(endpoint, 'GET', {'n':this.options.numberOfItems }, cb, callbackKO); }, showLoading: function (area) { $('.' + area).find('.loading').show(); }, hideLoading: function (area) { $('.' + area).find('.loading').hide(); } };