Working with Bunch Objects

ADsafe uses a wrapper object on top of DOM nodes called a bunch object. Bunch objects allow the user to easily capture one or multiple DOM nodes under a single bunch and then manipulate all or some of it as needed.

Bunch objects are the cornerstone of all data fetching and manipulation that you will do through ADsafe. They increase code modularity, reduce code bloat, and improve overall application performance.

Within the bunch node wrapper are a series of methods that allow you to capture information about the nodes in the bunch, change and manipulate their attributes and values, and perform actions like cloning the nodes or stripping them from the bunch.

Bunch GET methods

Fetching data about the nodes that are contained within a bunch is the most-often used functionality for bunch objects, and ADsafe makes a series of GET methods available for developers to do this. Depending on the number of nodes contained within the bunch, the return values and structures from the GET method calls will differ, as outlined here:

  • If a bunch contains no nodes, then the methods return undefined.

  • If a bunch contains one node, a single value is returned (usually as a string).

  • If a bunch contains more than one node, an array of values is returned.

There are a number of GET helper methods, described in Table 8-9, available for obtaining information about the bunch nodes returned from a query request.

Table 8-9. Bunch GET methods

Selector

Description

getCheck()

Obtains the checked value of the node.

var radioBtn = dom.q("input_radio");
var isChecked = radioBtn.getCheck();

getClass()

Obtains the class attribute of the node.

var divNode = dom.q("div.data");
var class = divNode.getClass();

getEnable()

Obtains the enable value of the node. The return value is a Boolean.

var btnSubmit = dom.q("input.submit");
var submitEnabled = btnSubmit.getEnable();

getMark()

Obtains the mark from the node.

//get a mark from a node with the id of CONTAINER_DATA
var mark = dom.q("#CONTAINER_DATA").getMark();
console.log(mark);

getName()

Obtains the name attribute from the node.

var inputText = dom.q("input_text");
var inputName = inputText.getName();

getOffsetHeight()

Obtains the offsetHeight of the nodes. The offsetHeight is calculated by the actual height of the node including the node border but excluding the node margin. This is especially helpful for calculating the positioning nodes in an absolute-positioned environment.

var divNode = dom.q("#innerDiv");
var divHeight = divNode.getOffsetHeight();

getOffsetWidth()

Obtains the offsetWidth of the nodes. The offsetWidth is calculated by the actual height of node including the node border but excluding the node margin. This is especially helpful for calculating the positioning nodes in an absolute-positioned environment.

var divNode = dom.q("#innerDiv");
var divWidth = divNode.getOffsetHeight();

getParent()

Obtains the parent of the node, formatted as a bunch. This request will return duplicate entries if the parent of two sibling nodes is requested. If the node whose parent is being requested is the root node, getParent() will throw an exception.

try{
   var node = dom.q("#divWrapper");
   var nodeParent = node.getParent();
} catch(err) {
   //exception thrown - root node
}

getSelection()

Obtains the text selected by the user in a textarea or input text node.

var input = dom.q("input_text");
var selected = input.getSelection();

getStyle(name)

Obtains the name CSS style of the node (i.e., where name is the CSS style type, such as position, float, or color).

var div = dom.q("div.positioned");
var divPosition = div.getStyle("position");

getTagName()

Obtains the tag name of the node.

var node = dom.q("*.content");
var tags = node.getTagName();

getTitle()

Obtains the title of the node.

//get title from node with an id of CONTAINER_DATA
var title = dom.q("#CONTAINER_DATA").getTitle();
console.log(title);

getValue()

Obtains the value of the node. If the node requested doesn’t have a value associated with it but has a child text node with a value, then that value will be used. If a password field is requested, undefined will be returned.

var input = dom.q("input.username").getValue();
var value = input.getValue();

By using these GET methods, you will greatly reduce the amount of code required to fetch data from DOM nodes within the ADsafe wrapper.

Bunch SET methods

ADsafe makes available a series of SET methods to allow you to manipulate markup, events, and text within your ADsafe application. These incredibly valuable helper methods are in place to make your life easier.

Many of these SET methods work in conjunction with the GET methods that we have just reviewed. Implementing a good mix of the two will allow you to take advantage of some of the advanced bunch manipulation features.

Using these methods combined with some advanced DOM queries executed through the q() method, you can develop highly targeted modules with a minimal amount of code.

