admin_dashboard/static/plugins/switchery/js/switchery.js

424 lines
8.2 KiB
JavaScript

/**
* Require the given path.
*
* @param {String} path
* @return {Object} exports
* @api public
*/
function require(path, parent, orig) {
var resolved = require.resolve(path);
// lookup failed
if (null == resolved) {
orig = orig || path;
parent = parent || 'root';
var err = new Error('Failed to require "' + orig + '" from "' + parent + '"');
err.path = orig;
err.parent = parent;
err.require = true;
throw err;
}
var module = require.modules[resolved];
// perform real require()
// by invoking the module's
// registered function
if (!module._resolving && !module.exports) {
var mod = {};
mod.exports = {};
mod.client = mod.component = true;
module._resolving = true;
module.call(this, mod.exports, require.relative(resolved), mod);
delete module._resolving;
module.exports = mod.exports;
}
return module.exports;
}
/**
* Registered modules.
*/
require.modules = {};
/**
* Registered aliases.
*/
require.aliases = {};
/**
* Resolve `path`.
*
* Lookup:
*
* - PATH/index.js
* - PATH.js
* - PATH
*
* @param {String} path
* @return {String} path or null
* @api private
*/
require.resolve = function(path) {
if (path.charAt(0) === '/') path = path.slice(1);
var paths = [
path,
path + '.js',
path + '.json',
path + '/index.js',
path + '/index.json'
];
for (var i = 0; i < paths.length; i++) {
var path = paths[i];
if (require.modules.hasOwnProperty(path)) return path;
if (require.aliases.hasOwnProperty(path)) return require.aliases[path];
}
};
/**
* Normalize `path` relative to the current path.
*
* @param {String} curr
* @param {String} path
* @return {String}
* @api private
*/
require.normalize = function(curr, path) {
var segs = [];
if ('.' != path.charAt(0)) return path;
curr = curr.split('/');
path = path.split('/');
for (var i = 0; i < path.length; ++i) {
if ('..' == path[i]) {
curr.pop();
} else if ('.' != path[i] && '' != path[i]) {
segs.push(path[i]);
}
}
return curr.concat(segs).join('/');
};
/**
* Register module at `path` with callback `definition`.
*
* @param {String} path
* @param {Function} definition
* @api private
*/
require.register = function(path, definition) {
require.modules[path] = definition;
};
/**
* Alias a module definition.
*
* @param {String} from
* @param {String} to
* @api private
*/
require.alias = function(from, to) {
if (!require.modules.hasOwnProperty(from)) {
throw new Error('Failed to alias "' + from + '", it does not exist');
}
require.aliases[to] = from;
};
/**
* Return a require function relative to the `parent` path.
*
* @param {String} parent
* @return {Function}
* @api private
*/
require.relative = function(parent) {
var p = require.normalize(parent, '..');
/**
* lastIndexOf helper.
*/
function lastIndexOf(arr, obj) {
var i = arr.length;
while (i--) {
if (arr[i] === obj) return i;
}
return -1;
}
/**
* The relative require() itself.
*/
function localRequire(path) {
var resolved = localRequire.resolve(path);
return require(resolved, parent, path);
}
/**
* Resolve relative to the parent.
*/
localRequire.resolve = function(path) {
var c = path.charAt(0);
if ('/' == c) return path.slice(1);
if ('.' == c) return require.normalize(p, path);
// resolve deps by returning
// the dep in the nearest "deps"
// directory
var segs = parent.split('/');
var i = lastIndexOf(segs, 'deps') + 1;
if (!i) i = 0;
path = segs.slice(0, i + 1).join('/') + '/deps/' + path;
return path;
};
/**
* Check if module is defined at `path`.
*/
localRequire.exists = function(path) {
return require.modules.hasOwnProperty(localRequire.resolve(path));
};
return localRequire;
};
require.register("switchery/switchery.js", function(exports, require, module){
/**
* Switchery 0.1.1
* http://abpetkov.github.io/switchery/
*
* Authored by Alexander Petkov
* https://github.com/abpetkov
*
* Copyright 2013, Alexander Petkov
* License: The MIT License (MIT)
* http://opensource.org/licenses/MIT
*
*/
/**
* Expose `Switchery`.
*/
module.exports = Switchery;
/**
* Set Switchery default values.
*
* @api public
*/
var defaults = {
color : '#64bd63'
, className: 'switchery'
, disabled : false
, speed : '0.1s'
};
/**
* Create Switchery object.
*
* @param {Object} element
* @param {Object} options
* @api public
*/
function Switchery(element, options) {
if (!(this instanceof Switchery)) return new Switchery(options);
this.element = element;
this.options = options || {};
for (var i in defaults) {
if (!(i in this.options)) {
this.options[i] = defaults[i];
}
}
if (this.element.type == 'checkbox') this.init();
}
/**
* 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.element.parentNode.appendChild(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;
return this.switcher;
};
/**
* See if input is checked.
*
* @returns {Boolean}
* @api private
*/
Switchery.prototype.isChecked = function() {
return this.element.checked;
};
/**
* See if switcher should be disabled.
*
* @returns {Boolean}
* @api private
*/
Switchery.prototype.isDisabled = function() {
return this.options.disabled || this.element.disabled;
};
/**
* 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) - jack.offsetWidth + 'px';
else jack.style.left = parseInt(switcher.currentStyle['width']) - jack.offsetWidth + 'px';
if (this.options.color) this.colorize();
} else {
jack.style.left = '0';
this.element.checked = false;
this.switcher.style.backgroundColor = '';
this.switcher.style.borderColor = '';
}
};
/**
* Set speed.
*
* @api private
*/
Switchery.prototype.setSpeed = function() {
this.switcher.style.transitionDuration = this.options.speed;
this.jack.style.transitionDuration = this.options.speed;
};
/**
* Copy the input name and id attributes.
*
* @api private
*/
Switchery.prototype.setAttributes = function() {
var id = this.element.getAttribute('id')
, name = this.element.getAttribute('name');
if (id) this.switcher.setAttribute('id', id);
if (name) this.switcher.setAttribute('name', name);
};
/**
* Set switch color.
*
* @api private
*/
Switchery.prototype.colorize = function() {
this.switcher.style.backgroundColor = this.options.color;
this.switcher.style.borderColor = this.options.color;
};
/**
* Handle the switch click event.
*
* @api private
*/
Switchery.prototype.handleClick = function() {
var $this = this
, switcher = this.switcher;
if (this.isDisabled() === false) {
if (switcher.addEventListener) {
switcher.addEventListener('click', function() {
$this.setPosition(true);
});
} else {
switcher.attachEvent('onclick', function() {
$this.setPosition(true);
});
}
} else {
this.element.disabled = true;
}
};
/**
* Initialize Switchery.
*
* @api private
*/
Switchery.prototype.init = function() {
this.hide();
this.show();
this.setSpeed();
this.setPosition();
this.setAttributes();
this.handleClick();
};
});
require.alias("switchery/switchery.js", "switchery/index.js");