Q.Tool Class
Returns Q.Event which occurs when a tool has been activated Generic callbacks can be assigned by setting toolName to ""
Constructor
Item Index
Methods
- $
- _loadToolScript
- ancestor
- byId static
- byName static
- calculateId static
- calculatePrefix static
- child
- children
- children
- clear static
- define static
- define.options static
- encodeOptions static
- forEachChild
- from static
- getElementsByClassName
- jQuery static
- jQuery.options static
- parent
- parentIds
- parents
- remove static
- remove
- rendering
- setUpElement
- setUpElement static
- setUpElementHTML
- setUpElementHTML static
- sibling
- stateChanged
- toString
Methods
$
-
selector
If jQuery is available, returns jQuery(selector, this.element). Just a tiny Backbone.js-style convenience helper; this.$ is similar to $, but scoped to the DOM tree of this tool.
Parameters:
-
selector
StringjQuery selector
Returns:
jQuery object matched by the given selector
_loadToolScript
-
toolElement
-
callback
-
[shared]
-
[parentId]
Loads the script corresponding to a tool
Parameters:
Returns:
whether the script needed to be loaded
ancestor
-
name
Returns the closest ancestor, if any, with the given tool name If more than one tool is activated with the same parent id, returns the first one.
Parameters:
-
name
String
Returns:
byId
-
id
-
name
Reference a tool by its id
Parameters:
Returns:
calculateId
-
id
Computes and returns a tool's id from some string that's likely to contain it, such as an HTML element's id, a tool's id, or a tool's prefix.
Parameters:
-
id
Stringthe id or prefix of an existing tool or its element
Returns:
calculatePrefix
-
id
Computes and returns a tool's prefix
Parameters:
-
id
Stringthe id or prefix of an existing tool or its element
Returns:
child
-
append
-
[name=""]
Gets one child tool contained in the tool, which matches the prefix based on the prefix of the tool.
Parameters:
Returns:
children
-
[name=""]
-
[levels=null]
Gets child tools contained in the tool, as determined by their ids.
Parameters:
Returns:
A two-level hash of pairs like {id: {name: Tool}}
children
()
Object
Gets sibling tools activated on the same element
Returns:
pairs of {normalizedName: tool}
clear
-
elem
-
removeCached
Traverses children in a particular container and removes + destroys all tools. Should be called before removing elements.
Parameters:
-
elem
HTMLElementThe container to traverse
-
removeCached
BooleanDefaults to false. Whether the tools whose containing elements have the "data-Q-retain" attribute should be removed...
define
-
name
-
ctor
-
[require]
-
defaultOptions
-
stateKeys
-
methods
Call this function to define a tool
Parameters:
-
name
String | ObjectThe name of the tool, e.g. "Q/foo". Also you can pass an object containing {name: filename} pairs instead.
-
ctor
FunctionYour tool's constructor. You can also pass a filename here, in which case the other parameters are ignored.
-
[require]
String | Array optionalOptionally name another tool (or array of tool names) that was supposed to already have been defined. This will cause your tool's constructor to make sure the required tool has been already loaded and activated on the same element.
-
defaultOptions
ObjectAn optional hash of default options for the tool
-
stateKeys
ArrayAn optional array of key names to copy from options to state
-
methods
ObjectAn optional hash of method functions to assign to the prototype
Returns:
The tool's constructor function
define.options
-
toolName
-
setOptions
Call this function to define default options for a tool constructor, even if has not been loaded yet. Extends existing options with Q.extend().
Returns:
the resulting pending options for the tool
encodeOptions
-
options
Returns a string that is already properly encoded and can be set as the value of an options attribute
Parameters:
-
options
Objectthe options to pass to a tool
Returns:
forEachChild
-
[name=""]
-
[levels]
-
[withSiblings=false]
-
callback
Do something for every and future child tool that is activated inside this tool
Parameters:
-
[name=""]
String optionalFilter by name of the child tools, such as "Q/inplace"
-
[levels]
Number optionalOptionally pass 1 here to get only the immediate children, 2 for immediate children and grandchildren, etc.
-
[withSiblings=false]
Boolean optionalOptionally pass true here to also get the sibling tools activated on the same element
-
callback
FunctionThe callback to execute at the right time
from
-
toolElement
-
[toolName]
Returns a tool corresponding to the given DOM element, if such tool has already been constructed.
Parameters:
Returns:
the tool corresponding to the given element, otherwise null
getElementsByClassName
-
className
Returns all subelements with the given class name.
Parameters:
-
className
Stringthe class name to look for
Returns:
a list of nodes with the given class name.
jQuery
-
name
-
ctor
-
defaultOptions
-
stateKeys
-
methods
Call this function to define a jQuery plugin, and a tool with the same name that uses it.
Parameters:
-
name
StringThe name of the jQuery plugin and tool, e.g. "Q/foo"
-
ctor
FunctionYour jQuery plugin's constructor
-
defaultOptions
ObjectAn optional hash of default options for the plugin
-
stateKeys
ArrayAn optional array of key names to copy from options to state
-
methods
ObjectAn optional hash of method functions to assign to the prototype
jQuery.options
-
pluginName
-
setOptions
Call this function to define default options for a jQuery tool constructor, even if it has not been loaded yet.
Returns:
the resulting pending options for the tool
parent
()
Q.Tool | Null
Returns the immediate parent tool, if any, by using parentIds(). If more than one tool is activated with the same parent id, returns the first one.
Returns:
parentIds
()
Array | Null
Gets the ids of the parent, grandparent, etc. tools (in that order) of the given tool
Returns:
parents
()
Object
Gets parent tools, as determined by parentIds() Note that several sibling tools may be activated on the same tool id.
Returns:
A two-level hash of pairs like {id: {name: Q.Tool}}
remove
-
elem
-
removeCached
Traverses elements in a particular container, including the container itself, and removes + destroys all tools. Should be called before removing elements.
Parameters:
-
elem
HTMLElementThe container to traverse
-
removeCached
BooleanDefaults to false. Whether the tools whose containing elements have the "data-Q-retain" attribute should be removed.
remove
-
removeCached
Called when a tool instance is removed, possibly being replaced by another. Typically happens after an AJAX call which returns markup for the new instance tool. You should call Q.Tool.remove unless, for some reason, you plan to remove this exact tool instance, and not its children or siblings.
Parameters:
-
removeCached
BooleanDefaults to false. Whether or not to remove the actual tool if its containing element has a "data-Q-retain" attribute.
Returns:
Returns whether the tool was removed.
rendering
-
fields
-
callback
-
[key=""]
-
[dontWaitForAnimationFrame=false]
When implementing tools, use this to implement rendering markup that can vary as a function of the tool's state (with no additional side effects).
Parameters:
-
fields
Array | StringThe names of fields to watch for, either as an array or comma-separated string. When stateChanged is called, if one of the fields named here really changed, the callback will be called.
-
callback
FunctionThe callback, which receives (changed, previous, timestamp). By default, Qbix defers the execution of your rendering handler until the next animation frame. If several calls to tool.stateChanged occurred in the meantime, Qbix aggregates all the changes and reports them to the rendering handler. If a field in the state was changed several times in the meantime, those intermediate values aren't given to the rendering handler, since the assumption is that the view depends on the state without any side effects. However, if the field was changed, even if it later went back to its original value, it will show up in the list of changed fields.
-
[key=""]
String optionalOptional key used when attaching event handlers to tool.Q.onStateChanged events.
-
[dontWaitForAnimationFrame=false]
Boolean optionalPass true here if you really don't want to wait for the next animation frame to do rendering (for example, if you insist on reading the DOM and will use a library like FastDOM to manage DOM thrashing)
setUpElement
-
element
-
toolName
-
toolOptions
-
id
Sets up element so that it can be used to activate a tool For example: $('container').append(Q.Tool.setUpElement('div', 'Streams/chat')).activate(options); The prefix and id of the element are derived from the tool on which this method is called.
Parameters:
Returns:
Returns an element you can append to things
setUpElement
-
element
-
toolName
-
[toolOptions]
-
[id=null]
-
[prefix]
Sets up element so that it can be used to activate a tool For example: $('container').append(Q.Tool.setUpElement('div', 'Streams/chat')).activate(options);
Parameters:
-
element
String | ElementThe tag of the element, such as "div", or a reference to an existing Element
-
toolName
String | ArrayThe type of the tool, such as "Q/tabs", or an array of types
-
[toolOptions]
Object | Array optionalThe options for the tool. If toolName is an array, this is the array of corresponding objects to use for options.
-
[id=null]
String | Function optionalOptional id of the tool, such as "Q_tabs_2", used if element doesn't have an "id" attribute. If null, calculates an automatically unique id beginning with the tool's name
-
[prefix]
String optionalOptional prefix to prepend to the tool's id
Returns:
Returns an element you can append to things
setUpElementHTML
-
element
-
toolName
-
toolOptions
-
id
Returns HTML for an element that it can be used to activate a tool. The prefix and id of the element are derived from the tool on which this method is called. For example: $('container').append(Q.Tool.make('Streams/chat')).activate(options);
Parameters:
Returns:
Returns HTML that you can include in templates, etc.
setUpElementHTML
-
element
-
toolName
-
toolOptions
-
[id]
-
[prefix]
Returns HTML for an element that it can be used to activate a tool
Parameters:
-
element
String | ElementThe tag of the element, such as "div", or a reference to an existing Element
-
toolName
StringThe type of the tool, such as "Q/tabs"
-
toolOptions
ObjectThe options for the tool
-
[id]
String | Function optionalOptional id of the tool, such as "Q_tabs_2"
-
[prefix]
String optionalOptional prefix to prepend to the tool's id
Returns:
Returns HTML that you can include in templates, etc.
stateChanged
-
names
Call this after changing one more values in the state. Unlike Angular and Ember, Q provides a more explicit mechanism for signaling that a tool's state has changed. Other parts of code can use the Tool.prototype.onState event factory to attach handlers to be run when the state changes.
Events
beforeRemove
Returns Q.Event which occurs when a tool is about to be removed Generic callbacks can be assigned by setting toolName to ""
Event Payload:
-
nameOrId
Stringthe name of the tool, such as "Q/inplace", or "id:" followed by tool's id
onConstruct
Q.Event which occurs when the tool was constructed
onInit
Q.Event which occurs when the tool was initialized
onInit
Returns Q.Event which occurs when a tool has been initialized Generic callbacks can be assigned by setting toolName to ""
Event Payload:
-
nameOrId
Stringthe name of the tool, such as "Q/inplace", or "id:" followed by tool's id
onRemove
Q.Event which occurs when the tool was removed
onRetain
Q.Event which occurs when the tool was retained while replacing some HTML
onStateChanged
Returns Q.Event which occurs when some fields in the tool's state changed
Event Payload:
-
name
StringThe name of the field. Can be "" to listen on all fields.