* zin: refactor avatar.

This commit is contained in:
陈涛
2023-02-21 17:59:24 +08:00
parent 703f585ebe
commit 882e904170
7 changed files with 126 additions and 118 deletions
+2 -2
View File
@@ -722,7 +722,7 @@ class commonModel extends model
*
* @static
* @access public
* @return void
* @return array
*/
public static function printVisionSwitcherZin()
{
@@ -773,7 +773,7 @@ class commonModel extends model
*
* @static
* @access public
* @return void
* @return array
*/
public static function printCreateListZin()
{
+3
View File
@@ -40,3 +40,6 @@ function forminput() {return createWg('forminput', func_get_args());}
function dropdown() {return createWg('dropdown', func_get_args());}
function pageheader() {return createWg('pageheader', func_get_args());}
function pageheading() {return createWg('pageheading', func_get_args());}
function pagenavbar() {return createWg('pagenavbar', func_get_args());}
function pagetoolbar() {return createWg('pagetoolbar', func_get_args());}
function avatar() {return createWg('avatar', func_get_args());}
+55 -74
View File
@@ -1,95 +1,76 @@
<?php
namespace zin\wg;
namespace zin;
use \zin\core\h5;
require_once dirname(dirname(__DIR__)) . DS . 'core' . DS . 'wg.class.php';
require_once dirname(dirname(__DIR__)) . DS . 'core' . DS . 'h5.class.php';
require_once dirname(__DIR__) . DS . 'icon' . DS . 'v1.php';
class avatar extends \zin\core\wg
class avatar extends wg
{
static $tag = 'div';
static $defaultProps = array(
'class' => 'userMenu dropdown-toggle',
'data-arrow' => true,
'data-toggle' => 'dropdown',
'data-trigger' => 'hover',
'data-placement' => 'bottom',
'href' => '#userMenu',
'theme' => 'success',
'radius' => 'circle',
'size' => '',
'outline' => '',
'showName' => false,
protected static $defineProps = array
(
'class?:string="userMenu dropdown-toggle"',
'data-arrow?:string="true"',
'data-toggle?:string="dropdown"',
'data-trigger?:string="hover"',
'data-placement?:string="bottom"',
'href?:string="#userMenu"',
'theme?:string="success"',
'radius?:string="circle"',
'size?:string',
'outline?:string',
'trigger?:string',
'showName' => array
(
'type' => 'bool',
'default' => false
)
);
static $customProps = 'text,theme,radius,size,outline,name,role';
private $skipProps = array('theme', 'radius', 'showName', 'outline', 'size', 'avatar', 'name', 'trigger');
private $skipTriggerProps = array('data-arrow', 'data-toggle', 'data-placement', 'data-trigger', 'href');
static function create($props)
protected function onAddChild($child)
{
$avatar = new avatar();
foreach($props as $key => $value) $avatar->prop($key, $value);
return $avatar;
}
protected function acceptChild($child, $strAsHtml = false)
{
$child = parent::acceptChild($child, $strAsHtml);
if(!$strAsHtml && is_string($child) && !$this->props->has('name'))
if(is_string($child) && !$this->props->has('name'))
{
$this->prop('name', strtoupper($child[0]));
return NULL;
$this->setProp('name', $child);
return false;
}
return $child;
}
protected function build($isPrint = false, $parent = NULL)
protected function build()
{
$builder = parent::build($isPrint, $parent);
$name = strtoupper($this->prop('name')[0]);
$name = $this->prop('name');
$avatar = $this->prop('avatar');
if(empty($name) and empty($avatar)) return $builder;
/* Without name and avatar url. */
if(empty($name) and empty($avatar)) return null;
/* Strig avatar. */
if(!empty($avatar))
{
/* Image avatar. */
$div = h5::div(h5::img(\zin\set('src', $this->prop('avatar'))))->addClass('avatar');
if(!empty($this->prop('radius'))) $div->addClass($this->prop('radius'));
if(!empty($this->prop('theme'))) $div->addClass($this->prop('theme'));
if(!empty($this->prop('outline'))) $div->addClass($this->prop('outline') . '-outline');
if(!empty($this->prop('size'))) $div->addClass($this->prop('size'));
$builder->append($div);
$radius = $this->prop('radius');
$theme = $this->prop('theme');
$outline = $this->prop('outline');
$size = $this->prop('size');
$role = $this->prop('role');
$showName = $this->prop('showName');
$trigger = $this->prop('trigger');
/* Show name and role. */
if(!empty($this->prop('showName')))
{
$builder->append(h5::span($name));
if(!empty($this->prop('role'))) $builder->append(h5::span($this->prop('role')));
}
$skipProps = !empty($trigger) ? $this->skipProps : array_merge($this->skipProps, $this->skipTriggerProps);
if(!empty($trigger)) $this->setProp('href', $trigger);
return $builder;
}
$div = h5::div($name)->addClass('avatar');
if(!empty($this->prop('radius'))) $div->addClass($this->prop('radius'));
if(!empty($this->prop('theme'))) $div->addClass($this->prop('theme'));
if(!empty($this->prop('outline'))) $div->addClass($this->prop('outline') . '-outline');
if(!empty($this->prop('size'))) $div->addClass($this->prop('size'));
$builder->append($div);
if(!empty($this->prop('showName')))
{
$builder->append(h5::span($name));
if(!empty($this->prop('role'))) $builder->append(h5::span($this->prop('role')));
}
return $builder;
return h::div
(
set($this->props->skip($skipProps)),
h::div
(
setClass('avatar'),
empty($radius) ? null : setClass($radius),
empty($theme) ? null : setClass($theme),
empty($outline) ? null : setClass($outline . '-outline'),
empty($size) ? null : setClass($size),
!empty($avatar) ? h::img(set('src', $avatar)) : strtoupper(mb_substr($name, 0, 1))
),
empty($showName) ? null : h::span($name),
empty($showName) or empty($role) ? null : h::span($role)
);
}
}
+18 -4
View File
@@ -3,11 +3,13 @@ namespace zin;
class dropdown extends wg
{
static $tag = 'menu';
protected static $defineProps = array
(
'class?:string="dropdown-menu menu"',
'id?:string'
);
static $defaultProps = array('class' => 'dropdown-menu menu');
static $customProps = 'items';
private $skipProps = array('items');
protected function buildItem($item)
{
@@ -65,4 +67,16 @@ class dropdown extends wg
return $li;
}
protected function build()
{
$items = $this->prop('items');
return h::menu
(
set($this->props->skip($this->skipProps)),
is_array($items) ? array_map(array($this, 'buildItem'), $items) : null,
$this->children()
);
}
}
+2
View File
@@ -19,6 +19,8 @@ class pageheading extends wg
$this->props->set('text', $child);
return false;
}
return $child;
}
/**
+3 -4
View File
@@ -1,9 +1,8 @@
<?php
namespace zin;
class pagenavbar extends wg
{
static $tag = 'nav';
require_once dirname(__DIR__) . DS . 'nav' . DS . 'v1.php';
static $defaultProps = array('id' => 'navbar');
class pagenavbar extends nav
{
}
+43 -34
View File
@@ -1,49 +1,58 @@
<?php
namespace zin;
use zin\core\h5;
use function zin\Icon;
require_once dirname(dirname(__DIR__)) . DS . 'core' . DS . 'wg.class.php';
require_once dirname(__DIR__) . DS . 'avatar' . DS . 'v1.php';
class pagetoolbar extends wg
{
static $tag = 'div';
static $defaultProps = array('id' => 'toolbar');
static $customProps = 'globalCreate,avatar,switcher';
private function buildGlobalCreate($props)
private function buildGlobalCreate()
{
$div = h::create('div', NULL, $props);
$div->append(h::div(Icon('plus'))->addClass('rounded-sm btn square size-sm secondary'));
$div->addClass('globalGreate');
$props = $this->prop('create');
return $div;
if(!isset($props['data-arrow'])) $props['data-arrow'] = true;
if(!isset($props['data-toggle'])) $props['data-toggle'] = 'dropdown';
if(!isset($props['data-trigger'])) $props['data-trigger'] = 'hover';
if(!isset($props['href'])) $props['href'] = '#globalCreateMenu';
return h::div
(
setClass('globalCreate'),
h::div
(
set($props),
setClass('rounded-sm btn square size-sm secondary'),
icon('plus')
)
);
}
private function buildAvatar($props)
private function buildSwitcher()
{
return Avatar::create($props);
$props = $this->prop('switcher');
if(!isset($props['data-arrow'])) $props['data-arrow'] = true;
if(!isset($props['data-toggle'])) $props['data-toggle'] = 'dropdown';
if(!isset($props['data-trigger'])) $props['data-trigger'] = 'hover';
if(!isset($props['href'])) $props['href'] = '#globalCreateMenu';
return h::div
(
setClass('vision-switcher'),
h::div
(
set($props),
setClass('switcher-text'),
$props['text']
)
);
}
private function buildSwitcher($props)
protected function build()
{
$div = h::create('div', NULL, $props);
$div->append(h::div($props['text'])->addClass('switcher-text'));
$div->addClass('vision-switcher');
return $div;
}
protected function build($isPrint = false, $parent = null)
{
$builder = parent::build($isPrint, $parent);
$builder->append($this->buildGlobalCreate($this->prop('globalCreate')));
$builder->append($this->buildAvatar($this->prop('avatar')));
$builder->append($this->buildSwitcher($this->prop('switcher')));
return $builder;
return h::div
(
setId('toolbar'),
$this->buildGlobalCreate(),
$this->block('avatar'),
$this->buildSwitcher()
);
}
}