Skip to content
This repository has been archived by the owner on Mar 3, 2022. It is now read-only.

Commit

Permalink
ON-41: Added new field for units of measure to choropleth map resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Weingarten committed Apr 10, 2014
1 parent efe6daa commit 0c10dfb
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
17 changes: 17 additions & 0 deletions choropleth.install
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ function choropleth_udpate_existing_fields() {
// schema.
db_add_field($table_name, $column_name, $schema['choropleth_data_column'] + array('fields' => array()));
}

// Adding choropleth_unit_of_measure.
$column_name = _field_sql_storage_columnname($field_name, 'choropleth_unit_of_measure');
if (!(db_field_exists($table_name, $column_name))) {
// _drupal_schema_initialize() expects a fields array from the
// schema.
db_add_field($table_name, $column_name, $schema['choropleth_unit_of_measure'] + array('fields' => array()));
}
// Adding choropleth_breakpoints.
$column_name = _field_sql_storage_columnname($field_name, 'choropleth_breakpoints');
if (!(db_field_exists($table_name, $column_name))) {
Expand Down Expand Up @@ -73,3 +81,12 @@ function choropleth_update_7002(&$sandbox) {
choropleth_udpate_existing_fields();
return $ret;
}

/**
* Add choropleth units of measure to existing recline field storage.
*/
function choropleth_update_7003(&$sandbox) {
$ret = array();
choropleth_udpate_existing_fields();
return $ret;
}
2 changes: 2 additions & 0 deletions choropleth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Drupal.behaviors.choropleth = {
attach: function(context) {
var choroDataColumn = [Drupal.settings.choropleth.choropleth_data_column] || [];
var choroUnitsOfMeasureColumn = [Drupal.settings.choropleth.choropleth_unit_of_measure][0] || [];
var choropleth = Drupal.settings.choropleth.choropleth || '';
var colorScale = Drupal.settings.choropleth.choropleth_color_scale || ['#FFEDA0', '#FEB24C', '#E31A1C', '#800026'];
var breakpoints = Drupal.settings.choropleth.choropleth_breakpoints || [];
Expand Down Expand Up @@ -131,6 +132,7 @@
label_to_map: map_label,
location_default: location_default,
selectable_fields: choroDataColumn,
unit_of_measure: choroUnitsOfMeasureColumn,
breakpoints: breakpoints,
base_color: colorScale,
});
Expand Down
88 changes: 88 additions & 0 deletions choropleth.module
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ function choropleth_custom_db_field_schema() {
'length' => '255',
'description' => 'Column to determine the choropleth map data.',
),
'choropleth_unit_of_measure' => array(
'type' => 'varchar',
'length' => '255',
'description' => 'Column to determine the choropleth map data.',
),
'choropleth_breakpoints' => array(
'type' => 'varchar',
'length' => '255',
Expand Down Expand Up @@ -92,6 +97,7 @@ function choropleth_default_formatter_output($variables) {
'choropleth' => $choropleth,
'choropleth_color_scale' => $choropleth_colors[$choropleth_colors_index],
'choropleth_data_column' => $variables['item']['choropleth_data_column'],
'choropleth_unit_of_measure' => $variables['item']['choropleth_unit_of_measure'],
'choropleth_breakpoints' => $variables['item']['choropleth_breakpoints'],
);
drupal_add_js($settings, 'setting');
Expand Down Expand Up @@ -150,6 +156,10 @@ function choropleth_resource_form_submit($form, $form_state) {
if ($value['view']['choropleth'] && !(in_array('state', $headers[0]))) {
form_set_error($field . "][und][0][view][choropleth", t("State by State Map view requires a State field."));
}

if ($value['view']['choropleth'] && !($value['choropleth_unit_of_measure'])) {
form_set_error($field . "][und][0]['choropleth_unit_of_measure'][choropleth", t("State by State Map view requires a a units of measure"));
}
}
}
}
Expand Down Expand Up @@ -191,6 +201,28 @@ function choropleth_field_widget_form_alter(&$element, &$form_state, $context) {
'#weight' => 1,
'#default_value' => $choropleth_data_column,
);

$choropleth_unit_of_measure = isset($element[$delta]['#default_value']['choropleth_unit_of_measure']) ? $element[$delta]['#default_value']['choropleth_unit_of_measure'] : FALSE;
$element[$delta]['choropleth_unit_of_measure'] = array(
'#title' => 'Unit Of Measure',
'#description' => t('Specify the units for the data being displayed to the user.'),
'#type' => 'textfield',
'#weight' => 4,
'#default_value' => $choropleth_unit_of_measure,
'#states' => array(
'visible' => array(
':input[name="field_upload[und][0][view][choropleth]"]' => array('checked' => TRUE),
),
'required' => array(
':input[name="field_upload[und][0][view][choropleth]"]' => array('checked' => TRUE),
),
),
'#weight' => 1,
);




