Skip to content

Commit

Permalink
Implemented events (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
timschlechter committed Jul 25, 2014
1 parent 3494210 commit 767f296
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 2 deletions.
56 changes: 55 additions & 1 deletion examples/bootstrap3/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ <h2>Methods</h2>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th colspan="2">method</th>
<th>method</th>
<th>description</th>
</tr>
</thead>
Expand Down Expand Up @@ -569,6 +569,60 @@ <h2>Methods</h2>
</tbody>
</table>
</section>

<section id="methods">
<div class="page-header">
<h2>Events</h2>
</div>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>event</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>beforeItemAdd</code></td>
<td>
Triggered just before an item gets added. Example:
<pre class="prettyprint linenums">$('input').on('beforeItemAdd', function(event) {
// event.item: contains the item
// event.cancel: set to true to prevent the item getting added
});</pre>
</td>
</tr>
<tr>
<td><code>itemAdded</code></td>
<td>
Triggered just after an item got added. Example:
<pre class="prettyprint linenums">$('input').on('itemAdded', function(event) {
// event.item: contains the item
});</pre>
</td>
</tr>
<tr>
<td><code>beforeItemRemove</code></td>
<td>
Triggered just before an item gets removed. Example:
<pre class="prettyprint linenums">$('input').on('beforeItemRemove', function(event) {
// event.item: contains the item
// event.cancel: set to true to prevent the item getting removed
});</pre>
</td>
</tr>
<tr>
<td><code>itemRemoved</code></td>
<td>
Triggered just after an item got removed. Example:
<pre class="prettyprint linenums">$('input').on('itemRemoved', function(event) {
// event.item: contains the item
});</pre>
</td>
</tr>
</tbody>
</table>
</section>
</div>
<footer class="bs-footer">
<p>Code licensed under <a href="https://raw.github.com/TimSchlechter/bootstrap-tagsinput/master/LICENSE" target="_blank">MIT License</a></p>
Expand Down
56 changes: 55 additions & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ <h2>Methods</h2>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th colspan="2">method</th>
<th>method</th>
<th>description</th>
</tr>
</thead>
Expand Down Expand Up @@ -557,6 +557,60 @@ <h2>Methods</h2>
</tbody>
</table>
</section>

<section id="methods">
<div class="page-header">
<h2>Events</h2>
</div>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>event</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>beforeItemAdd</code></td>
<td>
Triggered just before an item gets added. Example:
<pre class="prettyprint linenums">$('input').on('beforeItemAdd', function(event) {
// event.item: contains the item
// event.cancel: set to true to prevent the item getting added
});</pre>
</td>
</tr>
<tr>
<td><code>itemAdded</code></td>
<td>
Triggered just after an item got added. Example:
<pre class="prettyprint linenums">$('input').on('itemAdded', function(event) {
// event.item: contains the item
});</pre>
</td>
</tr>
<tr>
<td><code>beforeItemRemove</code></td>
<td>
Triggered just before an item gets removed. Example:
<pre class="prettyprint linenums">$('input').on('beforeItemRemove', function(event) {
// event.item: contains the item
// event.cancel: set to true to prevent the item getting removed
});</pre>
</td>
</tr>
<tr>
<td><code>itemRemoved</code></td>
<td>
Triggered just after an item got removed. Example:
<pre class="prettyprint linenums">$('input').on('itemRemoved', function(event) {
// event.item: contains the item
});</pre>
</td>
</tr>
</tbody>
</table>
</section>
</div>
<footer class="footer">
<p>Code licensed under <a href="https://raw.github.com/TimSchlechter/bootstrap-tagsinput/master/LICENSE" target="_blank">MIT License</a></p>
Expand Down
11 changes: 11 additions & 0 deletions src/bootstrap-tagsinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@
if (self.items().toString().length + item.length + 1 > self.options.maxInputLength)
return;

// raise beforeItemAdd arg
var beforeItemAddEvent = $.Event('beforeItemAdd', { item: item, cancel: false });
self.$element.trigger(beforeItemAddEvent);
if (beforeItemAddEvent.cancel)
return;

// register item in internal array and map
self.itemsArray.push(item);

Expand Down Expand Up @@ -155,6 +161,11 @@
}

if (item) {
var beforeItemRemoveEvent = $.Event('beforeItemRemove', { item: item, cancel: false });
self.$element.trigger(beforeItemRemoveEvent);
if (beforeItemRemoveEvent.cancel)
return;

$('.tag', self.$container).filter(function() { return $(this).data('item') === item; }).remove();
$('option', self.$element).filter(function() { return $(this).data('item') === item; }).remove();
if($.inArray(item, self.itemsArray) !== -1)
Expand Down
24 changes: 24 additions & 0 deletions test/bootstrap-tagsinput/events.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
describe("bootstrap-tagsinput", function() {

describe("events", function() {
testTagsInput('<input type="text" />', function() {
it("beforeItemAdd canceled", function() {
this.$element.on('beforeItemAdd', function(event) {
event.cancel = true;
});
this.$element.tagsinput('add', 'some');
expect(this.$element.tagsinput('items').length).toBe(0);
});
});

testTagsInput('<input type="text" value="1" />', function() {
it("beforeItemRemove canceled", function() {
this.$element.on('beforeItemRemove', function(event) {
event.cancel = true;
});
this.$element.tagsinput('remove', '1');
expect(this.$element.tagsinput('items').length).toBe(1);
});
});
});
});
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<script type="text/javascript" src="bootstrap-tagsinput/select_with_string_items.tests.js"></script>
<script type="text/javascript" src="bootstrap-tagsinput/select_with_object_items.tests.js"></script>
<script type="text/javascript" src="bootstrap-tagsinput/reproduced_bugs.tests.js"></script>
<script type="text/javascript" src="bootstrap-tagsinput/events.tests.js"></script>

<script type="text/javascript" src="bootstrap-tagsinput-angular.tests.js"></script>
</head>
Expand Down

0 comments on commit 767f296

Please sign in to comment.