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 = ''+ '
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 += '' + '