From 4fa96a5119becd4fe1fd25f9948bf0a980a1c28a Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Sun, 9 Apr 2017 21:00:19 +0100 Subject: [PATCH] Refactoring the Link Helper class --- helpers.php | 64 ++-- src/Helpers/UI/Link.php | 628 +++++++++++++++++++--------------- tests/Helpers/UI/LinkTest.php | 58 ++-- 3 files changed, 405 insertions(+), 345 deletions(-) diff --git a/helpers.php b/helpers.php index a6be0bd..1eae54f 100644 --- a/helpers.php +++ b/helpers.php @@ -108,13 +108,12 @@ function label_trashed_status(array $options = [], $withIcon = true) { * @param bool $active * @param string $url * @param array $attributes - * @param bool $withTooltip * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return Link */ - function link_activate_icon($active, $url, array $attributes = [], $withTooltip = true, $disabled = false) { - return Link::activateIcon($active, $url, $attributes, $withTooltip, $disabled); + function link_activate_icon($active, $url, array $attributes = [], $disabled = false) { + return Link::activateIcon($active, $url, $attributes, $disabled); } } @@ -125,13 +124,12 @@ function link_activate_icon($active, $url, array $attributes = [], $withTooltip * @param bool $active * @param string $url * @param array $attributes - * @param bool $withTooltip * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return Link */ - function link_activate_modal_icon($active, $url, array $attributes = [], $withTooltip = true, $disabled = false) { - return Link::activateModalIcon($active, $url, $attributes, $withTooltip, $disabled); + function link_activate_modal_icon($active, $url, array $attributes = [], $disabled = false) { + return Link::activateModalIcon($active, $url, $attributes, $disabled); } } @@ -141,13 +139,12 @@ function link_activate_modal_icon($active, $url, array $attributes = [], $withTo * * @param string $url * @param array $attributes - * @param bool $withTooltip * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return Link */ - function link_add_icon($url, array $attributes = [], $withTooltip = true, $disabled = false) { - return Link::addIcon($url, $attributes, $withTooltip, $disabled); + function link_add_icon($url, array $attributes = [], $disabled = false) { + return Link::addIcon($url, $attributes, $disabled); } } @@ -157,13 +154,12 @@ function link_add_icon($url, array $attributes = [], $withTooltip = true, $disab * * @param string $url * @param array $attributes - * @param bool $withTooltip * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return Link */ - function link_delete_modal_icon($url, array $attributes = [], $withTooltip = true, $disabled = false) { - return Link::deleteModalIcon($url, $attributes, $withTooltip, $disabled); + function link_delete_modal_icon($url, array $attributes = [], $disabled = false) { + return Link::deleteModalIcon($url, $attributes, $disabled); } } @@ -175,7 +171,7 @@ function link_delete_modal_icon($url, array $attributes = [], $withTooltip = tru * @param array $attributes * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return Link */ function link_delete_modal_with_icon($url, array $attributes = [], $disabled = false) { return Link::deleteModalWithIcon($url, $attributes, $disabled); @@ -191,7 +187,7 @@ function link_delete_modal_with_icon($url, array $attributes = [], $disabled = f * @param array $attributes * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return Link */ function link_activate_modal_with_icon($active, $url, array $attributes = [], $disabled = false) { return Link::activateModalWithIcon($active, $url, $attributes, $disabled); @@ -204,13 +200,12 @@ function link_activate_modal_with_icon($active, $url, array $attributes = [], $d * * @param string $url * @param array $attributes - * @param bool $withTooltip * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return Link */ - function link_detach_modal_icon($url, array $attributes = [], $withTooltip = true, $disabled = false) { - return Link::detachModalIcon($url, $attributes, $withTooltip, $disabled); + function link_detach_modal_icon($url, array $attributes = [], $disabled = false) { + return Link::detachModalIcon($url, $attributes, $disabled); } } @@ -220,13 +215,12 @@ function link_detach_modal_icon($url, array $attributes = [], $withTooltip = tru * * @param string $url * @param array $attributes - * @param bool $withTooltip * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return Link */ - function link_edit_icon($url, array $attributes = [], $withTooltip = true, $disabled = false) { - return Link::editIcon($url, $attributes, $withTooltip, $disabled); + function link_edit_icon($url, array $attributes = [], $disabled = false) { + return Link::editIcon($url, $attributes, $disabled); } } @@ -238,7 +232,7 @@ function link_edit_icon($url, array $attributes = [], $withTooltip = true, $disa * @param array $attributes * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return Link */ function link_edit_with_icon($url, array $attributes = [], $disabled = false) { return Link::editWithIcon($url, $attributes, $disabled); @@ -251,13 +245,12 @@ function link_edit_with_icon($url, array $attributes = [], $disabled = false) { * * @param string $url * @param array $attributes - * @param bool $withTooltip * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return Link */ - function link_restore_modal_icon($url, array $attributes = [], $withTooltip = true, $disabled = false) { - return Link::restoreModalIcon($url, $attributes, $withTooltip, $disabled); + function link_restore_modal_icon($url, array $attributes = [], $disabled = false) { + return Link::restoreModalIcon($url, $attributes, $disabled); } } @@ -269,7 +262,7 @@ function link_restore_modal_icon($url, array $attributes = [], $withTooltip = tr * @param array $attributes * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return Link */ function link_restore_modal_with_icon($url, array $attributes = [], $disabled = false) { return Link::restoreModalWithIcon($url, $attributes, $disabled); @@ -282,12 +275,11 @@ function link_restore_modal_with_icon($url, array $attributes = [], $disabled = * * @param string $url * @param array $attributes - * @param bool $withTooltip * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return Link */ - function link_show_icon($url, array $attributes = [], $withTooltip = true, $disabled = false) { - return Link::showIcon($url, $attributes, $withTooltip, $disabled); + function link_show_icon($url, array $attributes = [], $disabled = false) { + return Link::showIcon($url, $attributes, $disabled); } } diff --git a/src/Helpers/UI/Link.php b/src/Helpers/UI/Link.php index 57937ff..fedde8a 100644 --- a/src/Helpers/UI/Link.php +++ b/src/Helpers/UI/Link.php @@ -1,7 +1,7 @@ disabled = $disabled; + $this->action = $action; + $this->url = $url; + + $this->setAttributes($attributes); + } + + /* ----------------------------------------------------------------- + | Getters & Setters + | ----------------------------------------------------------------- + */ + /** + * Get the action title. * - * @return \Illuminate\Support\HtmlString + * @return string */ - public static function activateIcon($active, $url, array $attributes = [], $withTooltip = true, $disabled = false) + protected function getActionTitle() { - // TODO: Refactor the options to a dedicated config file. - $classes = [ - 'enable' => 'btn btn-xs btn-success', - 'disable' => 'btn btn-xs btn-inverse', - ]; - $trans = [ - 'enable' => 'core::actions.enable', - 'disable' => 'core::actions.disable', - ]; - $icons = [ - 'enable' => 'fa fa-fw fa-power-off', - 'disable' => 'fa fa-fw fa-power-off', - ]; + return Str::ucfirst(trans("core::actions.{$this->action}")); + } + + /** + * Set the attributes. + * + * @param array $attributes + * + * @return self + */ + public function setAttributes(array $attributes) + { + $this->attributes = collect($attributes); - $key = $active ? 'enable' : 'disable'; - $value = ''; + if ($this->disabled) + $this->cleanAttributes(); - $attributes['class'] = $classes[$key]; + return $this; + } - if ($withTooltip) { - $attributes['data-toggle'] = 'tooltip'; - $attributes['data-original-title'] = Str::ucfirst(trans($trans[$key])); - } + /** + * Set an attribute. + * + * @param string $key + * @param mixed $value + * + * @return self + */ + public function setAttribute($key, $value) + { + $this->attributes->put($key, $value); - return $disabled - ? static::generateDisabledForIcons($value, $attributes) - : static::generate($url, $value, $attributes); + return $this; + } + + protected function cleanAttributes() + { + $this->attributes = $this->attributes->reject(function ($value, $key) { + return Str::startsWith($key, ['data-']); + }); + + return $this; } /** - * Generate activate icon link for modals (reverse button based on active state). + * Set the size. * - * @param bool $active - * @param string $url - * @param array $attributes - * @param bool $withTooltip - * @param bool $disabled + * @param string $size * - * @return \Illuminate\Support\HtmlString + * @return self */ - public static function activateModalIcon($active, $url, array $attributes = [], $withTooltip = true, $disabled = false) + public function size($size) { - // TODO: Refactor the options to a dedicated config file. - $dataAttribute = 'data-current-status'; - $statuses = [ - 'enabled' => 'enabled', - 'disabled' => 'disabled', - ]; + $this->size = $size; + + return $this; + } + + /** + * @param bool $withTitle + * + * @return self + */ + public function withTitle($withTitle) + { + $this->withTitle = $withTitle; + + return $this; + } - if ($disabled) - $attributes = static::cleanAttributes($attributes); + /** + * Show/Hide the icon. + * + * @var bool $icon + * + * @return self + */ + public function icon($withIcon) + { + $this->withIcon = $withIcon; - $key = $active ? 'enabled' : 'disabled'; + return $this; + } - $attributes = array_merge($attributes, [ - $dataAttribute => $statuses[$key], - ]); + /** + * Enable the tooltip. + * + * @param bool $withTooltip + * + * @return self + */ + public function tooltip($withTooltip) + { + $this->withTooltip = $withTooltip; - return static::activateIcon( ! $active, $url, $attributes, $withTooltip, $disabled); + return $this; } /** - * Generate activate link with icon for modals (reverse button based on active state). + * Show only the icon and the title as tooltip. * - * @param bool $active + * @return self + */ + public function onlyIcon() + { + return $this->icon(true)->tooltip(true); + } + + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- + */ + /** + * @param string $action * @param string $url * @param array $attributes * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return self */ - public static function activateModalWithIcon($active, $url, array $attributes = [], $disabled = false) + public static function make($action, $url, array $attributes = [], $disabled = false) { - // TODO: Refactor the options to a dedicated config file. - $icons = [ - 'enabled' => 'fa fa-fw fa-power-off', - 'disabled' => 'fa fa-fw fa-power-off', - ]; - $classes = [ - 'enabled' => 'btn btn-sm btn-success', - 'disabled' => 'btn btn-sm btn-inverse', - ]; - $trans = [ - 'enabled' => 'core::actions.enable', - 'disabled' => 'core::actions.disable', - ]; + return new static($action, $url, $attributes, $disabled); + } - $key = $active ? 'enabled' : 'disabled'; + /* ----------------------------------------------------------------- + | Other Functions + | ----------------------------------------------------------------- + */ + /** + * Get content as a string of HTML. + * + * @return string + */ + public function toHtml() + { + return 'renderAttributes().'>'.$this->renderValue().''; + } - $attributes['class'] = $classes[$key]; + /** + * Render the link value. + * + * @return string + */ + protected function renderValue() + { + return ($this->withTooltip || ! $this->withTitle) + ? $this->renderIcon() + : $this->renderIcon().' '.$this->getActionTitle(); + } - $value = ' '.Str::ucfirst(trans($trans[$key])); + /** + * Render the icon. + * + * @return string + */ + protected function renderIcon() + { + $icon = Arr::get([ + 'add' => 'fa fa-fw fa-plus', + 'delete' => 'fa fa-fw fa-trash-o', + 'detach' => 'fa fa-fw fa-chain-broken', + 'disable' => 'fa fa-fw fa-power-off', + 'edit' => 'fa fa-fw fa-pencil', + 'enable' => 'fa fa-fw fa-power-off', + 'restore' => 'fa fa-fw fa-reply', + 'show' => 'fa fa-fw fa-search', + 'update' => 'fa fa-fw fa-pencil', + ], $this->action); - return $disabled - ? static::generateDisabled($value, $attributes) - : static::generate($url, $value, $attributes); + return ''; } /** - * Generate add icon link. + * Render the attributes. * - * @param string $url - * @param array $attributes - * @param bool $withTooltip - * @param bool $disabled - * - * @return \Illuminate\Support\HtmlString + * @return string */ - public static function addIcon($url, array $attributes = [], $withTooltip = true, $disabled = false) + protected function renderAttributes() { - // TODO: Refactor the options to a dedicated config file. - $class = 'btn btn-xs btn-primary'; - $icon = 'fa fa-fw fa-plus'; + $attributes = collect(); + $attributes->put('href', $this->disabled ? 'javascript:void(0);' : $this->url); + $attributes->put('class', $this->getLinkClass()); - $attributes['class'] = $class; + if ($this->withTooltip) { + $attributes->put('data-toggle', 'tooltip'); + $attributes->put('data-original-title', $this->getActionTitle()); + } - if ($withTooltip) { - $attributes['data-toggle'] = 'tooltip'; - $attributes['data-original-title'] = Str::ucfirst(trans('core::actions.add')); + if ($this->disabled) { + $attributes->put('disabled', 'disabled'); } - $value = ''; + $attributes = $attributes->merge($this->attributes); - return $disabled - ? static::generateDisabledForIcons($value, $attributes) - : static::generate($url, $value, $attributes); + return html()->attributes($attributes->toArray()); } /** - * Generate delete icon link for modals. + * Get the link class. + * + * @return string + */ + protected function getLinkClass() + { + return implode(' ', array_filter(['btn', $this->getLinkSize(), $this->getLinkColor()])); + } + + protected function getLinkColor() + { + if ($this->disabled) + return 'btn-default'; + + return Arr::get([ + 'add' => 'btn-primary', + 'delete' => 'btn-danger', + 'detach' => 'btn-danger', + 'disable' => 'btn-inverse', + 'edit' => 'btn-warning', + 'enable' => 'btn-success', + 'restore' => 'btn-primary', + 'show' => 'btn-info', + 'update' => 'btn-warning', + ], $this->action); + } + + protected function getLinkSize() + { + return Arr::get([ + 'lg' => 'btn-lg', + 'md' => '', + 'sm' => 'btn-sm', + 'xs' => 'btn-xs', + ], $this->size); + } + + /* ----------------------------------------------------------------- + | Helpers Methods + | ----------------------------------------------------------------- + */ + /** + * Generate activate icon link. * + * @param bool $active * @param string $url * @param array $attributes - * @param bool $withTooltip * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return self */ - public static function deleteModalIcon($url, array $attributes = [], $withTooltip = true, $disabled = false) + public static function activateIcon($active, $url, array $attributes = [], $disabled = false) { - // TODO: Refactor the options to a dedicated config file. - $icon = 'fa fa-fw fa-trash-o'; - $class = 'btn btn-xs btn-danger'; - $trans = 'core::actions.delete'; - - $value = ''; - $attributes['class'] = $class; - - if ($disabled) - $attributes = static::cleanAttributes($attributes); - - if ($withTooltip) { - $attributes['data-toggle'] = 'tooltip'; - $attributes['data-original-title'] = Str::ucfirst(trans($trans)); - } - - return $disabled - ? static::generateDisabledForIcons($value, $attributes) - : static::generate($url, $value, $attributes); + return self::make($active ? 'enable' : 'disable', $url, $attributes, $disabled) + ->size('xs') + ->withTitle(false) + ->icon(true); } /** - * Generate delete link with icon for modals. + * Generate activate icon link for modals (reverse button based on active state). * + * @param bool $active * @param string $url * @param array $attributes * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return self */ - public static function deleteModalWithIcon($url, array $attributes = [], $disabled = false) + public static function activateModalIcon($active, $url, array $attributes = [], $disabled = false) { - // TODO: Refactor the options to a dedicated config file. - $icon = 'fa fa-fw fa-trash-o'; - $class = 'btn btn-sm btn-danger'; - $trans = 'core::actions.delete'; - - $attributes['class'] = $class; - - $value = ' '.Str::ucfirst(trans($trans)); + $dataAttribute = 'data-current-status'; + $statuses = [ + 'enabled' => 'enabled', + 'disabled' => 'disabled', + ]; - return $disabled - ? static::generateDisabled($value, $attributes) - : static::generate($url, $value, $attributes); + return static::activateIcon( ! $active, $url, $attributes, $disabled) + ->setAttribute($dataAttribute, $statuses[$active ? 'enabled' : 'disabled']); } /** - * Generate detach icon link for modals. + * Generate activate link with icon for modals (reverse button based on active state). * + * @param bool $active * @param string $url * @param array $attributes - * @param bool $withTooltip * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return self */ - public static function detachModalIcon($url, array $attributes = [], $withTooltip = true, $disabled = false) + public static function activateModalWithIcon($active, $url, array $attributes = [], $disabled = false) { - // TODO: Refactor the options to a dedicated config file. - $icon = 'fa fa-fw fa-chain-broken'; - $class = 'btn btn-xs btn-danger'; - $trans = 'core::actions.detach'; - - $value = ''; - $attributes['class'] = $class; - - if ($disabled) - $attributes = static::cleanAttributes($attributes); - - if ($withTooltip) { - $attributes['data-toggle'] = 'tooltip'; - $attributes['data-original-title'] = Str::ucfirst(trans($trans)); - } + $dataAttribute = 'data-current-status'; + $statuses = [ + 'enabled' => 'enabled', + 'disabled' => 'disabled', + ]; - return $disabled - ? static::generateDisabledForIcons($value, $attributes) - : static::generate($url, $value, $attributes); + return static::activateIcon( ! $active, $url, $attributes, $disabled) + ->setAttribute($dataAttribute, $statuses[$active ? 'enabled' : 'disabled']) + ->size('sm') + ->withTitle(true) + ->tooltip(false); } /** - * Generate edit icon link. + * Generate add icon link. * * @param string $url * @param array $attributes - * @param bool $withTooltip * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return self */ - public static function editIcon($url, array $attributes = [], $withTooltip = true, $disabled = false) + public static function addIcon($url, array $attributes = [], $disabled = false) { - // TODO: Refactor the options to a dedicated config file. - $icon = 'fa fa-fw fa-pencil'; - $class = 'btn btn-xs btn-warning'; - - $attributes['class'] = $class; - - if ($withTooltip) { - $attributes['data-toggle'] = 'tooltip'; - $attributes['data-original-title'] = Str::ucfirst(trans('core::actions.edit')); - } - - $value = ''; - - return $disabled - ? static::generateDisabledForIcons($value, $attributes) - : static::generate($url, $value, $attributes); + return self::make('add', $url, $attributes, $disabled) + ->size('xs') + ->withTitle(false); } /** - * Generate edit link with icon. + * Generate delete icon link for modals. * * @param string $url * @param array $attributes * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return self */ - public static function editWithIcon($url, array $attributes = [], $disabled = false) + public static function deleteModalIcon($url, array $attributes = [], $disabled = false) { - // TODO: Refactor the options to a dedicated config file. - $icon = 'fa fa-fw fa-pencil'; - $class = 'btn btn-sm btn-warning'; - $trans = 'core::actions.edit'; - - $attributes['class'] = $class; - - $value = ' '.Str::ucfirst(trans($trans)); - - return $disabled - ? static::generateDisabled($value, $attributes) - : static::generate($url, $value, $attributes); + return self::make('delete', $url, $attributes, $disabled) + ->size('xs') + ->withTitle(false); } /** - * Generate restore icon link for modals. + * Generate delete link with icon for modals. * * @param string $url * @param array $attributes - * @param bool $withTooltip * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return self */ - public static function restoreModalIcon($url, array $attributes = [], $withTooltip = true, $disabled = false) + public static function deleteModalWithIcon($url, array $attributes = [], $disabled = false) { - // TODO: Refactor the options to a dedicated config file. - $icon = 'fa fa-fw fa-reply'; - $class = 'btn btn-xs btn-primary'; - $trans = 'core::actions.restore'; - - $attributes['class'] = $class; - - if ($disabled) - $attributes = static::cleanAttributes($attributes); - - if ($withTooltip) { - $attributes['data-toggle'] = 'tooltip'; - $attributes['data-original-title'] = Str::ucfirst(trans($trans)); - } - - $value = ''; - - return $disabled - ? static::generateDisabledForIcons($value, $attributes) - : static::generate($url, $value, $attributes); + return static::make('delete', $url, $attributes, $disabled) + ->size('sm') + ->withTitle(true) + ->tooltip(false); } /** - * Generate restore link with icon for modals. + * Generate detach icon link for modals. * * @param string $url * @param array $attributes * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return self */ - public static function restoreModalWithIcon($url, array $attributes = [], $disabled = false) + public static function detachModalIcon($url, array $attributes = [], $disabled = false) { - // TODO: Refactor the options to a dedicated config file. - $icon = 'fa fa-fw fa-reply'; - $class = 'btn btn-sm btn-primary'; - $trans = 'core::actions.restore'; - - $attributes['class'] = $class; - - $value = ' '.Str::ucfirst(trans($trans)); - - return $disabled - ? static::generateDisabled($value, $attributes) - : static::generate($url, $value, $attributes); + return self::make('detach', $url, $attributes, $disabled) + ->size('xs') + ->withTitle(false); } /** - * Generate show icon link. + * Generate edit icon link. * * @param string $url * @param array $attributes - * @param bool $withTooltip * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return self */ - public static function showIcon($url, array $attributes = [], $withTooltip = true, $disabled = false) + public static function editIcon($url, array $attributes = [], $disabled = false) { - // TODO: Refactor the options to a dedicated config file. - $icon = 'fa fa-fw fa-search'; - $class = 'btn btn-xs btn-info'; - - $attributes['class'] = $class; - - if ($withTooltip) { - $attributes['data-toggle'] = 'tooltip'; - $attributes['data-original-title'] = Str::ucfirst(trans('core::actions.show')); - } - - $value = ''; - - return $disabled - ? static::generateDisabledForIcons($value, $attributes) - : static::generate($url, $value, $attributes); + return self::make('edit', $url, $attributes, $disabled) + ->size('xs') + ->withTitle(false); } - /* ----------------------------------------------------------------- - | Other Methods - | ----------------------------------------------------------------- - */ /** - * @param string $value + * Generate edit link with icon. + * + * @param string $url * @param array $attributes + * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return self */ - protected static function generateDisabledForIcons($value, array $attributes) + public static function editWithIcon($url, array $attributes = [], $disabled = false) { - return static::generate('javascript:void(0);', $value, array_merge($attributes, [ - 'class' => 'btn btn-xs btn-default', - 'disabled' => 'disabled', - ])); + return self::make('edit', $url, $attributes, $disabled) + ->size('sm') + ->tooltip(false); } /** - * Generate a disabled link. + * Generate restore icon link for modals. * - * @param string $value + * @param string $url * @param array $attributes + * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return self */ - protected static function generateDisabled($value, array $attributes) + public static function restoreModalIcon($url, array $attributes = [], $disabled = false) { - return static::generate('javascript:void(0);', $value, array_merge($attributes, [ - 'class' => 'btn btn-sm btn-default', - 'disabled' => 'disabled', - ])); + return self::make('restore', $url, $attributes, $disabled) + ->size('xs') + ->withTitle(false); } /** - * Generate the link. + * Generate restore link with icon for modals. * * @param string $url - * @param string $value * @param array $attributes + * @param bool $disabled * - * @return \Illuminate\Support\HtmlString + * @return self */ - protected static function generate($url, $value, array $attributes) + public static function restoreModalWithIcon($url, array $attributes = [], $disabled = false) { - return new HtmlString( - 'attributes($attributes).'>'.$value.'' - ); + return static::make('restore', $url, $attributes, $disabled) + ->size('sm') + ->withTitle(true) + ->tooltip(false); } /** - * Clean the attributes if the link is disabled. + * Generate show icon link. * - * @param array $attributes + * @param string $url + * @param array $attributes + * @param bool $disabled * - * @return array + * @return self */ - private static function cleanAttributes(array $attributes) + public static function showIcon($url, array $attributes = [], $disabled = false) { - // TODO: Make the needles configurable - return Arr::where($attributes, function ($value, $key) { - return ! Str::startsWith($key, ['data-']); - }); + return self::make('show', $url, $attributes, $disabled) + ->size('xs') + ->withTitle(false); } } diff --git a/tests/Helpers/UI/LinkTest.php b/tests/Helpers/UI/LinkTest.php index 06c5aee..39c7e31 100644 --- a/tests/Helpers/UI/LinkTest.php +++ b/tests/Helpers/UI/LinkTest.php @@ -41,8 +41,8 @@ public function it_can_generate_activate_icon_link_without_tooltip() ]; foreach ($expectations as $expected) { - $this->assertSame($expected[1], Link::activateIcon($expected[0], $url, [], false)->toHtml()); - $this->assertSame($expected[1], link_activate_icon($expected[0], $url, [], false)->toHtml()); + $this->assertSame($expected[1], Link::activateIcon($expected[0], $url, [], false)->tooltip(false)->toHtml()); + $this->assertSame($expected[1], link_activate_icon($expected[0], $url, [], false)->tooltip(false)->toHtml()); } } @@ -56,8 +56,8 @@ public function it_can_generate_activate_icon_with_disabled_state() ]; foreach ($expectations as $expected) { - $this->assertSame($expected[1], Link::activateIcon($expected[0], $url, [], true, true)->toHtml()); - $this->assertSame($expected[1], link_activate_icon($expected[0], $url, [], true, true)->toHtml()); + $this->assertSame($expected[1], Link::activateIcon($expected[0], $url, [], true)->toHtml()); + $this->assertSame($expected[1], link_activate_icon($expected[0], $url, [], true)->toHtml()); } } @@ -67,7 +67,7 @@ public function it_can_generate_activate_icon_with_custom_attributes() $url = '#activateModal'; $expected = - ''. + ''. ''. ''; @@ -75,7 +75,7 @@ public function it_can_generate_activate_icon_with_custom_attributes() $this->assertSame($expected, link_activate_icon(true, $url, ['data-current-status' => 'disabled'])->toHtml()); $expected = - ''. + ''. ''. ''; @@ -88,17 +88,15 @@ public function it_can_generate_activate_icon_for_modals() { $url = '#activateModal'; - $expected = - ''. - ''. - ''; + $expected = ''. + ''. + ''; $this->assertSame($expected, Link::activateModalIcon(true, $url)->toHtml()); - $expected = - ''. - ''. - ''; + $expected = ''. + ''. + ''; $this->assertSame($expected, Link::activateModalIcon(false, $url)->toHtml()); } @@ -108,13 +106,17 @@ public function it_can_generate_activate_with_icon_for_modals() { $url = '#activateModal'; - $expected = ' Enable'; + $expected = ''. + ' Enable'. + ''; - $this->assertSame($expected, Link::activateModalWithIcon(true, $url)->toHtml()); + $this->assertSame($expected, Link::activateModalWithIcon(false, $url, ['data-id' => '12'])->toHtml()); - $expected = ' Disable'; + $expected = ''. + ' Disable'. + ''; - $this->assertSame($expected, Link::activateModalWithIcon(false, $url)->toHtml()); + $this->assertSame($expected, Link::activateModalWithIcon(true, $url, ['data-id' => '12'])->toHtml()); } /* ----------------------------------------------------------------- @@ -140,8 +142,8 @@ public function it_can_generate_add_icon_link_without_tooltip() $url = 'http://localhost/add'; $expected = ''; - $this->assertSame($expected, Link::addIcon($url, [], false)->toHtml()); - $this->assertSame($expected, link_add_icon($url, [], false)->toHtml()); + $this->assertSame($expected, Link::addIcon($url, [], false)->tooltip(false)->toHtml()); + $this->assertSame($expected, link_add_icon($url, [], false)->tooltip(false)->toHtml()); } /** @test */ @@ -176,7 +178,7 @@ public function it_can_generate_delete_icon_link_for_modals() public function it_can_generate_delete_icon_link_for_modals_with_attributes() { $url = '#deleteModal'; - $expected = ''. + $expected = ''. ''. ''; @@ -187,7 +189,7 @@ public function it_can_generate_delete_icon_link_for_modals_with_attributes() public function it_can_generate_delete_icon_link_for_modals_with_attributes_but_without_data_if_disabled() { $url = '#deleteModal'; - $expected = ''. + $expected = ''. ''. ''; @@ -230,7 +232,7 @@ public function it_can_generate_detach_icon_link_for_modals() public function it_can_generate_detach_icon_link_for_modals_with_attributes() { $url = '#detachModal'; - $expected = ''. + $expected = ''. ''. ''; @@ -241,7 +243,7 @@ public function it_can_generate_detach_icon_link_for_modals_with_attributes() public function it_can_generate_detach_icon_link_for_modals_with_attributes_but_without_data_if_disabled() { $url = '#detachModal'; - $expected = ''. + $expected = ''. ''. ''; @@ -296,8 +298,8 @@ public function it_can_generate_edit_icon_link_without_tooltip() $url = 'http://localhost/edit'; $expected = ''; - $this->assertSame($expected, Link::editIcon($url, [], false)->toHtml()); - $this->assertSame($expected, link_edit_icon($url, [], false)->toHtml()); + $this->assertSame($expected, Link::editIcon($url, [], false)->tooltip(false)->toHtml()); + $this->assertSame($expected, link_edit_icon($url, [], false)->tooltip(false)->toHtml()); } /** @test */ @@ -346,8 +348,8 @@ public function it_can_generate_show_icon_link_without_tooltip() $url = 'http://localhost/show'; $expected = ''; - $this->assertSame($expected, Link::showIcon($url, [], false)->toHtml()); - $this->assertSame($expected, link_show_icon($url, [], false)->toHtml()); + $this->assertSame($expected, Link::showIcon($url, [], false)->tooltip(false)->toHtml()); + $this->assertSame($expected, link_show_icon($url, [], false)->tooltip(false)->toHtml()); } /** @test */