Simple State Manager (SSM for short) is a javascript state manager for responsive websites. It is built to be light weight, has no dependencies (except javascript of course) and aims to be really easy to simply drop into your project ready to use.
DownloadAs a state manager, SSM allows you to target different javascript towards different states of your site. It allows you to define as many states as your site requires and allows you to have independent Enter, Leave and Resize events for each of the states.
To get started with SSM the first step is to include it in your project, you can do this in two ways
bower install SimpleStateManager
With Simple State Manager you can add multiple states based on your needs, the most simple way to add a state is to simply pass the information about your state to SSM using ssm.addState
. You are able to add as many states as you need, your states are able to overlap and your states can each have their own onEnter
, onLeave
and onResize
events. It is important to remember that the if you do not pass minWidth and maxWidth values they will revert to the default values (0 for minWidth and 99999 for maxWidth)
ssm.addState({ id: 'mobile', maxWidth: 767, onEnter: function(){ console.log('enter mobile'); } }); ssm.addState({ id: 'tablet', minWidth: 768, maxWidth: 1023, onEnter: function(){ console.log('enter tablet'); } }); ssm.addState({ id: 'desktop', minWidth: 1024, onEnter: function(){ console.log('enter desktop'); } });
If you wish to use one command to add multiple states using one command you can use ssm.addStates to which you should pass an array of states.
ssm.addStates([ { id: 'mobile', maxWidth: 767, onEnter: function(){ console.log('enter mobile'); } }, { id: 'tablet', minWidth: 768, maxWidth: 1023, onEnter: function(){ console.log('enter tablet'); } }, { id: 'desktop', minWidth: 1024, onEnter: function(){ console.log('enter desktop'); } } ]);
Sometimes it may be necessary to remove a state, if we have the id for the state we can easily remove the state, to remove the mobile state from our above example we simply use:
ssm.removeState('mobile');
Or if you want to remove multiple at the same time
ssm.removeStates(['tablet', 'mobile']);
Once you have finished setting up your states you should run ssm.ready()
which will setup the states.
ssm.ready();
As SSM is chaninable you can fire the ssm.ready()
method by simply adding .ready()
onto your original command
ssm.addState({ id: 'mobile', maxWidth: 767, onEnter: function(){ document.getElementById('hero').style.backgroundColor = "#daa23e"; } }).ready();
In SimpleStateManager options we pass to our states are called config options, we are able to add new config options to SimpleStateManager using ssm.addConfigOption
ssm.addConfigOption({name:"maxWidth", test: function(){ if(typeof this.state.maxWidth === "number" && this.state.maxWidth >= this.browserWidth){ return true; } else{ return false; } }});
This is the same method that SimpleStateManager uses to add the maxWidth config option so the example above is taken direct from the SimpleStateManager source. As you will notice we can use this
to access the state we are testing to determine whether the test should pass or fail.
Some simple examples of tests that you could add to SimpleStateManager are:
Method | Description |
---|---|
ssm.addState | Add a new state, expects an object literal, properties avaliable - id (optional), minWidth (optional), maxWidth (optional), onEnter (optional), onResize (optional), onLeave (optional) |
ssm.updateState | Update an existing state, expects two parameters, firstly the id of the state you wish to update and secondly an object literal of updated settings, properties avaliable - id (optional), minWidth (optional), maxWidth (optional), onEnter (optional), onResize (optional), onLeave (optional) |
ssm.addStates | Add multiple new states, expects an array of object literals, properties avaliable - id (optional), minWidth (optional), maxWidth (optional), onEnter (optional), onResize (optional), onLeave (optional) |
ssm.removeState | Remove a state, expects one property, the id of the state to be removed. |
ssm.removeStates | Remove multiple states, expects an array of strings |
ssm.removeAllStates | Clears all states from SSM |
ssm.getStates | By default ssm.getStates() will return all the states added to SSM however you optionally can pass an array of ID's of the states you want e.g ssm.getStates([ 'mobile' ,'desktop']); |
ssm.getCurrentStates | Get the currently applied states |
ssm.isActive | Check if a state is active using the ID you assigned, e.g. ssm.isActive("desktop"); |
ssm.ready | Tells the page that states have been added and we can fire the onEnter event for the current state/states. |
ssm.getBrowserWidth | Get the current value of the browser width |
ssm.setResizeTimeout | Set the value of the timeout used for the resize debounce |
ssm.getResizeTimeout | Get the current value the resize timeout |
ssm.addConfigOption | SSM allows you to define new rules by which a state can be enabled and disabled using ssm.addConfigOption . The method takes an object with 2 values as the parameter, firstly the name of the config option and secondly the method to test the option. Please note the test must return true or false to allow SSM to know if the test has passed or failed. A typical example of how you can add a test ssm.addConfigOption({name: "minHeight", test: function(){ return true; } }); |
One of the core features we integrated into SimpleStateManager 2.x is that we expose a lot more API's to access some of the values that are used everyday in responsive sites. The benefit of this is that it means SSM can be extended with new plugins that add extra functionality with ease.
If you decide to distribute your plugins and you add them to bower, please prefix the name of your plugin with SSM-
so that they are easy to find.
;(function ( $, window, document, undefined ) { var pluginName = "defaultPluginName", defaults = { propertyName: "value" }; // The actual plugin constructor function Plugin ( element, options ) { this.init(); } Plugin.prototype = { init: function(){ } } if(!ssm[pluginName]){ ssm[pluginName] = new Plugin(); } })( jQuery, window, document );
There are a growing number of official plugins for SimpleStateManager
While working on SSM 2.0 we explored ways in which we could make SSM easier to debug, we started by making a lot more of the SSM variables accessable however we wanted to take it a step further. Due to security restrictions of the browser a Chrome Extension wouldn't work so instead we decided to create a simple bookmarklet which adds a debugger to the page.
The debugger is very much an early alpha build and will be improved over time.
To get started with the SSM 2.0 debugger simply drag the SimpleStateManager Bookmarklet to your bookmarks bar
There are a number of browsers that we have tested support, as we test more browsers we will update this list
SimpleStateManager follows Semantic Versioning 2.0.0
Anybody is welcome to contribute to this project, this is a community led library and you can get involved in the following ways
Typically master is the latest version of SSM, new features are pushed into branches and when a branch has been fully tested and all the features are complete it will be merged into master. The milestone assigned in GitHub should be used for the branch name.
License: MIT (http://www.opensource.org/licenses/mit-license.php)
The MIT License (MIT)
Copyright (c) 2013 Jonathan Fielding
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.