Commit b9795596 by cahrens

Warn user when going to XML Editor. Cleanup and comments.

parent c7feee5b
<%include file="metadata-edit.html" /> <%include file="metadata-edit.html" />
<section class="problem-editor editor"> <section class="problem-editor editor">
<div class="row"> <div class="row">
%if markdown != '' or data == '<problem>\n</problem>\n':
<div class="editor-bar"> <div class="editor-bar">
<ul class="format-buttons"> <ul class="format-buttons">
<li><a href="#" class="multiple-choice-button" data-tooltip="Multiple Choice"><span <li><a href="#" class="multiple-choice-button" data-tooltip="Multiple Choice"><span
...@@ -16,11 +15,11 @@ ...@@ -16,11 +15,11 @@
class="problem-editor-icon dropdown"></span></a></li> class="problem-editor-icon dropdown"></span></a></li>
</ul> </ul>
<ul class="editor-tabs"> <ul class="editor-tabs">
<li><a href="#" class="simple-tab tab current" data-tab="simple">Simple</a></li> <li><a href="#" class="xml-tab tab" data-tab="xml">Edit Source XML</a></li>
<li><a href="#" class="xml-tab tab" data-tab="xml">XML</a></li>
<li><a href="#" class="cheatsheet-toggle" data-tooltip="Toggle Cheatsheet">?</a></li> <li><a href="#" class="cheatsheet-toggle" data-tooltip="Toggle Cheatsheet">?</a></li>
</ul> </ul>
</div> </div>
%if markdown != '' or data == '<problem>\n</problem>\n':
<textarea class="markdown-box">${markdown}</textarea> <textarea class="markdown-box">${markdown}</textarea>
%endif %endif
<textarea class="xml-box" rows="8" cols="40">${data | h}</textarea> <textarea class="xml-box" rows="8" cols="40">${data | h}</textarea>
......
...@@ -6,9 +6,10 @@ class @MarkdownEditingDescriptor extends XModule.Descriptor ...@@ -6,9 +6,10 @@ class @MarkdownEditingDescriptor extends XModule.Descriptor
@selectTemplate: "[[incorrect, (correct), incorrect]]\n" @selectTemplate: "[[incorrect, (correct), incorrect]]\n"
constructor: (element) -> constructor: (element) ->
$body.on('click', '.editor-tabs .tab', @changeEditor) @element = element
$body.on('click', '.editor-bar a', @onToolbarButton); @element.on('click', '.xml-tab', @showXMLEditor)
$body.on('click', '.cheatsheet-toggle', @toggleCheatsheet); @element.on('click', '.format-buttons a', @onToolbarButton);
@element.on('click', '.cheatsheet-toggle', @toggleCheatsheet);
@xml_editor = CodeMirror.fromTextArea($(".xml-box", element)[0], { @xml_editor = CodeMirror.fromTextArea($(".xml-box", element)[0], {
mode: "xml" mode: "xml"
...@@ -17,28 +18,50 @@ class @MarkdownEditingDescriptor extends XModule.Descriptor ...@@ -17,28 +18,50 @@ class @MarkdownEditingDescriptor extends XModule.Descriptor
}) })
@current_editor = @xml_editor @current_editor = @xml_editor
if $(".markdown-box", element).length != 0 if $(".markdown-box", @element).length != 0
@markdown_editor = CodeMirror.fromTextArea($(".markdown-box", element)[0], { @markdown_editor = CodeMirror.fromTextArea($(".markdown-box", element)[0], {
lineWrapping: true lineWrapping: true
mode: null mode: null
}) })
@setCurrentEditor(@markdown_editor) @setCurrentEditor(@markdown_editor)
else
@hideMarkdownElements()
###
Hides the toolbar buttons, as they only apply to the markdown editor.
###
hideMarkdownElements: () ->
$(@element.find('.editor-bar')).hide()
$(@element.find('.editor-tabs')).hide()
changeEditor: (e) => ###
User has clicked to show the XML editor. Before XML editor is swapped in,
the user will need to confirm the one-way conversion.
###
showXMLEditor: (e) =>
e.preventDefault(); e.preventDefault();
if not $(e.currentTarget).hasClass('current') if @confirmConversionToXml()
$('.editor-tabs .current').removeClass('current') @xml_editor.setValue(MarkdownEditingDescriptor.markdownToXml(@markdown_editor.getValue()))
$(e.currentTarget).addClass('current') @setCurrentEditor(@xml_editor)
if (@current_editor == @xml_editor) # Need this to get line numbers to display properly (and put caret position to 0)
@setCurrentEditor(@markdown_editor) @xml_editor.setCursor(0)
else @xml_editor.refresh()
@setCurrentEditor(@xml_editor) @hideMarkdownElements()
@xml_editor.setValue(MarkdownEditingDescriptor.markdownToXml(@markdown_editor.getValue()))
###
Have the user confirm the one-way conversion to XML.
Returns true if the user clicked OK, else false.
###
confirmConversionToXml: ->
# TODO: use something besides a JavaScript confirm dialog?
return confirm("If you convert to the XML source representation, you cannot go back to using markdown.\n\nProceed with conversion to XML?")
###
Event listener for toolbar buttons (only possible when markdown editor is visible).
###
onToolbarButton: (e) => onToolbarButton: (e) =>
e.preventDefault(); e.preventDefault();
selection = @markdown_editor.getSelection() selection = @markdown_editor.getSelection()
revisedSelection = null revisedSelection = null
switch $(e.currentTarget).attr('class') switch $(e.currentTarget).attr('class')
when "multiple-choice-button" then revisedSelection = MarkdownEditingDescriptor.insertMultipleChoice(selection) when "multiple-choice-button" then revisedSelection = MarkdownEditingDescriptor.insertMultipleChoice(selection)
...@@ -46,33 +69,41 @@ class @MarkdownEditingDescriptor extends XModule.Descriptor ...@@ -46,33 +69,41 @@ class @MarkdownEditingDescriptor extends XModule.Descriptor
when "number-button" then revisedSelection = MarkdownEditingDescriptor.insertNumberInput(selection) when "number-button" then revisedSelection = MarkdownEditingDescriptor.insertNumberInput(selection)
when "checks-button" then revisedSelection = MarkdownEditingDescriptor.insertCheckboxChoice(selection) when "checks-button" then revisedSelection = MarkdownEditingDescriptor.insertCheckboxChoice(selection)
when "dropdown-button" then revisedSelection = MarkdownEditingDescriptor.insertSelect(selection) when "dropdown-button" then revisedSelection = MarkdownEditingDescriptor.insertSelect(selection)
else # do nothing else # ignore click
if revisedSelection != null if revisedSelection != null
@markdown_editor.replaceSelection(revisedSelection) @markdown_editor.replaceSelection(revisedSelection)
@markdown_editor.focus() @markdown_editor.focus()
###
Event listener for toggling cheatsheet (only possible when markdown editor is visible).
###
toggleCheatsheet: (e) => toggleCheatsheet: (e) =>
e.preventDefault(); e.preventDefault();
if !$(@markdown_editor.getWrapperElement()).find('.simple-editor-cheatsheet')[0]
# TODO: don't base off of current_editor
if !$(@current_editor.getWrapperElement()).find('.simple-editor-cheatsheet')[0]
@cheatsheet = $($('#simple-editor-cheatsheet').html()) @cheatsheet = $($('#simple-editor-cheatsheet').html())
$(@current_editor.getWrapperElement()).append(@cheatsheet) $(@markdown_editor.getWrapperElement()).append(@cheatsheet)
setTimeout (=> @cheatsheet.toggleClass('shown')), 10 setTimeout (=> @cheatsheet.toggleClass('shown')), 10
###
Stores the current editor and hides the one that is not displayed.
###
setCurrentEditor: (editor) -> setCurrentEditor: (editor) ->
$(@current_editor.getWrapperElement()).hide() $(@current_editor.getWrapperElement()).hide()
@current_editor = editor @current_editor = editor
$(@current_editor.getWrapperElement()).show() $(@current_editor.getWrapperElement()).show()
$(@current_editor).focus(); $(@current_editor).focus();
###
Called when save is called. Listeners are unregistered because editing the block again will
result in a new instance of the descriptor. Note that this is NOT the case for cancel--
when cancel is called the instance of the descriptor is reused if edit is selected again.
###
save: -> save: ->
$body.off('click', '.editor-tabs .tab', @changeEditor) @element.off('click', '.xml-tab', @changeEditor)
$body.off('click', '.editor-bar a', @onToolbarButton) @element.off('click', '.format-buttons a', @onToolbarButton)
$body.off('click', '.cheatsheet-toggle', @toggleCheatsheet) @element.off('click', '.cheatsheet-toggle', @toggleCheatsheet)
# TODO when logic is in place to remove the markdown if xml is edited, ensure this doesn't overwrite that
if @current_editor == @markdown_editor if @current_editor == @markdown_editor
{ {
data: MarkdownEditingDescriptor.markdownToXml(@markdown_editor.getValue()) data: MarkdownEditingDescriptor.markdownToXml(@markdown_editor.getValue())
...@@ -80,7 +111,11 @@ class @MarkdownEditingDescriptor extends XModule.Descriptor ...@@ -80,7 +111,11 @@ class @MarkdownEditingDescriptor extends XModule.Descriptor
markdown: @markdown_editor.getValue() markdown: @markdown_editor.getValue()
} }
else else
data: @xml_editor.getValue() {
data: @xml_editor.getValue()
metadata:
markdown: null
}
@insertMultipleChoice: (selectedText) -> @insertMultipleChoice: (selectedText) ->
return MarkdownEditingDescriptor.insertGenericChoice(selectedText, '(', ')', MarkdownEditingDescriptor.multipleChoiceTemplate) return MarkdownEditingDescriptor.insertGenericChoice(selectedText, '(', ')', MarkdownEditingDescriptor.multipleChoiceTemplate)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment