Last active
March 30, 2016 21:06
-
-
Save h-collector/b8cb143799064114dec0f53e8d5ea8f2 to your computer and use it in GitHub Desktop.
This file contains 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 | |
namespace App\Entities\Fields; | |
use App\Entities\Fields\DateTime as DateTimeField; | |
class Date extends DateTimeField | |
{ | |
/** | |
* The name of the JavaScript class that is used to render this field. | |
* | |
* @return string | |
*/ | |
protected function modelClass() | |
{ | |
return 'Cruddy.Fields.DateEx'; | |
} | |
} |
This file contains 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 | |
namespace App\Entities\Fields; | |
use Illuminate\Database\Query\Builder; | |
use Kalnoy\Cruddy\Contracts\Filter; | |
use Kalnoy\Cruddy\Schema\Fields\Types\DateTime as BaseDateTime; | |
use Carbon\Carbon; | |
use Exception; | |
class DateTime extends BaseDateTime implements Filter | |
{ | |
/** | |
* The name of the JavaScript class that is used to render this field. | |
* | |
* @return string | |
*/ | |
protected function modelClass() | |
{ | |
return 'Cruddy.Fields.DateTimeEx'; | |
} | |
/** | |
* @param Builder $builder | |
* @param $input | |
* | |
* @return void | |
*/ | |
public function applyFilterConstraint(Builder $builder, $input) | |
{ | |
if (!$this->keep($input)) { | |
return; | |
} | |
try { | |
$range = preg_split('@[\s+]*~[\s+]*@', $input, 2, PREG_SPLIT_NO_EMPTY); | |
$range = array_map([$this, 'processForDateRange'], $range); | |
// if only one elment, then get on same day | |
if (count($range) === 1 || $range[0] == $range[1]) { | |
$range = [ | |
$range[0]->copy()->startOfDay(), | |
$range[0]->copy()->endOfDay(), | |
]; | |
} | |
$builder->whereBetween($this->id, $range); | |
} catch (Exception $e) { | |
; | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function keep($value) | |
{ | |
return trim($value) !== ''; | |
} | |
/** | |
* {@inheritdoc} | |
* | |
* @return \Carbon\Carbon | |
*/ | |
public function processForDateRange($value) | |
{ | |
return new Carbon(trim($value)); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function toArray() | |
{ | |
return [ | |
'moment' => $this->get('moment'), | |
] + parent::toArray(); | |
} | |
} |
This file contains 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
# Inputs | |
class Cruddy.Inputs.DateTimeEx extends Cruddy.Inputs.DateTime | |
# Filter input | |
class Cruddy.Inputs.DateRange extends Cruddy.Inputs.Text | |
events: | |
"datepicker-change" : "change" | |
"change": "change" | |
"keydown": "keydown" | |
initialize: (options) -> | |
# just in case fix | |
moment.locale = (->) unless moment?.locale | |
@$el.dateRangePicker( | |
startOfWeek: 'monday' | |
separator: ' ~ ' | |
format: 'YYYY-MM-DD' | |
# format: 'YYYY-MM-DD HH:mm' | |
# autoClose: false | |
# time: | |
# enabled: true | |
# defaultTime: moment().startOf('day').toDate() | |
# defaultEndTime: moment().endOf('day').toDate() | |
# endDate: moment().endOf('day') | |
showWeekNumbers: true | |
showTopbar: false | |
) | |
super | |
# Fields | |
class Cruddy.Fields.BaseDateTimeEx extends Cruddy.Fields.BaseDateTime | |
useMoment: true | |
initialize: (options) -> | |
@entity = options.entity | |
@useMoment = !! @attributes.moment if @attributes.moment? | |
super | |
createEditableInput: (model, inputId) -> new Cruddy.Inputs.DateTimeEx | |
model: model | |
key: @id | |
format: @inputFormat | |
mask: @mask | |
attributes: | |
id: @inputId | |
formatDate: (value) -> moment.unix(value).format @inputFormat | |
#todo new Input | |
createFilterInput: (model) -> new Cruddy.Inputs.DateRange | |
model: model | |
key: @id | |
# apply button | |
prepareAttribute: (value) -> | |
value | |
# parse unsearchable | |
parseFilterData: (value) -> | |
value.plusDecode() if typeof value is 'string' | |
class Cruddy.Fields.DateTimeEx extends Cruddy.Fields.BaseDateTimeEx | |
inputFormat: "YYYY-MM-DD HH:mm:ss" | |
mask: "9999-99-99 99:99:99" | |
formatDate: (value) -> | |
if @useMoment then moment.unix(value).fromNow() else super value | |
class Cruddy.Fields.DateEx extends Cruddy.Fields.BaseDateTimeEx | |
inputFormat: "YYYY-MM-DD" | |
mask: "9999-99-99" |
This file contains 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 | |
namespace App\Providers; | |
use Illuminate\Support\ServiceProvider; | |
use Kalnoy\Cruddy\Assets; | |
use Kalnoy\Cruddy\Schema\Fields\Factory; | |
use App\Entities\Fields\Date as DateField; | |
use App\Entities\Fields\DateTime as DateTimeField; | |
class PluginsServiceProvider extends ServiceProvider | |
{ | |
// protected $defer = true; | |
/** | |
* Register the service provider. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$this->app->resolving('cruddy.fields', function (Factory $factory) { | |
$this->registerFields($factory); | |
}); | |
$this->app->resolving('cruddy.assets', function (Assets $assets) { | |
$assets->css(asset('css/daterangepicker.css')); | |
$assets->js(asset('js/jquery.daterangepicker.js')); | |
$dev = $this->app->environment('local', 'devel'); | |
$assets->js(asset(sprintf('js/backend%s.js', $dev ? '-dev' : ''))); | |
}); | |
} | |
/** | |
* Register fields. | |
* | |
* @param Factory $factory | |
*/ | |
protected function registerFields(Factory $factory) | |
{ | |
$factory->register('date', DateField::class); | |
$factory->register('datetime', DateTimeField::class); | |
} | |
/** | |
* Bootstrap the application events. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
// publishes => plugins.js | |
} | |
public function provides() | |
{ | |
return [ | |
DateTimeField::class, | |
DateField::class, | |
'cruddy.fields', | |
'cruddy.assets', | |
]; | |
} | |
} |
This file contains 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
String::extractParams = -> | |
params = {} | |
query = @split(/[?#]/) | |
if !query[1] | |
return params | |
urlparams = query[1].split('&') | |
for x of urlparams | |
kvpair = urlparams[x].split('=') | |
params[kvpair[0]] = kvpair[1] | |
params | |
String::capitalize = -> | |
@charAt(0).toUpperCase() + @slice(1) | |
String::plusEncode = -> | |
encodeURIComponent(@).replace /\%20/gm, '+' | |
String::plusDecode = -> | |
decodeURIComponent @replace(/\+/gm, '%20') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment