123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- /**
- * Fizzy UI utils v2.0.7
- * MIT license
- */
- /*jshint browser: true, undef: true, unused: true, strict: true */
- ( function( window, factory ) {
- // universal module definition
- /*jshint strict: false */ /*globals define, module, require */
- if ( typeof define == 'function' && define.amd ) {
- // AMD
- define( [
- 'desandro-matches-selector/matches-selector'
- ], function( matchesSelector ) {
- return factory( window, matchesSelector );
- });
- } else if ( typeof module == 'object' && module.exports ) {
- // CommonJS
- module.exports = factory(
- window,
- require('desandro-matches-selector')
- );
- } else {
- // browser global
- window.fizzyUIUtils = factory(
- window,
- window.matchesSelector
- );
- }
- }( window, function factory( window, matchesSelector ) {
- 'use strict';
- var utils = {};
- // ----- extend ----- //
- // extends objects
- utils.extend = function( a, b ) {
- for ( var prop in b ) {
- a[ prop ] = b[ prop ];
- }
- return a;
- };
- // ----- modulo ----- //
- utils.modulo = function( num, div ) {
- return ( ( num % div ) + div ) % div;
- };
- // ----- makeArray ----- //
- var arraySlice = Array.prototype.slice;
- // turn element or nodeList into an array
- utils.makeArray = function( obj ) {
- if ( Array.isArray( obj ) ) {
- // use object if already an array
- return obj;
- }
- // return empty array if undefined or null. #6
- if ( obj === null || obj === undefined ) {
- return [];
- }
- var isArrayLike = typeof obj == 'object' && typeof obj.length == 'number';
- if ( isArrayLike ) {
- // convert nodeList to array
- return arraySlice.call( obj );
- }
- // array of single index
- return [ obj ];
- };
- // ----- removeFrom ----- //
- utils.removeFrom = function( ary, obj ) {
- var index = ary.indexOf( obj );
- if ( index != -1 ) {
- ary.splice( index, 1 );
- }
- };
- // ----- getParent ----- //
- utils.getParent = function( elem, selector ) {
- while ( elem.parentNode && elem != document.body ) {
- elem = elem.parentNode;
- if ( matchesSelector( elem, selector ) ) {
- return elem;
- }
- }
- };
- // ----- getQueryElement ----- //
- // use element as selector string
- utils.getQueryElement = function( elem ) {
- if ( typeof elem == 'string' ) {
- return document.querySelector( elem );
- }
- return elem;
- };
- // ----- handleEvent ----- //
- // enable .ontype to trigger from .addEventListener( elem, 'type' )
- utils.handleEvent = function( event ) {
- var method = 'on' + event.type;
- if ( this[ method ] ) {
- this[ method ]( event );
- }
- };
- // ----- filterFindElements ----- //
- utils.filterFindElements = function( elems, selector ) {
- // make array of elems
- elems = utils.makeArray( elems );
- var ffElems = [];
- elems.forEach( function( elem ) {
- // check that elem is an actual element
- if ( !( elem instanceof HTMLElement ) ) {
- return;
- }
- // add elem if no selector
- if ( !selector ) {
- ffElems.push( elem );
- return;
- }
- // filter & find items if we have a selector
- // filter
- if ( matchesSelector( elem, selector ) ) {
- ffElems.push( elem );
- }
- // find children
- var childElems = elem.querySelectorAll( selector );
- // concat childElems to filterFound array
- for ( var i=0; i < childElems.length; i++ ) {
- ffElems.push( childElems[i] );
- }
- });
- return ffElems;
- };
- // ----- debounceMethod ----- //
- utils.debounceMethod = function( _class, methodName, threshold ) {
- threshold = threshold || 100;
- // original method
- var method = _class.prototype[ methodName ];
- var timeoutName = methodName + 'Timeout';
- _class.prototype[ methodName ] = function() {
- var timeout = this[ timeoutName ];
- clearTimeout( timeout );
- var args = arguments;
- var _this = this;
- this[ timeoutName ] = setTimeout( function() {
- method.apply( _this, args );
- delete _this[ timeoutName ];
- }, threshold );
- };
- };
- // ----- docReady ----- //
- utils.docReady = function( callback ) {
- var readyState = document.readyState;
- if ( readyState == 'complete' || readyState == 'interactive' ) {
- // do async to allow for other scripts to run. metafizzy/flickity#441
- setTimeout( callback );
- } else {
- document.addEventListener( 'DOMContentLoaded', callback );
- }
- };
- // ----- htmlInit ----- //
- // http://jamesroberts.name/blog/2010/02/22/string-functions-for-javascript-trim-to-camel-case-to-dashed-and-to-underscore/
- utils.toDashed = function( str ) {
- return str.replace( /(.)([A-Z])/g, function( match, $1, $2 ) {
- return $1 + '-' + $2;
- }).toLowerCase();
- };
- var console = window.console;
- /**
- * allow user to initialize classes via [data-namespace] or .js-namespace class
- * htmlInit( Widget, 'widgetName' )
- * options are parsed from data-namespace-options
- */
- utils.htmlInit = function( WidgetClass, namespace ) {
- utils.docReady( function() {
- var dashedNamespace = utils.toDashed( namespace );
- var dataAttr = 'data-' + dashedNamespace;
- var dataAttrElems = document.querySelectorAll( '[' + dataAttr + ']' );
- var jsDashElems = document.querySelectorAll( '.js-' + dashedNamespace );
- var elems = utils.makeArray( dataAttrElems )
- .concat( utils.makeArray( jsDashElems ) );
- var dataOptionsAttr = dataAttr + '-options';
- var jQuery = window.jQuery;
- elems.forEach( function( elem ) {
- var attr = elem.getAttribute( dataAttr ) ||
- elem.getAttribute( dataOptionsAttr );
- var options;
- try {
- options = attr && JSON.parse( attr );
- } catch ( error ) {
- // log error, do not initialize
- if ( console ) {
- console.error( 'Error parsing ' + dataAttr + ' on ' + elem.className +
- ': ' + error );
- }
- return;
- }
- // initialize
- var instance = new WidgetClass( elem, options );
- // make available via $().data('namespace')
- if ( jQuery ) {
- jQuery.data( elem, namespace, instance );
- }
- });
- });
- };
- // ----- ----- //
- return utils;
- }));
|