InceptionJS
A collection of standard methods & utilites to use as a JS starting point for new projects.
INSTALLING WITH BOWER
If you want to it as is, you can install it with Bower. You can add it to your bower file as like so:
{
"name": "my-app",
"version": "0.1.0",
"dependencies": {
"inception" : "latest"
}
}
or to install manually, run the following command from terminal:
bower install inception
Inception [ INC ]
File: inception.js
The boilerplate is using the INC namespace. Feel free to customize this to match the name of your app. But all documentation will assume the INC namespace.
INC.config
Object
Lets you set basic config options for the site, such as debug mode to enable/disable logging to the console.
INC.init()
Function
Initialize any methods or functions you want to call/initialize on load. I recommend using this to call init functions of any modules that you choose to load.
Example
INC.Browser.init();
INC.Storage.init();
Include the following at the bottom of your HTML file to initialize everything.
window.INC.init();
INC.bind()
Function
Use this function for things like binding actions to elements (button click events for example). I recommend calling it with the INC.init() function.
INC.log()
Function
Mimics console.log(), but includes a fallback for browsers that don't support the console, can be disabled by setting INC.debug = false.
INC.info()
Function
Mimics console.info(), but includes a fallback for browsers that don't support the console, can be disabled by setting INC.debug = false.
INC.error()
Function
Mimics console.error(), but includes a fallback for browsers that don't support the console, can be disabled by setting INC.debug = false.
INC.warn()
Function
Mimics console.warn(), but includes a fallback for browsers that don't support the console, can be disabled by setting INC.debug = false.
INC.Browser
File: modules/browser.js
Basic utilities for interaction with the browser.
Browser.config()
Function
Basic config settings. Used to set px width used to determine if browesr is considered tablet or mobiile sized.
Browser.info()
Function
Returns basic info about the browser:
Browser.info('name'); // returns: chrome, firefox, ir, opera, safari,
Browser.info('version'); // return browser version: i.e. ""33""
Browser.domain()
Function
Return your sites domain name (useful determining if you're on a dev or production server for example). Returns just the domain name, ie. google.com
INC.Browser.domain()
Browser.forward()
Function
Use it to forward to another URL. Accepts an optional delay variable (if you omit it, it will forward immediately).
Call it:
INC.Browser.forward('http://google.com', 5000) // will recirect to http://google.com after 5 seconds
Browser.go()
Function
Go to a specific URL immediately. Useful for mimicking links (on a button for example).
Call it:
INC.Browser.go('/info.html') // will go to a /info.html page
Browser.isMobile()
Function
Use is to determine if the user is on a mobile-ish sized device (default, 768px wide or smaller)
Call it:
INC.Browser.isMobile()
Browser.isTablet()
Function
Use is to determine if the user is on a tablet-ish sized devoce (default, 1024px wide or smaller)
Call it:
INC.Browser.isTablet()
Browser.page()
Function
Will return the current page.
Call it:
INC.Browser.page() // For example if you are on http://mysite.com/controller/action, it will return the string "action".
Browser.path()
Function
Will return the full path of the current URL.
Call it:
INC.Browser.()/ / For example if you are on http://mysite.com/controller/action, it will return the string "controller/action".
Browser.query()
Function
Use it to find the value of url parameters. For example http://domain.com/search?query=sharknado&language=en
Call it:
// ASSUMING THE ABOVE URL STRUCTURE...
INC.Browser.get('search') // Will return "sharknado"
INC.Browser.get('language') // Will return "en"
Browser.segment()
Function
Use is to get the value of a specific URL segment:
Call it:
INC.Browser.segment(2) // For example if you are on http://mysite.com/controller/action, it will return the string "action".
Browser.url()
Function
Returns the full base URL of your site (useful for building links) ie. https://google.com
Call it:
INC.Browser.url()
INC.Form
File: _modules/form.js
Form.Submit()
Function
Submit a form with a given ID. Accepts 3 parameters: id (id of the form), action (allows you to override the URL the form will submit to), callback (optional callback function before form submission. Will pass the form id and action.)
Example:
myFunc( id ) {
INC.log('Form ' + id + ' is submitted.');
}
INC.Form.Submit('myform', 'users/login', myFunc)
INC.Storage
File: _modules/storage.js
This allows you to interact with both cookies and local storage.
Storage.cookieDispose()
Function
INC.Storage.cookieDispose('username'); // Deletes cookie named "username"
Storage.cookieRead()
Function
INC.Storage.cookieRead('username'); // returns value of cookie named "username"
Storage.cookieWrite()
Function
INC.Storage.cookieWrite('username', 'foobar'); // Create cookie named "username" with a value of "foobar"
Storage.check()
Function
INC.Storage.check(); // Returns true or fale and sets value of Storage.config.isAvailable to the same
Storage.clear()
Function
INC.Storage.clear(); // Clears all local storage
Storage.(get)
Function
INC.Storage.get('username'); // If local storage is available, gets value of "username" from storage, otherwise get value of "username" cookie
Storage.remove()
Function
INC.Storage.remove('username'); // Removes "username" from local storage, and deleted "uswername" cookie if it exists
Storage.set()
Function
INC.Storage.get('username', 'foobar'); // If local storage is available, sets value of "username" in storage as "foobar", otherwise creates a "username" cookie
INC.Utility
File: _modules/utility.js
Utility.arrayUnique()
Function
if you have an with duplate entries, this will remove the duplicates and return only the uniqe entries
var myArray = new Array('thing1', 'thing2', 'thing3', 'thing2')
console.log (myArray) // outputs ["thing1", "thing2", "thing3", "thing2"]
myArray = INC.Utility.Utility.arrayUnique()
console.log (myArray) // outputs ["thing1", "thing2", "thing3"]
Utility.isNumber()
Function
Tells you if a given value is a number or not.
INC.Utility.isNumber(4) // true
INC.Utility.isNumber("bobcat") // false
Utility.scrollTo()
Function
INC.Utility.scrollTo('my-div') // scroll the page to #my-div element
Utility.titleCase()
Function
INC.Utility.titleCase('the quick brown fox') // return ""The Quick Brown Fox""