forked from artefactual/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiInput.js
74 lines (66 loc) · 2.59 KB
/
multiInput.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
(function ($)
{
Drupal.behaviors.multiInput = {
attach: function (context)
{
$('ul.multiInput > li', context).click(function (event)
{
if (this === event.target)
{
// On click, remove <li/>
$(this).hide('fast', function ()
{
$(this).remove();
});
}
});
$('input.multiInput', context).each(function ()
{
var index = 0;
var name = this.name.replace('[new]', '');
$(this)
// Remove @name to avoid submitting value in the "new" <input/>
.removeAttr('name')
// Bind blur, click, or keydown events
.bind('blur click keydown', function (event)
{
// Don't fire on keydown other than tab (9) or enter (13)
if ($(this).val()
&& ('keydown' !== event.type
|| 9 === event.keyCode
|| 13 === event.keyCode))
{
// Cancel default action so as not to loose focus
if ('keydown' === event.type)
{
event.preventDefault();
}
var $ul = $(this).prev('ul.multiInput');
if (!$ul.length)
{
// Add <ul/> element, if it doesn't exist already (new
// object)
$ul = $('<ul class="multiInput"/>').insertBefore(this);
}
// Add input value to multiInput
var $li = $('<li><input type="text" name="' + name + '[new' + index++ + ']" value="' + this.value + '"/></li>')
// Bind click event to new list item
.click(function (event)
{
if (event.target === this)
{
// On click, remove <li/>
$(this).hide('fast', function ()
{
$(this).remove();
});
}
})
.appendTo($ul);
// Clear <input/>
this.value = '';
}
});
});
} };
})(jQuery);