File: /var/www/html/wp-content/themes/ganryujima_new_1602/js/discussion.js
/**
* 議論画面のスクリプト
*/
jQuery(function() {
init();
bindEvents();
function init() {
// サイト移行前から持ってきた既存のイベントを削除
jQuery('.comment_user_box').off('click');
jQuery('.add_shiori').off();
jQuery('.remove_shiori').off();
jQuery('.go_to_bottom').off();
jQuery('.back_to_top').off();
var shiori = docCookies.getItem('shiori-' + POST_ID);
if(shiori != null) {
jQuery('.m_single_discussion').addClass('has_shiori');
jQuery('#post_' + shiori + '').addClass('added_shiori');
}
}
function bindEvents() {
jQuery('.post-delete-link').on('click', commentRemoveLinkHasClicked);
jQuery('.comment_user_box').on('click', submitButtonHasClicked);
jQuery('.add_shiori').on('click', addShioriHasClicked);
jQuery('.remove_shiori').on('click', removeShioriHasClicked);
jQuery('#open-shiori').on('click', openShioriHasClicked);
}
/**
* コメントを削除するボタンがクリックされた
*/
function commentRemoveLinkHasClicked() {
if(!window.confirm('削除します、よろしいですか?')) {
return;
}
var commentId = jQuery(this).data('comment-id');
console.log(commentId);
// コメント削除リクエスト
jQuery.ajax(ajaxUrl, {
type: 'POST',
data: {
action: 'delete_comment',
comment_id: commentId
},
error: function() {
console.log('コメント削除時に通信エラーが発生しました');
},
success: function(response) {
alert('コメントを削除しました');
}
});
// 画面から削除
jQuery(this).parent().parent().parent().remove();
}
// 送信バリデーション
function submitButtonHasClicked() {
var comment = jQuery('#post_comment').val();
if(jQuery('#post_comment').val() == '') {
alert('コメントを入力して下さい');
return false;
}
comment = comment.replace(/\r\n/g, '<br>');
comment = comment.replace(/\n|\r/g, '<br>');
jQuery('#post_comment').val(comment);
}
/**
* しおりをつけるがクリックされた
*/
function addShioriHasClicked() {
var commentId = jQuery(this).data('comment-id');
docCookies.setItem('shiori-' + POST_ID, commentId);
// ページング表示の場合はページ番号も保存する
if(DISCUSSION_VIEW == 'paging') {
docCookies.setItem('shiori-page-' + POST_ID, CURRENT_PAGE);
}
jQuery('.m_single_discussion').addClass('has_shiori');
jQuery('.added_shiori').removeClass('added_shiori');
jQuery('#post_' + commentId + '').addClass('added_shiori');
return false;
}
/**
* しおりをはずすがクリックされた
*/
function removeShioriHasClicked() {
var commentId = jQuery(this).data('comment-id');
docCookies.removeItem('shiori-' + POST_ID);
jQuery('.m_single_discussion').removeClass('has_shiori');
jQuery(this).parents('article').removeClass('added_shiori');
return false;
}
/**
* しおりをひらくボタンがクリックされた
*/
function openShioriHasClicked() {
var shioriId = docCookies.getItem('shiori-' + POST_ID);
if(shioriId != null) {
var speed = 500;
var position = jQuery('#post_' + shioriId).offset().top - 56;
if(DISCUSSION_VIEW == 'all') {
jQuery("html, body").animate({scrollTop:position}, speed, "swing");
} else {
var shioriPage = docCookies.getItem('shiori-page-' + POST_ID);
if(shioriPage != null) {
if(shioriPage == CURRENT_PAGE) {
jQuery("html, body").animate({scrollTop:position}, speed, "swing");
} else {
location.href = location.protocol + '//' + location.host + location.pathname + '?paging=' + shioriPage + '#post_' + shioriId;
}
}
}
}
return false;
}
});
function execute() {
target = jQuery("#post_comment").val().match(/(?:^|[\s\u3000]+)((?:https?|ftp):\/\/[^\s\u3000]+)/);
if (target[0] != targetUrl) {
targetUrl = target[0];
doCrawl();
}
}
function doCrawl() {
jQuery("#disp_area").show(), jQuery.ajax({
//FIXME localとパスが違うのでチェック
url: "/open_graph/description.php",
data: {
target: targetUrl
},
dataType: "json",
cache: !0,
success: function(t) {
if(t.title[0]) {
console.log(t.targeturl);
jQuery("#_site_link_area").attr("id", "_site_link").attr("href", t.targeturl);
jQuery("#_title_area").text(t.title);
jQuery("#post_og_title").val(t.title);
jQuery("#post_og_url").val(t.url);
}
if(t.description[0]) {
jQuery("#_description_area").text(t.description);
jQuery("#post_og_description").val(t.description);
}
jQuery.ajax({
//FIXME localとパスが違うのでチェック
url: "/open_graph/image.php",
data: {
target: targetUrl
},
dataType: "json",
cache: !0,
success: function(t) {
t.length > 0 ? (imgData = t, current = 0, previewImg(t)) : (jQuery("#_img").remove(), imgData = null)
},
error: function(t, e) {
//何もしない
},
complete: function(t, e) {
"success" != e && jQuery("#_img").remove()
}
});
},
error: function(t, e) {
//何もしない
},
complete: function(t, e) {
"success" != e && jQuery("#_img").remove()
}
})
}
function previewImg(t) {
jQuery("#_img").attr("src", t);
jQuery("#post_og_thumbnail").val(t);
}
function resetPreview() {
jQuery("#disp_area").hide();
jQuery("#post_og_url").val("");
jQuery("#post_og_title").val("");
jQuery("#post_og_description").val("");
jQuery("#post_og_thumbnail").val("");
targetUrl = "";
}
var imtData, current, targetUrl = "";
jQuery(document).ready(function() {
jQuery("#post_comment").bind("paste", function() {
setTimeout(function() {
execute();
}, 100)
}), jQuery("#post_comment").keypress(function(t) {
return KEY_ENTER = 13, (t.which && t.which === KEY_ENTER || t.keyCode && t.keyCode === KEY_ENTER) && execute(), !0
}), jQuery("#_preview_reset_btn").click(function() {
resetPreview();
});
});