Skip to content
This repository was archived by the owner on Feb 1, 2019. It is now read-only.

Commit 88f42b5

Browse files
author
Steven Bellamy
committed
Adding fixes for modifiers; Adding standalone init method for row-links
1 parent 52f7911 commit 88f42b5

7 files changed

Lines changed: 54 additions & 32 deletions

File tree

build/example.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/main.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ <h3 id="sample-sortable-table">Sample Sortable Table</h3>
2121
</button>
2222
</th>
2323
<th>
24-
<button class="sortable sort-up" data-sort_type="number">
24+
<button class="sortable sorted-up" data-sort_type="number">
2525
Distance
2626
</button>
2727
</th>

examples/table-row-links.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
'use strict';
1010

11+
var closest = require( '../src/utilities/dom-closest' ).closest;
12+
1113
var TableRowLinks = {
1214

1315
events: {
@@ -25,10 +27,29 @@ var TableRowLinks = {
2527
*/
2628
onRowLinkClick: function onRowLinkClick( event ) {
2729
var target = event.target;
28-
var link = target && target.querySelector( 'a' );
30+
if( target && target.tagName === 'A' ) {
31+
return
32+
}
33+
target = closest( event.target, 'tr' );
34+
var link = target.querySelector( 'a' );
2935
if( link ) window.location = link.getAttribute( 'href' );
30-
}
36+
},
3137

38+
/**
39+
* Handle initilization of Table Row Links. Added for standalone
40+
* use cases.
41+
*
42+
* @param {Object} event Mouse event for click on the table.
43+
*/
44+
init: function() {
45+
var elements = document.querySelector( TableRowLinks.ui.base );
46+
for ( var i = 0; i < elements.length; ++i ) {
47+
if( elements[i].hasAttribute( 'data-bound' ) === false ) {
48+
elements[i].addEventListener( 'click', table,
49+
TableRowLinks.onRowLinkClick );
50+
}
51+
}
52+
}
3253
};
3354

3455
module.exports = TableRowLinks;

examples/table-sortable.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ var UNDEFINED = config.UNDEFINED;
1616
var TableSortable = {
1717

1818
classes: {
19-
sortDown: 'sorted-down',
20-
sortUp: 'sorted-up'
19+
sortDown: 'sorted-down',
20+
sortUp: 'sorted-up'
2121
},
2222

2323
events: {

src/components/AtomicComponent.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function AtomicComponent( element, attributes ) {
3333
this.uId = this.uniqueId( 'ac' );
3434
this.element = element;
3535
assign( this, ( attributes || {} ) );
36+
this.processModifiers();
3637
this.ensureElement();
3738
this.setCachedElements();
3839
this.initialize.forEach( function( func ) {
@@ -54,6 +55,29 @@ assign( AtomicComponent.prototype, Events, classList, {
5455
*/
5556
initialize: [function (){ return }],
5657

58+
/**
59+
* Function used to process class modifiers. These should
60+
* correspond with BEM modifiers.
61+
*
62+
* @param {Object} attributes - Hash of attributes to set on base element.
63+
* @param {Object} atomicComponent - Base component.
64+
*/
65+
processModifiers: function() {
66+
if ( !this.modifiers ) {
67+
return;
68+
}
69+
70+
this.modifiers.forEach( function( modifier ) {
71+
if ( classList.contains( this.element, modifier.ui.base ) ) {
72+
if ( modifier.initialize ) {
73+
this.initialize.push( modifier.initialize );
74+
delete modifier.initialize;
75+
}
76+
assign( this, modifier );
77+
}
78+
}, this );
79+
},
80+
5781
/**
5882
* Function used to render a template in Single Page Applications.
5983
*
@@ -249,12 +273,8 @@ AtomicComponent.extend = function extend( attributes ) {
249273
assign(child.prototype, attributes);
250274
assign(child, AtomicComponent);
251275

252-
if ( attributes.hasOwnProperty( 'modifiers' ) ) {
253-
_processModifiers( attributes, child.prototype );
254-
}
255-
256276
if ( attributes.hasOwnProperty( 'ui' ) &&
257-
attributes.ui.hasOwnProperty( 'base' ) ) {
277+
attributes.ui.hasOwnProperty( 'base' ) ) {
258278
child.selector = attributes.ui.base;
259279
}
260280

@@ -263,26 +283,6 @@ AtomicComponent.extend = function extend( attributes ) {
263283
return child;
264284
};
265285

266-
/**
267-
* Function used to process class modifiers. These should
268-
* correspond with BEM modifiers.
269-
*
270-
* @param {Object} attributes - Hash of attributes to set on base element.
271-
* @param {Object} atomicComponent - Base component.
272-
*/
273-
function _processModifiers( attributes, atomicComponent ) {
274-
attributes.modifiers.forEach( function( modifier ) {
275-
if ( modifier.initialize ) {
276-
atomicComponent.initialize.push( modifier.initialize );
277-
delete modifier.initialize;
278-
}
279-
if ( modifier.ui && modifier.ui.base ) {
280-
atomicComponent.ui.base += ', ' + modifier.ui.base
281-
delete modifier.ui.base;
282-
}
283-
assign( atomicComponent, modifier );
284-
} );
285-
}
286286

287287
/**
288288
* Function used to instantiate all instances of the particular

src/utilities/dom-class-list/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function addClass( element ) {
5757
* @returns {Boolean} indicating if element has class.
5858
*/
5959
function contains( element, className ) {
60+
className = className.replace( '.', '' );
6061
if ( hasClassList ) {
6162
return element.classList.contains( className );
6263
}

0 commit comments

Comments
 (0)