Skip to content

Commit

Permalink
Merge pull request #548 from skipperbent/v3-development
Browse files Browse the repository at this point in the history
Version 3.18.0
  • Loading branch information
skipperbent committed Apr 16, 2024
2 parents d050005 + b3bed07 commit c2fa4a4
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 155 deletions.
378 changes: 236 additions & 142 deletions resources/js/pecee-widget.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/ArrayUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public static function filter(array $array, ?callable $callback = null): array
return array_filter($array, $callback);
}

/**
* Remove empty and null values from array but allows false and 0.
*
* @param array $array
* @return array
*/
public static function clean(array $array): array
{
return array_filter($array, static function ($value) {
return ($value !== null && $value !== '');
});
}

public static function serialize(array $input): array
{
$output = [];
Expand Down
6 changes: 5 additions & 1 deletion src/Model/ModelQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Pecee\Model;

use Carbon\Carbon;
use Pecee\ArrayUtil;
use Pecee\Model\Collections\ModelCollection;
use Pecee\Model\Exceptions\ModelException;
use Pecee\Model\Exceptions\ModelNotFoundException;
Expand Down Expand Up @@ -43,7 +44,7 @@ public function __construct(Model $model, ?Connection $connection = null)
protected function createInstance(\stdClass $item)
{
$model = $this->model->onNewInstance($item);
$model->mergeRows((array)$item);
$model->mergeRows(ArrayUtil::clean((array)$item));
$model->setOriginalRows((array)$item);
$model->onInstanceCreate();

Expand Down Expand Up @@ -618,6 +619,8 @@ public function firstOrCreate(array $data = [])
$item = $this->createInstance((object)$data);
$item->setOriginalRows([]);
$item->save();
} else {
$item->mergeRows(ArrayUtil::clean($data));
}

return $item;
Expand All @@ -633,6 +636,7 @@ public function firstOrNew(array $data = [])
$item = $this->first();

if ($item !== null) {
$item->mergeRows(ArrayUtil::clean($data));
return $item;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Model/ModelRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ public function withEmpty(bool $empty = true): self

public function getDefaultFor(Model $parent)
{
$instance = $parent->newQuery();

if ($this->withDefault === null && $this->returnEmpty === true) {
return null;
}

$instance = $parent->newQuery();

if ($this->withDefault === null) {
return $instance;
}
Expand Down
13 changes: 13 additions & 0 deletions src/Model/Relation/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ public function addConstraints()
}
}

public function getDefaultFor(Model $parent)
{
if ($this->withDefault === null && $this->returnEmpty === true) {
return null;
}

if ($this->withDefault === null) {
return new ModelCollection();
}

return parent::getDefaultFor($parent);
}

/**
* @return Model|ModelCollection
* @throws \Pecee\Pixie\Exception
Expand Down
16 changes: 13 additions & 3 deletions src/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

class Session
{
public static string $name = 'pecee_session';
private static bool $active = false;
public static array $initialData = [];

public static function start(string $name = 'pecee_session'): void
public static function start(): void
{
if (static::$active === false) {
session_name($name);
session_name(static::$name);
session_start();
static::$active = true;
static::$initialData = $_SESSION;
Expand Down Expand Up @@ -40,6 +41,11 @@ public static function exists(string $id): bool
*/
public static function set(string $id, $value): void
{

if (static::$active === false) {
static::start();
}

$data = [
serialize($value),
app()->getSecret(),
Expand All @@ -52,6 +58,10 @@ public static function set(string $id, $value): void

public static function get($id, $defaultValue = null)
{
if (static::$active === false) {
static::start();
}

if (static::exists($id) === true) {

$value = $_SESSION[$id];
Expand All @@ -61,7 +71,7 @@ public static function get($id, $defaultValue = null)
$value = Guid::decrypt(app()->getSecret(), $value);
$data = explode('|', $value);

if (\is_array($data) && trim(end($data)) === app()->getSecret()) {
if (is_array($data) && trim(end($data)) === app()->getSecret()) {
return unserialize($data[0], ['allowed_classes' => true]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/UI/Taglib/Taglib.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function requireAttributes(\stdClass $attrs, array $names): void

public function getBody(): string
{
return $this->body;
return (string)$this->body;
}

public function setBody(string $body): void
Expand Down
12 changes: 6 additions & 6 deletions src/UI/Taglib/TaglibJs.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function parseJsTriggers(string $string): string
$event = $matches[1];
$callback = $matches[2];

return sprintf('on%1$s="return </%2$s>"; o+= t.addEvent({ event: "%1$s", viewId: viewId, view: view, index: this.index, callback: function(e) { %3$s } }) + "\"<%2$s>',
return sprintf('on%1$s="return </%2$s>"; o+= t.addEvent("%1$s", (e) => { %3$s }, viewId, view, this.index) + "\"<%2$s>',
$event,
static::$JS_WRAPPER_TAG,
$callback,
Expand Down Expand Up @@ -126,7 +126,7 @@ protected function handleInline(string $string): string
$result .= ($i == (count($parts) - 1)) ? 'return ' . $parts[$i] . ';' : $parts[$i] . ';';
}

return sprintf('(function(){%s})()', $result);
return sprintf('(()=>{%s})()', $result);
}

protected function replaceJsExpressions(string $string): string
Expand Down Expand Up @@ -206,7 +206,7 @@ protected function tagTemplate(\stdClass $attrs): void
$dataOutput = "$as = Object.assign($json, $as);";
}

$output = sprintf('$.%1$s = new %4$s.template(); $.%1$s.view = function(_d,g,w,viewId = null, view = null){ let t = w.template; let %5$s=_d; ' . $dataOutput . ' var o="<%3$s>%2$s</%3$s>"; return o;};',
$output = sprintf('$.%1$s = new %4$s.template(); $.%1$s.view = (_d,g,w,viewId = null, view = null) => { let t = w.template; let %5$s=_d; ' . $dataOutput . ' var o="<%3$s>%2$s</%3$s>"; return o;};',
$attrs->id,
$this->makeJsString($this->getBody()),
static::$JS_WRAPPER_TAG,
Expand Down Expand Up @@ -290,7 +290,7 @@ protected function tagRenderWidget(\stdClass $attrs): string

$class = isset($attrs->class) ? ' class=\"' . $attrs->class . '\"' : '';

return sprintf('</%1$s>"; var _w=%2$s; o+= "<div data-id=\"iw_" + _w.guid + "\"%3$s>"; _w.setContainer("div[data-id=iw_" + _w.guid + "]"); if(view !== null) { w.template.one(view.id, function() { _w.render(); widgets.clean(); }); } else { w.one("render", function() { _w.trigger("render"); widgets.clean(); }); } o+= _w.render(false, false); o += "</div><%1$s>',
return sprintf('</%1$s>"; var _w=%2$s; o+= "<div data-id=\"iw_" + _w.guid + "\"%3$s>"; _w.setContainer("div[data-id=iw_" + _w.guid + "]"); if(view !== null) { w.template.one(view.id, () => { _w.render(); widgets.clean(); }); } else { w.one("render", () => { _w.trigger("render"); widgets.clean(); }); } o+= _w.render(false, false); o += "</div><%1$s>',
static::$JS_WRAPPER_TAG,
$attrs->widget,
$class,
Expand Down Expand Up @@ -414,9 +414,9 @@ protected function tagView(\stdClass $attrs): string
$morph = (isset($attrs->morph) && strtolower($attrs->morph) === 'false') ? 'false' : 'true';
$hiddenHtml1 = ($hidden === 'true') ? '' : 'o += "<%2$s data-id=\"%9$s_%1$s' . $index . '\">" + ';
$hiddenHtml2 = ($hidden === 'true') ? ';' : ' + "</%3$s>";';
$morphHtml = ($morph === 'false') ? 'document.querySelectorAll(w.container + " [data-id=\"%9$s_%1$s' . $index . '\"]").forEach(i => i.innerHTML = o);' : 'document.querySelectorAll(w.container + " [data-id=\"%9$s_%1$s' . $index . '\"]").forEach(function(item) { let clone = item.cloneNode(true); clone.innerHTML=o; morphdom(item, clone); });';
$morphHtml = ($morph === 'false') ? 'document.querySelectorAll(w.container + " [data-id=\"%9$s_%1$s' . $index . '\"]").forEach(i => i.innerHTML = o);' : 'document.querySelectorAll(w.container + " [data-id=\"%9$s_%1$s' . $index . '\"]").forEach((item) => { let clone = item.cloneNode(true); clone.innerHTML=o; morphdom(item, clone); });';

return sprintf('</%6$s>"; ' . $hiddenHtml1 . ' t.addView({id: ' . $id . ', index: ' . ($attrs->index ?? 'null') . ', el: "%9$s_%1$s' . $index . '", hash: "%9$s", callback: function(%7$s, viewId = ' . $id . ', view = this, replace = true){ if(replace===false) { widgets.getViews(w.guid, this.id).find((v => this.index !== null && v.index === this.index || v.index === null && v.hash === this.hash)).triggers = []; } var _vid=this.id; var o="%5$s"; if(replace===true) { ' . $morphHtml . ' } else { if(typeof this.viewId === "undefined") { w.one("render", function() { w.template.triggerEvent(_vid, %7$s); }); } w.template.one("render", function() { w.template.triggerEvent(_vid, %7$s); }); } return o; }, data: %4$s, hidden: %8$s})' . $hiddenHtml2 . ' o+="<%6$s>',
return sprintf('</%6$s>"; ' . $hiddenHtml1 . ' t.addView({id: ' . $id . ', index: ' . ($attrs->index ?? 'null') . ', el: "%9$s_%1$s' . $index . '", hash: "%9$s", callback: function(%7$s, viewId = ' . $id . ', view = this, replace = true){ if(replace===false) { widgets.getViews(w.guid, this.id).find((v => this.index !== null && v.index === this.index || v.index === null && v.hash === this.hash)).triggers = []; } var _vid=this.id; var o="%5$s"; if(replace===true) { ' . $morphHtml . ' } else { if(typeof this.viewId === "undefined") { w.one("render", () => { w.template.triggerEvent(_vid, %7$s); }); } w.template.one("render", () => { w.template.triggerEvent(_vid, %7$s); }); } return o; }, data: %4$s, hidden: %8$s})' . $hiddenHtml2 . ' o+="<%6$s>',
$attrs->name,
$elStartTag,
$elEndTag,
Expand Down

0 comments on commit c2fa4a4

Please sign in to comment.