1
0
Fork 0
mirror of https://github.com/betaflight/betaflight-configurator.git synced 2025-07-24 16:55:24 +03:00

More work done…

This commit is contained in:
skaman82 2015-10-22 22:27:48 +02:00
parent b0b8cc00d7
commit 707e2c454b
70 changed files with 19666 additions and 70 deletions

6
js/libraries/switchery/.gitignore vendored Executable file
View file

@ -0,0 +1,6 @@
.DS_STORE
build/
components/
bower_components/
node_modules/
test.html

View file

@ -0,0 +1,139 @@
0.8.1 / 2015-06-26
==================
* Check disabled values properly, because of `.isDisabled()` method returning improper value
0.8.0 / 2015-04-03
==================
* Secondary jack color functionality
* Enable/disable switch dynamically
* Destroy event handlers attached to a Switchery instance
* Meteor.js integration
0.7.0 / 2014-12-09
==================
* Respect `readonly` attribute on checkbox input
* Large and small size switches
0.6.3 / 2014-11-02
==================
* Update component version
* Added separate Jack color option
* Add the CSS file to bower.json
* Fix bug in Hongmi phone
* Fixing issue found in IE9+ with change event dispatch
0.6.2 / 2014-08-22
==================
* Prevent text selection on double click
0.6.1 / 2014-07-11
==================
* Fix switch click state when inside label
0.6.0 / 2014-05-22
==================
* Corresponding label with `for` attribute of the native input, to change switch state as well
* Do not copy input's `id` and `name` attributes to switch
0.5.5 / 2014-05-14
==================
* Set secondary color to be background color on switch off state
0.5.4 / 2014-04-29
==================
* Fix box-sizing when using Bootstrap 3
0.5.3 / 2014-04-12
==================
* Detect native checkbox state change
0.5.2 / 2014-04-03
==================
* Enable event bubbles
0.5.1 / 2014-03-22
==================
* Fix jack position when switch is hidden
0.5.0 / 2014-02-07
==================
* Removes the internal validation for loaded switches and lets people do it on their own
* Sets a data attribute to handle multiple calls for a checkbox
0.4.2 / 2014-01-24
==================
* Resolve property method issues in IE
* Disable label checkbox change event
* Check if element is null
0.4.1 / 2014-01-18
==================
* Fix Event errors in IE8
0.4.0 / 2014-01-15
==================
* Use ftlabs/fastclick for mobile browser optimization
0.3.6 / 2014-01-04
==================
* Generate new standalone file
* Refactor code
* Use event constructors to create onchange and click events
* Updated standalone to dist in bower.json
* Required files
* Add development info
0.3.5 / 2013-12-31
==================
* Proper checkbox value with onchange event
0.3.4 / 2013-12-31
==================
* Merge ni-c:master
* Refactor onchange
0.3.3 / 2013-12-28
==================
* Merge pull request from tenbits:patch-1
* Leave first line empty
* Undefined option's property
* Update Readme
* Update standalone
* Changelog
* Standalone -> dist, update Readme, add changelog
0.3.2 / 2013-12-27
==================
* Standalone -> dist, update Readme, add changelog
* Merge pull request from vesln:min
* Add minified versions
* Minify CSS & JS
* Fix the standalone build & improve Makefile
* Ignore node_modules
* Add package.json
* Disabled opacity option
* Disabled opacity option
* Insert switch after target element
* Require Transitionize

89
js/libraries/switchery/Makefile Executable file
View file

@ -0,0 +1,89 @@
#
# Variables
#
NAME = Switchery
#
# Paths
#
COMPONENT_BUILD = node_modules/.bin/component-build
COMPONENT_INSTALL = node_modules/.bin/component-install
UGLIFYJS = node_modules/uglify-js/bin/uglifyjs
UGLIFYCSS = node_modules/uglifycss/uglifycss
JS_DEST = dist/switchery.js
JS_MIN_DEST = dist/switchery.min.js
CSS_DEST = dist/switchery.css
CSS_MIN_DEST = dist/switchery.min.css
#
# All
#
all: install
#
# Install
#
install: node_modules components build
#
# Make a new development build
#
build: components switchery.js switchery.css
@$(COMPONENT_BUILD) --dev
#
# Install components (+ dev)
#
components: component.json
@$(COMPONENT_INSTALL) --dev
#
# Make a standalone version that doesn't depend on component etc.
#
standalone: build components
@$(COMPONENT_BUILD) -s $(NAME) -o .
@mv build.js $(JS_DEST)
@mv build.css $(CSS_DEST)
@$(UGLIFYJS) $(JS_DEST) --output $(JS_MIN_DEST)
@$(UGLIFYCSS) $(CSS_DEST) > $(CSS_MIN_DEST)
#
# Install Node.js modules
#
node_modules:
@npm install
#
# Clean all
#
clean: clean-components clean-node
#
# Clean components & build
#
clean-components:
@rm -rf build
@rm -rf components
#
# Clean the installed Node.js modules
#
clean-node:
@rm -rf node_modules
#
# Instructions
#
.PHONY: clean build components

