Commit f242d829f69c4c8903f0e32fd2e3c11b95fb728c

Authored by Evandro Jr
1 parent ca7c5b78
Exists in master

selection working, need to clean to code

Showing 1 changed file with 41 additions and 14 deletions   Show diff stats
public/comment_paragraph_macro.js
1 var comment_paragraph_anchor; 1 var comment_paragraph_anchor;
2 2
  3 +function _a(x,y,val){
  4 + // return depending on parameters
  5 + switch(arguments.length){
  6 + case 0: return _a.a;
  7 + case 1: return _a.a[x];
  8 + case 2: return _a.a[x][y];
  9 + }
  10 +
  11 + // declare array if wasn't declared yet
  12 + if(typeof _a.a[x] == 'undefined')
  13 + _a.a[x] = [];
  14 +
  15 + _a.a[x][y] = val;
  16 +}
  17 +// declare static empty variable
  18 +_a.a = [];
  19 +
3 var ParagraphSelectionCache = {}; 20 var ParagraphSelectionCache = {};
  21 +var lastParagraph = [];
  22 +var lastSelectedArea = [];
  23 +
  24 +
  25 +function getIdCommentParagraph(paragraphId){
  26 + var idx = paragraphId.lastIndexOf('_');
  27 + return paragraphId.substring(idx+1, paragraphId.length)
  28 +}
4 29
5 jQuery(document).ready(function($) { 30 jQuery(document).ready(function($) {
6 rangy.init(); 31 rangy.init();
7 cssApplier = rangy.createCssClassApplier("commented-area", {normalize: false}); 32 cssApplier = rangy.createCssClassApplier("commented-area", {normalize: false});
8 //Undo previous highlight from the paragraph 33 //Undo previous highlight from the paragraph
9 $('.comment_paragraph').mousedown(function(){ 34 $('.comment_paragraph').mousedown(function(){
  35 + var paragraphId = getIdCommentParagraph($(this)[0].id);
10 $(this).find('.commented-area').replaceWith(function() { 36 $(this).find('.commented-area').replaceWith(function() {
11 return $(this).html(); 37 return $(this).html();
12 }); 38 });
13 - var rootElement = $(this).get(0);  
14 - if(ParagraphSelectionCache.last_paragraph){  
15 - rootElement.innerHTML = ParagraphSelectionCache.last_paragraph;  
16 - } 39 + var rootElement = $(this).get(0);
  40 + if(lastParagraph[paragraphId]){
  41 + rootElement.innerHTML = lastParagraph[paragraphId];
  42 + }
17 }); 43 });
18 44
19 //highlight area from the paragraph 45 //highlight area from the paragraph
20 $('.comment_paragraph').mouseup(function(){ 46 $('.comment_paragraph').mouseup(function(){
  47 + var paragraphId = getIdCommentParagraph($(this)[0].id);
21 var rootElement = $(this).get(0); 48 var rootElement = $(this).get(0);
22 49
23 - ParagraphSelectionCache.last_paragraph = rootElement.innerHTML; 50 + lastParagraph[paragraphId] = rootElement.innerHTML;
24 51
25 - console.log(rootElement) ; 52 + console.log(rootElement);
26 53
27 var selObj = rangy.getSelection(); 54 var selObj = rangy.getSelection();
28 var selected_area = rangy.serializeSelection(selObj, true,rootElement); 55 var selected_area = rangy.serializeSelection(selObj, true,rootElement);
29 56
30 cssApplier.toggleSelection(); 57 cssApplier.toggleSelection();
31 58
32 - ParagraphSelectionCache.last_selected_area = selected_area; 59 + lastSelectedArea[paragraphId] = selected_area;
33 //cssApplier.toggleSelection(); 60 //cssApplier.toggleSelection();
34 61
35 form = jQuery(this).parent().find('form'); 62 form = jQuery(this).parent().find('form');
@@ -85,12 +112,12 @@ jQuery(document).ready(function($) { @@ -85,12 +112,12 @@ jQuery(document).ready(function($) {
85 var paragraph_id = $(this).find('input.paragraph_id').val(); 112 var paragraph_id = $(this).find('input.paragraph_id').val();
86 var rootElement = $('#comment_paragraph_'+ paragraph_id).get(0); 113 var rootElement = $('#comment_paragraph_'+ paragraph_id).get(0);
87 114
88 - if(ParagraphSelectionCache.last_paragraph == null || ParagraphSelectionCache.last_paragraph == 'undefined'){ 115 + if(lastParagraph[paragraph_id] == null || lastParagraph[paragraph_id] == 'undefined'){
89 console.log(rootElement.innerHTML); 116 console.log(rootElement.innerHTML);
90 - ParagraphSelectionCache.last_paragraph = rootElement.innerHTML; 117 + lastParagraph[paragraph_id] = rootElement.innerHTML;
91 } 118 }
92 else { 119 else {
93 - rootElement.innerHTML = ParagraphSelectionCache.last_paragraph ; 120 + rootElement.innerHTML = lastParagraph[paragraph_id] ;
94 } 121 }
95 122
96 //console.log(rootElement.innerHTML); 123 //console.log(rootElement.innerHTML);
@@ -102,17 +129,17 @@ jQuery(document).ready(function($) { @@ -102,17 +129,17 @@ jQuery(document).ready(function($) {
102 $(document).on('mouseout', 'li.article-comment', function(){ 129 $(document).on('mouseout', 'li.article-comment', function(){
103 var paragraph_id = $(this).find('input.paragraph_id').val(); 130 var paragraph_id = $(this).find('input.paragraph_id').val();
104 var rootElement = $('#comment_paragraph_'+ paragraph_id).get(0); 131 var rootElement = $('#comment_paragraph_'+ paragraph_id).get(0);
105 - console.log(ParagraphSelectionCache.last_paragraph); 132 + console.log(lastParagraph[paragraph_id]);
106 133
107 // cssApplier.undoToSelection(); 134 // cssApplier.undoToSelection();
108 // 135 //
109 // cssApplier.toggleSelection(); 136 // cssApplier.toggleSelection();
110 137
111 138
112 - if(ParagraphSelectionCache.last_selected_area != null && ParagraphSelectionCache.last_selected_area != 'undefined' ){ 139 + if(lastSelectedArea[paragraph_id] != null && lastSelectedArea[paragraph_id] != 'undefined' ){
113 rootElement = $('#comment_paragraph_'+ paragraph_id).get(0); 140 rootElement = $('#comment_paragraph_'+ paragraph_id).get(0);
114 - rootElement.innerHTML = ParagraphSelectionCache.last_paragraph ;  
115 - rangy.deserializeSelection(ParagraphSelectionCache.last_selected_area, rootElement); 141 + rootElement.innerHTML = lastParagraph[paragraph_id];
  142 + rangy.deserializeSelection(lastSelectedArea[paragraph_id], rootElement);
116 cssApplier.toggleSelection(); 143 cssApplier.toggleSelection();
117 } else { 144 } else {
118 cssApplier.toggleSelection(); 145 cssApplier.toggleSelection();