$choropleth_breakpoints = isset($element[$delta]['#default_value']['choropleth_breakpoints']) ? $element[$delta]['#default_value']['choropleth_breakpoints'] : FALSE;
$element[$delta]['choropleth_breakpoints'] = array(
'#title' => 'Color Breakpoints',
Expand Down Expand Up @@ -264,3 +296,59 @@ function choropleth_field_widget_recline_widget_form_alter(&$element, &$form_sta
$collapsible_item['element']['#children'] = '';
$element[0]['#description'] .= theme('fieldset', $collapsible_item);
}


function hook_form_validate($form, &$form_state) {
// check for required field specified in the states array
foreach($form as $key => $field) {
if(is_array($field) && isset($field['#states']['required'])) {
$required = false;
$lang = $field['#language'];
foreach($field['#states']['required'] as $cond_field_sel => $cond_vals) {

// look for name= in the jquery selector - if that isn't there then give up (for now)
preg_match('/name="(.*)"/', $cond_field_sel, $matches);
if(isset(

$matches[1])) {

// remove language from field name
$cond_field_name = str_replace('[und]', '', $matches[1]);


// get value identifier (e.g. value, tid, target_id)
$value_ident = key($cond_vals);


// loop over the values of the conditional field
foreach($form_state['values'][$cond_field_name][$lang] as $cond_field_val) {


// check for a match
if($cond_vals[$value_ident] == $cond_field_val[$value_ident]) {
// now we know this field is required
$required = true;
break 2;
}
}
}
}
if(

$required) {
$field_name = $field[$lang]['#field_name'];
$filled_in = false;
foreach($form_state['values'][$field_name][$lang] as $item) {
if(array_pop($item)) {
$filled_in = true;
}
}
if(!$filled_in) {
form_set_error($field_name, t(':field is a required field', array(':field' => $field[$lang]['#title'])));
}
}
}


}
}
17 changes: 17 additions & 0 deletions static/recline.view.ChoroplethMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ this.recline.View = this.recline.View || {};

// Breakpoints and color scale.
this.breakpoints = options.breakpoints ? options.breakpoints : [];
this.unit_of_measure = options.unit_of_measure ? options.unit_of_measure : [];


this.breakpoints = this.breakpoints.length > 0 ? this._validateBreakpoints(this.breakpoints) : [];
this.dynamic_breakpoints = this.breakpoints.length > 0 ? false : true;

Expand Down Expand Up @@ -377,6 +380,8 @@ this.recline.View = this.recline.View || {};
});
}

this.menu.updateUnitOfMeasure(self.unit_of_measure);

// Updating color references.
this.menu.updateColorScale(self.breakpoints, self.base_color);

Expand Down Expand Up @@ -708,6 +713,7 @@ this.recline.View = this.recline.View || {};
</div> \
<input type="hidden" class="editor-id" value="chroropleth-map-1" /> \
</form> \
<label id="unit-of-measure"></label> \
<div id="color-scale" class="reference"></div> \
',
// Radio input template for active column.
Expand Down Expand Up @@ -777,6 +783,7 @@ this.recline.View = this.recline.View || {};
filter_by_year: this._renderSelect('year', 'Years'),
// Render the category select input.
filter_by_category: this._renderSelect('category', 'Categories'),
unit_of_measure: this.state.get('unitOfMeasure'),
}
);
// Attach html.
Expand Down Expand Up @@ -927,6 +934,15 @@ this.recline.View = this.recline.View || {};
});
}
},

/**
* Updates the units of measure label on the menu
* @param unitOfMeasure
*/
updateUnitOfMeasure: function(unitOfMeasure){
this.$el.find('#unit-of-measure').html(unitOfMeasure);
},

/**
* Updates color scale legend on menu
* @param {array} breakpoints
Expand All @@ -945,6 +961,7 @@ this.recline.View = this.recline.View || {};
temp += (i === 0 ? '0' : breakpoints[i - 1]) + '&ndash;' + breakpoints[i] + '<br />';
html = temp + html;
});

temp = '<i style="background:' + color_scale(1).hex() + '"></i>';
temp += breakpoints[breakpoints.length - 1] + '+<br />';
this.$el.find('#color-scale').html(temp + html);
Expand Down

0 comments on commit 0c10dfb

Please sign in to comment.