285
js/libraries/switchery/README.md Executable file
View file

@ -0,0 +1,285 @@
![Switchery](http://i.imgur.com/xJAM3Jq.png)
## Description
Switchery is a simple component that helps you turn your default HTML checkbox inputs into beautiful iOS 7 style switches in just few simple steps. You can easily customize switches, so that they match your design perfectly.
Supported by all modern browsers: Chrome, Firefox, Opera, Safari, IE8+
![Preview](http://i.imgur.com/0PcuTbO.jpg)
[Live Preview](http://abpetkov.github.io/switchery/)
## Installation
##### Standalone:
```html
<link rel="stylesheet" href="dist/switchery.css" />
<script src="dist/switchery.js"></script>
```
##### Component:
```shell
$ component install abpetkov/switchery
```
##### Bower:
```shell
$ bower install switchery
```
##### Rails
To use Switchery in your rails app, add this to your Gemfile:
```rails
gem 'switchery-rails'
```
Or go to [Switchery Rails gem page](https://rubygems.org/gems/switchery-rails) for more info, documentation and instructions.
##### Angular JS
For thorough installation and usage instructions on how to use Switchery with Angular JS, check out this repo: [servergrove/NgSwitchery](https://github.com/servergrove/NgSwitchery)
##### Meteor
You can install Switchery to your Meteor.js app via:
```shell
$ meteor add abpetkov:switchery
```
[Switchery on Atmosphere](https://atmospherejs.com/abpetkov/switchery)
## Usage
```js
var elem = document.querySelector('.js-switch');
var init = new Switchery(elem);
```
Use the above for the standalone version.
## Settings and Defaults
```js
defaults = {
color : '#64bd63'
, secondaryColor : '#dfdfdf'
, jackColor : '#fff'
, jackSecondaryColor: null
, className : 'switchery'
, disabled : false
, disabledOpacity : 0.5
, speed : '0.4s'
, size : 'default'
};
```
- `color` : color of the switch element (HEX or RGB value)
- `secondaryColor` : secondary color for background color and border, when the switch is off
- `jackColor` : default color of the jack/handle element
- `jackSecondaryColor` : color of unchecked jack/handle element
- `className` : class name for the switch element (by default styled in switchery.css)
- `disabled` : enable or disable click events and changing the state of the switch (boolean value)
- `disabledOpacity` : opacity of the switch when it's disabled (0 to 1)
- `speed` : length of time that the transition will take, ex. '0.4s', '1s', '2.2s' (Note: transition speed of the handle is twice shorter)
- `size` : size of the switch element (small or large)
## API
##### .destroy()
Unbinding all event handlers attached to the switch element to prepare the object for garbage collection.
##### .enable()
Enable disabled switch by re-adding event handlers and changing the opacity to 1.
##### .disable()
Disable switch by unbinding attached events and changing opacity to `disabledOpacity` value.
##### .isDisabled()
Check if switch is currently disabled by checking the `readonly` and `disabled` attributes on the checkbox and the `disabled` option set via JS. If any of those are present, the returned value is `true`.
## Examples
##### Checked
Only thing you need is to add a `checked` attribute to your checkbox input. Simple as that.
```html
<input type="checkbox" class="js-switch" checked />
```
##### Multiple switches
You can add as many switches as you like, as long as their corresponding checkboxes have the same class. Select them and make new instance of the Switchery class for every of them.
```js
var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));
elems.forEach(function(html) {
var switchery = new Switchery(html);
});
```
![Multiple](http://i.imgur.com/Ip4xy4s.jpg)
##### Multiple calls
You can filter out existing elements that have already been called by looking for `data-switchery="true"`.
##### Disabled
Use the `disabled` option to make your switch active or inactive.
```js
var switchery = new Switchery(elem, { disabled: true });
```
Customize the default opacity of the disabled switch, using the `disabledOpacity` option.
```js
var switchery = new Switchery(elem, { disabled: true, disabledOpacity: 0.75 });
```
Adding `disabled` or `readonly` attribute to the native input element will result in the switch being disabled as well.
##### Colored
You can change the primary(on) and secondary(off) color of the switch to fit your design perfectly. Accomplish this, changing the `color` and `secondaryColor` options. The jack colors are also customizable via the `jackColor` and the `jackSecondaryColor` options. Below is a good example of what you can accomplish using those.
```js
var switchery = new Switchery(elem, { color: '#7c8bc7', jackColor: '#9decff' });
```
![JackColor](http://i.imgur.com/7ztX29e.png)
or
```js
var switchery = new Switchery(elem, { color: '#faab43', secondaryColor: '#fC73d0', jackColor: '#fcf45e', jackSecondaryColor: '#c8ff77' });
```
![JackSecondaryColor](http://i.imgur.com/KS0H8ac.png)
Any other changes regarding colors you want to make, should take place in `switchery.css`.
##### Sizes
Since version 0.7.0 you can change the sizes of the switch element via `size`. Giving it a value of `small` or `large` will result in adding `switchery-small` or `switchery-large` classes respectively, which will change the switch size.
Not using this property will render the default sized switch element.
```js
var switchery = new Switchery(elem, { size: 'small' });
// ... or
var switchery = new Switchery(elem, { size: 'large' });
```
![SwitchSizes](http://i.imgur.com/TVlgvx7.png)
##### Checking state
In many cases, you'll need to have the current state of the checkbox, checked or not. I'll demostrate how to do this in the two most common situations - getting the state on click and on change.
On click:
```js
var clickCheckbox = document.querySelector('.js-check-click')
, clickButton = document.querySelector('.js-check-click-button');
clickButton.addEventListener('click', function() {
alert(clickCheckbox.checked);
});
```
On change:
```js
var changeCheckbox = document.querySelector('.js-check-change');
changeCheckbox.onchange = function() {
alert(changeCheckbox.checked);
};
```
##### Legacy browsers
If you are an adventurer and like to support legacy browsers, like IE8 and IE7, apply your favourite fix for rounded corners and box shadows and try a slightly different approach.
```js
var elems = document.querySelectorAll('.js-switch');
for (var i = 0; i < elems.length; i++) {
var switchery = new Switchery(elems[i]);
}
```
Personally I recommend using [CSS3 PIE](http://css3pie.com/). For working example you can check out the demo page.
## Development
If you've decided to go in development mode and tweak all of this a bit, there are few things you should do.
After you clone the repository, do this in your terminal ([NPM](http://npmjs.org/) required):
```shell
$ npm install
```
Add the following code before the rest:
```js
var Switchery = require('switchery');
```
Make sure you're using the `build/build.js` and `build/build.css` files and you're ready.
There are some useful commands you can use.
`$ make install` - will install Node.js modules, components etc.
`$ make build` - will create a build file
`$ make standalone` - will create a standalone and minified files
## Credits
Big thanks to:
- [Veselin Todorov](https://github.com/vesln)
## Contact
If you like this component, share your appreciation by following me in [Twitter](https://twitter.com/abpetkov), [GitHub](https://github.com/abpetkov) or [Dribbble](http://dribbble.com/apetkov).
## License
The MIT License (MIT)
Copyright (c) 2013-2015 Alexander Petkov
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.

View file

@ -0,0 +1,35 @@
{
"name": "switchery",
"main": [
"dist/switchery.js",
"dist/switchery.css"
],
"version": "0.8.1",
"homepage": "http://abpetkov.github.io/switchery/",
"author": [
"Alexander Petkov <abpetkov@gmail.com> (https://github.com/abpetkov)"
],
"description": "Component to create iOS 7 styled switches from default input checkboxes",
"keywords": [
"checkbox",
"input",
"switch",
"iOS7"
],
"license": "MIT",
"ignore": [
"build",
"components",
"node_modules",
"bower_components",
"component.json",
"Makefile",
"README.md",
".*"
],
"dependencies": {
"transitionize": "*",
"fastclick": "v0.6.11"
},
"devDependencies": {}
}

View file

@ -0,0 +1,27 @@
{
"name": "switchery",
"repo": "abpetkov/switchery",
"description": "Component to create iOS 7 styled switches from default input checkboxes",
"version": "0.8.1",
"keywords": [
"checkbox",
"input",
"switch",
"iOS7"
],
"license": "MIT",
"main": "switchery.js",
"scripts": [
"switchery.js"
],
"styles": [
"switchery.css"
],
"dependencies": {
"abpetkov/transitionize": "*",
"ftlabs/fastclick": "v0.6.11",
"component/classes": "1.2.1",
"component/events": "1.0.9"
},
"development": {}
}

64
js/libraries/switchery/dist/switchery.css vendored Executable file
View file

@ -0,0 +1,64 @@
/*
*
* Main stylesheet for Switchery.
* http://abpetkov.github.io/switchery/
*
*/
/* Switchery defaults. */
.switchery {
background-color: #fff;
border: 1px solid #dfdfdf;
border-radius: 20px;
cursor: pointer;
display: inline-block;
height: 30px;
position: relative;
vertical-align: middle;
width: 50px;
-moz-user-select: none;
-khtml-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
box-sizing: content-box;
background-clip: content-box;
}
.switchery > small {
background: #fff;
border-radius: 100%;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
height: 30px;
position: absolute;
top: 0;
width: 30px;
}
/* Switchery sizes. */
.switchery-small {
border-radius: 20px;
height: 20px;
width: 33px;
}
.switchery-small > small {
height: 20px;
width: 20px;
}
.switchery-large {
border-radius: 40px;
height: 40px;
width: 66px;
}
.switchery-large > small {
height: 40px;
width: 40px;
}

1955
js/libraries/switchery/dist/switchery.js vendored Executable file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1 @@
.switchery{background-color:#fff;border:1px solid #dfdfdf;border-radius:20px;cursor:pointer;display:inline-block;height:30px;position:relative;vertical-align:middle;width:50px;-moz-user-select:none;-khtml-user-select:none;-webkit-user-select:none;-ms-user-select:none;user-select:none;box-sizing:content-box;background-clip:content-box}.switchery>small{background:#fff;border-radius:100%;box-shadow:0 1px 3px rgba(0,0,0,0.4);height:30px;position:absolute;top:0;width:30px}.switchery-small{border-radius:20px;height:20px;width:33px}.switchery-small>small{height:20px;width:20px}.switchery-large{border-radius:40px;height:40px;width:66px}.switchery-large>small{height:40px;width:40px}

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,3 @@
/*global Switchery:true*/ // Meteor creates a file-scope global for exporting. This comment prevents a potential JSHint warning.
Switchery = window.Switchery;
delete window.Switchery;

View file

@ -0,0 +1,10 @@
'use strict';
Tinytest.add('Switchery integration', function (test) {
var checkbox = document.createElement('input');
checkbox.className = 'js-switch';
var switchy = new Switchery(checkbox);
test.instanceOf(switchy, Switchery, 'instantiation OK');
});

View file

@ -0,0 +1,24 @@
// package metadata file for Meteor.js
'use strict';
var packageName = 'abpetkov:switchery'; // https://atmospherejs.com/mediatainment/switchery
var where = 'client'; // where to install: 'client' or 'server'. For both, pass nothing.
Package.describe({
name: packageName,
summary: 'Switchery (official) - turns your default HTML checkbox inputs into beautiful iOS 7 style switches.',
version: "0.1.0", //packageJson.version,
git: 'https://github.com/abpetkov/switchery'
});
Package.onUse(function(api) {
api.versionsFrom(['METEOR@0.9.0', 'METEOR@1.0']);
api.export('Switchery');
api.addFiles(['dist/switchery.js', 'dist/switchery.css', 'meteor/export.js'], where);
});
Package.onTest(function(api) {
api.use(packageName, where);
api.use('tinytest', where);
api.addFiles('meteor/tests.js', where); // testing specific files
});

View file

@ -0,0 +1,22 @@
{
"name": "switchery",
"version": "0.8.1",
"description": "Create iOS 7 styled switches from default input checkboxes",
"main": "switchery.js",
"repository": {
"type": "git",
"url": "git://github.com/abpetkov/switchery.git"
},
"author": "Alexander Petkov",
"license": "MIT",
"bugs": {
"url": "https://github.com/abpetkov/switchery/issues"
},
"devDependencies": {
"uglify-js": "~2.4.8",
"component": "^1.0.0",
"uglifycss": "0.0.7",
"grunt-exec": "latest",
"spacejam": "latest"
}
}

View file

@ -0,0 +1,67 @@
/*
*
* Main stylesheet for Switchery.
* http://abpetkov.github.io/switchery/
*
*/
/* Switchery defaults. */
.switchery {
background-color: #fff;
border: 1px solid #dfdfdf;
border-radius: 20px;
cursor: pointer;
display: inline-block;
height: 30px;
position: relative;
vertical-align: middle;
width: 50px;
-moz-user-select: none;
-khtml-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
-webkit-background-clip: content-box;
background-clip: content-box;
}
.switchery > small {
background: #fff;
border-radius: 100%;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
height: 30px;
position: absolute;
top: 0;
width: 30px;
}
/* Switchery sizes. */
.switchery-small {
border-radius: 20px;
height: 20px;
width: 33px;
}
.switchery-small > small {
height: 20px;
width: 20px;
}
.switchery-large {
border-radius: 40px;
height: 40px;
width: 66px;
}
.switchery-large > small {
height: 40px;
width: 40px;
}

View file

@ -0,0 +1,384 @@
/**
* Switchery 0.8.1
* http://abpetkov.github.io/switchery/
*
* Authored by Alexander Petkov
* https://github.com/abpetkov
*
* Copyright 2013-2015, Alexander Petkov
* License: The MIT License (MIT)
* http://opensource.org/licenses/MIT
*
*/
/**
* External dependencies.
*/
var transitionize = require('transitionize')
, fastclick = require('fastclick')
, classes = require('classes')
, events = require('events');
/**
* Expose `Switchery`.
*/
module.exports = Switchery;
/**
* Set Switchery default values.
*
* @api public
*/
var defaults = {
color : '#64bd63'
, secondaryColor : '#dfdfdf'
, jackColor : '#fff'
, jackSecondaryColor: null
, className : 'switchery'
, disabled : false
, disabledOpacity : 0.5
, speed : '0.4s'
, size : 'default'
};
/**
* Create Switchery object.
*
* @param {Object} element
* @param {Object} options
* @api public
*/
function Switchery(element, options) {
if (!(this instanceof Switchery)) return new Switchery(element, options);
this.element = element;
this.options = options || {};
for (var i in defaults) {
if (this.options[i] == null) {
this.options[i] = defaults[i];
}
}
if (this.element != null && this.element.type == 'checkbox') this.init();
if (this.isDisabled() === true) this.disable();
}
/**
* Hide the target element.
*
* @api private
*/
Switchery.prototype.hide = function() {
this.element.style.display = 'none';
};
/**
* Show custom switch after the target element.
*
* @api private
*/
Switchery.prototype.show = function() {
var switcher = this.create();
this.insertAfter(this.element, switcher);
};
/**
* Create custom switch.
*
* @returns {Object} this.switcher
* @api private
*/
Switchery.prototype.create = function() {
this.switcher = document.createElement('span');
this.jack = document.createElement('small');
this.switcher.appendChild(this.jack);
this.switcher.className = this.options.className;
this.events = events(this.switcher, this);
return this.switcher;
};
/**
* Insert after element after another element.
*
* @param {Object} reference
* @param {Object} target
* @api private
*/
Switchery.prototype.insertAfter = function(reference, target) {
reference.parentNode.insertBefore(target, reference.nextSibling);
};
/**
* Set switch jack proper position.
*
* @param {Boolean} clicked - we need this in order to uncheck the input when the switch is clicked
* @api private
*/
Switchery.prototype.setPosition = function (clicked) {
var checked = this.isChecked()
, switcher = this.switcher
, jack = this.jack;
if (clicked && checked) checked = false;
else if (clicked && !checked) checked = true;
if (checked === true) {
this.element.checked = true;
if (window.getComputedStyle) jack.style.left = parseInt(window.getComputedStyle(switcher).width) - parseInt(window.getComputedStyle(jack).width) + 'px';
else jack.style.left = parseInt(switcher.currentStyle['width']) - parseInt(jack.currentStyle['width']) + 'px';
if (this.options.color) this.colorize();
this.setSpeed();
} else {
jack.style.left = 0;
this.element.checked = false;
this.switcher.style.boxShadow = 'inset 0 0 0 0 ' + this.options.secondaryColor;
this.switcher.style.borderColor = this.options.secondaryColor;
this.switcher.style.backgroundColor = (this.options.secondaryColor !== defaults.secondaryColor) ? this.options.secondaryColor : '#fff';
this.jack.style.backgroundColor = (this.options.jackSecondaryColor !== this.options.jackColor) ? this.options.jackSecondaryColor : this.options.jackColor;
this.setSpeed();
}
};
/**
* Set speed.
*
* @api private
*/
Switchery.prototype.setSpeed = function() {
var switcherProp = {}
, jackProp = {
'background-color': this.options.speed
, 'left': this.options.speed.replace(/[a-z]/, '') / 2 + 's'
};
if (this.isChecked()) {
switcherProp = {
'border': this.options.speed
, 'box-shadow': this.options.speed
, 'background-color': this.options.speed.replace(/[a-z]/, '') * 3 + 's'
};
} else {
switcherProp = {
'border': this.options.speed
, 'box-shadow': this.options.speed
};
}
transitionize(this.switcher, switcherProp);
transitionize(this.jack, jackProp);
};
/**
* Set switch size.
*
* @api private
*/
Switchery.prototype.setSize = function() {
var small = 'switchery-small'
, normal = 'switchery-default'
, large = 'switchery-large';
switch (this.options.size) {
case 'small':
classes(this.switcher).add(small)
break;
case 'large':
classes(this.switcher).add(large)
break;
default:
classes(this.switcher).add(normal)
break;
}
};
/**
* Set switch color.
*
* @api private
*/
Switchery.prototype.colorize = function() {
var switcherHeight = this.switcher.offsetHeight / 2;
this.switcher.style.backgroundColor = this.options.color;
this.switcher.style.borderColor = this.options.color;
this.switcher.style.boxShadow = 'inset 0 0 0 ' + switcherHeight + 'px ' + this.options.color;
this.jack.style.backgroundColor = this.options.jackColor;
};
/**
* Handle the onchange event.
*
* @param {Boolean} state
* @api private
*/
Switchery.prototype.handleOnchange = function(state) {
if (document.dispatchEvent) {
var event = document.createEvent('HTMLEvents');
event.initEvent('change', true, true);
this.element.dispatchEvent(event);
} else {
this.element.fireEvent('onchange');
}
};
/**
* Handle the native input element state change.
* A `change` event must be fired in order to detect the change.
*
* @api private
*/
Switchery.prototype.handleChange = function() {
var self = this
, el = this.element;
if (el.addEventListener) {
el.addEventListener('change', function() {
self.setPosition();
});
} else {
el.attachEvent('onchange', function() {
self.setPosition();
});
}
};
/**
* Handle the switch click event.
*
* @api private
*/
Switchery.prototype.handleClick = function() {
var switcher = this.switcher;
fastclick(switcher);
this.events.bind('click', 'bindClick');
};
/**
* Attach all methods that need to happen on switcher click.
*
* @api private
*/
Switchery.prototype.bindClick = function() {
var parent = this.element.parentNode.tagName.toLowerCase()
, labelParent = (parent === 'label') ? false : true;
this.setPosition(labelParent);
this.handleOnchange(this.element.checked);
};
/**
* Mark an individual switch as already handled.
*
* @api private
*/
Switchery.prototype.markAsSwitched = function() {
this.element.setAttribute('data-switchery', true);
};
/**
* Check if an individual switch is already handled.
*
* @api private
*/
Switchery.prototype.markedAsSwitched = function() {
return this.element.getAttribute('data-switchery');
};
/**
* Initialize Switchery.
*
* @api private
*/
Switchery.prototype.init = function() {
this.hide();
this.show();
this.setSize();
this.setPosition();
this.markAsSwitched();
this.handleChange();
this.handleClick();
};
/**
* See if input is checked.
*
* @returns {Boolean}
* @api public
*/
Switchery.prototype.isChecked = function() {
return this.element.checked;
};
/**
* See if switcher should be disabled.
*
* @returns {Boolean}
* @api public
*/
Switchery.prototype.isDisabled = function() {
return this.options.disabled || this.element.disabled || this.element.readOnly;
};
/**
* Destroy all event handlers attached to the switch.
*
* @api public
*/
Switchery.prototype.destroy = function() {
this.events.unbind();
};
/**
* Enable disabled switch element.
*
* @api public
*/
Switchery.prototype.enable = function() {
if (this.options.disabled) this.options.disabled = false;
if (this.element.disabled) this.element.disabled = false;
if (this.element.readOnly) this.element.readOnly = false;
this.switcher.style.opacity = 1;
this.events.bind('click', 'bindClick');
};
/**
* Disable switch element.
*
* @api public
*/
Switchery.prototype.disable = function() {
if (!this.options.disabled) this.options.disabled = true;
if (!this.element.disabled) this.element.disabled = true;
if (!this.element.readOnly) this.element.readOnly = true;
this.switcher.style.opacity = this.options.disabledOpacity;
this.destroy();
};