|
@@ -5,118 +5,53 @@ namespace Dcat\Admin\Grid\Displayers;
|
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Admin;
|
|
|
use Dcat\Admin\Support\Helper;
|
|
use Dcat\Admin\Support\Helper;
|
|
|
|
|
|
|
|
-class Editable extends AbstractDisplayer
|
|
|
|
|
|
|
+abstract class Editable extends AbstractDisplayer
|
|
|
{
|
|
{
|
|
|
- protected $selector = 'grid-column-editable';
|
|
|
|
|
|
|
+ protected $type;
|
|
|
|
|
|
|
|
- public function display($refresh = false)
|
|
|
|
|
- {
|
|
|
|
|
- $this->addScript();
|
|
|
|
|
- $this->addStyle();
|
|
|
|
|
|
|
+ protected $view;
|
|
|
|
|
+
|
|
|
|
|
+ protected $options = [
|
|
|
|
|
+ // 是否刷新页面
|
|
|
|
|
+ 'refresh' => false,
|
|
|
|
|
+ ];
|
|
|
|
|
|
|
|
- $value = Helper::render($this->value);
|
|
|
|
|
|
|
+ public function display($options = [])
|
|
|
|
|
+ {
|
|
|
|
|
+ if (is_bool($options)) {
|
|
|
|
|
+ $options = ['refresh' => $options];
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- $label = __('admin.save');
|
|
|
|
|
|
|
+ $this->options = array_merge($this->options, $options);
|
|
|
|
|
|
|
|
- return <<<HTML
|
|
|
|
|
-<div class="d-inline">
|
|
|
|
|
- <span class="{$this->selector}" contenteditable="true">{$value}</span>
|
|
|
|
|
- <span class="save hidden"
|
|
|
|
|
- data-value="{$this->value}"
|
|
|
|
|
- data-name="{$this->column->getName()}"
|
|
|
|
|
- data-id="{$this->getKey()}"
|
|
|
|
|
- data-refresh="{$refresh}"
|
|
|
|
|
- data-url="{$this->getUrl()}">
|
|
|
|
|
- {$label}
|
|
|
|
|
- </span>
|
|
|
|
|
- <div class="d-none"></div>
|
|
|
|
|
-</div>
|
|
|
|
|
-HTML;
|
|
|
|
|
|
|
+ return admin_view($this->view, array_merge($this->variables(), $this->defaultOptions() + $this->options));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- protected function getUrl()
|
|
|
|
|
|
|
+ protected function defaultOptions()
|
|
|
{
|
|
{
|
|
|
- return $this->resource().'/'.$this->getKey();
|
|
|
|
|
|
|
+ return [];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- protected function addStyle()
|
|
|
|
|
|
|
+ public function variables()
|
|
|
{
|
|
{
|
|
|
- $color = Admin::color()->link();
|
|
|
|
|
- $primary = Admin::color()->primary();
|
|
|
|
|
-
|
|
|
|
|
- Admin::style(
|
|
|
|
|
- <<<CSS
|
|
|
|
|
-.{$this->selector}{border-bottom:dashed 1px $color;color: $color;display: inline-block; -webkit-user-modify: read-write-plaintext-only;}
|
|
|
|
|
-.{$this->selector}+.save{margin-left: 0.4rem;color: $color}
|
|
|
|
|
-body.dark-mode .{$this->selector}{color: $primary;border-color: $primary;}
|
|
|
|
|
-body.dark-mode .{$this->selector}+.save{color: $primary}
|
|
|
|
|
-CSS
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ return [
|
|
|
|
|
+ 'key' => $this->getKey(),
|
|
|
|
|
+ 'class' => $this->getSelector(),
|
|
|
|
|
+ 'type' => $this->type,
|
|
|
|
|
+ 'display' => Helper::render($this->value),
|
|
|
|
|
+ 'value' => $this->column->getOriginal(),
|
|
|
|
|
+ 'name' => $this->column->getName(),
|
|
|
|
|
+ 'url' => $this->getUrl(),
|
|
|
|
|
+ ];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- protected function addScript()
|
|
|
|
|
|
|
+ protected function getSelector()
|
|
|
{
|
|
{
|
|
|
- $script = <<<JS
|
|
|
|
|
-$(".{$this->selector}").on("click focus", function() {
|
|
|
|
|
- $(this).next().removeClass("hidden");
|
|
|
|
|
-}).on('blur', function () {
|
|
|
|
|
- var icon = $(this).next();
|
|
|
|
|
- setTimeout(function () {
|
|
|
|
|
- icon.addClass("hidden")
|
|
|
|
|
- }, 200)
|
|
|
|
|
-});
|
|
|
|
|
-$('.{$this->selector}+.save').on("click",function() {
|
|
|
|
|
- var obj = $(this),
|
|
|
|
|
- url = obj.data('url'),
|
|
|
|
|
- name = obj.data('name'),
|
|
|
|
|
- refresh = obj.data('refresh'),
|
|
|
|
|
- old_value = obj.data('value'),
|
|
|
|
|
- value = obj.prev().html(),
|
|
|
|
|
- tmp = obj.next();
|
|
|
|
|
-
|
|
|
|
|
- tmp.html(value);
|
|
|
|
|
-
|
|
|
|
|
- value = tmp.text().replace(new RegExp("<br>","g"), '').replace(new RegExp(" ","g"), '').trim();
|
|
|
|
|
-
|
|
|
|
|
- var data = {};
|
|
|
|
|
- if (name.indexOf('.') === -1) {
|
|
|
|
|
- data[name] = value;
|
|
|
|
|
- } else {
|
|
|
|
|
- name = name.split('.');
|
|
|
|
|
-
|
|
|
|
|
- data[name[0]] = {};
|
|
|
|
|
- data[name[0]][name[1]] = value;
|
|
|
|
|
|
|
+ return 'grid-editable-'.$this->type;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- Dcat.NP.start();
|
|
|
|
|
- $.put({
|
|
|
|
|
- url: url,
|
|
|
|
|
- data: data,
|
|
|
|
|
- success: function (d) {
|
|
|
|
|
- var msg = d.data.message || d.message;
|
|
|
|
|
- if (d.status) {
|
|
|
|
|
- obj.attr('data-value',value).addClass("hidden").prev().html(value);
|
|
|
|
|
- Dcat.success(msg);
|
|
|
|
|
-
|
|
|
|
|
- refresh && Dcat.reload()
|
|
|
|
|
- } else {
|
|
|
|
|
- obj.prev().html(old_value);
|
|
|
|
|
- Dcat.error(msg);
|
|
|
|
|
- }
|
|
|
|
|
- },
|
|
|
|
|
- error:function(a,b,c) {
|
|
|
|
|
- obj.prev().html(old_value);
|
|
|
|
|
- Dcat.handleAjaxError(a, b, c);
|
|
|
|
|
- },
|
|
|
|
|
- complete:function(a,b) {
|
|
|
|
|
- Dcat.NP.done();
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- return false;
|
|
|
|
|
-})
|
|
|
|
|
-JS;
|
|
|
|
|
|
|
|
|
|
- Admin::script($script);
|
|
|
|
|
|
|
+ protected function getUrl()
|
|
|
|
|
+ {
|
|
|
|
|
+ return $this->resource().'/'.$this->getKey();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|