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 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 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 30 jQuery(document).ready(function($) {
6 31 rangy.init();
7 32 cssApplier = rangy.createCssClassApplier("commented-area", {normalize: false});
8 33 //Undo previous highlight from the paragraph
9 34 $('.comment_paragraph').mousedown(function(){
  35 + var paragraphId = getIdCommentParagraph($(this)[0].id);
10 36 $(this).find('.commented-area').replaceWith(function() {
11 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 45 //highlight area from the paragraph
20 46 $('.comment_paragraph').mouseup(function(){
  47 + var paragraphId = getIdCommentParagraph($(this)[0].id);
21 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 54 var selObj = rangy.getSelection();
28 55 var selected_area = rangy.serializeSelection(selObj, true,rootElement);
29 56  
30 57 cssApplier.toggleSelection();
31 58  
32   - ParagraphSelectionCache.last_selected_area = selected_area;
  59 + lastSelectedArea[paragraphId] = selected_area;
33 60 //cssApplier.toggleSelection();
34 61  
35 62 form = jQuery(this).parent().find('form');
... ... @@ -85,12 +112,12 @@ jQuery(document).ready(function($) {
85 112 var paragraph_id = $(this).find('input.paragraph_id').val();
86 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 116 console.log(rootElement.innerHTML);
90   - ParagraphSelectionCache.last_paragraph = rootElement.innerHTML;
  117 + lastParagraph[paragraph_id] = rootElement.innerHTML;
91 118 }
92 119 else {
93   - rootElement.innerHTML = ParagraphSelectionCache.last_paragraph ;
  120 + rootElement.innerHTML = lastParagraph[paragraph_id] ;
94 121 }
95 122  
96 123 //console.log(rootElement.innerHTML);
... ... @@ -102,17 +129,17 @@ jQuery(document).ready(function($) {
102 129 $(document).on('mouseout', 'li.article-comment', function(){
103 130 var paragraph_id = $(this).find('input.paragraph_id').val();
104 131 var rootElement = $('#comment_paragraph_'+ paragraph_id).get(0);
105   - console.log(ParagraphSelectionCache.last_paragraph);
  132 + console.log(lastParagraph[paragraph_id]);
106 133  
107 134 // cssApplier.undoToSelection();
108 135 //
109 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 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 143 cssApplier.toggleSelection();
117 144 } else {
118 145 cssApplier.toggleSelection();
... ...