UNPKG

microbejs

Version:

microbe.js - A modular JS library for DOM manipulation, and more

198 lines (171 loc) 5.76 kB
/** * pageStyles.js * * @author Mouse Braun <mouse@knoblau.ch> * @author Nicolas Brugneaux <nicolas.brugneaux@gmail.com> * * @package Microbe */ module.exports = function( Microbe ) { 'use strict'; /** * ## insertStyle * * builds a style tag for the given selector/ media query. Reference to the style * tag and object is saved in µ.__customCSSRules[ selector ][ media ]. * next rule with the same selector combines the old and new rules and overwrites * the contents * * @param {String} selector selector to apply it to * @param {Mixed} cssObj css object. _{String or Object}_ * @param {String} media media query * * @example µ.insertStyle( '.example', { display: 'block', color: '#000' } ); * @example µ.insertStyle( '.example', { display: 'block', color: '#000' }, 'min-width: 61.25em' ); * * @return _Object_ reference to the appropriate style object */ Microbe.insertStyle = function( selector, cssObj, media ) { var _s = selector.replace( / /g, '-' ); var _clss = media ? _s + media.replace( /[\s:\/\[\]\(\)]+/g, '-' ) : _s; media = media || 'none'; var createStyleTag = function() { var el = document.createElement( 'style' ); el.className = 'microbe--inserted--style__' + _clss; if ( media && media !== 'none' ) { el.setAttribute( 'media', media ); } document.head.appendChild( el ); return el; }; var _el, prop; var styleObj = Microbe.__customCSSRules[ _s ]; if ( styleObj && styleObj[ media ] ) { _el = styleObj[ media ].el; var obj = styleObj[ media ].obj; for ( prop in cssObj ) { obj[ prop ] = cssObj[ prop ]; } cssObj = obj; } else { _el = createStyleTag(); } var css = selector + '{'; for ( prop in cssObj ) { css += prop + ' : ' + cssObj[ prop ] + ';'; } css += '}'; _el.innerHTML = css; Microbe.__customCSSRules[ _s ] = Microbe.__customCSSRules[ _s ] || {}; Microbe.__customCSSRules[ _s ][ media ] = { el: _el, obj: cssObj }; return _el; }; // keep track of tags created with insertStyle Microbe.__customCSSRules = {}; /** * ## removeStyle * * removes a microbe added style tag for the given selector/ media query. If the * properties array is passed, rules are removed individually. If properties is * set to true, all tags for this selector are removed. The media query can * also be passed as the second variable * * @param {String} selector selector to apply it to * @param {Mixed} properties css properties to remove 'all' to remove all * selector tags string as media query {String or Array} * @param {String} media media query * * @example µ.removeStyle( '.example', 'all' ); * @example µ.removeStyle( '.example', 'display' ); * @example µ.removeStyle( '.example', [ 'display', 'color' ], 'min-width:70em' ); * * @return _Boolean_ removed or not */ Microbe.removeStyle = function( selector, properties, media ) { if ( !media && typeof properties === 'string' && properties !== 'all' ) { media = properties; properties = null; } media = media || 'none'; var _removeStyle = function( _el, _media ) { _el.parentNode.removeChild( _el ); delete Microbe.__customCSSRules[ selector ][ _media ]; }; var style = Microbe.__customCSSRules[ selector ]; if ( style ) { if ( properties === 'all' ) { for ( var _mq in style ) { _removeStyle( style[ _mq ].el, _mq ); } } else { style = style[ media ]; if ( style ) { if ( Microbe.isArray( properties ) && !Microbe.isEmpty( properties ) ) { for ( var i = 0, lenI = properties.length; i < lenI; i++ ) { if ( style.obj[ properties[ i ] ] ) { delete style.obj[ properties[ i ] ]; } } if ( Microbe.isEmpty( style.obj ) ) { _removeStyle( style.el, media ); } else { Microbe.insertStyle( selector, style.obj, media ); } } else { _removeStyle( style.el, media ); } } else { return false; } } } else { return false; } return true; }; /** * ## removeStyles * * removes all microbe added style tags for the given selector * * @param {String} selector selector to apply it to * * @example µ.removeStyle( '.example' ); * * @return _Boolean_ removed or not */ Microbe.removeStyles = function( selector ) { return Microbe.removeStyle( selector, 'all' ); }; };