Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions assets/jcforms-multifield.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
.jcmf-multi-field {
font-size: 16px;
padding: 0 0 50px 0;
position: relative;
box-sizing: border-box;
}
.jcmf-form-item {
padding: 5px 20px;
position: relative;
overflow: hidden;
}
.jcmf-form-item.has-error {
border-bottom: 2px solid #d00;
background: #ffdada;
}
.jcmf-remove-input {
position: absolute;
right: 10px;
top: 10px;
cursor: pointer;
color: #444;
}
.jcmf-add-input-btn {
background-color: #f5f5f5;
position: absolute;
bottom: 10px;
}
.jcmf-btn {
display: inline-block;
padding: 6px 12px;
font-size: 14px;
line-height: 1.5;
text-align: center;
white-space: nowrap;
vertical-align: middle;
cursor: pointer;
border: 1px solid rgba(0, 0, 0, 0);
border-radius: 4px;
}
.cols .jcmf-input {
float: left;
margin-right: 1%;
}
.col-2 .jcmf-input { width: 48.9%; }
.col-3 .jcmf-input { width: 31.9%; }
.col-4 .jcmf-input { width: 23.9%; }
.col-5 .jcmf-input { width: 18.9%; }
.col-6 .jcmf-input { width: 14.9%; }
.col-7 .jcmf-input { width: 12.9%; }
.col-8 .jcmf-input { width: 11.5%; margin-right: 0.5%; }
.col-9 .jcmf-input { width: 10.5%; margin-right: 0.5%; }
.col-10 .jcmf-input { width: 9%; margin-right: 0.5%; }

