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

Commit

Permalink
Merge pull request #718 from dwaltz/3.1.0-wip
Browse files Browse the repository at this point in the history
Pillbox: allowing data to be associated via jQuery with pillbox items
  • Loading branch information
Kevin Parkerson committed Oct 10, 2014
2 parents 26fea8f + e42a6e8 commit 0d74a57
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ define(function(require) {
"cssClass": "example-pill-class",
"style": "background-color: orange;",
"data-example-attribute": "true"
},
"data": {
"flora": true,
"color": "orange"
}
},
{
Expand All @@ -231,6 +235,10 @@ define(function(require) {
"cssClass": "example-pill-class",
"style": "background-color: midnightBlue;",
"data-example-attribute": "true"
},
"data": {
"flora": true,
"color": "blue"
}
},
{
Expand Down
45 changes: 28 additions & 17 deletions js/pillbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,22 @@

suggestionClick: function(e){
var $item = $(e.currentTarget);
var item = {
text: $item.html(),
value: $item.data('value')
};

e.preventDefault();
this.$addItem.val('');

if ( $item.data('attr') ) {
this.addItems({
text: $item.html(),
value: $item.data('value'),
attr: JSON.parse($item.data('attr'))
}, true);
}
else {
this.addItems({
text: $item.html(),
value: $item.data('value')
}, true);
item.attr = JSON.parse($item.data('attr'));
}

item.data = $item.data('data');

this.addItems(item, true);

// needs to be after addItems for IE
this._closeSuggestions();
},
Expand Down Expand Up @@ -197,6 +195,10 @@
data['attr'] = value.attr; // avoid confusion with $.attr();
}

if(value['data']){
data['data'] = value.data;
}

items[i] = data;

});
Expand Down Expand Up @@ -259,7 +261,7 @@
//First parameter is index (optional)
//Second parameter is new arguments
placeItems: function(){
var newHtml = '';
var $newHtml = [];
var items;
var index;
var $neighbor;
Expand Down Expand Up @@ -300,23 +302,27 @@

}

newHtml += $item.wrap('<div></div>').parent().html();
if(item['data']) {
$item.data('data', item.data);
}

$newHtml.push($item);
});

if( this.$pillGroup.children('.pill').length > 0 ) {
if( index ){
$neighbor = this.$pillGroup.find('.pill:nth-child(' + index + ')');

if( $neighbor.length ){
$neighbor.before(newHtml);
$neighbor.before($newHtml);
} else {
this.$pillGroup.children('.pill:last').after(newHtml);
this.$pillGroup.children('.pill:last').after($newHtml);
}
} else {
this.$pillGroup.children('.pill:last').after(newHtml);
this.$pillGroup.children('.pill:last').after($newHtml);
}
} else {
this.$pillGroup.prepend(newHtml);
this.$pillGroup.prepend($newHtml);
}

if( isInternal ){
Expand Down Expand Up @@ -595,6 +601,11 @@
if(value.attr) {
$suggestion.data('attr', JSON.stringify(value.attr));
}

if(value.data){
$suggestion.data('data', value.data);
}

$suggestionList.append($suggestion);
});

Expand Down

0 comments on commit 0d74a57

Please sign in to comment.