#[name]
[name] is a JavaScript library that enables developers to run, rerun, and undo
functions across window breakpoints.
##Installation
Add to project using NPM
npm install [packagename]
Import into project
import SymJs from '@thesymphonyagency/symphonyjs'
Basic usage
[name] comes with some built in actions, but the basic usage is as follows:
const actionSettings = {
windowEvents: ['ready', 'load','resize'],
breakpoints:['xs', 'sm', 'md', 'lg'],
dependency: 'body.home-page',
target: '.custom-class'
}
symJs.actionName(actionSettings)
action settings
Enqueued action settings control how and when action functions are run.
windowEvents
Controls which events the action function will run on.
Accepts an array of the following options:
breakpoints
When the resize is included in windowEvents, this setting controls
which breakpoints the action will run on. Some actions may include
undo functionality which will remove their effects on non-activated breakpoints.
Accepts an array of the following options:
dependency
This setting is determines whether or not the action is enqueued based
on whether or not the query returns any elements.
Accepts a string that can be passed to querySelectorAll
onlyOnBreakpointChanged
Enqueuing on resize will trigger a action to run a lot.
This setting will only allow the action to run when a breakpoint has changed.
runOnce
Some actions may need to be run only once, yet still have a reset function for
inactive breakpoints. Setting this to true will run the action only one time
upon entering an active breakpoint. If the reset function is called, then it
will reactivate after reentering the active breakpoint range.
Extending
TODO: Rewrite everything below that doesn't fit new model.
- Creates a new js file in
src/extensions.
- Write your function as a key/value pair, or as a named function.
module.exports = your object or function.
Option 1: Named function
The class generator will use the function's name as the prototype name
module.exports = function myNewFunction(){
}
const symphony = new SymphonyJs()
symphony.myNewFunction()
Option 2: Object
The class generator will add all keys that are functions to the class prototypes.
module.exports = {
myNewFunction: function(){
}
}
module.exports = {
myNewFunction(){
},
myNewFunction2(){
}
}
const symphony = new SymphonyJs()
symphony.myNewFunction()
symphony.myNewFunction2()
##Run functions on jQuery events (ready, load, resize)
In order to enqueue actions to run on events your function will need to make use
of some of the class methods - at the very least: this.addToQueues(). Every
action that can be enqueued or triggered must submit a common structure to
this.addToQueues() as either an object, for a single event, or array of objects.
object template:
let action = {
func: yourFunction,
options: {
windowEvents: ['ready', 'resize', 'load'],
breakpoints: ['xs', 'sm', 'md', 'lg'],
onlyOnBreakpointChange: true
}
}
-
func: can be an anonymous function, or a reference to one. This will be run by
the queuing service
-
options: The options object can contain triggers (found below), and custom values.
The entire object will be passed into func when the queuing service runs it, so feel
free to add custom stuff. Note: class level variables can be passed into the initializing
options of SymphonyJs, as this.options will be available to custom extension functions.
-
windowEvents: accepts String or Array of Strings. Defaults to 'ready'.
-
breakpoints: accepts String or Array of Strings. Defaults to all breakpoints.
-
onlyOnBreakpointChange: Boolean only runs initially, and when breakpoint changes.
##Utility functions
SymphonyJs exposes several utility functions to be used in custom functions.
processArrayOrType: Params('type', input, callback)
This function allows you to run a function once, or many times based on whether the
input is a single item, or an array of them.
*type: is the typeof of whatever parameter you're checking for.
*input: is the variable or array
*callback: is the function that will be run based on the number of inputs.
Usage:
module.exports = function sayThings(things){
this.processArrayOrType('string', things, (thing)=>{
console.log(`Saying: ${thing}`)
})
}
SymphoniJs.sayThings('Hello!')
SymphoniJs.sayTHings(['yes', 'maybe', 'no'])
domElementExists: Params(string target || Array of string targets)
*targets: 1 jQuery search per parameter as a string.
- returns: boolean
The purpose of the is to check if dependencies on the page exist before running a function.
This can be used in combination with custom action options to determine what pages the
action should be run on.
usage
module.exports = function sayThings(things){
if(this.domElementExists('body.sayThingPage')){
this.processArrayOrType('string', things, (thing)=>{
console.log(`Saying: ${thing}`)
})
}
}
##Using these functions in combination
Here is an example of creating a custom function extension of SymphonyJs
module.exports = function sayThings(optionsInput){
this.processArrayOrType('object', optionsInput, (options)=>{
if(this.domElementExists(optionsInput.dependencies)){
this.addToQueues({
func: function(options){
this.processArrayOrType('string', options.things, (thing)=>{
console.log(`Saying: ${thing}`)
})
},
options: options
})
})
}
}
const symphony = SymphonyJs()
symphony.sayThings([{
things: 'Hello',
},{
things: ['its', 'meeee'],
dependencies: '.its-me-page',
windowEvents: ['ready', 'resize'],
onlyOnBreakpointChange: true,
breakPoints: ['xs', 'md', 'lg']
}])
##extensions
###Custom()
The custom function is a useful utility to run custom JS on breakpoints.
const symphony = SymphonyJs()
symphony.custom({
windowEvents: ['ready', 'resize'],
onlyOnBreakpointChange: true,
breakPoints: ['xs', 'md', 'lg']
},
function(){
},
function(){
})