diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index ee20348..17e316f 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -8,7 +8,7 @@ @note = Note.new @note.user_id = params[:note][:user_id] - # create answer if we dont already have one + # create answer if we don't have one already if params[:note][:answer_id].present? @answer = Answer.find(params[:note][:answer_id]) else diff --git a/app/views/notes/_archive.html.erb b/app/views/notes/_archive.html.erb index ec8c78a..b58ba1e 100644 --- a/app/views/notes/_archive.html.erb +++ b/app/views/notes/_archive.html.erb @@ -6,7 +6,7 @@

<%= _('Are you sure you want to remove this note?')%>

- <%= f.button(_('Destroy'), class: "btn btn-default", type: "submit") %> + <%= f.button(_('Delete'), class: "btn btn-default", type: "submit") %> <%= f.button(_('Cancel'), class: "btn btn-default", type: "button") %>
<% end %> diff --git a/app/views/notes/_edit.html.erb b/app/views/notes/_edit.html.erb index dbbcf4e..2689639 100644 --- a/app/views/notes/_edit.html.erb +++ b/app/views/notes/_edit.html.erb @@ -3,8 +3,11 @@ <%= form_for(note, url: note_path(note), method: :put) do |f| %>
<%= f.label(:text, _('Edit comment to share with collaborators')) %> - <%= f.text_area(:text, class: 'form-control note', id: "note-#{note.id}") %> + <%= f.text_area(:text, class: 'form-control', id: "note-#{note.id}") %>
- <%= f.button(_('Update'), class: "btn btn-default", type: "submit") %> +
+ <%= f.button(_('Save'), class: "btn btn-default", type: "submit") %> + <%= f.button(_('Cancel'), class: "btn btn-default", type: "button") %> +
<% end %> <% end %> \ No newline at end of file diff --git a/app/views/notes/_layout.html.erb b/app/views/notes/_layout.html.erb index 3b541ce..5af088d 100644 --- a/app/views/notes/_layout.html.erb +++ b/app/views/notes/_layout.html.erb @@ -2,20 +2,21 @@ <% notes = answer.non_archived_notes %>
-
- <%= link_to(_('Add a Comment'), "#note_new#{question.id}", class: "btn btn-default note_new_link", role: "button") %> -
-
-
-
-
<%= render partial: "/notes/list", locals: {question_id: question.id, notes: notes, plan: plan}, formats: [:html] %>
-
" style="display: none"> +
"> <%= render partial: "/notes/new", locals: { question: question, answer: answer, plan: plan }, formats: [:html] %>
-
\ No newline at end of file +
+
+
+
+ <%= link_to(_('Add Comment'), "#note_new#{question.id}", class: "btn btn-default note_new_link", role: "button", style: "visibility: hidden") %> +
+
+
+ \ No newline at end of file diff --git a/app/views/notes/_list.html.erb b/app/views/notes/_list.html.erb index 7d5bf7e..bf330d6 100644 --- a/app/views/notes/_list.html.erb +++ b/app/views/notes/_list.html.erb @@ -15,10 +15,10 @@
  • <%= link_to(_('Show'), "#note_show#{note.id}", class: 'note_show_link') %>
  • <% if current_user.id == note.user_id %>
  • <%= link_to(_('Edit'), "#note_edit#{note.id}", class: 'note_edit_link') %>
  • -
  • <%= link_to(_('Remove'), "#note_archive#{note.id}", class: 'note_archive_link') %>
  • +
  • <%= link_to(_('Delete'), "#note_archive#{note.id}", class: 'note_archive_link') %>
  • <% else %> <% if plan.administerable_by?(current_user.id) %> -
  • <%= link_to(_('Remove'), "#note_archive#{note.id}", class: 'note_archive_link') %>
  • +
  • <%= link_to(_('Delete'), "#note_archive#{note.id}", class: 'note_archive_link') %>
  • <% end %> <% end %> diff --git a/app/views/notes/_new.html.erb b/app/views/notes/_new.html.erb index 502740b..378bfb7 100644 --- a/app/views/notes/_new.html.erb +++ b/app/views/notes/_new.html.erb @@ -8,5 +8,7 @@ <%= f.label(:text, _('Add comments to share with collaborators')) %> <%= f.text_area(:text, class: 'form-control note', id: "note-#{question.id}") %> - <%= f.button(_('Create'), class: "btn btn-default", type: "submit") %> +
    + <%= f.button(_('Save'), class: "btn btn-default", type: "submit") %> +
    <% end %> \ No newline at end of file diff --git a/lib/assets/javascripts/views/notes/index.js b/lib/assets/javascripts/views/notes/index.js index 1259cfe..1c2ce5f 100644 --- a/lib/assets/javascripts/views/notes/index.js +++ b/lib/assets/javascripts/views/notes/index.js @@ -2,7 +2,6 @@ import { isObject, isString } from '../../utils/isType'; $(() => { - let currentViewSelector = null; const success = (data) => { if (isObject(data) && isObject(data.notes) && @@ -22,33 +21,84 @@ }; const getAction = jQueryForm => jQueryForm.attr('action'); const getMethod = jQueryForm => jQueryForm.attr('method'); - const noteNewLinkHandler = (e) => { - $(e.target).css('visibility', 'hidden'); - if (currentViewSelector) { - $(currentViewSelector).hide(); - } - currentViewSelector = $(e.target).attr('href'); - $(currentViewSelector).show(); + const getCurrentViewSelector = el => $(el).closest('.notes').find('.currentViewSelector').html(); + const setCurrentViewSelector = (el, value) => { + $(el).closest('.notes').find('.currentViewSelector').html(value); }; - const noteOtherLinkHandler = (e) => { - $(e.target).closest('.notes').find('.note_new_link').css('visibility', 'visible'); - if (currentViewSelector) { - $(currentViewSelector).hide(); + const destroyCurrentViewEditor = (el) => { + const id = $(el).find('textarea').attr('id'); + if (id) { + Tinymce.destroyEditorById(id); } - currentViewSelector = $(e.target).attr('href'); - $(currentViewSelector).show(); + }; + const noteNewLinkHandler = (e) => { + const currentViewSelector = getCurrentViewSelector(e.target); + const target = $(e.target).attr('href'); + if (currentViewSelector !== target) { + $(currentViewSelector) + .hide({ complete: () => destroyCurrentViewEditor($(currentViewSelector)) }); + setCurrentViewSelector(e.target, target); + $(e.target).css('visibility', 'hidden'); + $(target).show(); + Tinymce.init({ selector: `#${$(target).find('textarea').attr('id')}` }); + } + }; + const noteShowLinkHandler = (e) => { + const currentViewSelector = getCurrentViewSelector(e.target); + const target = $(e.target).attr('href'); + if (currentViewSelector !== target) { + $(currentViewSelector) + .hide({ complete: () => destroyCurrentViewEditor($(currentViewSelector)) }); + setCurrentViewSelector(e.target, target); + $(e.target).closest('.notes').find('.note_new_link').css('visibility', 'visible'); + $(target).show(); + } + }; + const noteEditLinkHandler = (e) => { + const currentViewSelector = getCurrentViewSelector(e.target); + const target = $(e.target).attr('href'); + if (currentViewSelector !== target) { + $(currentViewSelector) + .hide({ complete: () => destroyCurrentViewEditor($(currentViewSelector)) }); + setCurrentViewSelector(e.target, target); + $(e.target).closest('.notes').find('.note_new_link').css('visibility', 'hidden'); + $(target).show(); + Tinymce.init({ selector: `#${$(target).find('textarea').attr('id')}` }); + } + }; + const noteArchiveLinkHandler = (e) => { + const currentViewSelector = getCurrentViewSelector(e.target); + const target = $(e.target).attr('href'); + if (currentViewSelector !== target) { + $(currentViewSelector) + .hide({ complete: () => destroyCurrentViewEditor($(currentViewSelector)) }); + setCurrentViewSelector(e.target, target); + $(e.target).closest('.notes').find('.note_new_link').css('visibility', 'hidden'); + $(target).show(); + } }; const newEditNoteHandler = (e) => { e.preventDefault(); const jQueryForm = $(e.target).closest('form'); const formElements = jQueryForm.serializeArray(); const noteText = formElements.find(el => el.name === 'note[text]'); - noteText.value = Tinymce.findEditorById($(e.target).closest('form').find('[name="note[text]"]').attr('id')).getContent(); + const id = $(e.target).closest('form').find('[name="note[text]"]').attr('id'); + noteText.value = Tinymce.findEditorById(id).getContent(); $.ajax({ method: getMethod(jQueryForm), url: getAction(jQueryForm), data: formElements, - }).done(success, error); + }).done((data) => { + success(data); + Tinymce.destroyEditorById(id); + }, error); + }; + const editNoteCancelHandler = (e) => { + const currentViewSelector = getCurrentViewSelector(e.target); + $(currentViewSelector) + .hide({ complete: () => destroyCurrentViewEditor($(currentViewSelector)) }); + $(e.target).closest('.notes').find('.note_new_link').css('visibility', 'visible'); + setCurrentViewSelector(e.target, ''); }; const archiveNoteDestroyHandler = (e) => { e.preventDefault(); @@ -60,16 +110,21 @@ data: formElements, }).done(success, error); }; - const archiveNoteCancelHandler = () => { - if (currentViewSelector) { - $(currentViewSelector).hide(); - } + const archiveNoteCancelHandler = (e) => { + const currentViewSelector = getCurrentViewSelector(e.target); + $(currentViewSelector) + .hide({ complete: () => destroyCurrentViewEditor($(currentViewSelector)) }); + $(e.target).closest('.notes').find('.note_new_link').css('visibility', 'visible'); + setCurrentViewSelector(e.target, ''); }; const eventHandlers = ({ attachment = 'off' }) => { $('.notes .note_new_link')[attachment]('click', noteNewLinkHandler); - $('.notes .note_show_link, .notes .note_edit_link, .notes .note_archive_link')[attachment]('click', noteOtherLinkHandler); + $('.notes .note_show_link')[attachment]('click', noteShowLinkHandler); + $('.notes .note_edit_link')[attachment]('click', noteEditLinkHandler); + $('.notes .note_archive_link')[attachment]('click', noteArchiveLinkHandler); $('.new_note')[attachment]('submit', newEditNoteHandler); $('.edit_note')[attachment]('submit', newEditNoteHandler); + $('.edit_note button[type="button"]')[attachment]('click', editNoteCancelHandler); $('.archive_note')[attachment]('submit', archiveNoteDestroyHandler); $('.archive_note button[type="button"]')[attachment]('click', archiveNoteCancelHandler); }; @@ -78,7 +133,6 @@ eventHandlers({ attachment: 'on' }); }; const clean = () => { - currentViewSelector = null; eventHandlers({ attachment: 'off' }); Tinymce.destroyEditorsByClassName('note'); };