Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a30f203
commit e147141
Showing
12 changed files
with
1,288 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
bower_components | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# CHANGELOG | ||
|
||
## v1.0.0 | ||
### Breaking changes | ||
- Plugin file name changed from `addel.jquery.js` to `addel.jquery.js` | ||
- CSS classes moved to their own`classes` object inside the `options` object | ||
- `del` property changed to `delete` | ||
- When adding, `clone()` is not called with `true` anymore and form elements receive the value of `null` | ||
|
||
### Added | ||
- Bower package, install using `bower install addel --save` | ||
|
||
- Custom events: `addel:add`, `addel:added`, `addel:delete` and `addel:deleted` | ||
- Enable defaults override via `$.fn.addel.defaults` | ||
- `hide` option (addel no longer hides by default) | ||
- `animation` options object | ||
- HTML example | ||
- addel now follows [Semantic Versioning](http://semver.org/) | ||
|
||
### Removed | ||
- Hardcoded `alert()` on delete | ||
|
||
### Dependencies | ||
- jQuery updated to v2.2.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
;(function ($) { | ||
|
||
'use strict'; | ||
|
||
var pluginName = 'addel'; | ||
|
||
var formElements = 'input, select, textarea'; | ||
|
||
function Plugin(element, options) { | ||
|
||
// vars | ||
var container = $(element); | ||
|
||
var settings = $.extend(true, {}, $.fn[pluginName].defaults, options); | ||
|
||
var targetClass = '.' + settings.classes.target; | ||
var addButtonClass = '.' + settings.classes.add; | ||
var deleteButtonClass = '.' + settings.classes.delete; | ||
|
||
var animation = {}; | ||
animation.duration = settings.animation.duration; | ||
animation.easing = settings.animation.easing; | ||
|
||
// hide feature | ||
if (settings.hide) { | ||
container.find(targetClass).hide().find(formElements).prop('disabled', true); | ||
} | ||
|
||
// add | ||
container.on('click', addButtonClass, function () { | ||
|
||
var target = container.find(targetClass).last(); | ||
|
||
// addel:add event | ||
var addEvent = $.Event('addel:add', {target: target}); | ||
container.trigger(addEvent); | ||
if (addEvent.isDefaultPrevented()) { | ||
return | ||
} | ||
|
||
// no visible targets | ||
if (target.filter(':visible').length === 0) { | ||
target.fadeIn(animation).find(formElements).prop('disabled', false); | ||
|
||
// visible target/s | ||
} else { | ||
target.clone().insertAfter(target).hide().fadeIn(animation).find(formElements).val(null); | ||
} | ||
|
||
container.find(targetClass).last().find(':input:enabled:visible:first').focus(); | ||
|
||
// addel:added event | ||
container.trigger($.Event('addel:added', {target: target})); | ||
|
||
}); | ||
|
||
// del | ||
container.on('click', deleteButtonClass, function () { | ||
|
||
var target = $(this).closest(targetClass); | ||
var prevTarget = target.prev(targetClass); | ||
var nextTarget = target.next(targetClass); | ||
|
||
// addel:delete event | ||
var deleteEvent = $.Event('addel:delete', {target: target}); | ||
container.trigger(deleteEvent); | ||
if (deleteEvent.isDefaultPrevented()) { | ||
return | ||
} | ||
|
||
// 1 target exists | ||
if (container.find(targetClass).length === 1) { | ||
|
||
target.fadeOut(animation).find(formElements).prop('disabled', true); | ||
container.find(addButtonClass).focus(); | ||
|
||
// >1 targets exist | ||
} else { | ||
|
||
target.fadeOut(animation.duration, animation.easing, function () { | ||
$(this).remove(); | ||
}); | ||
|
||
if (prevTarget.length === 1) { | ||
prevTarget.find(deleteButtonClass).focus(); | ||
} else { | ||
nextTarget.find(deleteButtonClass).focus(); | ||
} | ||
|
||
} | ||
|
||
// addel:deleted event | ||
container.trigger($.Event('addel:deleted', {target: target})); | ||
|
||
}); | ||
|
||
|
||
} | ||
|
||
$.fn[pluginName] = function (options) { | ||
return this.each(function () { | ||
if (!$.data(this, "plugin_" + pluginName)) { | ||
$.data(this, "plugin_" + pluginName, | ||
new Plugin(this, options)); | ||
} | ||
}); | ||
}; | ||
|
||
$.fn[pluginName].defaults = { | ||
hide: false, | ||
classes: { | ||
target: 'addel-target', | ||
add: 'addel-add', | ||
delete: 'addel-delete' | ||
}, | ||
animation: { | ||
duration: 0, | ||
easing: 'swing' | ||
} | ||
}; | ||
|
||
})(jQuery); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.