<commit_before>/*
 * Copyright (C) 2011  JTalks.org Team
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
var Utils = {};

function quote(postId, postNumber) {
	var callback = function (text) {
        $('#post').focus();
        console.log(text);
        var answer = $('#postBody');
        answer.focus();
        if (answer) {
            answer.val(answer.val() + text);
        }

    }

    $.ajax({
        url: baseUrl + '/posts/' + postId + '/quote',
        type: 'POST',
        data: {
            selection: getSelectedPostText(postNumber)
        },
        success: function (data) {
            callback(data.result);
        },
        error: function () {
            callback('');
        }
    });
}

function getSelectedPostText(postNumber) {
    var txt = '';
    console.log(postNumber);
    if (window.getSelection && isRangeInPost(window.getSelection().getRangeAt(0)) && isSelectedPostQuoted(postNumber)) {
        txt = window.getSelection().toString();
    }
    else if (document.selection && isRangeInPost(document.selection.createRange()) && isSelectedPostQuoted(postNumber)) {
        txt = document.selection.createRange().text;
    }
    return txt;
}

/**
 * Checks if selected document fragment is a part of the post content.
 * @param {Range} range Range object which represent current selection.
 * @return {boolean} <b>true</b> if if selected document fragment is a part of the post content
 *                   <b>false</b> otherwise.
 */
function isRangeInPost(range) {
    return range.startContainer.parentNode.classList.contains("post-content-body");
}

/**
 * Checks if "quote" button pressed on the post which was selected.
 * @param {Number} postNumber number of the post on the page which "quote" button was pressed.
 * @return {boolean} <b>true</> if selected text is a part of the post which will be quoted
 *                   <b>false</b> otherwise.
 */
function isSelectedPostQuoted(postNumber) {
    console.log("btn" + $(window.getSelection().getRangeAt(0).startContainer).closest('.post').length);
    return $(window.getSelection().getRangeAt(0).startContainer).closest('.post').prevAll().length == postNumber;
}

/**
 * Encodes given string by escaping special HTML characters
 *
 * @param s string to be encoded
 */
Utils.htmlEncode = function (s) {
    return $('<div/>').text(s).html();
};

/**
 * Do focus to element
 *
 * @param target selector of element to focus
 */
Utils.focusFirstEl = function (target) {
    $(target).focus();
}

/**
 * Replaces all \n characters by <br> tags. Used for review comments.<commit_msg>#JC-1853: Move focus to Create topic dialog on quoting: *Debug output replaced<commit_after>/*
 * Copyright (C) 2011  JTalks.org Team
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
var Utils = {};

function quote(postId, postNumber) {
	var callback = function (text) {
        $('#post').focus();
        console.log(text);
        var answer = $('#postBody');
        answer.focus();
        if (answer) {
            answer.val(answer.val() + text);
        }

    }

    $.ajax({
        url: baseUrl + '/posts/' + postId + '/quote',
        type: 'POST',
        data: {
            selection: getSelectedPostText(postNumber)
        },
        success: function (data) {
            callback(data.result);
        },
        error: function () {
            callback('');
        }
    });
}

function getSelectedPostText(postNumber) {
    var txt = '';
    if (window.getSelection && isRangeInPost(window.getSelection().getRangeAt(0)) && isSelectedPostQuoted(postNumber)) {
        txt = window.getSelection().toString();
    }
    else if (document.selection && isRangeInPost(document.selection.createRange()) && isSelectedPostQuoted(postNumber)) {
        txt = document.selection.createRange().text;
    }
    return txt;
}

/**
 * Checks if selected document fragment is a part of the post content.
 * @param {Range} range Range object which represent current selection.
 * @return {boolean} <b>true</b> if if selected document fragment is a part of the post content
 *                   <b>false</b> otherwise.
 */
function isRangeInPost(range) {
    return range.startContainer.parentNode.classList.contains("post-content-body");
}

/**
 * Checks if "quote" button pressed on the post which was selected.
 * @param {Number} postNumber number of the post on the page which "quote" button was pressed.
 * @return {boolean} <b>true</> if selected text is a part of the post which will be quoted
 *                   <b>false</b> otherwise.
 */
function isSelectedPostQuoted(postNumber) {
    return $(window.getSelection().getRangeAt(0).startContainer).closest('.post').prevAll().length == postNumber;
}

/**
 * Encodes given string by escaping special HTML characters
 *
 * @param s string to be encoded
 */
Utils.htmlEncode = function (s) {
    return $('<div/>').text(s).html();
};

/**
 * Do focus to element
 *
 * @param target selector of element to focus
 */
Utils.focusFirstEl = function (target) {
    $(target).focus();
}

/**
 * Replaces all \n characters by <br> tags. Used for review comments.
{'fraction_non_alphanumeric': 0.1189447701326751, 'fraction_numerical': 0.006633755013884603, 'mean_word_length': 3.074795725958517, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6275707', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1745, 'n_words': 641}