|
9 | 9 | <div class="form-group">
|
10 | 10 | <div class="row">
|
11 | 11 | <div class="col">
|
12 |
| - <%= f.text_area :comment, class: "form-control", placeholder: "Include a comment with your vote (required)" %> |
| 12 | + <%= f.text_area :comment, class: "form-control", placeholder: "Include a comment with your vote (required)", |
| 13 | + data: { paper_id: @paper.id }, id: "vote-comment" %> |
13 | 14 | </div>
|
14 | 15 | </div>
|
15 | 16 | </div>
|
|
28 | 29 | </div>
|
29 | 30 | </div>
|
30 | 31 |
|
| 32 | + <script> |
| 33 | + (function() { |
| 34 | + try { |
| 35 | + if (!window.localStorage) { |
| 36 | + console.error('localStorage is not available in this browser'); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + function initializeStorage() { |
| 41 | + const commentField = document.getElementById('vote-comment'); |
| 42 | + if (!commentField) { |
| 43 | + console.error('Could not find comment field with ID vote-comment'); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const paperId = commentField.dataset.paperId; |
| 48 | + if (!paperId) { |
| 49 | + console.error('No paper ID found in data attributes'); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + const storageKey = `vote_comment_${paperId}`; |
| 54 | + |
| 55 | + try { |
| 56 | + const savedComment = localStorage.getItem(storageKey); |
| 57 | + if (savedComment) { |
| 58 | + commentField.value = savedComment; |
| 59 | + } |
| 60 | + } catch (e) { |
| 61 | + console.error('Error accessing localStorage:', e); |
| 62 | + } |
| 63 | + |
| 64 | + commentField.addEventListener('input', function() { |
| 65 | + try { |
| 66 | + localStorage.setItem(storageKey, this.value); |
| 67 | + } catch (e) { |
| 68 | + console.error('Error saving to localStorage:', e); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + const form = commentField.closest('form'); |
| 73 | + if (form) { |
| 74 | + form.addEventListener('submit', function() { |
| 75 | + try { |
| 76 | + localStorage.removeItem(storageKey); |
| 77 | + } catch (e) { |
| 78 | + console.error('Error clearing localStorage:', e); |
| 79 | + } |
| 80 | + }); |
| 81 | + } else { |
| 82 | + console.error('Could not find parent form element'); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (document.readyState === 'complete' || document.readyState === 'interactive') { |
| 87 | + initializeStorage(); |
| 88 | + } |
| 89 | + |
| 90 | + document.addEventListener('DOMContentLoaded', initializeStorage); |
| 91 | + |
| 92 | + } catch (e) { |
| 93 | + console.error('Error in vote comment storage script:', e); |
| 94 | + } |
| 95 | + })(); |
| 96 | + </script> |
| 97 | + |
31 | 98 | <div class="form-group">
|
32 | 99 | <%= link_to "View vote summary »".html_safe, "#voteSummary", class: 'tooltips', title: 'View vote summary', data: { toggle: 'collapse'} %>
|
33 | 100 |
|
|
0 commit comments