.jcmf-input {
display: block;
}
input.jcmf-input {
border:1px solid #ddd;
border-bottom: 2px solid #558;
padding: 3px 10px;
color:#333;
}
select.jcmf-input {
color:#333;
border:1px solid #ddd;
border-bottom: 2px solid #556;
height: 29px;
}
textarea.jcmf-input {
color:#333;
height:auto;
min-height: 30px;
}
.jcmf-multi-field .handle {
position: absolute;
left: 0;
top: 25px;
transform: translateY(-50%);
width: 18px;
height: 32px;
background-color: #f3f3f3;
display: block;
}
.jcmf-multi-field .handle.sortable {
background-color: #b3b3b3;
cursor: move;
}
147 changes: 147 additions & 0 deletions assets/jcforms-multifield.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
jQuery(function($) {

/**
* Justcoded Form elements editor plugin
* @return {Object} - DOM element
*/
$.fn.jcMultiField = function(options) {
var counter = 0;

var settings = $.extend({
addButton: { class: 'jcmf-btn' },
removeButton: { class: 'jcmf-btn-remove' },
dragHandler: { class: 'jcmf-handle' }
}, options);

var $el = this,
$addInputBtn = $('<button class="jcmf-add-input-btn ' + settings.addButton.class + '" >Add item</button>'),
$row = $('<div class="jcmf-form-item input-item "><span class="handle sortable ' + settings.dragHandler.class + '" ></span><span class="jcmf-remove-input ' + settings.removeButton.class + '" ></span></div>');

/**
* Add inputs and add button elements to page and add events
*/
var init = function() {
$el.addClass('jcmf-multi-field');

if ( ( 'object' != typeof(settings.data) ) || settings.data.length == 0 ) {
settings.data = [];
settings.data.push( {} );
}

addRows();

$el.append($addInputBtn);

//EVENTS
$el.on("click", 'button.jcmf-add-input-btn', function(e) {
e.preventDefault();
addRow();
});

$el.on("click", '.jcmf-remove-input', function(e) {
e.preventDefault();
removeItem($(e.currentTarget).closest('.jcmf-form-item'));
});
//check sortable row
$el.on("keyup", '.jcmf-input', function(e) {
e.preventDefault();
checkSortableOrder($(e.currentTarget).closest('.jcmf-form-item'));
});
}

/**
* Append rows to module
*/
var addRows = function() {
var index = 0;
$.each(settings.data, function(i, d) {
addRow(index);
index++;
});
}

/**
* Create row, add inputs to row and append it to the module
* @param {[type]} index [description]
*/
var addRow = function(index) {
var row = $row.clone().addClass('cols col-' + settings.structure.length);
$el.append(row);

$.each(settings.structure, function(i, d) {
var value = settings.data[index]? settings.data[index][d.name] : null;
var title = d.placeholder ? d.placeholder : '';

switch (d.type) {
case 'select':
var control = $("<select>");
control.addClass('jcmf-input');
for ( var key in d.items ) {
$(control).append('<option value="' + key + '">' + d.items[key] + '</option>');
}
if ( typeof(value) == 'undefined' || null === value ) {
value = $(control).find('option:first').val();
}
break;
case 'textarea':
var control = $("<textarea>");
control.attr({
placeholder: title
}).addClass('jcmf-input');
break;
default:
var control = $("<input />");
control.attr({
placeholder: title,
type: d.type ? d.type : 'text',
}).addClass('jcmf-input');
}

$(control).val(value).attr({
name: settings.fieldId + '[' + counter + ']' + '[' + d.name + ']',
title: title
}).appendTo(row);

if ( settings.data[index] && settings.data[index]['_hasError'] ) {
$(control).parents('.jcmf-form-item').addClass('has-error');
}
});

checkSortableOrder(row);

counter++;
};

/**
* If one of the input not empty - row can be sortable
* @param {Object} row - row element
*/
var checkSortableOrder = function(row) {
var disableSorting = row.find('input').filter(function() {
return $.trim($(this).val()).length > 0
}).length > 0;
if (disableSorting) {
row.find('.handle').addClass('sortable');
}else{
row.find('.handle').removeClass('sortable');
}
};


/**
* Remove element form the DOM
* @param {Oblect} $item - element to delete
*/
var removeItem = function($item) {
$item.remove();
}

init();

return this.sortable({
items: ".jcmf-form-item",
handle: ".handle.sortable"
});
};

});
90 changes: 50 additions & 40 deletions components/checkbox/JustField_Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,29 @@ public function __construct()
*/
public function field()
{
// prepare options array
$values = $this->parsedSelectOptions($this->instance);

if ( empty($values) ) {
if ( empty($this->instance['settings']) ) {
echo '<p>' . __('Please check settings. Values are empty', \JustCustomFields::TEXTDOMAIN) . '</p>';
return false;
}

$single_checkbox = (count($values) == 1) ? true : false;
$single_checkbox = (count($this->instance['settings']) == 1) ? true : false;
?>
<div id="jcf_field-<?php echo $this->id; ?>" class="jcf_edit_field <?php echo $this->fieldOptions['classname']; ?>">
<?php echo $this->fieldOptions['before_widget']; ?>
<?php echo $this->fieldOptions['before_title'] . esc_html($this->instance['title']) . $this->fieldOptions['after_title']; ?>

<div class="checkboxes-set">
<div class="checkbox-row">
<?php foreach ( (array) $values as $key => $val ) : ?>
<?php foreach ( (array) $this->instance['settings'] as $val ) : ?>
<?php
if ( $single_checkbox ) {
$checked = ($val == $this->entry) ? true : false;
$checked = ($val['id'] == $this->entry) ? true : false;
}
else {
$checked = in_array($val, (array) $this->entry);
$checked = in_array($val['id'], (array) $this->entry);
}
?>
<label><input type="checkbox" name="<?php echo $this->getFieldName('val') . ($single_checkbox ? '' : '[]'); ?>" id="<?php echo $this->getFieldId('val'); ?>" value="<?php echo esc_attr($val); ?>" <?php echo checked(true, $checked, false); ?>/> <?php echo $key; ?></label>
<label><input type="checkbox" name="<?php echo $this->getFieldName('val') . ($single_checkbox ? '' : '[]'); ?>" id="<?php echo $this->getFieldId('val'); ?>" value="<?php echo esc_attr($val['id']); ?>" <?php echo checked(true, $checked, false); ?>/> <?php echo $val['label']; ?></label>
<?php endforeach; ?>
</div>
</div>
Expand All @@ -69,20 +66,58 @@ public function field()
public function form()
{
// Defaults
$instance = wp_parse_args((array) $this->instance, array( 'title' => '', 'settings' => '', 'description' => '' ));
$instance = wp_parse_args((array) $this->instance, array( 'title' => '', 'settings' => array(), 'description' => '' ));

$title = esc_attr($instance['title']);
$settings = esc_attr($instance['settings']);
$settings = $instance['settings'];
$description = esc_html($instance['description']);
?>
<p>
<label for="<?php echo $this->getFieldId('title'); ?>"><?php _e('Title:', \JustCustomFields::TEXTDOMAIN); ?></label>
<input class="widefat" id="<?php echo $this->getFieldId('title'); ?>" name="<?php echo $this->getFieldName('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->getFieldId('settings'); ?>"><?php _e('Settings:', \JustCustomFields::TEXTDOMAIN); ?></label>
<textarea class="widefat" id="<?php echo $this->getFieldId('settings'); ?>" name="<?php echo $this->getFieldName('settings'); ?>" ><?php echo $settings; ?></textarea>
<br/><small><?php _e('Parameters like (you can use just "label" if "id" is the same):<br>label1|id1<br>label2|id2<br>label3', \JustCustomFields::TEXTDOMAIN); ?></small>
<label for="<?php echo $this->getFieldId('settings'); ?>"><?php _e('Settings:', \JustCustomFields::TEXTDOMAIN); ?></label>
<div class="settings"></div>
<?php
$multi_field_config = array(
array(
'name' => 'label',
'placeholder' => 'Label',
'type' => 'text',
),
array(
'name' => 'id',
'placeholder' => 'ID',
'type' => 'text',
),
);
?>
<script type="text/javascript">
( function( $ ) {

$(document).ready(function() {
$('.settings').jcMultiField({
addButton: { class: 'button' },
removeButton: { class: 'dashicons dashicons-no-alt' },
dragHandler: { class: 'dashicons dashicons-menu' },

fieldId: '<?php echo $this->getFieldName('settings'); ?>',
structure: <?php echo json_encode( $multi_field_config ) ?>,
data: <?php echo json_encode( $settings ) ?>,
});
});

}( jQuery ));
</script>
<style type="text/css">
.jcmf-multi-field .handle { background-color: transparent !important; color: #aaa; }
.jcmf-multi-field .handle.sortable { color: #333; }
.jcmf-multi-field .button { margin-left: 23px; }
.jtmce_help .dashicons{ text-decoration: none !important; }
.jtmce_help_box { max-width: 800px; padding: 0 0 30px; }
.jtmce_help_box.hidden { display: none;}
</style>
</p>
<p>
<label for="<?php echo $this->getFieldId('description'); ?>"><?php _e('Description:', \JustCustomFields::TEXTDOMAIN); ?></label>
Expand All @@ -107,36 +142,11 @@ public function update( $new_instance, $old_instance )
{
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['settings'] = strip_tags($new_instance['settings']);
$instance['settings'] = $this->orderOptions($new_instance['settings']);
$instance['description'] = strip_tags($new_instance['description']);
return $instance;
}

/**
* prepare list of options
*
* @param array $instance current instance
* @return array
*/
protected function parsedSelectOptions( $instance )
{
$values = array();
$v = explode("\n", $instance['settings']);

foreach ( $v as $val ) {
$val = trim($val);

if ( strpos($val, '|') !== FALSE ) {
$a = explode('|', $val);
$values[$a[0]] = $a[1];
}
elseif ( !empty($val) ) {
$values[$val] = $val;
}
}
return $values;
}

/**
* print fields values from shortcode
*/
Expand Down
Loading