Table 8-10 shows the full list of SET methods available to you.

Table 8-10. Bunch SET methods

Selector

Description

append(appendees)

Appends an appendees bunch as children to an existing bunch. If there is more than one appendee, they will be deeply cloned before insertion.

//append CONTAINER_TEXT node to CONTAINER_DATA node
var data = dom.q("#CONTAINER_TEXT");
var appended = dom.q("#CONTAINER_DATA").append(data);

blur()

Removes the focus from a node.

//remove focus for input box with an id of CONTAINER_USER
dom.q("#CONTAINER_USER").blur();

check(value)

Sets the checked state (on or off) for radio or checkbox inputs.

//auto-check checkbox with a class name of checker
var checkBtn = dom.q("input_checkbox.checker").check(true);

each(func)

Allows you to loop through a series of nodes in a bunch. The func argument is a function that takes a bunch argument.

//loop through all <div> nodes in the application
var dataNode = dom.q("div");
dataNode.each(function(bunch){
   console.log(bunch.getClass());
});

empty()

Removes all children from a node.

//remove all children of node with id of CONTAINER_PARENT
var parentDiv = dom.q("#CONTAINER_PARENT");
var emptyParent = parentDiv.empty();

enable(boolean)

Sets the enabled status of a node. boolean is set to true for enabled or false for disabled.

//disable submit button
var submitBtn = dom.q("input_submit").enable(false);

ephemeral()

Removes all nodes when:

  • The mouse button moves up.

  • The escape key is pressed.

  • Another bunch is set as ephemeral.

//set ephemeral state for node with and id of   CONTAINER_PARENT
var parentDiv = dom.q("#CONTAINER_PARENT").ephemeral();

fire(event)

Fires an event on the node. The event argument may be:

  • A string (e.g., click, keypress.)

  • An object with an event property (e.g., {"event": "click"})

//fire click event for button with an id of
CONTAINER_BUTTON
var button = dom.q("#CONTAINER_BUTTON");
button.fire("click");

focus()

Sets focus to the selected node.

//set focus for input box with an id of CONTAINER_USER   
dom.q("#CONTAINER_USER").focus();

klass(string)

Sets the CSS class of a node to that added as the string argument to the method.

//set class for node with an id of CONTAINER_DATA to
"red"
var dataObj = dom.q("#CONTAINER_DATA").klass("red");

mark(value)

Sets a mark on all nodes in a bunch. This essentially just allows you to tag nodes with data or notes, such as text or JSON structures. These marks can later be consumed with the getMark() bunch GET method.

//set mark on node with an id of CONTAINER_DATA
var mark = dom.q("#CONTAINER_DATA").mark("value123");

off(eventName, func)

Removes event handlers from provided nodes. The options for removing specific events are:

  • bunch.off() with no arguments will remove all event handlers from the nodes.

  • bunch.off("eventName") will remove all eventName events from the nodes.

  • bunch.off("eventName", function) will remove a specific eventName event from the bunch.

//remove click event for node with an id of CONTAINER_BUTTON
var button = dom.q("#CONTAINER_BUTTON");
button.off("click");

