Sha256: 8d0cbcc7abfeba4dad3ed50736173651d79351ddd017a75747610207d1be5078

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

$ ->
  statusIndicatorClass = 'image-editor-dragover'

  activateForFieldType = (fieldType) ->
    selector = 'img[data-scrivito-field-type=' + fieldType  + ']:not([data-editor])'
    scrivito.editors._file_drop_zone(selector, save)

  save = (event, element, file) ->
    fieldType = element.attr('data-scrivito-field-type')

    createImage(file).then (obj) ->
      value = switch
        when fieldType == 'reference' then obj.id
        when fieldType == 'linklist' then [{obj_id: obj.id}]
        else $.error('Field type must be "reference" or "linklist".')

      element.scrivito('save', value).then ->
        element.scrivito('reload')

  createImage = (file) ->
    name = file.name.replace(/[^a-z0-9_.$\-]/ig, '-')
    path = '_resources/' + Math.floor(Math.random() * 1000) + '/' + name

    scrivito.create_obj
      blob: file
      _path: path
      _obj_class: 'Image'

  # Integrates an image drag & drop editor.
  scrivito.on 'load', ->
    if scrivito.in_editable_view()
      # Activate the image editor for all supported field types.
      activateForFieldType('linklist')
      activateForFieldType('reference')

      # Activate the image editor if it is explicitely selected.
      scrivito.editors._file_drop_zone('[data-editor~="image"]', save,statusIndicatorClass)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
scrivito_editors-0.40.0 app/assets/javascripts/scrivito_editors/image_editor.js.coffee
scrivito_editors-0.40.0.rc2 app/assets/javascripts/scrivito_editors/image_editor.js.coffee
scrivito_editors-0.40.0.rc1 app/assets/javascripts/scrivito_editors/image_editor.js.coffee