/*
* Userpicker
* Version 1.0.0
* Written by: Andreas Strobel
*
* @property String $inputId is the ID of the input HTML Element
* @property Int $maxUsers the maximum of users in this dropdown
* @property String $userSearchUrl the url of the search, to find the users
* @property String $currentValue is the current value of the parent field.
*
*/
var userCount = 0;
$.fn.userpicker = function (options) {
// set standard options
options = $.extend({
inputId: "",
maxUsers: 0,
searchUrl: "",
currentValue: "",
renderType: "normal", // possible values are "normal", "partial"
focus: false,
userGuid: "",
data: {},
placeholderText: 'Add a user'
}, options);
var chosen = "";
var uniqueID = "";
init();
function init() {
uniqueID = options.inputId.substr(1);
var _template = '
';
// remove picker if existing
$('.'+uniqueID+'_user_picker_container').remove();
if ($('.' + uniqueID + '_user_picker_container').length == 0) {
// insert the new input structure after the original input element
$(options.inputId).after(_template);
}
// hide original input element
$(options.inputId).hide();
if (options.currentValue != "") {
// restore data from database
restoreUserTags(options.currentValue);
}
// add placeholder text to input field
$('#' + uniqueID + '_tag_input_field').attr('placeholder', options.placeholderText);
if (options.focus == true) {
// set focus to input
$('#' + uniqueID + '_tag_input_field').focus();
$('#' + uniqueID + '_invite_tags').addClass('focus');
}
// simulate focus in
$('#' + uniqueID + '_tag_input_field').focusin(function () {
$('#' + uniqueID + '_invite_tags').addClass('focus');
})
// simulate focus out
$('#' + uniqueID + '_tag_input_field').focusout(function () {
$('#' + uniqueID + '_invite_tags').removeClass('focus');
})
}
function restoreUserTags(html) {
// add html structure for input element
$('#' + uniqueID + '_invite_tags .userInput').remove();
$('#' + uniqueID + '_invite_tags').prepend(html);
// create function for every user tag to remove the element
$('#' + uniqueID + '_invite_tags .userInput i').each(function () {
$(this).click(function () {
// remove user tag
$(this).parent().remove();
// reduce the count of added user
userCount--;
})
// raise the count of added user
userCount++;
})
}
// Set focus on the input field, by clicking the