Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
NeotericEditor = (el, options) ->
  defaults =
    deepMerge: true
    imageUpload: "/redactor_rails/pictures?"
    fileUpload: "/redactor_rails/documents?"
    imageGetJson: "/redactor_rails/pictures"
    fileGetJson: "/redactor_rails/documents"
    path: "/assets/redactor-rails"
    plugins: ['files', 'images']
    fixed: true
    wym: true
    buttonsAdd: ["|", "addAttributes"]
    buttonsCustom:
      addAttributes:
        title: "Add Attributes"
        dropdown:
          "id":
            title: "Add id"
            callback: addAttribute
          "class":
            title: "Add class",
            callback: addAttribute

  @options = $.extend defaults, options

  getSelectionParentElement = ->
    if window.getSelection
      sel = window.getSelection()

      if sel.rangeCount
        parentEl = sel.getRangeAt(0).commonAncestorContainer
        unless parentEl.nodeType is 1
          parentEl = parentEl.parentNode
    else
      if document.selection and
         document.selection.type isnt "Control"

        parentEl = document.selection.createRange().parentElement()

    parentEl

  addAttribute = (obj, e, key) ->
    obj.$el.setFocus()
    $el = $(getSelectionParentElement())
    curr_attr = $el.attr(key)
    attr = prompt("Enter " + key, curr_attr)

    if attr
      $el.attr key, attr

  csrf = ->
    this.string ||= ''
    return this.string if this.string.length

    $.each ['token', 'param'], (k, v) =>
      this[v] = $("meta[name=csrf-#{v}]").attr("content")

    if this.param and this.token
      this.string = this.param + "=" + encodeURIComponent(this.token)

  init = =>
    @options = $.extend
      imageUpload: @options.imageUpload + csrf()
      fileUpload: @options.fileUpload + csrf()
    , @options

    $(el).redactor(@options)
    # setupHtmlEditor()

  setupHtmlEditor = =>
    id     = $(el).attr("id")
    ace_id = "#{id}-ace"

    $(el).wrap "<div id='#{ace_id}'>"

    editor = ace.edit(ace_id)
    editor.setTheme "ace/theme/tomorrow"
    editor.getSession().setMode "ace/mode/html"
    editor.getSession().setTabSize 2
    editor.getSession().setUseSoftTabs true

    editor.on 'change', (e) ->
      console.log(e)

  init()

name = 'editor'
$.fn[name] = (option) ->
  data = $.data $(this)[0], name
  return if data

  this.each ->
    options = option if typeof option == 'object'
    $(this).data name, new NeotericEditor(this, options)