Created
February 21, 2014 09:25
-
-
Save adieuadieu/9131259 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
addressbook_autocomplete: function() { | |
if ($('input[data-bind="addressbook-autocomplete"]').length > 0) { | |
$('input[data-bind="addressbook-autocomplete"]').autocomplete('destroy'); | |
$('input[data-bind="addressbook-autocomplete"]').show(); | |
function aao_handle_select(that, label, address) { | |
var div = document.createElement('div'); | |
$('div[data-handle-for="' + $(that).attr('name') + '"]').remove(); | |
$(div).css('width', $(that).width() + 'px'); | |
$(div).attr('data-handle-for', $(that).attr('name')); | |
$(div).html('<div><a href="#"><img src="/images/cancel.png" alt="Remove" title="Remove"></a> <span><a href="#addressbook/edit/' + address.id + '" title="' + label + '">' + label + '</a></span></div>'); | |
$('a:first', div).click(function() { | |
$(div).remove(); | |
$(that).val(''); | |
$(that).change(); | |
$(that).show(); | |
if ($(that).attr('data-populate-field')) { | |
var populate_fields = $(that).attr('data-populate-field').split(','); | |
for (i in populate_fields) { | |
$('[name="' + populate_fields[i] + '"]').val('').change(); | |
} | |
} | |
return false; | |
}); | |
$(that).after(div); | |
$(that).hide(); | |
} | |
$('input[data-bind="addressbook-autocomplete"]').autocomplete({ | |
source: function( request, response ) { | |
var that = this; | |
var _firstrun = false; | |
var query = { | |
'limit': 25, | |
'expiration': {age: 300, onDataChange: 'addressbook'}, | |
'conditions': {} | |
}; | |
if ($(this.element).attr('data-autocomplete-column')) { | |
query.conditions[$(this.element).attr('data-autocomplete-column')] = '%' + request.term + '%'; | |
query.conditions.is_company = 1; | |
} | |
else query.conditions.name = '%' + request.term + '%'; | |
if ($(this.element).attr('data-autocomplete-limit-to-staff')) { | |
query.conditions.is_staff = 1; | |
} | |
if ($(this.element).attr('data-autocomplete-limit-to-individuals')) { | |
query.conditions.is_company = 0; | |
query.conditions.is_staff = 0; | |
} | |
if ($(this.element).attr('data-autocomplete-limit-to-companies')) { | |
query.conditions.is_company = 1; | |
} | |
if (!isNaN(request.term)) { | |
delete query.conditions.name; | |
query.conditions.id = parseInt(request.term); | |
} | |
SR.net.load(query, 'search_addressbook', function() { | |
response( $.map( this.data.results, function( item ) { | |
return { | |
label: 1 == item.is_company ? item.company_name : item.last_name + ', ' + item.first_name + (1 == item.is_staff ? '' : ' (' + item.company_name + ')'), | |
value: item.id, | |
address: item | |
} | |
})); | |
}); | |
}, | |
create: function(event, ui) { | |
var that = this; | |
var v = $(this).val(); | |
var query = { | |
'limit': 25, | |
'expiration': {age: 300, onDataChange: 'addressbook'}, | |
'conditions': { | |
'id': parseInt(v) | |
} | |
}; | |
if ('' != v && !isNaN(v)) { | |
SR.net.load(query, 'search_addressbook', function() { | |
var address = this.data.results[0]; | |
if (address) aao_handle_select(that, 1 == address.is_company ? address.company_name : address.last_name + ', ' + address.first_name + ( 1 == address.is_staff ? '' : ' (' + address.company_name + ')'), address); | |
}); | |
} | |
}, | |
select: function(event, ui) { | |
var that = this; | |
if ($(this).attr('data-populate-field') && $(this).attr('data-populate-with')) { | |
var address = ui.item.address; | |
var populate_fields = $(this).attr('data-populate-field').split(','); | |
var populate_with = $(this).attr('data-populate-with').split(','); | |
for (i in populate_fields) { | |
var value = ''; | |
if ('name' == populate_with[i]) { | |
if (1 == address.is_company) value = address.company_name; | |
else value = address.last_name + ' ' + address.first_name; | |
} | |
else if ('address' == populate_with[i]) { | |
value = address.postcode + ' ' + address.address_string; | |
} | |
else var value = address[populate_with[i]]; | |
if (value && value.length > 0) $('[name="' + populate_fields[i] + '"]').val(value).change(); | |
} | |
} | |
aao_handle_select(that, ui.item.label, ui.item.address); | |
}, | |
change: function(event, ui) { | |
var v = $(this).val(); | |
if (!isNaN(v) || 0 == v.length) $(this).change(); | |
else { | |
$(this).val(''); | |
return false; | |
} | |
} | |
}); | |
} | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment