Doc-amd
Doc-amd library
doc.js is a small library to manipulate the DOM in any browser. We focused on the most used and common use cases to create this. This library uses amd structure.
Why?
We needed a small footprint alternative for jquery and still have the ease-of-use for the most commonly used methods on it.
Then, we built it from scratch.
Installation
Install with Npm: npm install elo7-doc-amd
Dependencies
Doc-amd depends on an amd implementation. We suggest define-async implementation for dependency lookup. Doc-amd also depends on events-amd.
Methods
doc
doc(querySelector)
doc(HTMLCollection)
doc(NodeList)
doc(Array)
doc(Node)
Description:
Returns a Doc object with all the methods below.
Parameters:
querySelector: String //A CSS selector. Note that, if it is a class name with dots, the dots must be escaped. E.g.: doc(".my\.class")
Sample:
;
els
.els()
Description:
Returns an array with matched Elements.
Sample:
;
size
.size
Description:
Returns the length of elements.
Sample:
;
each
Usage:
.each(callback)
Description:
Iterate through a Doc object, executing a Function on each one of them.
Parameters:
callback: Function() //A function to call.
Sample:
;
data
.data(key [,value])
Description:
Get or set the value of a data-* attribute
Parameters:
key: String //A string naming the piece of data to set or get.
value: String //The new data value.
Sample:
;
val
.val([value])
Description:
Get or set the value of the element
Parameters:
value: String //The new value.
Sample:
;
html
.html([value])
Description:
Get or set the value of the inner html
Parameters:
value: String //The new value.
Sample:
;
prepend
.prepend(element)
Description:
Prepends the existing DOM element, doc-amd element or text as child of the Doc object
Parameters:
element: Element //The element you want to prepend as a child of your selector.
Sample:
;
append
.append(element)
Description:
Append the DOM element as child of the Doc object
Parameters:
element: Element //The element you want to append as a child of your selector.
Sample:
;
text
.text(value)
Description:
Set the inner text of the element
Parameters:
value: String //New value for the text of the element.
Sample:
;
attr
.attr(key [,value])
or .attr(object)
Description:
Get or set the value of the attribute
Parameters
key: String //A string naming the attribute to set or get. value: String //The new value of the attribute.
or
value: Object //A javascript object
Sample:
;
hasClass
.hasClass(class)
Description:
Verify if the Doc object has the CSS class
Parameters:
class: String //The CSS class of the element. Return boolean.
Sample:
Lorem
;
addClass
.addClass(class)
Description:
Adds a CSS class to the element.
Parameters:
class: String //The CSS class of the element.
Sample:
;
removeClass
.removeClass(class)
Description:
Removes the CSS class from the element.
Parameters:
class: String //The CSS class of the element.
Sample:
;
toggleClass
.toggleClass(class)
Description:
Adds or removes the CSS class from the element.
Parameters:
class: String //The CSS class you want to add or remove.
Sample:
;
removeItem
.removeItem()
Description:
Remove the element from the DOM.
Sample:
;
find
.find(querySelector)
Description:
Get the descendants of the Doc object, filtered by querySelector.
Parameters:
querySelector: String //Returns the query's matched elements.
Sample:
;
closest
.closest(querySelector)
Description:
Get the first ascendant of the Doc object, filtered by querySelector.
Parameters:
querySelector: String //Should not be a composite. e.g. "#id .class" or "tag.class" due to compatibility issues. Prefer simple selectors. e.g. '.class'. Returns the query's matched elements.
Sample:
;
filter
.filter(attribute || callback)
Description:
There are two use cases for this method. If you use an attribute, it will return a Doc object containing all elements containing the attribute. If you use callback, filter() will test each element in the set against your callback and return boolean for each. The result will be a Doc object matching your Function.
Parameters:
attribute: String //A string containing an attribute to match the current set of elements against.
callback: Function() //A function used as a test for each element in the set. This is the current DOM element.
Sample:
;
first
.first()
Description:
Returns the first DOM element from the query list.
Sample:
;
last
.last()
Description:
Returns the last DOM element from the query list.
Sample:
;
previous
.previous()
Description:
Returns the query's previous Doc object.
Sample:
;
next
.next()
Description:
Returns the query's next Doc object.
Sample:
;
parent
.parent()
Description:
Returns the query's parent element.
Sample:
;
isEmpty
Description:
This method is deprecated. Use the method isPresent()
instead.
isPresent
Description:
Verifies if the element exists on the DOM. Returns boolean.
Sample:
;
checked
.checked(value)
Description:
Set or verify the checkbox or radio field checked state.
Parameters:
value: boolean //To indicate whether the element should be checked or not
Sample:
;
on
.on(event, callback [,eventCategory])
or
.on(event, callback [, { named: eventCategory, passive: passive } ])
Description:
Adds an event listener on a Doc object.
Parameters:
event: String //One or more events that you want to attach to your selector
callback: Function() //A function to call when the event is triggered
eventCategory: String //You can add multiple 'events' of the same type (e.g: click) and use the eventCategory parameter to remove certain events when needed. Use .off() to remove the attached handler.
passive: Boolean // If true, indicates that the function specified by listener will never call preventDefault()
Please note that this method does not work like jquery's .on(). If you append new elements in the page you will have to call .on again on those elements.
Sample:
;
throttle
.throttle(event, callback [,timeout] [,eventCategory])
Description:
Prevents a function from being called multiple times on a set amount of time. Useful to prevent multiple requests.
Parameters
event: String //The event you want to
callback: Function() //The function to be called
timeout: Number //The time in ms you want the application to prevent multiple calls from being made. Default value is 1000.
eventCategory: You can add multiple 'events' of the same type (e.g: click) and use the eventCategory parameter to prevent one event from being called.
Sample:
;
debounce
.debounce(event, callback [,timeout] [,eventCategory])
Description:
Prevents a function from being called multiple times before a defined amount of time. Useful to prevent multiple requests.
Parameters
event: String //The event you want to
callback: Function() //The function to be called
timeout: Number //The time in ms you want the application to prevent multiple calls from being made. Default value is 1000.
eventCategory: You can add multiple 'events' of the same type (e.g: click) and use the eventCategory parameter to prevent one event from being called.
Sample:
;
off
.off(event [,eventCategory])
Description:
Removes the event listener from the Doc object.
Parameters:
event: String //The event you want to remove from your selector
eventCategory: String //Removes the attached handler of this category. See .on() for details.
Sample:
;
trigger
.trigger(event [, data])
Description:
Dispatches an event that can be listened using on()
.
Parameters:
event: String //The event you want to trigger for your selector
data: Additional data for custom triggered events
Sample:
;
;
selectedText
.selectedText()
Description:
Returns the selected text on a given input field.
Sample:
;
focus
.focus()
Description:
Focus the first matched element
Sample:
;
removeAttr
.removeAttr(attrName)
Description:
Removes the attribute from the element.
Parameters:
attrName: String //The name of the element's attribute.
Sample:
;
scrollIntoView
.scrollIntoView([options])
Description:
Makes the first element matched by Doc visible at the top of the viewport.
Parameters:
options: boolean //To indicate whether the element should be aligned to the top of the visible area (default) or to the bottom object //An object with the properties block ("start" or "end", indicating the same as the boolean version above) and behavior ("auto" or "instant" or "smooth")
Sample:
;
broadcast
doc.broadcast(event [, data])
note: WITHOUT selector
Description:
Dispatches a global event that can be listened using on()
by multiple elements.
Important note:
If you wish to use this function in IE8, you need to use the indexOf
polyfill for arrays before usage.
ArrayprototypeindexOf||Arrayprototype{var n;ifnull==thisthrow '"this" is null or not defined';var e=Objectthisi=elength>>>0;if0===ireturn-1;var a=+t||0;ifMath===1/0&&a=0a>=ireturn-1;forn=Math;i>n;ifn in e&&en===rreturn n;n++return-1};
Parameters:
event: String //The event you want to trigger globally
data: Additional data for custom triggered events
Sample:
;
insertBefore
.insertBefore()
Description:
Inserts an element before each matched element
Sample:
;
insertAfter
.insertAfter()
Description:
Inserts an element after each matched element
Sample:
;
Script Commands
Script | Description |
---|---|
npm start |
Start the server. |
npm run start:dev |
Run the start script and then the watch script. |
npm run test |
Run test locally. |
npm run test:ci |
Run the server and the tests. Useful for a quick local check and for CI environment. |
License
Doc-amd is released under the BSD. Have at it.
Copyright ©️ 2015 Elo7# doc-amd