Commit 82b2dc71 by Don Mitchell

Merge pull request #1427 from MITx/feature/christina/misc2

Do not do finds within the event method. Decreases likelihood of hitting...
parents ecce3e57 286616e6
...@@ -10,7 +10,8 @@ class @HTMLEditingDescriptor ...@@ -10,7 +10,8 @@ class @HTMLEditingDescriptor
lineWrapping: true lineWrapping: true
}) })
$(@advanced_editor.getWrapperElement()).addClass(HTMLEditingDescriptor.isInactiveClass) @$advancedEditorWrapper = $(@advanced_editor.getWrapperElement())
@$advancedEditorWrapper.addClass(HTMLEditingDescriptor.isInactiveClass)
# This is a workaround for the fact that tinyMCE's baseURL property is not getting correctly set on AWS # This is a workaround for the fact that tinyMCE's baseURL property is not getting correctly set on AWS
# instances (like sandbox). It is not necessary to explicitly set baseURL when running locally. # instances (like sandbox). It is not necessary to explicitly set baseURL when running locally.
...@@ -43,16 +44,21 @@ class @HTMLEditingDescriptor ...@@ -43,16 +44,21 @@ class @HTMLEditingDescriptor
theme_advanced_blockformats : "p,pre,h1,h2,h3", theme_advanced_blockformats : "p,pre,h1,h2,h3",
width: '100%', width: '100%',
height: '400px', height: '400px',
setup : HTMLEditingDescriptor.setupTinyMCE, setup : @setupTinyMCE,
# Cannot get access to tinyMCE Editor instance (for focusing) until after it is rendered. # Cannot get access to tinyMCE Editor instance (for focusing) until after it is rendered.
# The tinyMCE callback passes in the editor as a paramter. # The tinyMCE callback passes in the editor as a paramter.
init_instance_callback: @focusVisualEditor init_instance_callback: @focusVisualEditor
}) })
@showingVisualEditor = true @showingVisualEditor = true
@element.on('click', '.editor-tabs .tab', this, @onSwitchEditor) # Doing these find operations within onSwitchEditor leads to sporadic failures on Chrome (version 20 and older).
$element = $(element)
@$htmlTab = $element.find('.html-tab')
@$visualTab = $element.find('.visual-tab')
@setupTinyMCE: (ed) -> @element.on('click', '.editor-tabs .tab', @onSwitchEditor)
setupTinyMCE: (ed) =>
ed.addButton('wrapAsCode', { ed.addButton('wrapAsCode', {
title : 'Code', title : 'Code',
image : '/static/images/ico-tinymce-code.png', image : '/static/images/ico-tinymce-code.png',
...@@ -67,22 +73,23 @@ class @HTMLEditingDescriptor ...@@ -67,22 +73,23 @@ class @HTMLEditingDescriptor
command.setActive('wrapAsCode', e.nodeName == 'CODE') command.setActive('wrapAsCode', e.nodeName == 'CODE')
) )
onSwitchEditor: (e)=> @visualEditor = ed
e.preventDefault();
if not $(e.currentTarget).hasClass('current') onSwitchEditor: (e) =>
element = e.data.element e.preventDefault();
$(e.currentTarget).addClass('current') $currentTarget = $(e.currentTarget)
$(element).find('table.mceToolbar').toggleClass(HTMLEditingDescriptor.isInactiveClass) if not $currentTarget.hasClass('current')
$(@advanced_editor.getWrapperElement()).toggleClass(HTMLEditingDescriptor.isInactiveClass) $currentTarget.addClass('current')
@$mceToolbar.toggleClass(HTMLEditingDescriptor.isInactiveClass)
@$advancedEditorWrapper.toggleClass(HTMLEditingDescriptor.isInactiveClass)
visualEditor = @getVisualEditor(element) visualEditor = @getVisualEditor()
if $(e.currentTarget).attr('data-tab') is 'visual' if $currentTarget.data('tab') is 'visual'
$(element).find('.html-tab').removeClass('current') @$htmlTab.removeClass('current')
@showVisualEditor(visualEditor) @showVisualEditor(visualEditor)
else else
$(element).find('.visual-tab').removeClass('current') @$visualTab.removeClass('current')
@showAdvancedEditor(visualEditor) @showAdvancedEditor(visualEditor)
# Show the Advanced (codemirror) Editor. Pulled out as a helper method for unit testing. # Show the Advanced (codemirror) Editor. Pulled out as a helper method for unit testing.
...@@ -104,20 +111,24 @@ class @HTMLEditingDescriptor ...@@ -104,20 +111,24 @@ class @HTMLEditingDescriptor
@focusVisualEditor(visualEditor) @focusVisualEditor(visualEditor)
@showingVisualEditor = true @showingVisualEditor = true
focusVisualEditor: (visualEditor) -> focusVisualEditor: (visualEditor) =>
visualEditor.focus() visualEditor.focus()
if not @$mceToolbar?
@$mceToolbar = $(@element).find('table.mceToolbar')
getVisualEditor: (element) -> getVisualEditor: () ->
### ###
Returns the instance of TinyMCE. Returns the instance of TinyMCE.
This is different from the textarea that exists in the HTML template (@tiny_mce_textarea. This is different from the textarea that exists in the HTML template (@tiny_mce_textarea.
Pulled out as a helper method for unit test.
### ###
return tinyMCE.get($(element).find('.tiny-mce').attr('id')) return @visualEditor
save: -> save: ->
@element.off('click', '.editor-tabs .tab', @onSwitchEditor) @element.off('click', '.editor-tabs .tab', @onSwitchEditor)
text = @advanced_editor.getValue() text = @advanced_editor.getValue()
visualEditor = @getVisualEditor(@element) visualEditor = @getVisualEditor()
if @showingVisualEditor and visualEditor.isDirty() if @showingVisualEditor and visualEditor.isDirty()
text = visualEditor.getContent({no_events: 1}) text = visualEditor.getContent({no_events: 1})
data: text data: text
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