engine/app/assets/javascripts/locomotive/views/inline_editor/application_view.js.coffee

97 lines
2.5 KiB
CoffeeScript

Locomotive.Views.InlinEditor ||= {}
#= require ./toolbar_view
class Locomotive.Views.InlinEditor.ApplicationView extends Backbone.View
el: 'body'
initialize: ->
super
@iframe = @$('#page iframe')
_.bindAll(@, '_$')
@toolbar_view = new Locomotive.Views.InlinEditor.ToolbarView(target: @iframe)
render: ->
super
@enable_iframe_autoheight()
@toolbar_view.render()
enable_iframe_autoheight: ->
iframe = @iframe
iframe.load =>
console.log('iframe loading')
if @_$('meta[name=inline-editor]').size() > 0
# bind the resize event. When the iFrame's size changes, update its height
iframe_content = iframe.contents().find('body')
iframe_content.resize ->
elem = $(this)
if elem.outerHeight(true) > $('body').outerHeight(true) # Resize the iFrame.
iframe.css height: elem.outerHeight(true)
# Resize the iFrame immediately.
iframe_content.resize()
else
@toolbar_view.show_status('disabled', true).hide_editing_mode_block()
# keep the user in the admin mode
@enhance_iframe_links()
set_page: (attributes) ->
console.log('set_page')
@page = new Locomotive.Models.Page(attributes)
@toolbar_view.model = @page
@enhance_iframe()
@toolbar_view.refresh()
enhance_iframe: ->
_window = @iframe[0].contentWindow
_window.Aloha.settings.locale = window.locale
# set main window title
window.document.title = _window.document.title
# keep the user in the admin mode
@enhance_iframe_links _window.Aloha.jQuery
# notify the toolbar about changes
_window.Aloha.bind 'aloha-editable-deactivated', (event, editable) =>
@toolbar_view.notify editable.editable
enhance_iframe_links: (_jQuery) ->
toolbar_view = @toolbar_view
_jQuery ||= @_$
_jQuery('a').each ->
link = _jQuery(this)
url = link.attr('href')
if url? && url.indexOf('#') != 0 && /^(www|http)/.exec(url) == null && /(\/_edit)$/.exec(url) == null
url = '/index' if url == '/'
unless url.indexOf('_edit') > 0
if url.indexOf('?') > 0
link.attr('href', url.replace('?', '/_edit?'))
else
link.attr('href', "#{url}/_edit")
link.bind 'click', ->
toolbar_view.show_status 'loading'
window.history.pushState('Object', 'Title', link.attr('href').replace('_edit', '_admin'))
_$: (selector) ->
$(selector, @iframe[0].contentWindow.document)