Sha256: 84447fad15bbef1c8802431139356d73c599bc0cdb22d507084e35a552c3bfa0

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

class Lanes.lib.Dom

    constructor: (el) ->
        @el = el

    qs: (selector, options = {}) ->
        el = @el.querySelector(selector)
        if not el and options.raise isnt false
            throw new TypeError("Unable to find element for selector #{selector}")
        new Lanes.lib.Dom(el || @el)

    qsa: (selector) ->
        @el.querySelectorAll(selector)

    remove: ->
        @el.parentElement.removeChild(@el)

    focusAndSelect: ->
        @el.select?()
        @el.focus?()

    closest: (selector) ->
        # Traverse the DOM up with a while loop
        el = @el
        while el.nodeType isnt 9 and !el.matches(selector)
            # Increment the loop to the parent node
            el = el.parentNode
            return null unless el
        _.dom(el)

wrapArg = (fn) ->
    return ->
        fn(this.el, arguments...)

chain = (fn) ->
    return ->
        fn(this.el, arguments...)
        return this

for name, func of Lanes.Vendor.dom
    Lanes.lib.Dom::[name] = if name.match(/^has/)
        wrapArg
    else
        chain(func)

Object.defineProperties(Lanes.lib.Dom.prototype, {

    text:
        get: -> @el.textContent
        set: (txt) ->
            Lanes.Vendor.dom.text(@el, txt)
    html:
        get: -> @el.innerHTML
        set: (html) ->
            Lanes.Vendor.dom.html(@el, html)

    value:
        get: -> @el.value
        set: (v) -> @el.value = v
})



_.dom = (unknown, query) ->
    el = if _.isBlank(unknown)
        throw new TypeError("Selector / DOM node is not present")
    else if _.isElement(unknown)
        unknown
    else if unknown.isReactComponent or unknown.render
        Lanes.Vendor.ReactDOM.findDOMNode(unknown)
    else if unknown.nodeType is 9 # body tag
        unknown
    else if unknown
        throw new TypeError("Unable to obtain dom reference to #{unknown}")

    if query
        el = el.querySelector(query)
    new Lanes.lib.Dom(el)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lanes-0.5.5 client/lanes/lib/dom.coffee