Skip to content

Commit

Permalink
ready callback to allow manual re-indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Detering authored and Brian Detering committed Sep 19, 2015
1 parent e6517ea commit 26a44e9
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 48 deletions.
9 changes: 8 additions & 1 deletion README.md
Expand Up @@ -78,10 +78,17 @@ Names get reindexed if an item is added or deleted.
}
},
// (Optional)
// You can use this if you need to manually re-index the list
// for example if you are using a drag and drop library to reorder
// list items.
ready: function (setIndexes) {
$dragAndDrop.on('drop', setIndexes);
},
// (Optional)
// Removes the delete button from the first list item,
// defaults to false.
isFirstItemUndeletable: true
})
});
</script>
```
```
2 changes: 1 addition & 1 deletion bower.json
@@ -1,7 +1,7 @@
{
"name": "jquery.repeater",
"main": "jquery.repeater.js",
"version": "1.0.2",
"version": "1.1.0",
"homepage": "https://github.com/DubFriend/jquery.repeater",
"description": "repeatable form input interface",
"keywords": [
Expand Down
56 changes: 40 additions & 16 deletions index.html
Expand Up @@ -18,41 +18,41 @@ <h1>Repeater</h1>
<div data-repeater-list="group-a">
<div data-repeater-item>
<input name="untyped-input" value="A"/>

<input type="text" name="text-input" value="A"/>

<input type="date" name="tel-input"/>

<input type="date" name="date-input"/>
<textarea name="textarea-input">A</textarea>

<input type="radio" name="radio-input" value="A" checked/>
<input type="radio" name="radio-input" value="B"/>

<input type="checkbox" name="checkbox-input" value="A" checked/>
<input type="checkbox" name="checkbox-input" value="B"/>

<select name="select-input">
<option value="A" selected>A</option>
<option value="B">B</option>
</select>

<input data-repeater-delete type="button" value="Delete"/>
</div>
<div data-repeater-item>
<input name="untyped-input" value="A"/>

<input type="text" name="text-input" value="B"/>

<input type="date" name="tel-input"/>

<input type="date" name="date-input"/>
<textarea name="textarea-input">B</textarea>

<input type="radio" name="radio-input" value="A" />
<input type="radio" name="radio-input" value="B" checked/>

<input type="checkbox" name="checkbox-input" value="A"/>
<input type="checkbox" name="checkbox-input" value="B" checked/>

<select name="select-input">
<option value="A">A</option>
<option value="B" selected>B</option>
Expand All @@ -62,13 +62,34 @@ <h1>Repeater</h1>
</div>
<input data-repeater-create type="button" value="Add"/>
</form>


<script src="jquery-1.11.1.js"></script>
<script src="jquery.repeater.js"></script>
<script>
$(document).ready(function () {
'use strict';

// $('.repeater').repeater(function () {
// return {
// defaultValues: {
// 'textarea-input': 'foo',
// 'text-input': 'bar',
// 'select-input': 'B',
// 'checkbox-input': ['A', 'B'],
// 'radio-input': 'B'
// },
// show: function () {
// $(this).slideDown();
// },
// hide: function (deleteElement) {
// if(confirm('Are you sure you want to delete this element?')) {
// $(this).slideUp(deleteElement);
// }
// }
// };
// });

$('.repeater').repeater({
defaultValues: {
'textarea-input': 'foo',
Expand All @@ -84,6 +105,9 @@ <h1>Repeater</h1>
if(confirm('Are you sure you want to delete this element?')) {
$(this).slideUp(deleteElement);
}
},
ready: function (setIndexes) {

}
});
});
Expand Down
24 changes: 24 additions & 0 deletions index.pre.html
Expand Up @@ -21,6 +21,27 @@ <h1>Repeater</h1>
<script>
$(document).ready(function () {
'use strict';

// $('.repeater').repeater(function () {
// return {
// defaultValues: {
// 'textarea-input': 'foo',
// 'text-input': 'bar',
// 'select-input': 'B',
// 'checkbox-input': ['A', 'B'],
// 'radio-input': 'B'
// },
// show: function () {
// $(this).slideDown();
// },
// hide: function (deleteElement) {
// if(confirm('Are you sure you want to delete this element?')) {
// $(this).slideUp(deleteElement);
// }
// }
// };
// });

$('.repeater').repeater({
defaultValues: {
'textarea-input': 'foo',
Expand All @@ -36,6 +57,9 @@ <h1>Repeater</h1>
if(confirm('Are you sure you want to delete this element?')) {
$(this).slideUp(deleteElement);
}
},
ready: function (setIndexes) {

}
});
});
Expand Down
19 changes: 6 additions & 13 deletions jquery.repeater.js
@@ -1,6 +1,6 @@
// jquery.repeater version 1.0.2
// jquery.repeater version 1.1.0
// https://github.com/DubFriend/jquery.repeater
// (MIT) 09-08-2015
// (MIT) 19-09-2015
// Brian Detering <BDeterin@gmail.com> (http://www.briandetering.net/)
(function ($) {
'use strict';
Expand Down Expand Up @@ -698,19 +698,8 @@ $.fn.inputClear = function () {
}(jQuery));

$.fn.repeater = function(fig) {

fig = fig || {};



var inputVal = function ($self, value) {

};

var inputClear = function ($self) {

};

$(this).each(function () {

var $self = $(this);
Expand Down Expand Up @@ -763,6 +752,10 @@ $.fn.repeater = function(fig) {

setIndexes();

if(fig.ready) {
fig.ready(setIndexes);
}

var setItemsValues = function ($item, values) {
var index;
index = $item.find('[name]').first()
Expand Down
6 changes: 3 additions & 3 deletions jquery.repeater.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "jquery.repeater",
"version": "1.0.2",
"version": "1.1.0",
"description": "repeatable form input interface",
"main": "jquery.repeater.js",
"directories": {
Expand Down
4 changes: 2 additions & 2 deletions repeater.jquery.json
@@ -1,6 +1,6 @@
{
"name": "repeater",
"version": "1.0.1",
"version": "1.1.0",
"title": "Repeater",
"author": {
"name": "Brian Detering",
Expand All @@ -27,4 +27,4 @@
"dependencies": {
"jquery": ">=1.11"
}
}
}
15 changes: 4 additions & 11 deletions src/repeater.js
@@ -1,17 +1,6 @@
$.fn.repeater = function(fig) {

fig = fig || {};



var inputVal = function ($self, value) {

};

var inputClear = function ($self) {

};

$(this).each(function () {

var $self = $(this);
Expand Down Expand Up @@ -64,6 +53,10 @@ $.fn.repeater = function(fig) {

setIndexes();

if(fig.ready) {
fig.ready(setIndexes);
}

var setItemsValues = function ($item, values) {
var index;
index = $item.find('[name]').first()
Expand Down
25 changes: 25 additions & 0 deletions test/test.js
Expand Up @@ -210,3 +210,28 @@ QUnit.test('isFirstItemUndeletable configuration option', function (assert) {

assert.strictEqual($firstDeleteButton.length, 0, 'first delete button is removed');
});

QUnit.asyncTest('has ready callback option and setIndexes', function (assert) {
expect(3);
var $list = this.$secondRepeater.find('[data-repeater-list]');
this.$secondRepeater.repeater({
ready: function (setIndexes) {
assert.ok(isFunction(setIndexes), 'passed setIndexes function');
var $lastItem = $list.find('[data-repeater-item]').last();
$list.prepend($lastItem.clone());
$lastItem.remove();
setIndexes();

var indeces = $list.find('[name]').map(function () {
return $(this).attr('name').match(/\[([0-9])+\]/)[1];
}).get();

console.log(indeces[0], indeces[10]);

assert.strictEqual(indeces[0], '0');
assert.strictEqual(indeces[10], '1');

QUnit.start();
}
});
});

0 comments on commit 26a44e9

Please sign in to comment.