|
/*jslint browser: true, regexp: true */ |
|
/*globals jQuery */ |
|
|
|
(function ($) { |
|
'use strict'; |
|
|
|
$.fn.tagger = function (params) { |
|
var defaults = { |
|
forbid: /[^a-zA-Z0-9\-]/g, |
|
text: { |
|
placeholder: 'Write tags then press return…', |
|
remove: 'Click to remove' |
|
} |
|
}, nextTagId = 0; |
|
|
|
params = $.extend(defaults, params); |
|
|
|
return this.each(function () { |
|
var jq, taggerList, taggerAdd; |
|
|
|
jq = $(this); |
|
jq.addClass('tagger'); |
|
|
|
taggerList = $('<div />', { |
|
'class': 'tagger-list' |
|
}).insertAfter(jq); |
|
|
|
taggerAdd = $('<textarea />', { |
|
'class': 'tagger-add' |
|
}).appendTo(taggerList); |
|
|
|
// updates text input with tags divs |
|
function updateInput() { |
|
var tagList = [], lastTag = taggerList.find('.tag').last(), |
|
padding; |
|
|
|
// list all tags in text/object form |
|
$('.tag').each(function () { |
|
tagList.push(($(this)).text()); |
|
}); |
|
|
|
// adds a padding to the textbox depending on last tag position |
|
if (lastTag.length > 0) { |
|
padding = lastTag.position().left + |
|
lastTag.outerWidth(true); |
|
} else { |
|
padding = parseInt(taggerList.css('padding-left'), 10); |
|
} |
|
|
|
taggerAdd.css('padding-left', padding); |
|
|
|
// display placeholder |
|
if (tagList.length === 0) { |
|
taggerAdd.attr('placeholder', params.text.placeholder); |
|
} else { |
|
taggerAdd.attr('placeholder', ''); |
|
} |
|
|
|
// update text input with real values |
|
jq.val(tagList.join(',')); |
|
} |
|
|
|
// adds a new tag |
|
function addTag(tag) { |
|
tag = tag.replace(params.forbid, ''); |
|
|
|
if (!tag) { |
|
return; |
|
} |
|
|
|
var width, el; |
|
|
|
el = $('<div />', { |
|
'class': 'tag', |
|
'data-tagid': nextTagId, |
|
'title': params.text.remove |
|
}); |
|
|
|
el. |
|
text(tag). |
|
appendTo(taggerList); |
|
|
|
width = el.width(); |
|
updateInput(); |
|
|
|
el.addClass('animate').width(0); |
|
el.width(width); |
|
|
|
setTimeout(function () { |
|
el.removeClass('animate'); |
|
}, 500); |
|
|
|
nextTagId += 1; |
|
} |
|
|
|
// deletes a tag |
|
function deleteTag(id) { |
|
taggerList.find('[data-tagid="' + id + '"]').remove(); |
|
updateInput(); |
|
} |
|
|
|
// delete a tag by clicking it |
|
taggerList.on('click', '.tag', function () { |
|
deleteTag($(this).attr('data-tagid')); |
|
}); |
|
|
|
// add/delete tag on return/back |
|
taggerAdd.on('keyup', function (e) { |
|
var tags, length, i, lastTagId; |
|
|
|
if (e.which === 13 || e.keyCode === 13) { |
|
tags = taggerAdd.val().split(','); |
|
length = tags.length; |
|
|
|
for (i = 0; i < length; i += 1) { |
|
addTag($.trim(tags[i])); |
|
} |
|
|
|
taggerAdd.val(''); |
|
|
|
// prevents from sending form |
|
e.preventDefault(); |
|
} |
|
|
|
if ((e.which === 8 || e.keyCode === 8) |
|
&& taggerAdd.val() === '') { |
|
lastTagId = taggerList.find('.tag').last().attr('data-tagid'); |
|
|
|
deleteTag(parseInt(lastTagId, 10)); |
|
|
|
// prevents from going back |
|
e.preventDefault(); |
|
} |
|
}); |
|
|
|
// inital update |
|
updateInput(); |
|
}); |
|
}; |
|
}(jQuery)); |