-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatePickerHtmlBuilder.php
63 lines (53 loc) · 2.54 KB
/
DatePickerHtmlBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace n2nutil\jquery\datepicker;
use n2n\impl\web\ui\view\html\HtmlView;
use n2n\l10n\N2nLocale;
use n2n\impl\web\ui\view\html\HtmlUtils;
use n2n\l10n\DateTimeFormat;
use n2n\impl\web\ui\view\html\HtmlElement;
use n2n\impl\web\ui\view\html\HtmlBuilderMeta;
use n2nutil\jquery\JQueryLibrary;
class DatePickerHtmlBuilder {
private $view;
private $addCss;
public function __construct(HtmlView $view, $addCss = true) {
$this->view = $view;
$this->addCss = $addCss;
}
public function getDatePicker($dateStyle = DateTimeFormat::DEFAULT_DATE_STYLE, $timeStyle = null,
\DateTimeZone $timeZone = null, $simpleFormat = null, $attrs = null, N2nLocale $locale = null) {
if (null == $locale) {
$locale = $this->view->getRequest()->getN2nLocale();
}
$attrs = $this->extendAttrs($attrs);
$this->requireScripts();
return new HtmlElement('input', HtmlUtils::mergeAttrs((array) $attrs, DatePickerUtils::getDatePickerOptionsFactory(
$locale, $timeZone)->createDatePickerOptions(DatePickerUtils::determinePattern($locale, $dateStyle, $timeStyle,
$timeZone, $simpleFormat))->buildHtmlAttrs()));
}
public function datePicker($dateStyle = DateTimeFormat::DEFAULT_DATE_STYLE, $timeStyle = null,
\DateTimeZone $timeZone = null, $simpleFormat = null, $attrs = null, N2nLocale $locale = null) {
$this->view->out($this->getDatePicker($dateStyle, $timeStyle, $timeZone, $simpleFormat, $attrs, $locale));
}
public function getFormDatePicker($propertyExpression = null, $attrs = null) {
$attrs = $this->extendAttrs($attrs);
$this->requireScripts();
$factory = new InputDatePickerFactory($this->view->getHtmlProperties()->getForm());
return $factory->create($this->view->getFormHtmlBuilder()->meta()->createPropertyPath($propertyExpression), $attrs);
}
public function formDatePicker($propertyExpression = null, $attrs = null) {
$this->view->out($this->getFormDatePicker($propertyExpression, $attrs));
}
private function extendAttrs($attrs) {
return HtmlUtils::mergeAttrs((array) $attrs, array('class' => 'util-jquery-datepicker'));
}
private function requireScripts() {
$html = $this->view->getHtmlBuilder();
$html->meta()->addLibrary(new JQueryLibrary(3));
$html->meta()->bodyEnd()->addJs('js/ajah.js', 'n2n\impl\web\ui');
$html->meta()->addJs('datepicker/js/datePicker.js', 'n2nutil\jquery', false, false, null, HtmlBuilderMeta::TARGET_BODY_END);
if ($this->addCss) {
$html->meta()->addCss('datepicker/css/datePicker.css', 'screen', 'n2nutil\jquery');
}
}
}