Skip to content

Commit

Permalink
Updating to the latest jquery.addressfield (v0.1.2).
Browse files Browse the repository at this point in the history
  • Loading branch information
iamEAP committed May 13, 2014
1 parent a4e0b6c commit 5b3c06d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
35 changes: 31 additions & 4 deletions js/jquery.addressfield/jquery.addressfield.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! Address Field - v0.1.1 - 2014-05-04
/*! Address Field - v0.1.2 - 2014-05-12
* https://github.com/tableau-mkt/jquery.addressfield
* Copyright (c) 2014 Eric Peterson; Licensed GPL-2.0 */
(function ($) {
Expand Down Expand Up @@ -90,11 +90,19 @@
* Updates a given select field's options with given options.
*/
$.fn.addressfield.updateOptions = function (options) {
var $self = $(this);
var $self = $(this),
oldVal = $self.data('_saved') || $self.val();

$self.children('option').remove();
$.each(options, function (value, label) {
$self.append($('<option></option>').attr('value', value).text(label));
});

// Ensure the old value is still reflected after options are updated.
$self.val(oldVal).change();

// Clean up the data attribute; no-op if it was not previously populated.
$self.removeData('_saved');
};

/**
Expand All @@ -103,11 +111,17 @@
$.fn.addressfield.convertToText = function () {
var $self = $(this),
$input = $('<input />').attr('type', 'text');

$.each($self[0].attributes, function () {
if ($.inArray(this.name, ['class', 'id', 'name']) !== -1) {
$input.attr(this.name, this.value);
}
});

// Ensure the old value is still reflected after conversion.
$input.val($self.val());

// Replace the existing element with our new one; also return it.
$self.replaceWith($input);
return $input;
};
Expand All @@ -118,11 +132,17 @@
$.fn.addressfield.convertToSelect = function() {
var $self = $(this),
$select = $('<select></select>');

$.each($self[0].attributes, function () {
if ($.inArray(this.name, ['class', 'id', 'name']) !== -1) {
$select.attr(this.name, this.value);
}
});

// Save the old input value to a data attribute, for use in updateOptions.
$select.data('_saved', $self.val());

// Replace the existing element with our new one; also return it.
$self.replaceWith($select);
return $select;
};
Expand Down Expand Up @@ -157,7 +177,11 @@
if (i in order) {
// Save off the element container over its class selector in order.
$element = $(this).find('.' + order[i]).parent('div, section');
order[i] = $element;
order[i] = {
'element': $element.clone(),
'class': order[i],
'value': $(this).find('.' + order[i]).val()
};

// Remove the original element from the page.
$element.remove();
Expand All @@ -168,7 +192,10 @@
// the correct order.
for (i = 0; i < length; ++i) {
if (i in order) {
$(this).append(order[i]);
$element = $(this).append(order[i].element);

// The clone process doesn't seem to copy input values; apply that here.
$element.find('.' + order[i].class).val(order[i].value).change();
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.addressfield/jquery.addressfield.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scale_addressfield.module
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function scale_addressfield_library() {
$path = drupal_get_path('module', 'scale_addressfield');
$items['jquery.addressfield'] = array(
'title' => 'jquery.addressfield',
'version' => '0.1.1',
'version' => '0.1.2',
'js' => array(
$path . '/js/jquery.addressfield/jquery.addressfield.min.js' => array(
'group' => JS_LIBRARY,
Expand Down

0 comments on commit 5b3c06d

Please sign in to comment.