Guest User

Untitled

a guest
Aug 25th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. # Example integration of a background-image uploader
  2. # Author: Guillaume Piot
  3. # Email: guillaume@cotidia.com
  4. # Company: Cotidia Ltd
  5. # Licence: MIT
  6. #
  7. # The div holder is absolute positioned within the parent div
  8. #
  9. # <div class="[ article__image ] [ article-image ] [ editable ] [ parallax ]" data-name="article_image">
  10. # <div
  11. # data-ce-tag="bkgimg"
  12. # class="article-image__container"
  13. # style="background-image:url('/static/img/blog-header-placeholder.jpg')"></div>
  14. # </div>
  15.  
  16. class ContentEdit.BackgroundImage extends ContentEdit.Element
  17.  
  18. # An editable background image element
  19. # (e.g <div data-ce-type="bkgimg" data-url="..."></div>).
  20.  
  21. constructor: (attributes) ->
  22. super('bkgimg', attributes)
  23.  
  24. # Read-only properties
  25.  
  26. cssTypeName: () ->
  27. return 'background-image'
  28.  
  29. type: () ->
  30. # Return the type of element (this should be the same as the class name)
  31. return 'BackgroundImage'
  32.  
  33. typeName: () ->
  34. # Return the name of the element type (e.g BackgroundImage, List item)
  35. return 'BackgroundImage'
  36.  
  37. # Methods
  38.  
  39. html: (indent='') ->
  40. # Return a HTML string for the node
  41. bkgimg = "#{ indent }<div#{ @_attributesToString() }></div>"
  42. return bkgimg
  43.  
  44. mount: () ->
  45. # Mount the element on to the DOM
  46.  
  47. # Create the DOM element to mount
  48. @_domElement = document.createElement('div')
  49.  
  50. # Set the classes for the backgound image element
  51. classes = ''
  52.  
  53. if @_attributes['class']
  54. classes += ' ' + @_attributes['class']
  55.  
  56. @_domElement.setAttribute('class', classes)
  57.  
  58. # Set the background image for the dom element
  59. style = if @_attributes['style'] then @_attributes['style'] else ''
  60.  
  61. @_domElement.setAttribute('style', style)
  62.  
  63. # Add the button to edit the background image
  64. @_domButtonElement = document.createElement("button")
  65. buttonText = document.createTextNode("Upload background image");
  66. @_domButtonElement.appendChild(buttonText)
  67. @_domButtonElement.className = 'btn btn--upload-background-image'
  68. @_domButtonElement.style.position = 'absolute'
  69.  
  70. # Just below the toolbox level
  71. @_domButtonElement.style.zIndex = '9998'
  72. @_domElement.appendChild(@_domButtonElement)
  73.  
  74. super()
  75.  
  76. # Get the selected element position
  77. rect = @_domElement.getBoundingClientRect()
  78. @_domButtonElement.style.bottom = "-16px"
  79. @_domButtonElement.style.right = "-16px"
  80.  
  81. _addDOMEventListeners: () ->
  82. super()
  83.  
  84. @_domButtonElement.addEventListener 'click', (ev) =>
  85. ev.preventDefault()
  86. editor = ContentTools.EditorApp.get()
  87.  
  88. modal = new ContentTools.ModalUI(transparent=false, allowScrolling=false)
  89. dialog = new ContentTools.ImageDialog()
  90.  
  91. # Support cancelling the dialog
  92. dialog.addEventListener 'cancel', () =>
  93. modal.hide()
  94. dialog.hide()
  95.  
  96. # Insert the image url into the element once saved
  97. dialog.addEventListener 'save', (ev) =>
  98.  
  99. detail = ev.detail()
  100. imageURL = detail.imageURL
  101. imageSize = detail.imageSize
  102. imageAttrs = detail.imageAttrs
  103.  
  104. if not imageAttrs
  105. imageAttrs = {}
  106.  
  107. imageAttrs.height = imageSize[1]
  108. imageAttrs.src = imageURL
  109. imageAttrs.width = imageSize[0]
  110.  
  111. # node = document.querySelector('[data-name="page_image"]')
  112. # element.style.backgroundImage = "url('#{imageURL}')";
  113. # imageNode = region.descendants()[0]
  114.  
  115. style = "background-image: url('#{imageURL}')"
  116. @attr('style', style)
  117.  
  118. console.log(@_domElement.style)
  119.  
  120. modal.hide()
  121. dialog.hide()
  122.  
  123.  
  124. editor.attach(modal)
  125. editor.attach(dialog)
  126. modal.show()
  127. dialog.show()
  128.  
  129. # Class properties
  130.  
  131. # Class methods
  132.  
  133. @fromDOMElement: (domElement) ->
  134. # Convert an element (DOM) to an element of this type
  135. attributes = @getDOMElementAttributes(domElement)
  136.  
  137. return new @(attributes)
  138.  
  139.  
  140. # Register `ContentEdit.BackgroundImage` the class with associated tag names
  141. ContentEdit.TagNames.get().register(ContentEdit.BackgroundImage, 'bkgimg')
Add Comment
Please, Sign In to add comment