//try to fire event - fire will cause exception
try{
   button.fire("click");
} catch(err) {
   console.log("click event failed: event
  removed");
}

on(eventName, func)

Adds an event handler to a node.

//attach click event to button with an id of CONTAINER_BUTTON
var button = dom.q("#CONTAINER_BUTTON");
button.on("click", function(e){
   console.log("Button Clicked");
});

prepend(prependees)

Prepends an appendees bunch to an existing bunch. If there is more than one appendee, they will be deeply cloned before insertion.

//prepend CONTAINER_TEXT node to CONTAINER_DATA node
var data = dom.q("#CONTAINER_TEXT");
var prepended = dom.q("#CONTAINER_DATA").prepend(data);

protect()

Protects the provided nodes. When a node is protected, calls to getParent will throw an exception instead of providing access to the node’s parent. In addition, events will not bubble up past a protected node.

//protect node with an the id of CONTAINER_PROTECT
var node = dom.q("#CONTAINER_PROTECT").protect();

select()

Sets focus and selects the text within a given input or textarea node. If the bunch provided does not contain exactly one node, an exception is thrown.

//when button clicked, select textarea
var button = dom.q("#CONTAINER_TAREABTN");
button.on("click", function(e){
   var textarea = dom.q("#CONTAINER_TEXTAREA").select();
});

selection(string)

Swaps out the user-selected text from a text input or textarea node with the value of the string argument.

//attach click event to button with id of
CONTAINER_MODIFY
var button = dom.q("#CONTAINER_MODIFY");
button.on("click", function(e){
   //when button clicked, change selected text for input
  //with an id of CONTAINER_SEL
var modInput = dom.q("#CONTAINER_SEL").selection("updated");
});

style(name, value)

Sets the CSS name style of a node to a particular value.

//set background color and font color for node
var text = dom.q("#CONTAINER_TEXT");
text.style("background-color",   "#000");
text.style("color", "#fff");

title(value)

Sets the title of a node.

//set title for node with an id of CONTAINER_DATA
var title = dom.q("#CONTAINER_DATA").title("My data div");

value(value)

Sets the value of a node.

//set default value text for all text input nodes
var inputs = dom.q("input_text").value("default text");

As we can see from this table, there is an extensive list of features that are available to you for manipulating the bunch objects that ADsafe uses as wrappers for DOM nodes.

Bunch miscellaneous methods

In addition to the standard GET and SET methods that are available for working with and manipulating bunch objects, ADsafe also makes available a number of miscellaneous methods (i.e., methods that don’t specifically fit in one of the getter or setter blocks). Table 8-11 lists these methods.

Table 8-11. Bunch miscellaneous methods

Selector

Description

clone(deep, number)

Clones the nodes in the provided bunch. The new clone is not attached to the document tree. Should the deep argument be set to true, all child nodes will be cloned as well. If a numeric identifier is set as the number argument, then an array of number clones will be created and returned.

//make three deep clones of node
var parentNode = dom.q("#CONTAINER_PARENT");
var clones = parentNode.clone(true, 3);

count()

Returns the number of main nodes in a bunch, excluding the child nodes.

//get number of nodes in the bunch search for all div
//nodes
var numNodes = dom.q("div").count();

explode()

Creates an array of bunches that each contain one node from an original bunch of nodes.

//break all div nodes into individual bunch objects
var node = dom.q("div");
var nodeArray = node.explode();

fragment()

Creates a new HTML document fragment.

//create new fragment from bunch with id of
//CONTAINER_PARENT
var fragment = dom.q("#CONTAINER_PARENT").fragment();

q(query)

Allows you to perform a subquery on the nodes within an already obtained bunch.

//find all div nodes located within a node with
//an id of CONTAINER_PARENT
var parentNode = dom.q("#CONTAINER_PARENT");
var childNode = parentNode.q("div");

remove()

Removes the node and all children from the DOM. This call also removes all events of the node and its children.

//remove node with the id of CONTAINER_PARENT
var removedNode = dom.q("#CONTAINER_PARENT").remove();

replace(replacement)

Replaces a node with a bunch object or an array of bunch objects. The replaced node must have a parent node; otherwise, it will not be replaced by the request. If replacement is empty, the node is removed.

//replace node with id of CONTAINER_OLD with all div

//nodes that have a class of replacement
var replaceNodes = dom.q("div.replacement");
dom.q("#CONTAINER_OLD").replace(replaceNodes);

tag(tagName, type, name)

Creates a new tagName. If the tag is an input field or button, you may specify the type field to be able to set the appropriate input type. If the input type requires a name, you should specify name to group the fields together. This method will return a bunch containing the text node, not attached to the document tree.

//create new radio button and append to DOM
var insertNode = dom.q("#CONTAINER_PARENT");
var newNode = insertNode.tag("input", "radio", "optCheck");
         insertNode.append(newNode);

text(string)

Creates a new text node if a string was specified, or a series of text nodes if an array of strings was specified. This method returns a bunch with the text node(s), not attached to the document tree.

//create two new text nodes and append to DOM
var insertNode = dom.q("#CONTAINER_PARENT");
var newNodes = insertNode.text(["text node 1",
                                "text node 2"]);
insertNode.append(newNodes);

The purpose of the GET, SET, and miscellaneous methods is to provide an abstraction layer on top of the complexity of the secure ADsafe system object. Using their combined power, you can create complex manipulation utilities with simple code constructs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.213.44