Created
August 9, 2011 06:26
-
-
Save Zauberfisch/1133508 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class DaysSliderField extends SliderField { | |
function Value() { | |
$Day = _t("Date.DAY", "day"); | |
$Days = _t("Date.DAYS", "days"); | |
$AndMore = _t('TravelType.AndMore', 'and more'); | |
$values = $this->getConfig('values'); | |
$str = $values[0]; | |
$str .= ($values[0] == 1)?' '.$Day:' '.$Days; | |
if (isset($values[1])) { | |
if ($values[1] == 35) { | |
$str .= " ".$AndMore; | |
} elseif ($values[0] != $values[1]){ | |
$str .= " - ".$values[1]; | |
$str .= ($values[1] == 1)?' '.$Day:' '.$Days; | |
} | |
} | |
return $str; | |
} | |
function setValue($value) { | |
// if we have "and more" we want to replace it with -35 to get the range up to 35 | |
$AndMore = _t('TravelType.AndMore', 'und mehr'); | |
$value = str_replace($AndMore, '-35', $value); | |
parent::setValue($value); | |
} | |
} | |
class SliderField extends FormField { | |
/** | |
* @var array | |
*/ | |
protected $events = array( | |
'slide' => | |
"function( event, ui ) { | |
if (ui.values) { | |
$('#'+inputID).val(ui.values[0]+' - '+ui.values[1]); | |
} else { | |
$('#'+inputID).val(ui.value); | |
} | |
}" | |
); | |
/** | |
* For informations about the events check out http://jqueryui.com/demos/slider | |
* @param string $name eg: "create" | |
* @param string $jsCode eg: "function(event, ui) { ... }" | |
*/ | |
function setEvent($name, $jsCode) { | |
$this->events[$name] = $jsCode; | |
} | |
/** | |
* @param String $name | |
* @return String | |
*/ | |
function getEvent($name) { | |
return $this->events[$name]; | |
} | |
/** | |
* @var array | |
*/ | |
protected $configs = array( | |
'disabled' => false, | |
'animate' => true, | |
'min' => 0, | |
'max' => 100, | |
'orientation' => 'horizontal', | |
'range' => false, | |
'step' => 1, | |
'value' => 0, | |
'values' => null | |
); | |
/** | |
* For informations about the configs check out http://jqueryui.com/demos/slider | |
* @param string $name | |
* @param mixed $val | |
*/ | |
function setConfig($name, $val) { | |
$this->configs[$name] = $val; | |
} | |
/** | |
* @param String $name | |
* @return mixed | |
*/ | |
function getConfig($name) { | |
return $this->configs[$name]; | |
} | |
/** | |
* Returns JS ready configs in array | |
* @return string | |
*/ | |
function getConfigsAndEvents() { | |
$return = array(); | |
foreach ($this->configs as $key => $val) { | |
if (is_array($val)) { | |
$return[] = "$key: [".implode(',', $val).']'; | |
} elseif (is_numeric($val)) { | |
$return[] = "$key: $val"; | |
} elseif (is_bool($val)) { | |
$return[] = ($val)?"$key: true":"$key: false"; | |
} elseif ($val !== null) { | |
$return[] = "$key: '$val'"; | |
} | |
} | |
foreach ($this->events as $key => $val) | |
$return[] = "$key: $val"; | |
return implode(',',$return); | |
} | |
function setValue($value) { | |
if ($this->getConfig('range') || is_array($this->getConfig('values'))) { | |
$values = explode('-', $value); | |
foreach ($values as $key => $val) { | |
$values[$key] = (int)$val; | |
} | |
$this->setConfig('values', $values); | |
} | |
$this->setConfig('value', $value); | |
} | |
function Value() { | |
if (is_array($this->getConfig('values'))) | |
return implode(' - ', $this->getConfig('values')); | |
return $this->getConfig('value'); | |
} | |
function __construct($name, $title = null, $configs = array(), $events = array(), $form = null) { | |
foreach ($configs as $name => $val) | |
$this->setConfig($name, $val); | |
foreach ($events as $name => $jsCode) | |
$this->setEvent($name, $jsCode); | |
parent::__construct($name, ($title===null)?$name:$title, null, $form); | |
} | |
/** | |
* @return string HTML tag for this field | |
*/ | |
function Field() { | |
Requirements::css(THIRDPARTY_DIR . '/jquery-ui-themes/smoothness/jquery.ui.all.css'); | |
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); | |
Requirements::javascript(SAPPHIRE_DIR . '/javascript/jquery_improvements.js'); | |
// Requirements::javascript(THIRDPARTY_DIR . '/jquery-ui/jquery.ui.core.js'); | |
// Requirements::javascript(THIRDPARTY_DIR . '/jquery-ui/jquery.ui.widget.js'); | |
// Requirements::javascript(THIRDPARTY_DIR . '/jquery-ui/jquery.ui.mouse.js'); | |
Requirements::javascript(THIRDPARTY_DIR . '/jquery-ui/jquery-ui-1.8rc3.custom.js'); | |
// @todo i should not need the entire UI file ... | |
$SliderID = $this->id().'_RangeSlider'; | |
$attributes = array( | |
'type' => 'text', | |
'class' => 'text RangeSlider ' . $SliderID . ($this->extraClass() ? $this->extraClass() : ''), | |
'id' => $this->id(), | |
'name' => $this->Name(), | |
'value' => $this->Value() | |
); | |
if($this->disabled) { | |
$attributes['disabled'] = 'disabled'; | |
$this->setConfig('disabled', true); | |
} | |
$input = $this->createTag('input', $attributes); | |
Requirements::customScript( | |
"(function($) { | |
$(document).ready(function() { | |
var inputID = '{$this->id()}'; | |
var sliderID = '{$SliderID}'; | |
$('#$SliderID').slider({". | |
$this->getConfigsAndEvents(). | |
"}); | |
}); | |
})(jQuery);" | |
); | |
$attributes = array( | |
'class' => 'RangeSlider' . ($this->extraClass() ? $this->extraClass() : ''), | |
'id' => $SliderID, | |
); | |
return $input . $this->createTag('div', $attributes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment