Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 14d39b2
Showing
7 changed files
with
356 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 laravel-enso | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Select | ||
Select data builder | ||
|
||
## Don't forget to | ||
|
||
Add LaravelEnso\Select\SelectServiceProvider::class to config/app.php. | ||
|
||
Publish the vue components with php artisan vendor:publish --tag=select-component. | ||
|
||
After that include the component in your app.js and run gulp. | ||
|
||
## Upgrade from laravel-enso v2 | ||
|
||
Correct all the includes for the helper classes included in this package (Classes/*) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "laravel-enso/select", | ||
"description": "Select data builder", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "mishu", | ||
"email": "mishu@xtelecom.ro" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.6.4", | ||
"laravel/framework": "5.4.*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"LaravelEnso\\Select\\": "src/" | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<template> | ||
<div :id="'bs-select-' + _uid"> | ||
<i class="fa fa-times reset btn-box-tool" | ||
@click="removeSelection" | ||
v-if="!multiple && reset && selectedOptions"> | ||
</i> | ||
<select v-model="selectedOptions" | ||
:multiple="multiple" | ||
:id="'select-' + _uid" | ||
:name="name" | ||
:disabled="disabled" | ||
class="form-control" | ||
@change="$emit('input', selectedOptions)"> | ||
<option v-for="option in optionsList" | ||
:value="option.key" | ||
v-html="option.value"> | ||
</option> | ||
</select> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
props: { | ||
name: { | ||
type: String, | ||
default: null | ||
}, | ||
disabled: { | ||
type: Boolean, | ||
default: false | ||
}, | ||
multiple: { | ||
type: Boolean, | ||
default: false | ||
}, | ||
source: { | ||
type: String, | ||
default: null | ||
}, | ||
selected: { | ||
default: null | ||
}, | ||
options: { | ||
type: Array, | ||
default: null | ||
}, | ||
reset: { | ||
type: Boolean, | ||
default: false | ||
}, | ||
customParams: { | ||
type: Object, | ||
default: null | ||
} | ||
}, | ||
computed: { | ||
isServerSide: function () { | ||
return this.options ? false : true; | ||
} | ||
}, | ||
watch: { | ||
/* should only be used with server side vue select functionality */ | ||
customParams: { | ||
handler: 'getOptionsList', | ||
deep: true | ||
}, | ||
selected: { | ||
handler: 'handleSelectedChange' | ||
} | ||
}, | ||
data: function () { | ||
return { | ||
optionsList: this.options || [], | ||
selectedOptions: this.selected ? this.selected : (this.multiple ? [] : this.selected) | ||
}; | ||
}, | ||
methods: { | ||
getOptionsList: function () { | ||
var query = $('#bs-select-' + this._uid + ' input').val() || '', //we don't want undefined | ||
params = { | ||
customParams: this.customParams, | ||
query: query, | ||
selected: this.selectedOptions | ||
}; | ||
axios.get(this.source, {params: params}).then((response) => { | ||
this.optionsList = response.data; | ||
if (this.multiple && this.optionsList.length === 0) { | ||
this.optionsList = [ { key: null, value: ''} ]; | ||
} | ||
}).then(() => { | ||
$('#select-' + this._uid).selectpicker('refresh'); | ||
}); | ||
}, | ||
removeSelection: function () { | ||
this.selectedOptions = ''; | ||
//we need next tick for the race condition when selectpicker | ||
//runs before vue finishes updating the DOM | ||
this.$nextTick(function () { | ||
$('#select-' + this._uid).selectpicker('refresh'); | ||
this.$emit('input', this.selectedOptions); | ||
}); | ||
}, | ||
handleSelectedChange: function() { | ||
this.selectedOptions = this.selected; | ||
if (this.isServerSide) { | ||
this.getOptionsList(); | ||
} else { | ||
$('#select-' + this._uid).selectpicker('val', this.selectedOptions); | ||
this.$emit('input', this.selectedOptions); | ||
} | ||
} | ||
}, | ||
mounted: function () { | ||
$('#select-' + this._uid).selectpicker({ | ||
width: '100%', | ||
liveSearch: true, | ||
size: 5, | ||
actionsBox: true, | ||
title: $.fn.selectpicker.defaults.noneResultsText | ||
}); | ||
if (this.selectedOptions) { | ||
this.$emit('input', this.selectedOptions); | ||
//necesary for using without server-side | ||
$('#select-' + this._uid).selectpicker('val', this.selectedOptions); | ||
} | ||
if (this.isServerSide) { | ||
this.getOptionsList(); | ||
var self = this; | ||
$('#bs-select-' + this._uid + ' input').on('input', _.throttle(self.getOptionsList, 200)); | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<style> | ||
i.reset { | ||
z-index: 10; | ||
position: absolute; | ||
right: 35px; | ||
bottom: 24px; | ||
cursor: pointer; | ||
} | ||
</style> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace LaravelEnso\Select; | ||
|
||
class SelectListBuilder | ||
{ | ||
|
||
private $attribute; // 'name' => optional | ||
private $pivotParams; // ['column' => 'table'] => optional | ||
private $class; // 'App\Model' => required | ||
private $query; | ||
|
||
public function __construct($class, $attribute, $pivotParams) | ||
{ | ||
$this->class = $class; | ||
$this->attribute = $attribute; | ||
$this->pivotParams = $pivotParams; | ||
$this->query = $class::query(); | ||
} | ||
|
||
public function getOptionsList() | ||
{ | ||
$ids = (array) request('selected'); | ||
|
||
if (request('customParams')) { | ||
|
||
$this->processPivotParams(); | ||
} | ||
|
||
$models = $this->query->where($this->attribute, 'like', '%' . request('query') . '%') | ||
->orderBy($this->attribute)->limit(10)->get(); | ||
$selected = $this->query->whereIn('id', $ids)->get(); | ||
$result = $models->merge($selected)->pluck('name', 'id'); | ||
$response = static::buildSelectList($result); | ||
|
||
return $response; | ||
} | ||
|
||
private function processPivotParams() | ||
{ | ||
$customParams = json_decode(request('customParams')); | ||
|
||
foreach ($customParams as $key => $value) { | ||
|
||
if (!in_array($key, array_keys($this->pivotParams))) { | ||
|
||
$this->query = $this->query->where($key, $value); | ||
} else { | ||
|
||
$this->query = $this->query->whereHas($this->pivotParams[$key], function ($query) use ($value) { | ||
|
||
$this->query->whereId($value); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
public static function buildSelectList($data) | ||
{ | ||
$response = []; | ||
|
||
foreach ($data as $key => $value) { | ||
|
||
$response[] = [ | ||
|
||
'key' => $key, | ||
'value' => $value, | ||
]; | ||
} | ||
|
||
return json_encode($response); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace LaravelEnso\Select; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class SelectServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Bootstrap the application services. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
$this->publishes([ | ||
__DIR__.'/../resources/assets/js/components/core' => base_path('resources/assets/js/components/core'), | ||
], 'select-component'); | ||
} | ||
|
||
/** | ||
* Register the application services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
// | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace LaravelEnso\Select\Traits; | ||
|
||
use LaravelEnso\Select\SelectListBuilder; | ||
|
||
trait SelectListBuilderTrait | ||
{ | ||
|
||
public function getOptionsList() | ||
{ | ||
$attribute = isset($this->selectAttribute) ? $this->selectAttribute : 'name'; | ||
$pivotParams = isset($this->selectPivotParams) ? $this->selectPivotParams : []; | ||
$selectListBuilder = new SelectListBuilder($this->selectSourceClass, $attribute, $pivotParams); | ||
|
||
return $selectListBuilder->getOptionsList(); | ||
} | ||
|
||
public function buildSelectList($data) | ||
{ | ||
return SelectListBuilder::buildSelectList($data); | ||
} | ||
} |