Skip to content

Commit 8243f66

Browse files
committed
Add date picker.
1 parent 3bc0230 commit 8243f66

9 files changed

+1373
-0
lines changed

DatePicker.php

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace jDate;
4+
5+
use yii\helpers\Json;
6+
use yii\base\Model;
7+
use yii\base\InvalidConfigException;
8+
use yii\helpers\Html;
9+
/**
10+
* Jalali date & time.
11+
* @author Mohammad Mahdi Gholomian.
12+
* @copyright 2014 [email protected]
13+
*/
14+
class DatePicker extends \yii\base\Widget
15+
{
16+
/**
17+
* @var Model the data model that this widget is associated with.
18+
*/
19+
public $model;
20+
/**
21+
* @var string the model attribute that this widget is associated with.
22+
*/
23+
public $attribute;
24+
/**
25+
* @var string the input name. This must be set if [[model]] and [[attribute]] are not set.
26+
*/
27+
public $name;
28+
/**
29+
* @var string the input value.
30+
*/
31+
public $value;
32+
/**
33+
* @var array Input options.
34+
*/
35+
public $options = [];
36+
/**
37+
* @var array Date picker options.
38+
*/
39+
public $clientOptions = ['formatDate' => "YYYY/0M/0D"];
40+
41+
public function init()
42+
{
43+
parent::init();
44+
if (!isset($this->options['id'])) {
45+
$this->options['id'] = $this->getId();
46+
}
47+
if (!$this->hasModel() && $this->name === null) {
48+
throw new InvalidConfigException("Either 'name', or 'model' and 'attribute' properties must be specified.");
49+
}
50+
if ($this->hasModel() && !isset($this->options['id'])) {
51+
$this->options['id'] = Html::getInputId($this->model, $this->attribute);
52+
}
53+
}
54+
55+
function run()
56+
{
57+
$this->registerAsset();
58+
59+
echo $this->renderInput();
60+
61+
$this->renderJsCode();
62+
}
63+
/**
64+
* Register datepicker asset into view.
65+
*/
66+
function registerAsset()
67+
{
68+
DatePickerAsset::register($this->getView());
69+
}
70+
/**
71+
* Render input.
72+
*/
73+
function renderInput()
74+
{
75+
if ($this->hasModel()) {
76+
return Html::activeTextInput($this->model, $this->attribute, $this->options);
77+
} else {
78+
return Html::textInput($this->name, $this->value, $this->options);
79+
}
80+
}
81+
/**
82+
* Render Js code.
83+
*/
84+
function renderJsCode()
85+
{
86+
$name = 'persianDatepicker';
87+
$id = $this->options['id'];
88+
$options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions);
89+
$js = "jQuery('#$id').$name($options);";
90+
$this->getView()->registerJs($js);
91+
}
92+
/**
93+
* @return boolean whether this widget is associated with a data model.
94+
*/
95+
protected function hasModel()
96+
{
97+
return $this->model instanceof Model && $this->attribute !== null;
98+
}
99+
}

DatePickerAsset.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace jDate;
4+
5+
use yii\web\AssetBundle;
6+
7+
/**
8+
* @author Mohammad Mahdi Gholomian.
9+
* @copyright 2014 [email protected]
10+
*/
11+
class DatePickerAsset extends AssetBundle
12+
{
13+
public $sourcePath = '@vendor/mohammad-mahdy/yii2-jdate/assets';
14+
public $js = [
15+
'js/persianDatepicker.min.js',
16+
];
17+
public $css = [
18+
'css/persianDatepicker-default.css',
19+
];
20+
public $depends = [
21+
'yii\web\JqueryAsset',
22+
];
23+
}

assets/LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013 Mohammad hasan Behzadi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

assets/README.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
##[Demos and information](http://mbehzadi.com/persianDatepicker "mbehzadi.com")
2+
3+
##About
4+
5+
persianDatepicker is A lightweight jQuery plugin that select persian(jalali) date.
6+
7+
- **Browsers:** >IE8, Chrome, Firefox, safari, opera
8+
- **Light weight:** ~14k minified
9+
- **Beautiful themes:** default, dark
10+
- **Size and font:** set the size (width & height) and fontsize for datepicker cells
11+
- **Show persian numbers:** (۰ - ۹)
12+
- **Select gregorian date:** (good way to convert jalali date to gregorian date)
13+
- **Multi formatting date:** like ("YYYY/0M/DD hh:ss") becomes like **1392/07/22 16:45**
14+
- **Selectable months and years:** [1, 3, 4, 12]
15+
16+
17+
##Usage
18+
1- include jQuery & persianDatepicker.js & persianDatepicker.css
19+
```html
20+
<link type="text/css" rel="stylesheet" href="css/persianDatepicker.css" />
21+
<script type="text/javascript" src="js/jquery-1.10.1.min.js"></script>
22+
<script type="text/javascript" src="js/persianDatepicker.min.js"></script>
23+
```
24+
2- add your html element (input or span or etc)
25+
```html
26+
<input type="text" id="input1" />
27+
<span id="span1"></span>
28+
```
29+
3- call the persianDatepicker plugin
30+
```html
31+
<script type="text/javascript">
32+
$(function() {
33+
$("#input1, #span1").persianDatepicker();
34+
});
35+
</script>
36+
```
37+
38+
39+
##Options
40+
To customize persian datepicker, simply pass in an options object: (defaults shown)
41+
```javascript
42+
$("#input1")persianDatePicker({
43+
months: ["فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند"],
44+
dowTitle: ["شنبه", "یکشنبه", "دوشنبه", "سه شنبه", "چهارشنبه", "پنج شنبه", "جمعه"],
45+
shortDowTitle: ["ش", "ی", "د", "س", "چ", "پ", "ج"],
46+
showGregorianDate: false,
47+
persianNumbers: true,
48+
formatDate: "YYYY/MM/DD",
49+
prevArrow: '\u25c4',
50+
nextArrow: '\u25ba',
51+
theme: 'default',
52+
alwaysShow: false,
53+
selectableYears: null,
54+
selectableMonths: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
55+
cellWidth: 25,
56+
cellHeight: 20,
57+
fontSize: 13,
58+
isRTL: false,
59+
calendarPosition: {
60+
x: 0,
61+
y: 0,
62+
},
63+
onShow: function(calendar) {
64+
calendar.show();
65+
},
66+
onHide: function(calendar) {
67+
calendar.hide();
68+
},
69+
});
70+
```
71+
72+
##Credit
73+
Created by [@kharabati](http://twitter.com/kharabati "@kharabati"), [blog](http://mbehzadi.com/ "mbehzadi.com")
74+
75+
use, share , fork , enjoy! , ...

0 commit comments

Comments
 (0)