DOM Astraction Layer. A DOM manipulator micro-library
- Less than 200 lines of annotated source
- Chainable API
<div id="content">Bad news...</div>
<script src="dal.js"></script>
var content = dal('content');
content
.clear()
.add('h1', 'It works!')
.color({ bg: '#F99', fg: 'red' })
.size({ width: '300px', height: '300px' });
while (!content.full()) content.add('h1', 'foo');
content.add('h1', 'bar');
If it works you'll end with something like this
$ component install dal
DAL is a work-in-progress and is only intended for my personal use at this time.
Gets a DOM element and returns an object with methods for dealing with that element
el
(Optional): If no element is passed, a div
element will be created
and used, but also detached from the document.body
(so is invisible) by
default
- [String] id of the DOM element. If it does not exist, returns an attached div element, using the argument received as id of that div.
- [Object: DOM Element]
Returns the innerHTML value from el
Returns the HTML representation of el
Returns an array of the childNodes of el
as dal
elements
Returns an object
{
bg: `el` background color,
fg: `el` foreground color
}
Determines if target
and el
are the same DOM element
target
- [Object: DOM Element]
- [Object: DAL instance]
Determines whether el
is descendant of target
target
- [Object: DOM Element]
- [Object: DAL instance]
Determines whether el
is overflowing its bounds
Determines whether el
has any child
Determines whether el
is a tag and (if passed) its a tagName
tag
tagName
(Optional): [String]
Determines whether className
is a class of el
className
: [String]
The rest is a collection of methods for managing the DOM element el
received with the constructor
Every one of them is chainable
Attaches el
to the document.body
if it was not attached yet
Detaches el
from his parent if it has one
Attaches a new element to el
newEl
:
-
[Object: DAL instance]
-
[Object: DOM Element]
-
[String] An element is created and attached
self().add('h1', 'Hello world!')
content
(Optional): [String] Default content for the new element
Removes all the children of el
Returns a copy of itself excluding its children nodes
Returns a clone of itself including its children nodes
Returns the parent of el
Returns the first child of el
Returns the last child of el
Returns the last leaf element el
traversing through its lastChild
s
Traverses recursively through the elements in el
opts
: [Object]
-
before (el)
(Optional) [Function] Called before any of the children are traversed -
each (el)
(Optional) [Function] Called on each child -
after (el)
(Optional) [Function] Called after any of the children are traversed
The node received by before
, each
and after
is a dal object pointing
to the current DOM element in the recursion
Returns a new dal
object containing a tree composed with shallow copies
from target
to el
If el
is not under target
, returns undefined
If el
and target
are the same returns a shallow copy of el
Makes el
visible (is if is attached to the document.body
)
Makes el
invisible and ignored by the layout, but behaving as when
visible
Makes el
invisible but respected by the layout
Makes the element absolute positioned at (opts.x, opts.y)
Setting the optional opts.relative
to true
causes the movement to be
relative to its parent. Its required to alter the parent position
to
relative
for this to work, so this setting may cause unexpected
behaviour.
Changes the background and text color of el
opts
: [Object]
opts.bg
: Determines the backgroud coloropts.fg
: Determines the text coloropts.opacity
: Determines the opacity
Changes the size of el
opts
: [Object]
opts.width
: Determines the width of the elementopts.height
: Determines the height of the element
Collapses el
data
Uncollapses el
data
Adds class
as a CSS class of el
className
: [String] A CSS class to be added
Deletes class
from the CSS classes of el
className
: [String] The CSS class to be deleted
Sets the innerHTML
value of the element
Get the sources at github: http://github.com/pfraces/dal
(The MIT License)
Copyright (c) 2012 pfraces
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.