Skip to content

Commit

Permalink
added global handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
jejacks0n committed Jan 2, 2012
1 parent e18a826 commit ca6d023
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
7 changes: 4 additions & 3 deletions spec/javascripts/mercury/page_editor_spec.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,11 @@ describe "Mercury.PageEditor", ->
@pageEditor = new Mercury.PageEditor('', {appendTo: $('#test')})

it "takes the location and removes the /editor", ->
expect(@pageEditor.iframeSrc('http://foo.com/editor/path')).toEqual('http://foo.com/path?mercury_frame=true')
expect(@pageEditor.iframeSrc('http://foo.com/editor/path')).toEqual('http://foo.com/path')

it "takes handles the query param appropriately if there's already params", ->
expect(@pageEditor.iframeSrc('http://foo.com/editor/path?something=true')).toEqual('http://foo.com/path?something=true&mercury_frame=true')
it "adds query params", ->
expect(@pageEditor.iframeSrc('http://foo.com/editor/path', true)).toEqual('http://foo.com/path?mercury_frame=true')
expect(@pageEditor.iframeSrc('http://foo.com/editor/path?something=true', true)).toEqual('http://foo.com/path?something=true&mercury_frame=true')


describe "#hijackLinksAndForms", ->
Expand Down
21 changes: 20 additions & 1 deletion vendor/assets/javascripts/mercury.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,31 @@ window.Mercury = {
// You can see how the behavior matches up directly with the button names. It's also important to note that the
// callback functions are executed within the scope of the given region, so you have access to all it's methods.
behaviors: {
//exit: function() { window.location.href = window.mercuryInstance.iframeSrc() },
//foreColor: function(selection, options) { selection.wrap('<span style="color:' + options.value.toHex() + '">', true) },
htmlEditor: function() { Mercury.modal('/mercury/modals/htmleditor.html', { title: 'HTML Editor', fullHeight: true, handler: 'htmlEditor' }); }
},


// ## Global Behaviors
//
// Global behaviors are much like behaviors, but are more "global". Things like save, exit, etc. can be included
// here. They'll only be called once, and execute within the scope of whatever editor is instantiated (eg.
// PageEditor).
//
// An example of changing how saving works:
//
// save: function() {
// var data = top.JSON.stringify(this.serialize(), null, ' ');
// var content = '<textarea style="width:500px;height:200px" wrap="off">' + data + '</textarea>';
// Mercury.modal(null, {title: 'Saving', closeButton: true, content: content})
// }
//
// This is a nice way to add functionality to the toolbar, when you don't want the behaviors aren't region specific.
globalBehaviors: {
exit: function() { window.location.href = this.iframeSrc() }
},


// ## Ajax and CSRF Headers
//
// Some server frameworks require that you provide a specific header for Ajax requests. The values for these CSRF
Expand Down
16 changes: 12 additions & 4 deletions vendor/assets/javascripts/mercury/page_editor.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class @Mercury.PageEditor
@resize()

@iframe.on 'load', => @initializeFrame()
@iframe.get(0).contentWindow.document.location.href = @iframeSrc()
@iframe.get(0).contentWindow.document.location.href = @iframeSrc(null, true)


initializeFrame: ->
Expand Down Expand Up @@ -102,7 +102,11 @@ class @Mercury.PageEditor
Mercury.on 'toggle:interface', => @toggleInterface()
Mercury.on 'reinitialize', => @initializeRegions()
Mercury.on 'mode', (event, options) => @previewing = !@previewing if options.mode == 'preview'
Mercury.on 'action', (event, options) => @save() if options.action == 'save'
Mercury.on 'action', (event, options) =>
action = Mercury.config.globalBehaviors[options.action] || @[options.action]
return unless typeof(action) == 'function'
options.already_handled = true
action.call(@, options)

@document.on 'mousedown', (event) ->
Mercury.trigger('hide:dialogs')
Expand Down Expand Up @@ -157,9 +161,13 @@ class @Mercury.PageEditor
Mercury.trigger('resize')


iframeSrc: (url = null) ->
iframeSrc: (url = null, params = false) ->
url = (url ? window.location.href).replace(/([http|https]:\/\/.[^\/]*)\/editor\/?(.*)/i, "$1/$2")
"#{url}#{if url.indexOf('?') > -1 then '&' else '?'}mercury_frame=true"
url = url.replace(/[\?|\&]mercury_frame=true/gi, '')
if params
return "#{url}#{if url.indexOf('?') > -1 then '&' else '?'}mercury_frame=true"
else
return url


hijackLinksAndForms: ->
Expand Down
2 changes: 1 addition & 1 deletion vendor/assets/javascripts/mercury/region.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class @Mercury.Region
@pushHistory() unless action == 'redo'

Mercury.log('execCommand', action, options.value)
Mercury.changes = true
Mercury.changes = true unless options.already_handled


pushHistory: ->
Expand Down

0 comments on commit ca6d023

Please sign in to comment.