Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chambaz committed Dec 3, 2013
0 parents commit 64c3d99
Show file tree
Hide file tree
Showing 4 changed files with 286 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Numerous always-ignore extensions
*.bak
*.diff
*.err
*.orig
*.log
*.rej
*.swo
*.swp
*.vi
.sass-cache

# OS or Editor folders
.DS_Store
._*
Thumbs.db
.cache
.project
.settings
.tmproj
nbproject
*.sublime-project
*.sublime-workspace

# Dreamweaver added files
_notes
dwsync.xml

# Komodo
*.komodoproject
.komodotools

# Espresso
*.esproj
*.espressostorage

# Rubinius
*.rbc

# Folders to ignore
.hg
.svn
.CVS
.idea
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
Gumby Comments
==============

This extension provides a purely client-side solution to conditional loading. Refactored into a Gumby UI module from the awesome [ResponsiveComments](http://responsivecomments.com/). For more detailed documentation please check out the [Gumby docs](http://gumbyframework.com).

Installation
------------

A bower package is available to install this module into your project. We recommend using this method to install Gumby and any extra UI modules, however you can alternatively move the individuals files into your project.

$ bower install gumby-comments

Include gumby.comments.js in the same fashion as your other UI modules, after gumby.js and before gumby.init.js. In production you should minify JavaScript files into a single optimized gumby.min.js file, ensuring the order (gumby.js, UI modules, gumby.init.js) is retained.

<!--
Include gumby.js followed by UI modules.
Or concatenate and minify into a single file-->
<script src="/bower_components/gumby/js/libs/gumby.js"></script>
<script src="/bower_components/gumby/js/libs/ui/gumby.skiplink.js"></script>
<script src="/bower_components/gumby/js/libs/ui/gumby.toggleswitch.js"></script>
<script src="/bower_components/gumby-comments/gumby.comments.js"></script>
<script src="/bower_components/gumby/js/libs/gumby.init.js"></script>

<!-- In production minifiy and combine the above files into gumby.min.js -->
<script src="js/gumby.min.js"></script>
<script src="js/plugins.js"></script>
<script src="js/main.js"></script>

Usage
-----
### Media Queries

To get started using Gumby Comments, add a `[gumby-comment-media]` attribute containing a valid media query to any element containing children that you wish to conditionally load. The Gumby Comments concept will only work in a progressivelty enhanced experience so make sure you use minimum width media queries.

```html
<div gumby-comment-media="(min-width: 769px)">...</div>
```

The `[gumby-comment-media]` containers should contain one commented-out segment of markup as well as any other HTML they require.

```html
<div gumby-comment-media="(min-width: 769px)">
<!-- <div><p>Any content can go in here</p></div> -->
</div>
```

### Feature Detection

Gumby Comments also supports feature detection using [Modernizr](http://modernizr.com/). Make sure you include a Modernizr build with all the tests you need before the Gumby JS. A Modernizr test, or a comma separated list of multiple Modernizr tests, can then be specified in `[gumby-comment-supports]`.

```html
<div gumby-comment-supports="svg">
<!-- <div><p>Any content can go in here, including images</p></div> -->
</div>
```

The `[gumby-comment-supports]` containers should also contain one commented out segment of markup as well as any other HTML they require.

### DOM Insertion

The commented out markup inside the Gumby Comments container will be inserted into the DOM when the specified media query or feature detection passes. Any valid [insertAdjacentHTML](https://developer.mozilla.org/en-US/docs/Web/API/Element.insertAdjacentHTML) insertion type can be specified in `[data-responsive-comment-insert]`, valid options are `beforebegin`, `afterbegin`, `beforeend`, `afterend` with a default of `beforeend`.

```html
<div gumby-comment-insert="afterbegin"
gumby-comment-media="(min-width: 769px)">
<!-- <div><p>Any content can go in here, including images</p></div> -->
</div>
```

Check out the [ResonsiveComments website](http://responsivecomments.com/) for a detailed look at how the module works.


**MIT Open Source License**

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5 changes: 5 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "gumby-comments",
"version": "1.0.0",
"main": ["./gumby.comments.js"]
}
151 changes: 151 additions & 0 deletions gumby.comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/**
* Gumby Comments
*/
!function($) {

'use strict';

// data attributes
var attrs = {
'media' : 'gumby-comment-media',
'supports' : 'gumby-comment-supports',
'insert' : 'gumby-comment-insert'
};

// cache responsive comments nodes and data
function prepNodes(nodes) {
var i = 0, l = nodes.length, mqs = [], el, ins, obj;
for(; i < l; i++) {
el = nodes[i];
// beforeend default insert type
ins = el.getAttribute(attrs.insert) || 'beforeend';
// store element, insert type, media query / support test
obj = {
'element' : el,
'insert' : ins,
'media' : el.getAttribute(attrs.media) || false,
'supports' : el.getAttribute(attrs.supports) || false
};

mqs.push(obj);
}
return mqs;
}

// call required function (media/supports) on each array element
function testNodes(type) {
// if type present first argument is event object
type = typeof type === 'string' ? type : false;
this.forEach(function(x) {
if((type === 'supports' || !type) && x.supports) {
testSupportNodes.apply(x);
} else if((type === 'media' || !type) && x.media) {
testMediaNodes.apply(x);
}
});
}

// test media query nodes, requires matchMedia
function testMediaNodes() {
// matchMedia and media query itself required
if(!this.media || !window.matchMedia) {
return;
}

// media query passes and attribute not already set to complete
if(window.matchMedia(this.media).matches &&
this.element.getAttribute(attrs.media) !== 'complete') {
childNodes.apply(this);
return;
}

return;
}

// test media query nodes, requires matchMedia
function testSupportNodes() {
// Modernizr and tests required
if(!this.supports || !Modernizr ||
// test already been carried out
this.element.getAttribute(attrs.support) === 'complete') {
return;
}

if(featureDetection(this.supports.split(','))) {
childNodes.apply(this);
return;
}

return;
}

// handle Modernir feature detection
// if multiple features hand off to multiFeatureDetection
function featureDetection(features) {
// multiple feature detections
if(features.length > 1) {
return multiFeatureDetection(features);
}

// single feature detection
return Modernizr[features];
}

// handle multiple modernizr feature detections
function multiFeatureDetection(features) {
var passed = true;
features.forEach(function(feature) {
if(!Modernizr[feature]) {
passed = false;
}
});
return passed;
}

// loop round child nodes, find commented content
function childNodes() {
var l = this.element.childNodes.length,
i = 0;

for(; i < l; i++) {
// node type 8 is comment
if(this.element.childNodes[i].nodeType === 8) {
insertComment.apply(this, [i]);
}
}
}

// insert commented content into DOM, mark as complete and trigger event
function insertComment(index) {
this.element.insertAdjacentHTML(this.insert, this.element.childNodes[index].textContent);
this.element.setAttribute(attrs.media, 'complete');
dispatchEvent.apply(this);
}

// dispatch CustomEvent if supported with media query and insert type detail
function dispatchEvent() {
$(this.element).trigger('gumby.onInsert');
}

// add initialisation
Gumby.addInitalisation('comments', function() {
// find and cache responsive comments nodes
var els = prepNodes(
document.querySelectorAll('['+attrs.media+'],['+attrs.supports+']')
);

// test media nodes only on resize
window.addEventListener('resize', testNodes.bind(els, 'media'));
// test media and support nodes on load
window.addEventListener('load', testNodes.bind(els));
});

// register UI module
Gumby.UIModule({
module: 'comments',
events: ['onInsert'],
init: function() {
Gumby.initialize('comments');
}
});
}(jQuery);

0 comments on commit 64c3d99

Please sign in to comment.