Changes from Version 1 of documentation/LuaManual/Elements

Show
Ignore:
Author:
gambini (IP: 96.38.104.126)
Timestamp:
05/20/12 05:31:48 (5 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • documentation/LuaManual/Elements

    v0 v1  
     1[[PageOutline(1-5, Contents)]] 
     2= Elements = 
     3 
     4== Interface == 
     5 
     6The Lua interface for Rocket elements closely resembles [http://developer.mozilla.org/en/docs/DOM:element Gecko's HTML DOM element interface], similarly to the C++ element interface. 
     7 
     8=== Properties === 
     9 
     10|| '''Rocket property''' || '''Brief description''' || '''Equivalent DOM property''' || 
     11|| ''absolute_left'' || The distance from the context's left edge and the element's left border. || || 
     12|| ''absolute_top'' || The distance from the context's top edge and the element's top border. || || 
     13|| ''attributes'' || All attributes associated with an element. || ''attributes'' || 
     14|| ''child_nodes'' || All child nodes of an element. || ''childNodes'' || 
     15|| ''class_name'' || Gets/sets the class of the element. || ''className'' || 
     16|| ''client_height'' || The inner height of an element. || ''clientHeight'' || 
     17|| ''client_left'' || The width of the left border of an element. || ''clientLeft'' || 
     18|| ''client_top'' || The width of the top border of an element. || ''clientTop'' || 
     19|| ''client_width'' || The inner width of an element. || ''clientWidth'' || 
     20|| ''first_child'' || The first direct child node of an element. || ''firstChild'' || 
     21|| ''id'' || Gets/sets the id of the element. || ''id'' || 
     22|| ''inner_rml'' || Gets/sets the markup and content of the element. || ''innerHTML'' || 
     23|| ''last_child'' || The last direct child node of an element. || ''lastChild'' || 
     24|| ''next_sibling'' || The node immediately following the given one in the tree. || ''nextSibling'' || 
     25|| ''offset_height'' || The height of an element, relative to the layout. || ''offsetHeight'' || 
     26|| ''offset_left'' || The distance from this element's left border to its offset parent's left border. || ''offsetLeft'' || 
     27|| ''offset_parent'' || The element from which all offset calculations are currently computed. || ''offsetParent'' || 
     28|| ''offset_top'' || The distance from this element's top border to its offset parent's top border. || ''offsetTop'' || 
     29|| ''offset_width'' || The width of an element, relative to the layout. || ''offsetWidth'' || 
     30|| ''owner_document'' || The document that this node is in. || ''ownerDocument'' || 
     31|| ''parent_node'' || The parent element of this node. || ''parentNode'' || 
     32|| ''previous_sibling'' || The node immediately preceding the given one in the tree. || ''previousSibling'' || 
     33|| ''scroll_height'' || The scroll view height of an element. || ''scrollHeight'' || 
     34|| ''scroll_left'' || Gets/sets the left scroll offset of an element. || ''scrollLeft'' || 
     35|| ''scroll_top'' || Gets/sets the top scroll offset of an element. || ''scrollTop'' || 
     36|| ''scroll_width'' || The scroll view width of an element. || ''scrollWidth'' || 
     37|| ''style'' || An object representing the declarations of an element's style attributes. || ''style'' || 
     38|| ''tag_name'' || The name of the tag for the given element. || ''tagName'' || 
     39 
     40==== Table returning properties ==== 
     41There are three properties of Elements that return tables of data. They are ''child_nodes'', ''attributes'', and ''style''. They are implemented in C++ as [wiki:documentation/LuaManual/ProxyTables proxy tables] with regards to their Lua interface. 
     42 
     43The ''child_nodes'' property is accessed as an array; it is indexed numerically, from the front or back, and its length can be queried by getting the actual Lua table from the proxy table, and using the Lua length operator(#) on that table. 
     44 
     45''child_nodes'' is an array of element types. The array only includes visible elements; Lua has no way of querying [wiki:documentation/C++Manual/HiddenElements hidden elements]. The following example iterates over all of an element's children, printing their tag names, ids and classes: 
     46 
     47{{{ 
     48for key,child in ipairs(element.child_nodes:GetTable()) do 
     49    local address = child.tag_name 
     50 
     51    if child.id != "" then 
     52        address .. "#" .. child.id 
     53    end 
     54 
     55    --class_name is a space-separated list of class names. We want to turn that in to a comma-separated list 
     56    --and then compile that in to a table. Think of it as a string.split function (Lua doesn't have one) 
     57    local classes = loadstring("return {"..child.class_name:gsub(" ",",").."}")() 
     58    for notused,class in pairs(classes) do 
     59        address .. "." .. class 
     60    end 
     61 
     62    print address 
     63end 
     64}}} 
     65 
     66''attributes'' is a table of attribute types, with a string key and a [wiki:documentation/LuaManual/Variant variant] value. The following example iterates over an element's attributes, printing their names and values: 
     67 
     68{{{ 
     69for name,attribute in pairs(element.attributes:GetTable()) do 
     70    print key .. ": " .. attribute 
     71end 
     72}}} 
     73 
     74 
     75The ''style'' property operates identically to its counterpart in Javascript. Properties are accessed as members of the ''style'' property by name and can be read or written to. The value of a property is always an unparsed string in this context; ie, "200px", "center", "rgb(255,0,0)", etc. 
     76 
     77The following example demonstrates uses of the ''style'' property: 
     78 
     79{{{ 
     80element.style.width = "150px"; 
     81if element.style.float != "none": 
     82        element.style.clear = "left"; 
     83}}} 
     84 
     85=== Methods === 
     86 
     87|| '''Rocket methods''' || '''Brief description''' || '''Equivalent DOM method''' || 
     88|| ''AddEventListener()'' || Register an event handler to a specific event type on the element. || ''addEventListener()'' || 
     89|| ''AppendChild()'' || Insert a node as the last child node of this element. || ''appendChild()'' || 
     90|| ''Blur()'' || Removes keyboard focus from the current element. || ''blur()'' || 
     91|| ''Click()'' || Simulates a click on the current element. || ''click()'' || 
     92|| ''DispatchEvent()'' || Dispatch an event to this node in the DOM. || ''dispatchEvent()'' || 
     93|| ''Focus()'' || Gives keyboard focus to the current element. || ''focus()'' || 
     94|| ''GetAttribute()'' || Retrieve the value of the named attribute from the current node. || ''getAttribute()'' || 
     95|| ''GetElementById()'' || Returns an object reference to the identified element. || ''getElementById()'' || 
     96|| ''GetElementsByTagName()'' || Retrieve a set of all descendant elements, of a particular tag name, from the current element. || ''getElementsByTagName()'' || 
     97|| ''HasAttribute()'' || Check if the element has the specified attribute, or not. || ''hasAttribute()'' || 
     98|| ''HasChildNodes()'' || Check if the element has any child nodes, or not. || ''hasChildNodes()'' || 
     99|| ''InsertBefore()'' || Inserts the first node before the second, child, node in the DOM. || ''insertBefore()'' || 
     100|| ''RemoveAttribute()'' || Remove the named attribute from the current node. || ''removeAttribute()'' || 
     101|| ''RemoveChild()'' || Removes a child node from the current element. || ''removeChild()'' || 
     102|| ''ReplaceChild()'' || Replaces one child node in the current element with another. || ''replaceChild()'' || 
     103|| ''ScrollIntoView()'' || Scrolls the page until the element gets into the view. || ''scrollIntoView()'' || 
     104|| ''SetAttribute()'' || Set the value of the named attribute from the current node. || ''setAttribute()'' || 
     105 
     106== Dispatching events == 
     107 
     108Events can be generated on an element from within Lua with the ''DispatchEvent()'' function. When calling this function, the parameters are given as a Lua table; Rocket will convert this into a Rocket dictionary when the C++ event is created. 
     109 
     110{{{ 
     111element.DispatchEvent("open", {"object" = "trapdoor", "priority" = 11}, false) 
     112}}} 
     113 
     114Parameter keys must be strings, and values must be strings, integers, floating-point, boolean, or Lua userdata. 
     115 
     116== Creating elements == 
     117 
     118Elements can be created dynamically in Lua using the document's ''CreateElement()'' or ''CreateTextNode()'' method. The following code sample uses ''CreateElement()'' to dynamically create a form control. 
     119 
     120{{{ 
     121input_element = document.CreateElement("input") 
     122input_element.SetAttribute("type", "radio") 
     123input_element.SetAttribute("name", "graphics") 
     124input_element.SetAttribute("value", "ok") 
     125document.AppendChild(input_element) 
     126 
     127text_element = document.CreateTextNode("OK") 
     128document.AppendChild(text_element) 
     129}}}