Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
Updated all dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
cferdinandi committed Apr 7, 2017
1 parent bd13dae commit 6122b53
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 807 deletions.
267 changes: 1 addition & 266 deletions dist/js/myplugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,269 +5,4 @@
* http://github.com/cferdinandi/Plugin
*/

(function (root, factory) {
if ( typeof define === 'function' && define.amd ) {
define([], factory(root));
} else if ( typeof exports === 'object' ) {
module.exports = factory(root);
} else {
root.myPlugin = factory(root);
}
})(typeof global !== 'undefined' ? global : this.window || this.global, (function (root) {

'use strict';

//
// Variables
//

var myPlugin = {}; // Object for public APIs
var supports = 'querySelector' in document && 'addEventListener' in root; // Feature test
var settings, eventTimeout;

// Default settings
var defaults = {
someVar: 123,
initClass: 'js-myplugin',
callback: function () {}
};


//
// Methods
//

/**
* A simple forEach() implementation for Arrays, Objects and NodeLists
* @private
* @param {Array|Object|NodeList} collection Collection of items to iterate
* @param {Function} callback Callback function for each iteration
* @param {Array|Object|NodeList} scope Object/NodeList/Array that forEach is iterating over (aka `this`)
*/
var forEach = function (collection, callback, scope) {
if (Object.prototype.toString.call(collection) === '[object Object]') {
for (var prop in collection) {
if (Object.prototype.hasOwnProperty.call(collection, prop)) {
callback.call(scope, collection[prop], prop, collection);
}
}
} else {
for (var i = 0, len = collection.length; i < len; i++) {
callback.call(scope, collection[i], i, collection);
}
}
};

/**
* Merge two or more objects. Returns a new object.
* @private
* @param {Boolean} deep If true, do a deep (or recursive) merge [optional]
* @param {Object} objects The objects to merge together
* @returns {Object} Merged values of defaults and options
*/
var extend = function () {

// Variables
var extended = {};
var deep = false;
var i = 0;
var length = arguments.length;

// Check if a deep merge
if ( Object.prototype.toString.call( arguments[0] ) === '[object Boolean]' ) {
deep = arguments[0];
i++;
}

// Merge the object into the extended object
var merge = function (obj) {
for ( var prop in obj ) {
if ( Object.prototype.hasOwnProperty.call( obj, prop ) ) {
// If deep merge and property is an object, merge properties
if ( deep && Object.prototype.toString.call(obj[prop]) === '[object Object]' ) {
extended[prop] = extend( true, extended[prop], obj[prop] );
} else {
extended[prop] = obj[prop];
}
}
}
};

// Loop through each object and conduct a merge
for ( ; i < length; i++ ) {
var obj = arguments[i];
merge(obj);
}

return extended;

};

/**
* Convert data-options attribute into an object of key/value pairs
* @private
* @param {String} options Link-specific options as a data attribute string
* @returns {Object}
*/
var getDataOptions = function ( options ) {
return !options || !(typeof JSON === 'object' && typeof JSON.parse === 'function') ? {} : JSON.parse( options );
};

/**
* Get the closest matching element up the DOM tree.
* @private
* @param {Element} elem Starting element
* @param {String} selector Selector to match against (class, ID, data attribute, or tag)
* @return {Boolean|Element} Returns null if not match found
*/
var getClosest = function ( elem, selector ) {

// Variables
var firstChar = selector.charAt(0);
var supports = 'classList' in document.documentElement;
var attribute, value;

// If selector is a data attribute, split attribute from value
if ( firstChar === '[' ) {
selector = selector.substr(1, selector.length - 2);
attribute = selector.split( '=' );

if ( attribute.length > 1 ) {
value = true;
attribute[1] = attribute[1].replace( /"/g, '' ).replace( /'/g, '' );
}
}

// Get closest match
for ( ; elem && elem !== document; elem = elem.parentNode ) {

// If selector is a class
if ( firstChar === '.' ) {
if ( supports ) {
if ( elem.classList.contains( selector.substr(1) ) ) {
return elem;
}
} else {
if ( new RegExp('(^|\\s)' + selector.substr(1) + '(\\s|$)').test( elem.className ) ) {
return elem;
}
}
}

// If selector is an ID
if ( firstChar === '#' ) {
if ( elem.id === selector.substr(1) ) {
return elem;
}
}

// If selector is a data attribute
if ( firstChar === '[' ) {
if ( elem.hasAttribute( attribute[0] ) ) {
if ( value ) {
if ( elem.getAttribute( attribute[0] ) === attribute[1] ) {
return elem;
}
} else {
return elem;
}
}
}

// If selector is a tag
if ( elem.tagName.toLowerCase() === selector ) {
return elem;
}

}

return null;

};

// @todo Do something...

/**
* Handle events
* @private
*/
var eventHandler = function (event) {
var toggle = event.target;
var closest = getClosest(toggle, '[data-some-selector]');
if ( closest ) {
// run methods
}
};

/**
* On window scroll and resize, only run events at a rate of 15fps for better performance
* @private
* @param {Function} eventTimeout Timeout function
* @param {Object} settings
*/
var eventThrottler = function () {
if ( !eventTimeout ) {
eventTimeout = setTimeout((function() {
eventTimeout = null;
actualMethod( settings );
}), 66);
}
};

/**
* Destroy the current initialization.
* @public
*/
myPlugin.destroy = function () {

// If plugin isn't already initialized, stop
if ( !settings ) return;

// Remove init class for conditional CSS
document.documentElement.classList.remove( settings.initClass );

// @todo Undo any other init functions...

// Remove event listeners
document.removeEventListener('click', eventHandler, false);

// Reset variables
settings = null;
eventTimeout = null;

};

/**
* Initialize Plugin
* @public
* @param {Object} options User settings
*/
myPlugin.init = function ( options ) {

// feature test
if ( !supports ) return;

// Destroy any existing initializations
myPlugin.destroy();

// Merge user options with defaults
settings = extend( defaults, options || {} );

// Add class to HTML element to activate conditional CSS
document.documentElement.classList.add( settings.initClass );

// @todo Do something...

// Listen for events
document.addEventListener('click', eventHandler, false);

};


//
// Public APIs
//

return myPlugin;

}));
// JavaScript goes here...
1 change: 0 additions & 1 deletion dist/js/myplugin.min.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/*! gulp-boilerplate v5.0.0 | (c) 2017 Chris Ferdinandi | MIT License | http://github.com/cferdinandi/Plugin */
!(function(t,e){"function"==typeof define&&define.amd?define([],e(t)):"object"==typeof exports?module.exports=e(t):t.myPlugin=e(t)})("undefined"!=typeof global?global:this.window||this.global,(function(t){"use strict";var e,n,r={},o="querySelector"in document&&"addEventListener"in t,i={someVar:123,initClass:"js-myplugin",callback:function(){}},s=function(){var t={},e=!1,n=0,r=arguments.length;"[object Boolean]"===Object.prototype.toString.call(arguments[0])&&(e=arguments[0],n++);for(;n<r;n++){var o=arguments[n];!(function(n){for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e&&"[object Object]"===Object.prototype.toString.call(n[r])?t[r]=s(!0,t[r],n[r]):t[r]=n[r])})(o)}return t},c=function(t,e){var n,r,o=e.charAt(0),i="classList"in document.documentElement;for("["===o&&(e=e.substr(1,e.length-2),n=e.split("="),n.length>1&&(r=!0,n[1]=n[1].replace(/"/g,"").replace(/'/g,"")));t&&t!==document;t=t.parentNode){if("."===o)if(i){if(t.classList.contains(e.substr(1)))return t}else if(new RegExp("(^|\\s)"+e.substr(1)+"(\\s|$)").test(t.className))return t;if("#"===o&&t.id===e.substr(1))return t;if("["===o&&t.hasAttribute(n[0])){if(!r)return t;if(t.getAttribute(n[0])===n[1])return t}if(t.tagName.toLowerCase()===e)return t}return null},l=function(t){var e=t.target;c(e,"[data-some-selector]")};return r.destroy=function(){e&&(document.documentElement.classList.remove(e.initClass),document.removeEventListener("click",l,!1),e=null,n=null)},r.init=function(t){o&&(r.destroy(),e=s(i,t||{}),document.documentElement.classList.add(e.initClass),document.addEventListener("click",l,!1))},r}));
Loading

0 comments on commit 6122b53

Please sign in to comment.