lb.base.dom.factory

DOM (Document Object Model) Factory, Adapter Module for Base Library

This module provides the base implementation of a factory to create DOM elements, listeners and events.  It is intended to be replaced with a custom factory creating widgets on top of regular DOM elements for the support of Rich Internet Applications.

How to design a custom factory

A custom factory is an object with the same methods defined in this module.  All the methods defined in the base factory must be supported by your custom factory.

The custom factory can be configured by calling setOptions on the application core:

lb.core.application.setOptions({ lbFactory: your.customFactory })

To develop your own custom factory, you can start by creating a new module as a closure assigned to your own namespace.  You can then add all required methods, just calling the same method in the base factory to use the default implementation.  You may find it handy to declare an alias for the base factory at the start of your module:

var baseFactory = lb.base.dom.factory;

In addition to the mandatory methods defined by the base factory, you may optionally support the initElement method, which is an extra extension point intended for use in custom factories:

Authors

Copyright

Eric Bréchemier © 2011, Some Rights Reserved Legal-Box SAS © 2010-2011, All Rights Reserved

License

BSD License http://creativecommons.org/licenses/BSD/

Version

2011-08-14

Summary
lb.base.dom.factoryDOM (Document Object Model) Factory, Adapter Module for Base Library
Functions
initElement(element)(optional) Customize a newly inserted element.
createElement(name[,attributes[,childNodes]]): DOM ElementCreate a new element with given name, attributes and child nodes.
destroyElement(element)Terminate usage of a DOM element by removing it from its parent.
createListener(element, type, callback[, useCapture])Create a new listener for a type of event on a DOM element.
destroyListener(listener)Terminate a listener by removing it from the target DOM element.
createEvent(element, type[, properties[, useCapture]])Create a new DOM event and fire it on given target element.
destroyEvent(event)Terminate a DOM event: prevent default action and stop propagation.

Functions

initElement(element)

(optional) Customize a newly inserted element.  Not implemented in the base factory.

The method differs from createElement which is responsible for the actual creation of the element node and is called before the node is inserted in the DOM.  On the contrary, this method will be called on elements already part of the DOM.

When available on the configured factory, this method is currently called before a module starts, with the box element at the root of the module.  It is also intended to get called in a template engine, to be added in a future version of the library, after inserting new contents in the box.

A custom factory may, for example, iterate recursively on the children of the given element, creating Rich Internet Application widgets when expected CSS classes are found on an element.

Parameter

elementDOM Element, an element part of the document.

createElement(name[,attributes[,childNodes]]): DOM Element

Create a new element with given name, attributes and child nodes.

Parameters

namestring, the name of the element, e.g.  ‘div’
attributesobject, the set of attributes, e.g.  {id:’myDiv’, ‘class’:’big box’}
childNodesarray or list, the list of child nodes.  The child nodes may be provided as an array, or as a list of arguments (after name and attributes).

Returns

DOM Element, the newly created element

destroyElement(element)

Terminate usage of a DOM element by removing it from its parent.

Parameter

elementDOM element, an element (with or without parent)

Note

Nothing happens in case the element has no parent.

createListener(element, type, callback[, useCapture])

Create a new listener for a type of event on a DOM element.

Parameters

elementDOM Element, an element
typestring, the name of an event (without ‘on’) e.g.  ‘click’
callbackfunction, a function to call when the event is dispatched.
useCaptureboolean, whether the callback is set for capture phase.  Optional: defaults to false.  See [1] for details.

Returns

object, a new instance of lb.base.dom.Listener

Reference

[1] DOM Level 2 Events: addEventListener http://bit.ly/9SQoL4

destroyListener(listener)

Terminate a listener by removing it from the target DOM element.

Parameter

listenerobject, the listener returned by createListener, instance of lb.base.dom.Listener

createEvent(element, type[, properties[, useCapture]])

Create a new DOM event and fire it on given target element.

Parameters

elementDOM element, the target element for the event dispatch
typestring, the name of an event (without ‘on’) e.g.  ‘click’
propertiesobject, optional properties to set to the new event.
useCaptureboolean, whether the callback is set for capture phase.  Optional: defaults to false.  See [1] for details.

Returns

object, the new DOM Event [2] created

References

[1] DOM Level 2 Events: addEventListener http://bit.ly/9SQoL4

[2] DOM Level 2 Events: Event interface http://bit.ly/b7KwF5

destroyEvent(event)

Terminate a DOM event: prevent default action and stop propagation.

Nothing happens in case the event is undefined, or lacks both of the expected stopPropagation() and preventDefault() methods.  In case only one of the methods is missing, the other will get called.

Parameter

eventobject, the DOM Event [1]

Reference

[1] DOM Level 2 Events: Event interface http://bit.ly/b7KwF5

(optional) Customize a newly inserted element.
DOM (Document Object Model) Listener Adapter Module for Base Library
Close