microbejs
Version:
microbe.js - A modular JS library for DOM manipulation, and more
1,855 lines (1,593 loc) • 130 kB
JavaScript
/*!
* Microbe JavaScript Library v0.5.2
* http://m.icro.be
*
* Copyright 2014-2016 Sociomantic Labs and other contributors
* Released under the MIT license
* http://m.icro.be/license
*
* Date: Tue May 03 2016
*/
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.µ=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/**
* ## Microbe
*
* Builds the Microbe object
*
* @author Mouse Braun <mouse@knoblau.ch>
* @author Nicolas Brugneaux <nicolas.brugneaux@gmail.com>
*
* @package Microbe
*/
/*jshint globalstrict: true*/
'use strict';
var _type = '[object Microbe]';
var _version = require( './version' );
var Microbe = function( selector, scope, elements )
{
return new Microbe.core.__init__( selector, scope, elements );
};
require( './selectorEngine/init' )( Microbe, _type, _version );
require( './modules/tools' )( Microbe );
require( './modules/pageStyles' )( Microbe );
require( './modules/dom' )( Microbe );
require( './modules/elements' )( Microbe );
require( './modules/http' )( Microbe );
require( './modules/data' )( Microbe );
require( './modules/events' )( Microbe );
module.exports = Microbe.core.constructor = Microbe;
},{"./modules/data":9,"./modules/dom":10,"./modules/elements":11,"./modules/events":12,"./modules/http":13,"./modules/pageStyles":14,"./modules/tools":15,"./selectorEngine/init":19,"./version":22}],2:[function(require,module,exports){
(function (process){
// Use the fastest possible means to execute a task in a future turn
// of the event loop.
// linked list of tasks (single, with head node)
var head = {task: void 0, next: null};
var tail = head;
var flushing = false;
var requestFlush = void 0;
var isNodeJS = false;
function flush() {
/* jshint loopfunc: true */
while (head.next) {
head = head.next;
var task = head.task;
head.task = void 0;
var domain = head.domain;
if (domain) {
head.domain = void 0;
domain.enter();
}
try {
task();
} catch (e) {
if (isNodeJS) {
// In node, uncaught exceptions are considered fatal errors.
// Re-throw them synchronously to interrupt flushing!
// Ensure continuation if the uncaught exception is suppressed
// listening "uncaughtException" events (as domains does).
// Continue in next event to avoid tick recursion.
if (domain) {
domain.exit();
}
setTimeout(flush, 0);
if (domain) {
domain.enter();
}
throw e;
} else {
// In browsers, uncaught exceptions are not fatal.
// Re-throw them asynchronously to avoid slow-downs.
setTimeout(function() {
throw e;
}, 0);
}
}
if (domain) {
domain.exit();
}
}
flushing = false;
}
if (typeof process !== "undefined" && process.nextTick) {
// Node.js before 0.9. Note that some fake-Node environments, like the
// Mocha test runner, introduce a `process` global without a `nextTick`.
isNodeJS = true;
requestFlush = function () {
process.nextTick(flush);
};
} else if (typeof setImmediate === "function") {
// In IE10, Node.js 0.9+, or https://github.com/NobleJS/setImmediate
if (typeof window !== "undefined") {
requestFlush = setImmediate.bind(window, flush);
} else {
requestFlush = function () {
setImmediate(flush);
};
}
} else if (typeof MessageChannel !== "undefined") {
// modern browsers
// http://www.nonblocking.io/2011/06/windownexttick.html
var channel = new MessageChannel();
channel.port1.onmessage = flush;
requestFlush = function () {
channel.port2.postMessage(0);
};
} else {
// old browsers
requestFlush = function () {
setTimeout(flush, 0);
};
}
function asap(task) {
tail = tail.next = {
task: task,
domain: isNodeJS && process.domain,
next: null
};
if (!flushing) {
flushing = true;
requestFlush();
}
};
module.exports = asap;
}).call(this,require('_process'))
},{"_process":3}],3:[function(require,module,exports){
// shim for using process in browser
var process = module.exports = {};
process.nextTick = (function () {
var canSetImmediate = typeof window !== 'undefined'
&& window.setImmediate;
var canPost = typeof window !== 'undefined'
&& window.postMessage && window.addEventListener
;
if (canSetImmediate) {
return function (f) { return window.setImmediate(f) };
}
if (canPost) {
var queue = [];
window.addEventListener('message', function (ev) {
var source = ev.source;
if ((source === window || source === null) && ev.data === 'process-tick') {
ev.stopPropagation();
if (queue.length > 0) {
var fn = queue.shift();
fn();
}
}
}, true);
return function nextTick(fn) {
queue.push(fn);
window.postMessage('process-tick', '*');
};
}
return function nextTick(fn) {
setTimeout(fn, 0);
};
})();
process.title = 'browser';
process.browser = true;
process.env = {};
process.argv = [];
function noop() {}
process.on = noop;
process.addListener = noop;
process.once = noop;
process.off = noop;
process.removeListener = noop;
process.removeAllListeners = noop;
process.emit = noop;
process.binding = function (name) {
throw new Error('process.binding is not supported');
}
// TODO(shtylman)
process.cwd = function () { return '/' };
process.chdir = function (dir) {
throw new Error('process.chdir is not supported');
};
},{}],4:[function(require,module,exports){
'use strict';
module.exports = require('./lib/core.js')
require('./lib/done.js')
require('./lib/es6-extensions.js')
require('./lib/node-extensions.js')
},{"./lib/core.js":5,"./lib/done.js":6,"./lib/es6-extensions.js":7,"./lib/node-extensions.js":8}],5:[function(require,module,exports){
'use strict';
var asap = require('asap')
module.exports = Promise;
function Promise(fn) {
if (typeof this !== 'object') throw new TypeError('Promises must be constructed via new')
if (typeof fn !== 'function') throw new TypeError('not a function')
var state = null
var value = null
var deferreds = []
var self = this
this.then = function(onFulfilled, onRejected) {
return new self.constructor(function(resolve, reject) {
handle(new Handler(onFulfilled, onRejected, resolve, reject))
})
}
function handle(deferred) {
if (state === null) {
deferreds.push(deferred)
return
}
asap(function() {
var cb = state ? deferred.onFulfilled : deferred.onRejected
if (cb === null) {
(state ? deferred.resolve : deferred.reject)(value)
return
}
var ret
try {
ret = cb(value)
}
catch (e) {
deferred.reject(e)
return
}
deferred.resolve(ret)
})
}
function resolve(newValue) {
try { //Promise Resolution Procedure: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure
if (newValue === self) throw new TypeError('A promise cannot be resolved with itself.')
if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) {
var then = newValue.then
if (typeof then === 'function') {
doResolve(then.bind(newValue), resolve, reject)
return
}
}
state = true
value = newValue
finale()
} catch (e) { reject(e) }
}
function reject(newValue) {
state = false
value = newValue
finale()
}
function finale() {
for (var i = 0, len = deferreds.length; i < len; i++)
handle(deferreds[i])
deferreds = null
}
doResolve(fn, resolve, reject)
}
function Handler(onFulfilled, onRejected, resolve, reject){
this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null
this.onRejected = typeof onRejected === 'function' ? onRejected : null
this.resolve = resolve
this.reject = reject
}
/**
* Take a potentially misbehaving resolver function and make sure
* onFulfilled and onRejected are only called once.
*
* Makes no guarantees about asynchrony.
*/
function doResolve(fn, onFulfilled, onRejected) {
var done = false;
try {
fn(function (value) {
if (done) return
done = true
onFulfilled(value)
}, function (reason) {
if (done) return
done = true
onRejected(reason)
})
} catch (ex) {
if (done) return
done = true
onRejected(ex)
}
}
},{"asap":2}],6:[function(require,module,exports){
'use strict';
var Promise = require('./core.js')
var asap = require('asap')
module.exports = Promise
Promise.prototype.done = function (onFulfilled, onRejected) {
var self = arguments.length ? this.then.apply(this, arguments) : this
self.then(null, function (err) {
asap(function () {
throw err
})
})
}
},{"./core.js":5,"asap":2}],7:[function(require,module,exports){
'use strict';
//This file contains the ES6 extensions to the core Promises/A+ API
var Promise = require('./core.js')
var asap = require('asap')
module.exports = Promise
/* Static Functions */
function ValuePromise(value) {
this.then = function (onFulfilled) {
if (typeof onFulfilled !== 'function') return this
return new Promise(function (resolve, reject) {
asap(function () {
try {
resolve(onFulfilled(value))
} catch (ex) {
reject(ex);
}
})
})
}
}
ValuePromise.prototype = Promise.prototype
var TRUE = new ValuePromise(true)
var FALSE = new ValuePromise(false)
var NULL = new ValuePromise(null)
var UNDEFINED = new ValuePromise(undefined)
var ZERO = new ValuePromise(0)
var EMPTYSTRING = new ValuePromise('')
Promise.resolve = function (value) {
if (value instanceof Promise) return value
if (value === null) return NULL
if (value === undefined) return UNDEFINED
if (value === true) return TRUE
if (value === false) return FALSE
if (value === 0) return ZERO
if (value === '') return EMPTYSTRING
if (typeof value === 'object' || typeof value === 'function') {
try {
var then = value.then
if (typeof then === 'function') {
return new Promise(then.bind(value))
}
} catch (ex) {
return new Promise(function (resolve, reject) {
reject(ex)
})
}
}
return new ValuePromise(value)
}
Promise.all = function (arr) {
var args = Array.prototype.slice.call(arr)
return new Promise(function (resolve, reject) {
if (args.length === 0) return resolve([])
var remaining = args.length
function res(i, val) {
try {
if (val && (typeof val === 'object' || typeof val === 'function')) {
var then = val.then
if (typeof then === 'function') {
then.call(val, function (val) { res(i, val) }, reject)
return
}
}
args[i] = val
if (--remaining === 0) {
resolve(args);
}
} catch (ex) {
reject(ex)
}
}
for (var i = 0; i < args.length; i++) {
res(i, args[i])
}
})
}
Promise.reject = function (value) {
return new Promise(function (resolve, reject) {
reject(value);
});
}
Promise.race = function (values) {
return new Promise(function (resolve, reject) {
values.forEach(function(value){
Promise.resolve(value).then(resolve, reject);
})
});
}
/* Prototype Methods */
Promise.prototype['catch'] = function (onRejected) {
return this.then(null, onRejected);
}
},{"./core.js":5,"asap":2}],8:[function(require,module,exports){
'use strict';
//This file contains then/promise specific extensions that are only useful for node.js interop
var Promise = require('./core.js')
var asap = require('asap')
module.exports = Promise
/* Static Functions */
Promise.denodeify = function (fn, argumentCount) {
argumentCount = argumentCount || Infinity
return function () {
var self = this
var args = Array.prototype.slice.call(arguments)
return new Promise(function (resolve, reject) {
while (args.length && args.length > argumentCount) {
args.pop()
}
args.push(function (err, res) {
if (err) reject(err)
else resolve(res)
})
var res = fn.apply(self, args)
if (res && (typeof res === 'object' || typeof res === 'function') && typeof res.then === 'function') {
resolve(res)
}
})
}
}
Promise.nodeify = function (fn) {
return function () {
var args = Array.prototype.slice.call(arguments)
var callback = typeof args[args.length - 1] === 'function' ? args.pop() : null
var ctx = this
try {
return fn.apply(this, arguments).nodeify(callback, ctx)
} catch (ex) {
if (callback === null || typeof callback == 'undefined') {
return new Promise(function (resolve, reject) { reject(ex) })
} else {
asap(function () {
callback.call(ctx, ex)
})
}
}
}
}
Promise.prototype.nodeify = function (callback, ctx) {
if (typeof callback != 'function') return this
this.then(function (value) {
asap(function () {
callback.call(ctx, null, value)
})
}, function (err) {
asap(function () {
callback.call(ctx, err)
})
})
}
},{"./core.js":5,"asap":2}],9:[function(require,module,exports){
/**
* data.js
*
* @author Mouse Braun <mouse@knoblau.ch>
* @author Nicolas Brugneaux <nicolas.brugneaux@gmail.com>
*
* @package Microbe
*/
module.exports = function( Microbe )
{
'use strict';
/**
* ## get
*
* gets the saved value from each element in the microbe in an array
*
* @param {String} _prop property to get
*
* @example µ( '.example' ).get( 'moon' );
*
* @return _Array_ array of values
*/
Microbe.core.get = function( prop )
{
var _get = function( _el )
{
if ( ! prop )
{
return _el.data;
}
else
{
if ( _el.data && _el.data[ prop ] )
{
return _el.data[ prop ];
}
else
{
return false;
}
}
};
return this.map( _get );
};
/**
* ## set
*
* Sets the value to the data object in the each element in the microbe
*
* @param {String} prop property to set
* @param {String} value value to set to
*
* @example µ( '.example' ).set( 'moon', 'doge' );
*
* @return _Microbe_ reference to original microbe
*/
Microbe.core.set = function( prop, value )
{
var _set = function( _el )
{
_el.data = _el.data || {};
if ( Microbe.isArray( value ) )
{
value = Microbe.extend( [], value );
}
else if ( Microbe.isObject( value ) )
{
value = Microbe.extend( {}, value );
}
_el.data[ prop ] = value;
};
this.each( _set );
return this;
};
};
},{}],10:[function(require,module,exports){
/**
* dom.js
*
* @author Mouse Braun <mouse@knoblau.ch>
* @author Nicolas Brugneaux <nicolas.brugneaux@gmail.com>
*
* @package Microbe
*/
var _events;
module.exports = function( Microbe )
{
'use strict';
/**
* ## append
*
* Appends an element or elements to the microbe. if there is more than
* one target the next ones are cloned. The strings this accepts can be
* a selector string, an element creation string, or an html string
*
* @param {Mixed} _ele element(s) to append (Element, Array, string, or Microbe)
*
* @example µ( '.example' ).append( '<div class="new-div">test</div>' );
* @example µ( '.example' ).append( µMicrobeExample );
* @example µ( '.example' ).append( _el );
* @example µ( '.example' ).append( [ _el1, _el2, _el3 ] );
* @example µ( '.example' ).append( '<div.example>' );
*
* @return _Microbe_ new microbe filled with the inserted content
*/
Microbe.core.append = (function()
{
/**
* ## _appendHTML
*
* in the case of html passed in it will get appended or prepended to the
* innerHTML
*
* @param {String} _html html string
* @param {Boolean} _pre prepend or not
*
* @return _Microbe_
*/
var _appendHTML = function( _html, _pre )
{
var _el;
for ( var k = 0, lenK = this.length; k < lenK; k++ )
{
_el = this[ k ];
if ( _pre )
{
_el.innerHTML = _html + _el.innerHTML;
}
else
{
_el.innerHTML += _html;
}
}
return this;
};
/**
* ## _append
*
* appends the child to the parent
*
* @param {Element} _parentEl parent element
* @param {Element} _elm child element
*
* @return _Microbe_
*/
var _append = function( _parentEl, _elm )
{
_parentEl.appendChild( _elm );
};
/**
* ## checkElement
*
* reformats the element into an iterable object
*
* @param {Mixed} _el element(s) to be reformatted (String, DOMElement, Array, Microbe)
*
* @return _Mixed_ formatted element(s) (Microbe, Array)
*/
var checkElement = function( _el )
{
if ( typeof _el === 'string' )
{
return new Microbe( _el );
}
else if ( !_el.length )
{
return [ _el ];
}
return _el;
}
/**
* ## _prepend
*
* prepends the child to the parent
*
* @param {Element} _parentEl parent element
* @param {Element} _elm child element
*
* @return _Microbe_
*/
var _prepend = function( _parentEl, _elm )
{
var firstChild = _parentEl.firstElementChild;
_parentEl.insertBefore( _elm, firstChild );
};
/**
* ## append main function
*
* takes input fromappend, appendTo, prepend, and prependTo, sorts out
* the booleans and targets, then passes them to the correct function
*
* @param {Mixed} _el element to attach or attach to
* @param {Boolean} prepend prepend or append
* @param {Boolean} to determines the parent child relationship
*
* @return _Microbe_
*/
return function( _el, prepend, to )
{
var _cb = prepend ? _prepend : _append;
var elementArray = [], node;
_el = checkElement( _el );
var self = to ? _el : this;
_el = to ? this : _el;
if ( _el.indexOf( '/' ) !== -1 )
{
return _appendHTML.call( self, _el, prepend );
}
self.forEach( function( s, i )
{
_el.forEach( function( e, j )
{
node = i === 0 ? e : e.cloneNode( true );
elementArray.push( node );
_cb( self[ i ], node );
} );
} );
return this.constructor( elementArray );
};
}());
/**
* ## appendTo
*
* Prepends a microbe to an element or elements. if there is more than
* one target the next ones are cloned. The strings this accepts can be
* a selector string, an element creation string, or an html string
*
* @param {Mixed} _ele element(s) to prepend _{Element, Array, String, or Microbe}_
*
* @example µ( '.example' ).appendTo( '<div class="new-div">test</div>' );
* @example µ( '.example' ).appendTo( µMicrobeExample );
* @example µ( '.example' ).appendTo( _el );
* @example µ( '.example' ).appendTo( [ _el1, _el2, _el3 ] );
* @example µ( '.example' ).appendTo( '<div.example>' );
*
* @return _Microbe_ new microbe filled with the inserted content
*/
Microbe.core.appendTo = function( _el )
{
return this.append( _el, false, true );
};
/**
* ## insertAfter
*
* Inserts the given element after each of the elements given (or passed through this).
* if it is an elemnet it is wrapped in a microbe object. if it is a string it is created
*
* @param {Mixed} _elAfter element to insert {Object or String}
*
* @example µ( '.example' ).insertAfter( '<div class="new-div">test</div>' );
* @example µ( '.example' ).insertAfter( µMicrobeExample );
* @example µ( '.example' ).insertAfter( _el );
* @example µ( '.example' ).insertAfter( [ _el1, _el2, _el3 ] );
* @example µ( '.example' ).insertAfter( '<div.example>' );
*
* @return _Microbe_ new microbe filled with the inserted content
*/
Microbe.core.insertAfter = function( _elAfter )
{
var elementArray = [];
var _insertAfter = function( _elm, i )
{
var _arr = Array.prototype.slice.call( _elm.parentNode.children );
var nextIndex = _arr.indexOf( _elm ) + 1;
var node, nextEle = _elm.parentNode.children[ nextIndex ];
for ( var j = 0, lenJ = _elAfter.length; j < lenJ; j++ )
{
node = i === 0 ? _elAfter[ j ] : _elAfter[ j ].cloneNode( true );
elementArray.push( node );
if ( nextEle )
{
nextEle.parentNode.insertBefore( node, nextEle );
}
else
{
_elm.parentNode.appendChild( node );
}
}
};
if ( typeof _elAfter === 'string' )
{
_elAfter = new Microbe( _elAfter );
}
else if ( ! _elAfter.length )
{
_elAfter = [ _elAfter ];
}
var i, len;
for ( i = 0, len = this.length; i < len; i++ )
{
_insertAfter( this[ i ], i );
}
return this.constructor( elementArray );
};
/**
* ## prepend
*
* Prepends an element or elements to the microbe. if there is more than
* one target the next ones are cloned. The strings this accepts can be
* a selector string, an element creation string, or an html string
*
* @param {Mixed} _ele element(s) to prepend _{Element, Array, String, or Microbe}_
*
* @example µ( '.example' ).prepend( '<div class="new-div">test</div>' );
* @example µ( '.example' ).prepend( µMicrobeExample );
* @example µ( '.example' ).prepend( _el );
* @example µ( '.example' ).prepend( [ _el1, _el2, _el3 ] );
* @example µ( '.example' ).prepend( '<div.example>' );
*
* @return _Microbe_ new microbe filled with the inserted content
*/
Microbe.core.prepend = function( _el )
{
return this.append( _el, true );
};
/**
* ## prependTo
*
* Prepends a microbe to an element or elements. if there is more than
* one target the next ones are cloned. The strings this accepts can be
* a selector string, an element creation string, or an html string
*
* @param {Mixed} _ele element(s) to prepend _{Element, Array, String, or Microbe}_
*
* @example µ( '.example' ).prependTo( '<div class="new-div">test</div>' );
* @example µ( '.example' ).prependTo( µMicrobeExample );
* @example µ( '.example' ).prependTo( _el );
* @example µ( '.example' ).prependTo( [ _el1, _el2, _el3 ] );
* @example µ( '.example' ).prependTo( '<div.example>' );
*
* @return _Microbe_ new microbe filled with the inserted content
*/
Microbe.core.prependTo = function( _el )
{
return this.append( _el, true, true );
};
/**
* ## ready
*
* Waits until the DOM is ready to execute
*
* @param {Function} _cb callback to run on ready
* @param {Array} args parameters to pass to the callback
*
* @example µ.ready( function( a, b ){ return a + b; }, [ 1, 2 ] );
* @example µ( function( a, b ){ return a + b; }, [ 1, 2 ] );
*
* @return _void_
*/
Microbe.ready = function( _cb, args )
{
if ( document.readyState === 'complete' )
{
return _cb.apply( this, args );
}
if ( window.addEventListener )
{
window.addEventListener( 'load', _cb, false );
}
else if ( window.attachEvent )
{
window.attachEvent( 'onload', _cb );
}
else
{
window.onload = _cb;
}
};
/**
* ## remove
*
* Removes an element or elements from the dom and all events bound to it
*
* @example µ( '.example' ).remove();
*
* @return _Microbe_ reference to original microbe
*/
Microbe.core.remove = function()
{
if ( this.off )
{
this.off();
}
this.forEach( function( _el )
{
_el.remove();
} );
return this;
};
/**
* ## remove polyfill
*
* Polyfill for IE because IE
*
* @return _void_
*/
if ( !( 'remove' in Element.prototype ) )
{
Element.prototype.remove = function()
{
this.parentElement.removeChild( this );
};
}
};
},{}],11:[function(require,module,exports){
module.exports = function( Microbe )
{
'use strict';
var _type = Microbe.type;
/**
* ## addClass
*
* Adds the passed class to the current element(s)
*
* @param {Mixed} _class class to remove. this accepts
* strings and array of strings.
* the strings can be a class or
* classes seperated with spaces _{String or Array}_
*
* @example µ( '.example' ).addClass( 'moon' );
* @example µ( '.example' ).addClass( [ 'moon', 'doge' ] );
*
* @return _Microbe_ reference to original microbe
*/
Microbe.core.addClass = function( _class )
{
var _addClass = function( _el )
{
for ( var i = 0, lenI = _class.length; i < lenI; i++ )
{
var _c = _class[ i ].split( ' ' );
for ( var j = 0, lenJ = _c.length; j < lenJ; j++ )
{
if ( _c[ j ] !== '' )
{
_el.classList.add( _c[ j ] );
}
}
}
};
if ( typeof _class === 'string' )
{
_class = [ _class ];
}
this.each( _addClass );
return this;
};
/**
* ## attr
*
* Changes the attribute by writing the given property and value to the
* supplied elements. If the value is omitted, simply returns the current
* attribute value of the element. Attributes can be bulk added by passing
* an object (property: value)
*
* @param {Mixed} _attribute attribute name {String or Object}
* @param {String} _value attribute value (optional)
*
* @example µ( '.example' ).attr( 'moon', 'doge' );
* @example µ( '.example' ).attr( { 'moon' : 1,
* 'doge' : 2 } );
* @example µ( '.example' ).attr( 'moon' );
*
* @return _Microbe_ reference to original microbe (set)
* @return _Array_ array of values (get)
*/
Microbe.core.attr = function ( _attribute, _value )
{
var attrObject = !!Microbe.isObject( _attribute );
var _setAttr = function( _elm )
{
var _set = function( _a, _v )
{
if ( !_elm.getAttribute )
{
_elm[ _a ] = _v;
}
else
{
_elm.setAttribute( _a, _v );
}
};
if ( _value === null )
{
_removeAttr( _elm );
}
else
{
var _attr;
if ( !attrObject )
{
_set( _attribute, _value );
}
else
{
for ( _attr in _attribute )
{
_value = _attribute[ _attr ];
_set( _attr, _value );
}
}
}
};
var _getAttr = function( _elm )
{
return _elm.getAttribute( _attribute );
};
var _removeAttr = function( _elm )
{
if ( _elm.getAttribute( _attribute ) === null )
{
delete _elm[ _attribute ];
}
else
{
_elm.removeAttribute( _attribute );
}
};
if ( _value !== undefined || attrObject )
{
this.each( _setAttr );
return this;
}
return this.map( _getAttr );
};
/**
* ## css
*
* Changes the CSS by writing the given property and value inline to the
* supplied elements. (properties should be supplied in javascript format).
* If the value is omitted, simply returns the current css value of the element.
*
* @param {String} _attribute css property
* @param {String} _value css value (optional)
*
* @example µ( '.example' ).css( 'background-color', '#fff' );
* @example µ( '.example' ).css( { 'background-color' : '#fff',
* 'color' : '#000' } );
* @example µ( '.example' ).css( 'background-color' );
*
* @return _Microbe_ reference to original microbe (set)
* @return _Array_ array of values (get)
*/
Microbe.core.css = function( _property, _value )
{
var isObject = Microbe.isObject( _property );
var _setCss = function( _elm )
{
_elm.style[ _property ] = _value;
};
var _getCss = function( _elm )
{
return window.getComputedStyle( _elm ).getPropertyValue( _property );
};
if ( _value || _value === null || _value === '' || isObject )
{
if ( isObject )
{
for ( var prop in _property )
{
this.css( prop, _property[ prop ] );
}
return this;
}
_value = ( _value === null ) ? '' : _value;
this.each( _setCss );
return this;
}
return this.map( _getCss );
};
/**
* ## getParentIndex
*
* Gets the index of the item in it's parentNode's children array
*
* @example µ( '.example' ).getParentIndex();
*
* @return _Array_ array of index values
*/
Microbe.core.getParentIndex = function()
{
var _getParentIndex = function( _elm )
{
return Array.prototype.indexOf.call( _elm.parentNode.children, _elm );
};
return this.map( _getParentIndex );
};
/**
* ## hasClass
*
* Checks if the current object or the given element has the given class
*
* @param {String} _class class to check
*
* @example µ( '.example' ).hasClass( 'example' );
*
* @return _Array_ Array of Boolean values
*/
Microbe.core.hasClass = function( _class )
{
var _hasClass = function( _elm )
{
return _elm.classList.contains( _class );
};
return this.map( _hasClass );
};
/**
* ## height
*
* syntactic sugar for css height
*
* @paran {String} _height (optional) parameter to set height
* @return _Microbe_
*/
Microbe.core.height = function( _height )
{
return _height ? this.css( 'height', _height ) : this.css( 'height' );
};
/**
* ## html
*
* Changes the innerHtml to the supplied string or microbe. If the value is
* omitted, simply returns the current inner html value of the element.
*
* @param {Mixed} _value html value (accepts Microbe String)
*
* @example µ( '.example' ).html( '<span>things!</span>' );
* @example µ( '.example' ).html();
*
* @return _Microbe_ reference to original microbe (set)
* @return _Array_ array of values (get)
*/
Microbe.core.html = function ( _value )
{
var _append;
if ( _value && _value.type === _type )
{
_append = _value;
_value = '';
}
var _getHtml = function( _elm )
{
return _elm.innerHTML;
};
if ( _value && _value.nodeType === 1 )
{
return _getHtml( _value );
}
if ( _value || _value === '' || _value === 0 )
{
var _setHtml = function( _elm )
{
_elm.innerHTML = _value;
};
this.each( _setHtml );
if ( _append )
{
return this.append( _append );
}
else
{
return this;
}
}
return this.map( _getHtml );
};
/**
* ## offset
*
* returns an array of objects { top, left } of the position (in px) relative to an object's parent
* the returned array has the aditional properties top and left attached to
* it which arrays containing only the top or left
*
* @example µ( '.example' ).offset();
* @example µ( '.example' ).offset().top;
* @example µ( '.example' ).offset().left;
*
* @return _Array_ array of objects or numbers
*/
Microbe.core.offset = function()
{
var len = this.length;
var _top = Array( len );
var _left = Array( len );
var _offset = function( _elm, i )
{
var top = _top[ i ] = _elm.offsetTop;
var left = _left[ i ] = _elm.offsetLeft;
return { top : top, left :left };
};
var res = this.map( _offset );
res.top = _top;
res.left = _left;
return res;
};
/**
* ## position
*
* returns an array of objects { top, left } of the position (in px) relative to an object's parent
* the returned array has the aditional properties top and left attached to
* it which arrays containing only the top or left
*
* @example µ( '.example' ).position();
* @example µ( '.example' ).position().top;
* @example µ( '.example' ).position().left;
*
* @return _Array_ array of objects or numbers
*/
Microbe.core.position = function()
{
var len = this.length;
var _top = Array( len );
var _left = Array( len );
var _position = function( _elm, i )
{
var top = 0, left = 0;
while( _elm )
{
top += _elm.offsetTop;
left += _elm.offsetLeft;
_elm = _elm.offsetParent;
}
_top[ i ] = top;
_left[ i ] = left;
return { top : top, left : left };
};
var res = this.map( _position );
res.top = _top;
res.left = _left;
return res;
};
/**
* ## removeClass
*
* Method removes the given class from the current object or the given element.
*
* @param {Mixed} _class class to remove. this accepts
* strings and array of strings.
* the strings can be a class or
* classes seperated with spaces {String Array}
*
* @example µ( '.example' ).removeClass( 'moon' );
* @example µ( '.example' ).removeClass( [ 'moon', 'doge' ] );
*
* @return _Microbe_ reference of the original microbe
*/
Microbe.core.removeClass = function( _class )
{
var _removeClass = function( _el )
{
for ( var i = 0, lenI = _class.length; i < lenI; i++ )
{
var _c = _class[ i ].split( ' ' );
for ( var j = 0, lenJ = _c.length; j < lenJ; j++ )
{
if ( _c[ j ] !== '' )
{
_el.classList.remove( _c[ j ] );
}
}
}
};
if ( typeof _class === 'string' )
{
_class = [ _class ];
}
this.each( _removeClass );
return this;
};
/**
* ## scroll
*
* returns an array of objects { top, left } of the scroll position of each element
* the returned array has the aditional properties top and left attached to
* it which arrays containing only the top or left
*
* @example µ( '.example' ).scroll();
* @example µ( '.example' ).scroll().top;
* @example µ( '.example' ).scroll().left;
*
* @return _Array_ array of objects numbers
*/
Microbe.core.scroll = function()
{
var len = this.length;
var _top = Array( len );
var _left = Array( len );
var _scroll = function( _elm, i )
{
var top = _top[ i ] = _elm.scrollTop;
var left = _left[ i ] = _elm.scrollLeft;
return { top : top, left :left };
};
var res = this.map( _scroll );
res.top = _top;
res.left = _left;
return res;
};
/**
* ## text
*
* Changes the inner text to the supplied string. If the value is omitted,
* simply returns the current inner text value of each element.
*
* @param {String} _value Text value (optional)
*
* @example µ( '.example' ).text( 'things!' );
* @example µ( '.example' ).text();
*
* @return _Microbe_ reference to original microbe (set)
* @return _Array_ array of values (get)
*/
Microbe.core.text = function( _value )
{
var _setText = function( _el )
{
if ( document.all )
{
_el.innerText = _value;
}
else // FF
{
_el.textContent = _value;
}
};
var _getText = function( _el )
{
if ( document.all )
{
return _el.innerText;
}
else // FF
{
return _el.textContent;
}
};
if ( _value || _value === '' || _value === 0 )
{
this.each( _setText );
return this;
}
return this.map( _getText );
};
/**
* ## toggleClass
*
* adds or removes a class on the current element, depending on
* whether it has it already.
*
* @param {String} _class class to add
*
* @example µ( '.example' ).toggleClass( 'moon' );
* @example µ( '.example' ).toggleClass( [ 'moon', 'doge' ] );
*
* @return _Microbe_ reference of the original microbe
*/
Microbe.core.toggleClass = function( _class )
{
var _cls;
if ( !Array.isArray( _class ) )
{
_class = [ _class ];
}
var _toggleClass = function( _el )
{
if ( _el.classList.contains( _cls ) )
{
_el.classList.remove( _cls );
}
else
{
_el.classList.add( _cls );
}
};
for ( var i = 0, lenI = _class.length; i < lenI; i++ )
{
_cls = _class[ i ];
this.each( _toggleClass );
}
return this;
};
/**
* ## value
*
* retieves or sets the value of an element
*
* @param {String} _value value to set
*
* @example µ( '.example' ).value( 'moon' );
* @example µ( '.example' ).value();
*
* @return _Microbe_ reference of the original microbe
*/
Microbe.core.value = function( _val )
{
var _value = function( _el )
{
if ( _val || _val === '' )
{
_el.value = _val;
return _el;
}
else
{
return _el.value;
}
};
var res = this.map( _value );
return res;
};
/**
* ## width
*
* syntactic sugar for css width
*
* @paran {String} _width (optional) parameter to set width
*
* @example µ( '.example' ).width( '200px' );
* @example µ( '.example' ).width();
*
* @return _Microbe_
*/
Microbe.core.width = function( _width )
{
return _width ? this.css( 'width', _width ) : this.css( 'width' );
};
};
},{}],12:[function(require,module,exports){
/**
* events.js
*
* @author Mouse Braun <mouse@knoblau.ch>
* @author Nicolas Brugneaux <nicolas.brugneaux@gmail.com>
*
* @package Microbe
*/
module.exports = function( Microbe )
{
'use strict';
/**
* ## emit
*
* Emits a custom event to the HTMLElements of the current object
*
* @param {String} _event HTMLEvent
* @param {Object} _data event data
* @param {Boolean} _bubbles event bubbles?
* @param {Boolean} _cancelable cancelable?
*
* @example µ( '.example' ).emit( 'custom-click', { type : 'microbe-click' } );
* @example µ( '.example' ).emit( 'custom-click', { type : 'microbe-click' }, true, true );
*
* @return _Microbe_ reference to original microbe
*/
Microbe.core.emit = function ( _event, _data, _bubbles, _cancelable )
{
_bubbles = _bubbles || false;
_cancelable = _cancelable || false;
var _emit = function( _elm )
{
var _evt = new CustomEvent( _event, {
detail : _data,
cancelable : _cancelable,
bubbles : _bubbles
} );
_elm.dispatchEvent( _evt );
};
this.each( _emit );
return this;
};
/**
* ## off
*
* Unbinds an/all events.
*
* @param {String} _event event name
* @param {Function} _callback callback function
* @param {Object} _el HTML element to modify (optional)
*
* @example µ( '.example' ).off( 'custom-click' );
* @example µ( '.example' ).off();
*
* @return _Microbe_ reference to original microbe
*/
Microbe.core.off = function( _event, _callback )
{
var filterFunction = function( e ){ return e; };
var _off = function( _elm, _e )
{
var _cb = _callback;
var prop = '_' + _e + '-bound-function';
if ( ! _cb && _elm.data && _elm.data[ prop ] )
{
_cb = _elm.data[ prop ];
}
else if ( ! ( _elm.data && _elm.data[ prop ] ) )
{
return null;
}
if ( _cb )
{
if ( ! Microbe.isArray( _cb ) )
{
_cb = [ _cb ];
}
var _index, _d;
for ( var k = 0, lenK = _cb.length; k < lenK; k++ )
{
_d = _elm.data[ prop ];
_index = _d.indexOf( _cb[ k ] );
if ( _index !== -1 )
{
_elm.removeEventListener( _e, _cb[ k ] );
_d[ _index ] = null;
}
}
_elm.data[ prop ] = _elm.data[ prop ].filter( filterFunction );
}
};
var _checkBoundEvents = function ( _elm )
{
if ( !_event && _elm.data && _elm.data.__boundEvents && _elm.data.__boundEvents )
{
_event = _elm.data.__boundEvents;
}
else
{
_elm.data = _elm.data || {};
_elm.data.__boundEvents = _elm.data.__boundEvents || {};
}
if ( !Microbe.isArray( _event ) )
{
_event = [ _event ];
}
for ( var j = 0, lenJ = _event.length; j < lenJ; j++ )
{
_off( _elm, _event[ j ] );
}
_elm.data.__boundEvents = _event.filter( filterFunction );
}
this.each( _checkBoundEvents );
return this;
};
/**
* ## on
*
* Binds an event to the HTMLElements of the current object or to the
* given element.
*
* @param {String} _event HTMLEvent
* @param {Function} _callback callback function
*
* @example µ( '.example' ).on( 'custom-click', function( e ){ return e.target;} );
*
* @return _Microbe_ reference to original microbe
*/
Microbe.core.on = function ( _event, _callback )
{
var _on = function( _elm )
{
var prop = '_' + _event + '-bound-function';
_elm.data = _elm.data || {};
_elm.data[ prop ] = _elm.data[ prop ] || [];
_elm.data.__boundEvents = _elm.data.__boundEvents || [];
_elm.addEventListener( _event, _callback );
_elm.data[ prop ].push( _callback );
_elm.data.__boundEvents.push( _event );
};
this.each( _on );
return this;
};
/**
* ## _CustomEvent polyfill
*
* CustomEvent polyfill for IE <= 9
*
* @param {String} _event HTMLEvent
* @param {Object} _data event data
*
* @return _void_
*/
if ( typeof CustomEvent !== 'function' )
{
( function()
{
function CustomEvent( event, data )
{
data = data || { bubbles: false, cancelable: false, detail: undefined };
var evt = document.createEvent( 'CustomEvent' );
evt.initCustomEvent( event, data.bubbles, data.cancelable, data.detail );
return evt;
}
CustomEvent.prototype = window.Event.prototype;
window.CustomEvent = CustomEvent;
} )();
}
};
},{}],13:[function(require,module,exports){
/**
* http.js
*
* @author Mouse Braun <mouse@knoblau.ch>
* @author Nicolas Brugneaux <nicolas.brugneaux@gmail.com>
*
* @package Microbe
*/
module.exports = function( Microbe )
{
'use strict';
var Promise = require( 'promise' );
/**
* ## http
*
* Method takes as many as necessary parameters, with url being the only required.
* The return then has the methods `.then( _cb )` and `.error( _cb )`
*
* @param {Object} _parameters http parameters. possible properties
* method, url, data, user, password, headers, async
*
* @example µ.http( {url: './test.html', method: 'POST', data: { number: 67867} } ).then( function(){} ).catch( function(){} );
*/
Microbe.http = function( _parameters )
{
var req, method, url, data, user, password, headers, async;
if ( !_parameters )
{
return new Error( 'No parameters given' );
}
else
{
if ( typeof _parameters === 'string' )
{
_parameters = { url: _parameters };
}
req = new XMLHttpRequest();
method = _parameters.method || 'GET';
url = _parameters.url;
data = JSON.stringify( _parameters.data ) || null;
user = _parameters.user || '';
password = _parameters.password || '';
headers = _parameters.headers || null;
asyn