comment_paragraph_macro.js
3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var comment_paragraph_anchor;
jQuery(document).ready(function($) {
rangy.init();
cssApplier = rangy.createCssClassApplier("commented-area", {normalize: false});
//Undo previous highlight from the paragraph
$('.comment_paragraph').mousedown(function(){
$(this).find('.commented-area').replaceWith(function() {
return $(this).html();
});
});
//highlight area from the paragraph
$('.comment_paragraph').mouseup(function(){
rootElement = $(this).get(0);
console.log(rootElement)
cssApplier.toggleSelection();
var selObj = rangy.getSelection();
var selected_area = rangy.serializeSelection(selObj, true,rootElement);
form = jQuery(this).parent().find('form');
if (form.find('input.selected_area').length === 0){
jQuery('<input>').attr({
class: 'selected_area',
type: 'hidden',
name: 'comment[comment_paragraph_selected_area]',
value: selected_area
}).appendTo(form)
}else{
form.find('input.selected_area').val(selected_area)
}
rootElement.focus();
});
// em <li id="comment-31" class="article-comment"> colocar um data-paragraph e data-selected-area
//highlight area from the paragraph
$('.article-comment').mouseover(function(){
rootElement = $('#comment_paragraph_' + this).get(0);
var selObj = rangy.getSelection();
var se = $('#result').val();
rangy.deserializeSelection(se, rootElement);
cssApplier.toggleSelection();
});
function processSomething(){
var anchor = window.location.hash;
if(anchor.length==0) return;
var val = anchor.split('-'); //anchor format = #comment-\d+
if(val.length!=2 || val[0]!='#comment') return;
if($('div[data-macro=comment_paragraph_plugin/allow_comment]').length==0) return; //comment_paragraph_plugin/allow_comment div must exists
var comment_id = val[1];
if(!/^\d+$/.test(comment_id)) return; //test for integer
comment_paragraph_anchor = anchor;
var url = '/plugin/comment_paragraph/public/comment_paragraph/'+comment_id;
$.getJSON(url, function(data) {
if(data.paragraph_id!=null) {
var button = $('div.comment_paragraph_'+ data.paragraph_id + ' a');
button.click();
$.scrollTo(button);
}
});
}
processSomething();
});
function selectAreaForComment(paragraph){
// console.log(this)
// alert("Paragrafo " + paragraph)
// cssApplier.toggleSelection();
// saveSelection();
// var selObj = rangy.getSelection();
// var se = rangy.serializeSelection(selObj, true,rootElement);
}
function toggleParagraph(paragraph) {
var div = jQuery('div.comments_list_toggle_paragraph_'+paragraph);
var visible = div.is(':visible');
if(!visible)
jQuery('div.comment-paragraph-loading-'+paragraph).addClass('comment-button-loading');
div.toggle('fast');
return visible;
}
function loadCompleted(paragraph) {
jQuery('div.comment-paragraph-loading-'+paragraph).removeClass('comment-button-loading')
if(comment_paragraph_anchor) {
jQuery.scrollTo(jQuery(comment_paragraph_anchor));
comment_paragraph_anchor = null;
}
}
//Return a string with the beggining and the end of the selection of a text area separated by colon
function getSelectionBounderies(textareaId){
// var textarea = document.getElementById(textareaId);
// if ('selectionStart' in textarea) {
// // check whether some text is selected in the textarea
// if (textarea.selectionStart != textarea.selectionEnd) {
// alert(textarea.selectionStart + ":" + textarea.selectionEnd)
// return textarea.selectionStart + ":" + textarea.selectionEnd;
// }
// }
return false
}