Sha256: 044c6bb1d9642407c3f2591e2a94e19789dcd701a0448cf2f8809f5579af131a

Contents?: true

Size: 1.97 KB

Versions: 15

Compression:

Stored size: 1.97 KB

Contents

#= require ../shared/form_view

Locomotive.Views.Snippets ||= {}

class Locomotive.Views.Snippets.FormView extends Locomotive.Views.Shared.FormView

  el: '#content'

  events:
    'click a#image-picker-link':   'open_image_picker'
    'click a#copy-template-link':  'replace_template'
    'submit':                      'save'

  initialize: ->
    _.bindAll(@, 'insert_image')

    @model = new Locomotive.Models.Snippet(@options.snippet)

    @image_picker_view = new Locomotive.Views.ThemeAssets.ImagePickerView
      collection: new Locomotive.Models.ThemeAssetsCollection()
      on_select:  @insert_image

    @image_picker_view.render()

    Backbone.ModelBinding.bind @

  render: ->
    super()

    # slugify the slug field from name
    @slugify_name()

    # liquid code textarea
    @enable_liquid_editing()

    return @

  slugify_name: ->
    @$('#snippet_name').slugify
      target:     @$('#snippet_slug')
      url:        window.permalink_service_url

  open_image_picker: (event) ->
    event.stopPropagation() & event.preventDefault()
    @image_picker_view.editor = @editor
    @image_picker_view.fetch_assets()

  insert_image: (path) ->
    text = "{{ '#{path}' | theme_image_url }}"
    @editor.replaceSelection(text)
    @image_picker_view.close()

  replace_template: (event) ->
    event.stopPropagation() & event.preventDefault()

    link = $(event.target).closest('a')

    $.rails.ajax
      url:       link.attr('href')
      type:      'get'
      dataType:  'json'
      success:  (data) =>
        @editor.setValue(data.template)

  enable_liquid_editing: ->
    input = @$('#snippet_template')
    @editor = CodeMirror.fromTextArea input.get()[0],
      mode:             'liquid'
      autoMatchParens:  false
      lineNumbers:      false
      passDelay:        50
      tabMode:          'shift'
      theme:            'default medium'

    @editor.on 'change', (editor, change) => @model.set(template: editor.getValue())

  after_inputs_fold: ->
    @editor.refresh()

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
locomotive_cms-2.5.7 app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee
locomotive_cms-2.5.6 app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee
locomotive_cms-2.5.6.rc2 app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee
locomotive_cms-2.5.6.rc1 app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee
locomotive_cms-2.5.5 app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee
locomotive_cms-2.5.4 app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee
locomotive_cms-2.5.3 app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee
locomotive_cms-2.5.2 app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee
locomotive_cms-2.5.1 app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee
locomotive_cms-2.5.0 app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee
locomotive_cms-2.5.0.rc3 app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee
locomotive_cms-2.5.0.rc2 app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee
locomotive_cms-2.5.0.rc1 app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee
locomotive_cms-2.4.1 app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee
locomotive_cms-2.4.0 app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee