Skip to content

Commit

Permalink
Changes for select_advanced:
Browse files Browse the repository at this point in the history
- change folder name to `select2` only
- make it a child field of `select` so we can forget about how it save/get data
- make `placeholder` work
  • Loading branch information
rilwis committed Nov 1, 2012
1 parent 69c40b6 commit a8ad29e
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 2,477 deletions.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
85 changes: 19 additions & 66 deletions inc/fields/select-advanced.php
Expand Up @@ -2,9 +2,12 @@
// Prevent loading this file directly
defined( 'ABSPATH' ) || exit;

// Make sure "select" field is loaded
require_once RWMB_FIELDS_DIR . 'select.php';

if ( !class_exists( 'RWMB_Select_Advanced_Field' ) )
{
class RWMB_Select_Advanced_Field
class RWMB_Select_Advanced_Field extends RWMB_Select_Field
{
/**
* Enqueue scripts and styles
Expand All @@ -13,10 +16,11 @@ class RWMB_Select_Advanced_Field
*/
static function admin_enqueue_scripts()
{
wp_enqueue_style( 'select2', RWMB_CSS_URL . 'select2-css/select2.css', array(), '3.2' );
wp_enqueue_style( 'select2', RWMB_CSS_URL . 'select2/select2.css', array(), '3.2' );
wp_enqueue_style( 'rwmb-select-advanced', RWMB_CSS_URL . 'select-advanced.css', array(), RWMB_VER );
wp_register_script( 'select2', RWMB_JS_URL . 'select2-js/select2.js', array(), '3.2', true );
wp_enqueue_script( 'select_advanced', RWMB_JS_URL . 'select-advanced.js', array('select2'), RWMB_VER, true );

wp_register_script( 'select2', RWMB_JS_URL . 'select2/select2.min.js', array(), '3.2', true );
wp_enqueue_script( 'rwmb-select-advanced', RWMB_JS_URL . 'select-advanced.js', array( 'select2' ), RWMB_VER, true );
}

/**
Expand All @@ -31,12 +35,15 @@ static function admin_enqueue_scripts()
static function html( $html, $meta, $field )
{
$html = sprintf(
'<select class="rwmb-select-advanced" name="%s" id="%s"%s data-options ="%s">',
'<select class="rwmb-select-advanced" name="%s" id="%s"%s data-options="%s">',
$field['field_name'],
$field['id'],
$field['multiple'] ? ' multiple="multiple"' : '',
esc_attr( json_encode( $field['js_options'] ))
esc_attr( json_encode( $field['js_options'] ) )
);
if ( !empty( $field['js_options']['placeholder'] ) )
$html .= '<option></option>';

$option = '<option value="%s" %s>%s</option>';

foreach ( $field['options'] as $value => $label )
Expand All @@ -53,59 +60,6 @@ static function html( $html, $meta, $field )
return $html;
}

/**
* Get meta value
* If field is cloneable, value is saved as a single entry in DB
* Otherwise value is saved as multiple entries (for backward compatibility)
*
* @see "save" method for better understanding
*
* TODO: A good way to ALWAYS save values in single entry in DB, while maintaining backward compatibility
*
* @param $meta
* @param $post_id
* @param $saved
* @param $field
*
* @return array
*/
static function meta( $meta, $post_id, $saved, $field )
{
$single = $field['clone'] || !$field['multiple'];
$meta = get_post_meta( $post_id, $field['id'], $single );
$meta = ( !$saved && '' === $meta || array() === $meta ) ? $field['std'] : $meta;

$meta = array_map( 'esc_attr', (array) $meta );

return $meta;
}

/**
* Save meta value
* If field is cloneable, value is saved as a single entry in DB
* Otherwise value is saved as multiple entries (for backward compatibility)
*
* TODO: A good way to ALWAYS save values in single entry in DB, while maintaining backward compatibility
*
* @param $new
* @param $old
* @param $post_id
* @param $field
*/
static function save( $new, $old, $post_id, $field )
{
if ( !$field['clone'] )
{
RW_Meta_Box::save( $new, $old, $post_id, $field );
return;
}

if ( empty( $new ) )
delete_post_meta( $post_id, $field['id'] );
else
update_post_meta( $post_id, $field['id'], $new );
}

/**
* Normalize parameters for field
*
Expand All @@ -115,19 +69,18 @@ static function save( $new, $old, $post_id, $field )
*/
static function normalize_field( $field )
{
$field = parent::normalize_field( $field );

$field = wp_parse_args( $field, array(
'js_options' => array(),
) );

$field['js_options'] = wp_parse_args( $field['js_options'], array(
'allowClear' => true,
'width' => 'resolve',
'placeholder' => "Select a Value"
'allowClear' => true,
'width' => 'resolve',
'placeholder' => __( 'Select a value', 'rwmb' )
) );

$field['field_name'] = $field['id'];
if ( !$field['clone'] && $field['multiple'] )
$field['field_name'] .= '[]';

return $field;
}
}
Expand Down
8 changes: 4 additions & 4 deletions js/select-advanced.js
@@ -1,20 +1,20 @@
/**
* Update date picker element
* Update select2
* Used for static & dynamic added elements (when clone)
*/
function rwmb_update_select_advanced()
{
var $ = jQuery;

$( '.rwmb-select-advanced' ).each( function()
$( '.rwmb-select-advanced' ).each( function ()
{
var $this = $( this ),
options = $this.data( 'options' );
$this.select2(options);
$this.select2( options );
} );
}

jQuery( document ).ready( function()
jQuery( document ).ready( function ()
{
rwmb_update_select_advanced();
} );

0 comments on commit a8ad29e

Please sign in to comment.