A way for jquery developers to use the closure library in a familiar way
Thanks to JQuery and Zepto for ideas and the Google guys for the great set of closure tools
You can initialize a new G object by:
G(selector)
the selector can either be an array, an element or a string. The string should be in one of the below forms and selects an array of elements:
- "#elementId"
- ".className"
- "tagName[ .className]"
the className is optional in the last selector. You can also pass through an element as a second argument to the G object with the above selectors to use as a root node to search from.
If you are using a browser that supports querySelectorAll then G will use that and any valid selector for that browser can be passed.
You can also add in some css filters by putting :filter at the end of the string. Here are the filters that are supported
- visible
- hidden
- selected
- checked
- first
- last
- even
- odd
You can now also change the selector engine. Just call GG.setSelectorEngine(engine). The engine should be a function that takes in a selector and an optional element to look under. Some valid engines include goog.dom.query and Sizzle.
You can also set the matches engines with GG.setMatchesEngine(engine) which should take an element and a selector string. A valid matches engine could be Sizzle.matchesSelector. The default matching engine may use getEls to match an element if there is no native matchesSelector function.
will return an array of elements matching the selector underneath the optional context element
returns whether an element matches a selector string or function that returns a boolean
extends the first object with the objects given and return that object
wait milliseconds before running the function with the optional handler, return id for use with GG.clearWait
clear the function to be run
whether the container element holds the child element in it's tree
the data that was saved on the element
sets the data for an element and returns the element in a G object
runs the callback function taking the element and the index on each element in the collection, returning the collection in a G object
filters an array based on the filter function. You can optionally invert the results
returns the index of a value in the array optionally searching from an index
returns an array where the values are transformed by a function that takes in the element and the index and returns the new value
changes an object in to a parameter string where arrays are broken out: a = [1,2] -> a=1&a=2 and objects are recursively given square brackets a = {a:1, b:2} -> a[a]=1&a[b]=2
merge two arrays together
convert a json string to an object
binds 'this' to the given object for a function
trims whitespace from a string
removes duplicates from an array
a blank function
function takes two elements and returns -1,0,1 - same as native Array sort
same as native Array reverse
accepts a function to run on each element in the array. The function takes three arguments. The value, the index and the array. You can pass in an optional handler to be the "this" in the array. returns the G object
the function can take three arguments, the value, the index and the array and should return true or false. An optional handler can be passed in to act as this. a new G object containing only the values returning true will be returned.
You can also pass in ":odd",":even",":first" or ":last" to get those elements
returns the inverse of filter
the function can take three arguments, the value, the index and the array and should return a value that will replace the existing value. An optional handler can be passed in to act as this. a new G object containing only the values returning true will be returned
will return whatever object is at the index in G. Negative values count from the end of G backwards
returns the first object in G
returns the last element
returns the elements as an array
return G object with just the element at index
returns the length of G
adds an array on to the end of G
removes an element from G
tests if the array contains an object
these will only work on G objects containing elements
sets a style on all elements
sets a hash map of style keys and values on each element
gets the computed style on the first element for name
returns the top offset of the first element
sets the element.style.top attribute
returns the left offset of the first element
sets the element.style.left attribute
returns the width of the first element
sets the element.style.width attribute
returns the height offset of the first element
sets the element.style.height attribute
returns the index of the first element that matches a selector
return a G with all the elements matching the selector under the current elements in G
returns if it hasn't been set to invisible
pass in a function that takes the element and returns a boolean, or a boolean
makes the elements visible
hides the elements
return a G array holding the values of the attribute for the element
sets the attribute pair, or pairs in an object to the elements
return a G array holding the values of the "data-string" attribute for the element
sets the data pair of the elements
removes the nodes from the document
replaces the first element in G with the element and returns a new G with the new node only
returns a G array of the value of form elements
sets the value of form elements
remove all child nodes under the elements
return the next sibling of the node
return the previous sibling of the node
returns a G with the children of the elements optionally filtering on a selector
returns a G or the unique parents
removes elements that match the selector fromt he document
adds class to all elements taking string or function
removes a class from all elements taking a string or function
toggles the className, optionally pass a function that returns a boolean given the element or a boolean
return a G array of the elements that have the given class
returns a G which is filtered based on the selector
appends the input to all elements, can be a function, string or Node/NodeList or G, will clone the input if there is more than one element
appends all the elements to the element which matches the input which can be a G, element, selector or function returning and element
inserts the input after the element which can be a function, string or Node/NodeList or G. If there is more than one element then the input is cloned
puts the elements after the input which can be a function, string or Node/NodeList or G
inserts the input before the element which can be a function, string or Node/NodeList or G. If there is more than one element then the input is cloned
puts the elements before the input which can be a function, string or Node/NodeList or G
returns a G with cloned nodes, can pass whether to do a deep copy
return the innerHTML of the elements
sets the innerHTML of the elements taking a string or function
returns the outerHTML of all elements together
gets the textContent of the elements
sets the textContent of the elements taking a string or function
these are usually used for element arrays, but can also be applied to EventTargets
same as goog.events.listen
pass an array of listener ids to turn them off, passes back if it was succesfule
takes in the event type (i.e. goog.events.CLICK or "click"), an optional selector (must be a string) to test against the target, optional data to get on event.data (must be an Object), the handling function which takes in the Event, an optional handler to be used as "this" and an optional eventObject (use this with goog.ui.Component and pass in this.getHandler()). returns a list of uids that can be passed to G.off
alias for on
removes an event registered with on, passes back it it was succesful
alias for off
dispatch the event (defaults to UIEvent) on all elements
the same as bind but the eventType is set to click for you
- .focus()
- .blur()
- .mouseup()
- .mousedown()
- .mouseover()
- .mouseout()
- can now change the selector engine
- visible, html, text, addClas, removeClass, toggleClass can take a function
- added parent
- new functions after/insertAfter/before/insertBefore/outerHTML
- handles document fragments by getting their child nodes
- uses document fragments to speed up append operations
- will clone nodes when there are multiple elements to append to
- if this.getHandler() to .on() is passed then get it's id
- more functions can take a G object (including the constructor)
- small enhancements in code
- on now mimics JQuery's latest (see signature above) and code comments
- can use - for first selector character instead of .
- util functions moved to GG
- can use $ or $$ instead of G or GG (not safe, overwrites $ and $$)
- blur and focus are called using capture phase & IE uses focusin and focus out (see quirksmode)
- more utility functions
- behaves more like jquery
- more docs
- some css filters work
- fixes throughout
- passed compilation
- passes strict linting
- fixed compile warnings
- removed proto
- tested and working in IE8
- reverse, sort from array
- toArray to get back simple array
- filter can accept strings
- height, width, top, left
- contains
- append
- addClass, removeClass
- change bind to on and off
- use querySelectorAll when available
start versioning