Files
atom/src/dom-elements-pool.coffee
Antonio Scandurra 6b2e7a6765 🎨
2015-09-14 17:36:45 +02:00

26 lines
721 B
CoffeeScript

{toArray} = require 'underscore-plus'
module.exports =
class DomElementsPool
constructor: ->
@freeElementsByTagName = {}
build: (tagName, className, textContent) ->
element = @freeElementsByTagName[tagName]?.pop()
element ?= document.createElement(tagName)
element.className = className
element.textContent = textContent
element.removeAttribute("style")
element
free: (element) ->
element.remove()
@freeElementsByTagName[element.tagName.toLowerCase()] ?= []
@freeElementsByTagName[element.tagName.toLowerCase()].push(element)
freeElementAndDescendants: (element) ->
@free(element)
for child in toArray(element.children)
@freeElementAndDescendants(child)