import from la bonne adresse and first refactoring for ouidade.com
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"name": "fizzy-ui-utils",
|
||||
"version": "1.0.1",
|
||||
"authors": [
|
||||
"David DeSandro"
|
||||
],
|
||||
"description": "UI utilities",
|
||||
"main": "utils.js",
|
||||
"dependencies": {
|
||||
"doc-ready": "~1.0.4",
|
||||
"matches-selector": "~1.0.2"
|
||||
},
|
||||
"moduleType": [
|
||||
"amd",
|
||||
"globals",
|
||||
"node"
|
||||
],
|
||||
"keywords": [
|
||||
"utility",
|
||||
"ui"
|
||||
],
|
||||
"license": "MIT",
|
||||
"ignore": [
|
||||
"**/.*",
|
||||
"node_modules",
|
||||
"bower_components",
|
||||
"test",
|
||||
"tests",
|
||||
"package.json"
|
||||
],
|
||||
"homepage": "https://github.com/metafizzy/fizzy-ui-utils",
|
||||
"_release": "1.0.1",
|
||||
"_resolution": {
|
||||
"type": "version",
|
||||
"tag": "v1.0.1",
|
||||
"commit": "823b543b583f4831d25aadf94c26fc6018e62172"
|
||||
},
|
||||
"_source": "git://github.com/metafizzy/fizzy-ui-utils.git",
|
||||
"_target": "~1.0.1",
|
||||
"_originalSource": "fizzy-ui-utils"
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
# Fizzy UI utils
|
||||
|
||||
UI utility & helper functions
|
||||
|
||||
Used in [Flickity](http://flickity.metafizzy.co), [Isotope](http://isotope.metafizzy.co), [Masonry](http://masonry.desandro.com), [Draggabilly](http://draggabilly.desandro.com)
|
||||
|
||||
## Install
|
||||
|
||||
Bower: `bower install fizzy-ui-utils --save`
|
||||
|
||||
npm: `npm install fizzy-ui-utils --save`
|
||||
|
||||
## API
|
||||
|
||||
``` js
|
||||
// fizzyUIUtils is the browser global
|
||||
var utils = fizzyUIUtils;
|
||||
|
||||
// ---- ---- //
|
||||
|
||||
utils.extend( a, b )
|
||||
// extend object
|
||||
|
||||
utils.modulo( num, div )
|
||||
// num [modulo] div
|
||||
|
||||
utils.isArray( obj )
|
||||
// check if object is Array
|
||||
|
||||
utils.makeArray( obj )
|
||||
// make array from object
|
||||
|
||||
utils.indexOf( ary, obj )
|
||||
// get index of object in array
|
||||
|
||||
utils.removeFrom( ary, obj )
|
||||
// remove object from array
|
||||
|
||||
utils.isElement( obj )
|
||||
// check if object is an element
|
||||
|
||||
utils.setText( elem, text )
|
||||
// set text of an element
|
||||
|
||||
utils.getParent( elem, selector )
|
||||
// get parent element of an element, given a selector string
|
||||
|
||||
utils.getQueryElement( elem )
|
||||
// if elem is a string, use it as a selector and return element
|
||||
|
||||
Class.prototype.handleEvent = utils.handleEvent;
|
||||
// enable Class.onclick when element.addEventListener( 'click', this, false )
|
||||
|
||||
utils.filterFindElements( elems, selector )
|
||||
// iterate through elems, filter and find all elements that match selector
|
||||
|
||||
utils.debounceMethod( Class, methodName, threhold )
|
||||
// debounce a class method
|
||||
|
||||
utils.toDashed( str )
|
||||
// 'camelCaseString' -> 'camel-case-string'
|
||||
|
||||
utils.htmlInit( Class, namespace )
|
||||
// on document ready, initialize Class on every element
|
||||
// that matches js-namespace
|
||||
// pass in JSON options from element's data-options-namespace attribute
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
MIT license. Have at it.
|
||||
|
||||
By [Metafizzy](http://metafizzy.co)
|
||||
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"name": "fizzy-ui-utils",
|
||||
"version": "1.0.1",
|
||||
"authors": [
|
||||
"David DeSandro"
|
||||
],
|
||||
"description": "UI utilities",
|
||||
"main": "utils.js",
|
||||
"dependencies": {
|
||||
"doc-ready": "~1.0.4",
|
||||
"matches-selector": "~1.0.2"
|
||||
},
|
||||
"moduleType": [
|
||||
"amd",
|
||||
"globals",
|
||||
"node"
|
||||
],
|
||||
"keywords": [
|
||||
"utility",
|
||||
"ui"
|
||||
],
|
||||
"license": "MIT",
|
||||
"ignore": [
|
||||
"**/.*",
|
||||
"node_modules",
|
||||
"bower_components",
|
||||
"test",
|
||||
"tests",
|
||||
"package.json"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,270 @@
|
||||
/**
|
||||
* Fizzy UI utils v1.0.1
|
||||
* MIT license
|
||||
*/
|
||||
|
||||
/*jshint browser: true, undef: true, unused: true, strict: true */
|
||||
|
||||
( function( window, factory ) {
|
||||
/*global define: false, module: false, require: false */
|
||||
'use strict';
|
||||
// universal module definition
|
||||
|
||||
if ( typeof define == 'function' && define.amd ) {
|
||||
// AMD
|
||||
define( [
|
||||
'doc-ready/doc-ready',
|
||||
'matches-selector/matches-selector'
|
||||
], function( docReady, matchesSelector ) {
|
||||
return factory( window, docReady, matchesSelector );
|
||||
});
|
||||
} else if ( typeof exports == 'object' ) {
|
||||
// CommonJS
|
||||
module.exports = factory(
|
||||
window,
|
||||
require('doc-ready'),
|
||||
require('desandro-matches-selector')
|
||||
);
|
||||
} else {
|
||||
// browser global
|
||||
window.fizzyUIUtils = factory(
|
||||
window,
|
||||
window.docReady,
|
||||
window.matchesSelector
|
||||
);
|
||||
}
|
||||
|
||||
}( window, function factory( window, docReady, 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;
|
||||
};
|
||||
|
||||
// ----- isArray ----- //
|
||||
|
||||
var objToString = Object.prototype.toString;
|
||||
utils.isArray = function( obj ) {
|
||||
return objToString.call( obj ) == '[object Array]';
|
||||
};
|
||||
|
||||
// ----- makeArray ----- //
|
||||
|
||||
// turn element or nodeList into an array
|
||||
utils.makeArray = function( obj ) {
|
||||
var ary = [];
|
||||
if ( utils.isArray( obj ) ) {
|
||||
// use object if already an array
|
||||
ary = obj;
|
||||
} else if ( obj && typeof obj.length == 'number' ) {
|
||||
// convert nodeList to array
|
||||
for ( var i=0, len = obj.length; i < len; i++ ) {
|
||||
ary.push( obj[i] );
|
||||
}
|
||||
} else {
|
||||
// array of single index
|
||||
ary.push( obj );
|
||||
}
|
||||
return ary;
|
||||
};
|
||||
|
||||
// ----- indexOf ----- //
|
||||
|
||||
// index of helper cause IE8
|
||||
utils.indexOf = Array.prototype.indexOf ? function( ary, obj ) {
|
||||
return ary.indexOf( obj );
|
||||
} : function( ary, obj ) {
|
||||
for ( var i=0, len = ary.length; i < len; i++ ) {
|
||||
if ( ary[i] === obj ) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
|
||||
// ----- removeFrom ----- //
|
||||
|
||||
utils.removeFrom = function( ary, obj ) {
|
||||
var index = utils.indexOf( ary, obj );
|
||||
if ( index != -1 ) {
|
||||
ary.splice( index, 1 );
|
||||
}
|
||||
};
|
||||
|
||||
// ----- isElement ----- //
|
||||
|
||||
// http://stackoverflow.com/a/384380/182183
|
||||
utils.isElement = ( typeof HTMLElement == 'function' || typeof HTMLElement == 'object' ) ?
|
||||
function isElementDOM2( obj ) {
|
||||
return obj instanceof HTMLElement;
|
||||
} :
|
||||
function isElementQuirky( obj ) {
|
||||
return obj && typeof obj == 'object' &&
|
||||
obj.nodeType == 1 && typeof obj.nodeName == 'string';
|
||||
};
|
||||
|
||||
// ----- setText ----- //
|
||||
|
||||
utils.setText = ( function() {
|
||||
var setTextProperty;
|
||||
function setText( elem, text ) {
|
||||
// only check setTextProperty once
|
||||
setTextProperty = setTextProperty || ( document.documentElement.textContent !== undefined ? 'textContent' : 'innerText' );
|
||||
elem[ setTextProperty ] = text;
|
||||
}
|
||||
return setText;
|
||||
})();
|
||||
|
||||
// ----- getParent ----- //
|
||||
|
||||
utils.getParent = function( elem, selector ) {
|
||||
while ( 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 = [];
|
||||
|
||||
for ( var i=0, len = elems.length; i < len; i++ ) {
|
||||
var elem = elems[i];
|
||||
// check that elem is an actual element
|
||||
if ( !utils.isElement( elem ) ) {
|
||||
continue;
|
||||
}
|
||||
// filter & find items if we have a selector
|
||||
if ( selector ) {
|
||||
// filter siblings
|
||||
if ( matchesSelector( elem, selector ) ) {
|
||||
ffElems.push( elem );
|
||||
}
|
||||
// find children
|
||||
var childElems = elem.querySelectorAll( selector );
|
||||
// concat childElems to filterFound array
|
||||
for ( var j=0, jLen = childElems.length; j < jLen; j++ ) {
|
||||
ffElems.push( childElems[j] );
|
||||
}
|
||||
} else {
|
||||
ffElems.push( elem );
|
||||
}
|
||||
}
|
||||
|
||||
return ffElems;
|
||||
};
|
||||
|
||||
// ----- debounceMethod ----- //
|
||||
|
||||
utils.debounceMethod = function( _class, methodName, threshold ) {
|
||||
// original method
|
||||
var method = _class.prototype[ methodName ];
|
||||
var timeoutName = methodName + 'Timeout';
|
||||
|
||||
_class.prototype[ methodName ] = function() {
|
||||
var timeout = this[ timeoutName ];
|
||||
if ( timeout ) {
|
||||
clearTimeout( timeout );
|
||||
}
|
||||
var args = arguments;
|
||||
|
||||
var _this = this;
|
||||
this[ timeoutName ] = setTimeout( function() {
|
||||
method.apply( _this, args );
|
||||
delete _this[ timeoutName ];
|
||||
}, threshold || 100 );
|
||||
};
|
||||
};
|
||||
|
||||
// ----- 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 .js-namespace class
|
||||
* htmlInit( Widget, 'widgetName' )
|
||||
* options are parsed from data-namespace-option attribute
|
||||
*/
|
||||
utils.htmlInit = function( WidgetClass, namespace ) {
|
||||
docReady( function() {
|
||||
var dashedNamespace = utils.toDashed( namespace );
|
||||
var elems = document.querySelectorAll( '.js-' + dashedNamespace );
|
||||
var dataAttr = 'data-' + dashedNamespace + '-options';
|
||||
|
||||
for ( var i=0, len = elems.length; i < len; i++ ) {
|
||||
var elem = elems[i];
|
||||
var attr = elem.getAttribute( dataAttr );
|
||||
var options;
|
||||
try {
|
||||
options = attr && JSON.parse( attr );
|
||||
} catch ( error ) {
|
||||
// log error, do not initialize
|
||||
if ( console ) {
|
||||
console.error( 'Error parsing ' + dataAttr + ' on ' +
|
||||
elem.nodeName.toLowerCase() + ( elem.id ? '#' + elem.id : '' ) + ': ' +
|
||||
error );
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// initialize
|
||||
var instance = new WidgetClass( elem, options );
|
||||
// make available via $().data('layoutname')
|
||||
var jQuery = window.jQuery;
|
||||
if ( jQuery ) {
|
||||
jQuery.data( elem, namespace, instance );
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// ----- ----- //
|
||||
|
||||
return utils;
|
||||
|
||||
}));
|
||||
Reference in New Issue
Block a user