Commit ab1290df authored by targoo's avatar targoo

add support for opacity for textfield and simple widget

parent ab1fdead
......@@ -8,7 +8,7 @@
/**
* The color plugin URI.
*/
define('COLOR_FIELD_SIMPLE_COLOR_PICKER_DOWNLOAD_URI', 'http://recursive-design.com/projects/jquery-simple-color/jquery-simple-color.1.0.1.zip');
define('COLOR_FIELD_SIMPLE_COLOR_PICKER_DOWNLOAD_URI', 'https://github.com/recurser/jquery-simple-color/archive/master.zip');
define('COLOR_FIELD_EYECON_COLOR_PICKER_DOWNLOAD_URI', 'http://www.eyecon.ro/colorpicker/colorpicker.zip');
define('COLOR_FIELD_DEMATTE_COLOR_PICKER_DOWNLOAD_URI', 'http://www.dematte.at/colorPicker/cP_v0.91.zip');
......@@ -62,8 +62,9 @@ function color_field_drush_help($section) {
/**
* Commands to download the Simple Color Picker plugin.
*/
function drush_color_field_color_fied_plugin_scp($path = 'sites/all/libraries') {
$filename = 'jquery-simple-color.1.0.1.zip';
function drush_color_field_plugin_scp($path = 'sites/all/libraries') {
$filename = 'master.zip';
$unzipfilename = 'jquery-simple-color-master';
$dirname = 'jquery-simple-color';
if (!drush_shell_exec('type unzip')) {
......@@ -96,13 +97,17 @@ function drush_color_field_color_fied_plugin_scp($path = 'sites/all/libraries')
drush_shell_exec('curl -kOL ' . COLOR_FIELD_SIMPLE_COLOR_PICKER_DOWNLOAD_URI);
}
// If file has been downloaded.
if (is_file($filename)) {
// Remove old library.
if (is_dir($dirname)) {
drush_shell_exec('rm -rf ' . $dirname);
}
// Decompress the zip archive.
drush_shell_exec('unzip -qq -o ' . $filename);
drush_shell_exec('mv ' . $unzipfilename . ' ' . $dirname);
// Remove the zip archive.
drush_op('unlink', $filename);
}else {
drush_log(dt('Simple Color Picker plugin could not been downloaded from @url', array('@url' => COLOR_FIELD_SIMPLE_COLOR_PICKER_DOWNLOAD_URI)), 'error');
}
// Set working directory back to the previous working directory.
......@@ -119,10 +124,9 @@ function drush_color_field_color_fied_plugin_scp($path = 'sites/all/libraries')
/**
* Commands to download the EyeCon Color Picker plugin.
*/
function drush_color_field_color_fied_plugin_ecp($path = 'sites/all/libraries') {
function drush_color_field_plugin_ecp($path = 'sites/all/libraries') {
$filename = 'colorpicker.zip';
$unzipfilename = 'cP_v0.91';
$dirname = 'eyecon_colorpicker';
$dirname = 'eyecon-color-picker';
if (!drush_shell_exec('type unzip')) {
return drush_set_error(dt('Missing dependency: unzip. Install it before using this command.'));
......@@ -154,12 +158,16 @@ function drush_color_field_color_fied_plugin_ecp($path = 'sites/all/libraries')
}
if (is_file($filename)) {
drush_shell_exec('mkdir ' . $dirname);
// Decompress the zip archive.
drush_shell_exec('unzip -qq -o ' . $filename);
// Remove old librairy.
// Remove the zip archive.
//drush_op('unlink', $filename);
drush_op('unlink', $filename);
drush_shell_exec('mv css ' . $dirname);
drush_shell_exec('mv images ' . $dirname);
drush_shell_exec('mv js ' . $dirname);
drush_shell_exec('mv index.html ' . $dirname);
}
// Set working directory back to the previous working directory.
......@@ -176,7 +184,7 @@ function drush_color_field_color_fied_plugin_ecp($path = 'sites/all/libraries')
/**
* Commands to download the Dematte Color Picker plugin.
*/
function drush_color_field_color_fied_plugin_dcp($path = 'sites/all/libraries') {
function drush_color_field_plugin_dcp($path = 'sites/all/libraries') {
$filename = 'cP_v0.91.zip';
$unzipfilename = 'cP_v0.91';
$dirname = 'dematte_colorpicker';
......
This diff is collapsed.
name = Color Field
description = Color Field using a hexadecimal notation (HEX)
description = Color Field storing a hexadecimal notation (HEX) and opacity
package = Fields
core = 7.x
files[] = color_field.test
dependencies[] = libraries
This diff is collapsed.
<?php
/**
* @file
* Theme functions.
*/
/**
* Formats a default color field widget.
*/
function theme_color_field_simple_widget($vars) {
$element = $vars['element'];
// Add library.
drupal_add_library('color_field', 'jquery-simple-color');
// Javascript settings.
$settings = array();
$settings['id'] = $element['rgb']['#id'];
$settings['cell_width'] = (isset($field['settings']['cell_width']) && $field['settings']['cell_width'] != '') ? (int) $field['settings']['cell_width'] : 10;
$settings['cell_height'] = (isset($field['settings']['cell_height']) && $field['settings']['cell_height'] != '') ? (int) $field['settings']['cell_height'] : 10;
$settings['cell_margin'] = (isset($field['settings']['cell_margin']) && $field['settings']['cell_margin'] != '') ? (int) $field['settings']['cell_margin'] : 1;
$settings['box_width'] = (isset($field['settings']['box_width']) && $field['settings']['box_width'] != '') ? $field['settings']['box_width'] . 'px' : '115px';
$settings['box_height'] = (isset($field['settings']['box_height']) && $field['settings']['box_height'] != '') ? $field['settings']['box_height'] . 'px' : '20px';
$settings['columns'] = (isset($field['settings']['columns']) && $field['settings']['columns'] != '') ? $field['settings']['columns'] : '16';
// Attach javascript and style.
$element['rgb']['#attached'] = array(
'js' => array(
drupal_get_path('module', 'color_field') . '/color_field_jquery_simple_color/color_field_jquery_simple_color.jquery.js',
array(
'data' => array('color_field_jquery_simple_color' => array('#' . $element['rgb']['#id'] => $settings)),
'type' => 'setting',
),
),
'css' => array(drupal_get_path('module', 'color_field') . '/color_field_jquery_simple_color/color_field_jquery_simple_color.css'),
);
$output = '';
$output .= '<div class="link-field-subrow clearfix">';
$output .= '<div class="link-field-title color-field-column">' . drupal_render($element['rgb']) . '</div>';
if (isset($element['opacity'])) {
$output .= '<div class="link-field-title color-field-column">' . drupal_render($element['opacity']) . '</div>';
}
$output .= '</div>';
$output .= drupal_render_children($element);
return $output;
}
/**
* Formats a default color field widget.
*/
function theme_color_field_default_widget($vars) {
$element = $vars['element'];
// Javascript settings.
$settings = array();
$settings['id'] = $element['rgb']['#id'];
$settings['divid'] = 'div-' . $element['rgb']['#id'];
$settings['value'] = $element['#value']['rgb'];
$default_colors = (isset($element['#settings']['default_colors'])) ? $element['#settings']['default_colors'] : '';
preg_match_all("/#[0-9a-fA-F]{6}/", $default_colors, $default_colors, PREG_SET_ORDER);
foreach ($default_colors as $color) {
$settings['colors'][] = $color[0];
}
// Attach javascript and style.
$element['rgb']['#attached'] = array(
'js' => array(
drupal_get_path('module', 'color_field') . '/color_field_default_widget/color_field.js',
drupal_get_path('module', 'color_field') . '/color_field_default_widget/color_field.jquery.js',
array(
'data' => array('color_field' => array('#' . $settings['id'] => $settings)),
'type' => 'setting',
),
),
'css' => array(drupal_get_path('module', 'color_field') . '/color_field_default_widget/color_field.css'),
);
$element['rgb']['#suffix'] = '<label> ' . $element['rgb']['#title'] . ' </label><div id=div-' . $settings['id'] . '></div><div class="description">' . $element['rgb']['#description'] . '</div>';
unset($element['rgb']['#title']);
unset($element['rgb']['#description']);
// Hide the input field.
$element['rgb']['#attributes']['class'] = array('element-invisible');
$output = '';
$output .= '<div class="link-field-subrow clearfix">';
$output .= '<div class="link-field-title color-field-column">' . drupal_render($element['rgb']) . '</div>';
if (isset($element['opacity'])) {
$output .= '<div class="link-field-title color-field-column">' . drupal_render($element['opacity']) . '</div>';
}
$output .= '</div>';
$output .= drupal_render_children($element);
return $output;
}
/**
* Formats a plain color field widget.
*/
function theme_color_field_plain_text($vars) {
drupal_add_css(drupal_get_path('module', 'color_field') . '/color_field_plain_text/color_field.css');
$element = $vars['element'];
$output = '';
$output .= '<div class="link-field-subrow clearfix">';
$output .= '<div class="link-field-title color-field-column">' . drupal_render($element['rgb']) . '</div>';
if (isset($element['opacity'])) {
$output .= '<div class="link-field-title color-field-column">' . drupal_render($element['opacity']) . '</div>';
}
$output .= '</div>';
$output .= drupal_render_children($element);
return $output;
}
/**
* Formats a color swatch.
*/
function theme_color_swatch($variables) {
$color = $variables['color'];
$width = $variables['width'];
$height = $variables['height'];
return '<div class="color-swatch" style="background-color: ' . $color . '; width: ' . $width . 'px; height: ' . $height . 'px;"></div>';
}
<?php
/**
* @file
* Date administration code.
*/
/**
* Helper function for color_field_field_instance_settings_form().
*/
function _color_field_field_instance_settings_form($field, $instance) {
$widget = $instance['widget'];
$settings = $instance['settings'];
$form = array();
switch ($widget['type']) {
case 'color_field_default_widget':
$form['default_colors'] = array(
'#type' => 'textarea',
'#title' => t('Default colors'),
'#default_value' => $settings['default_colors'],
'#required' => TRUE,
'#description' => t('Default colors for pre-selected color boxes'),
);
break;
case 'color_field_simple_widget':
$form['cell_width'] = array(
'#type' => 'textfield',
'#title' => t('Cell width'),
'#default_value' => $settings['cell_width'],
'#required' => TRUE,
'#description' => t('Width of each individual color cell.'),
);
$form['cell_height'] = array(
'#type' => 'textfield',
'#title' => t('Height width'),
'#default_value' => $settings['cell_height'],
'#required' => TRUE,
'#description' => t('Height of each individual color cell.'),
);
$form['cell_margin'] = array(
'#type' => 'textfield',
'#title' => t('Cell margin'),
'#default_value' => $settings['cell_margin'],
'#required' => TRUE,
'#description' => t('Margin of each individual color cell.'),
);
$form['box_width'] = array(
'#type' => 'textfield',
'#title' => t('Box width'),
'#default_value' => $settings['box_width'],
'#required' => TRUE,
'#description' => t('Width of the color display box.'),
);
$form['box_height'] = array(
'#type' => 'textfield',
'#title' => t('Box height'),
'#default_value' => $settings['box_height'],
'#required' => TRUE,
'#description' => t('Height of the color display box.'),
);
$form['columns'] = array(
'#type' => 'textfield',
'#title' => t('Columns number'),
'#default_value' => $settings['columns'],
'#required' => TRUE,
'#description' => t('Number of columns to display. Color order may look strange if this is altered.'),
);
break;
}
return $form;
}
/**
* Helper function for color_field_field_settings_form().
*/
function _color_field_field_settings_form($field, $instance, $has_data) {
$widget = $instance['widget'];
$settings = $field['settings'];
$form = array();
$form['opacity'] = array(
'#type' => 'checkbox',
'#title' => t('Record opacity'),
'#default_value' => $settings['opacity'],
'#description' => t('If checked, the opacity will be recorded.'),
);
return $form;
}
/**
* Helper function for color_field_field_settings_form().
*/
function _color_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
// Current widget.
$widget = $instance['widget'];
// Default value.
$default_value = isset($instance['default_value'][0]['rgb']) ? $instance['default_value'][0]['rgb'] : '';
$default_opacity = isset($instance['default_value'][0]['opacity']) ? $instance['default_value'][0]['opacity'] : '';
$value = isset($items[$delta]['rgb']) ? $items[$delta]['rgb'] : $default_value;
$opacity = isset($items[$delta]['opacity']) ? $items[$delta]['opacity'] : $default_opacity;
$element['rgb'] = array(
'#type' => 'textfield',
'#title' => t('Color'),
'#description' => t('Format #FFFFFF'),
'#default_value' => $value,
'#size' => 7,
'#maxlength' => 7,
);
if (isset($field['settings']['opacity']) && $field['settings']['opacity']) {
$element['opacity'] = array(
'#type' => 'textfield',
'#title' => t('Opacity'),
'#description' => t('Between 0 and 1'),
'#default_value' => $opacity,
'#size' => 4,
'#maxlength' => 4,
);
}
switch ($widget['type']) {
case 'color_field_default_widgett':
$element['rgb']['#type'] = 'token';
break;
}
$element += array(
'#settings' => $instance['settings'],
'#type' => $widget['type'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : NULL,
);
return $element;
}
......@@ -27,3 +27,12 @@
.hide_input {
diplay: none;
}
.color-field-column {
float: left;
width: 48%;
}
.color-field-column .form-text {
width: 95%;
}
......@@ -2,20 +2,23 @@
* @file
* Javascript for Color Field.
*/
(function ($) {
Drupal.behaviors.color_field = {
attach: function (context) {
$.each(Drupal.settings.color_field, function (selector) {
var id = selector.replace("#div","edit");
var value = $('#' + id).val();
// selector is the textfield.
// Try to get the current selected value so we don't lose the value
// if the form si submitted but not valid.
var value = $(selector).val();
if (value == '') value = this.value;
$(selector).empty().addColorPicker({
$('#' + this.divid).empty().addColorPicker({
currentColor:value,
colors:this.colors,
clickCallback: function(c) {
id = selector;
id = id.replace("#div","edit");
$('#' + id).val(c);
$(selector).val(c);
}
});
});
......
......@@ -4,3 +4,11 @@
.simpleColorChooser {
z-index: 1;
}
.color-field-column {
float: left;
width: 48%;
}
.color-field-column .form-text {
width: 95%;
}
.color-field-column {
float: left;
width: 48%;
}
.color-field-column .form-text {
width: 95%;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment