Adding Wowhead link component with WowheadPower Tooltip library.
This commit is contained in:
parent
0db095fbdc
commit
61a6d366fd
4 changed files with 237 additions and 0 deletions
104
app/View/Components/WowheadLink.php
Normal file
104
app/View/Components/WowheadLink.php
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
<?php
|
||||
|
||||
namespace App\View\Components;
|
||||
|
||||
use Illuminate\View\Component;
|
||||
|
||||
/**
|
||||
* Render a wowhead tooltip link.
|
||||
*/
|
||||
class WowheadLink extends Component
|
||||
{
|
||||
/**
|
||||
* Identifier
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* Type of link
|
||||
*/
|
||||
public $type;
|
||||
|
||||
protected $href;
|
||||
|
||||
/**
|
||||
* True if wowhead should rename the link, false if it should leave it alone.
|
||||
*/
|
||||
protected $renameLink;
|
||||
|
||||
/**
|
||||
* Posible values: null (default), tiny, small, medium and large
|
||||
*/
|
||||
protected $iconSize;
|
||||
|
||||
/**
|
||||
* Wowhead domain to use.
|
||||
*/
|
||||
protected $domain;
|
||||
|
||||
/**
|
||||
* Create a new component instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($whId, $type = 'item', $href = false,
|
||||
$iconSize = null, $renameLink = null, $domain = null)
|
||||
{
|
||||
$this->id = $whId;
|
||||
$this->type = $type;
|
||||
$this->href = $href;
|
||||
$this->renameLink = $renameLink;
|
||||
$this->iconSize = $iconSize;
|
||||
$this->domain = $domain ?? config('wowhead.domain');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the view / contents that represent the component.
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\View|\Closure|string
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
$attr = collect([
|
||||
'data-wh-rename-link' => $this->renameLink,
|
||||
'data-wh-icon-size' => $this->iconSize
|
||||
]);
|
||||
|
||||
// If we have custom url, we must set the
|
||||
// 'data-wowhead' attribute with the options.
|
||||
if ($this->href) {
|
||||
$attr['href'] = $this->href;
|
||||
$attr['data-wowhead'] = $this->formatOptions();
|
||||
}
|
||||
// Otherwise, get a wowhead url that automaticlly will be picked up
|
||||
// by the javascript code and render a tooltip.
|
||||
else {
|
||||
$attr['href'] = $this->renderUrl();
|
||||
$attr['target'] = '_blank';
|
||||
}
|
||||
|
||||
return view("components.wowhead-link", [
|
||||
'component_attr' => $attr->filter()->toArray()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a wowhead url.
|
||||
*/
|
||||
protected function renderUrl() : string
|
||||
{
|
||||
return sprintf('https://%s.wowhead.com/%s=%s',
|
||||
$this->domain, $this->type, $this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format options for 'data-wowhead' attribute.
|
||||
*/
|
||||
protected function formatOptions() : string
|
||||
{
|
||||
return http_build_query([
|
||||
$this->type => $this->id,
|
||||
'domain' => $this->domain
|
||||
]);
|
||||
}
|
||||
}
|
||||
43
app/View/Components/WowheadScript.php
Normal file
43
app/View/Components/WowheadScript.php
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace App\View\Components;
|
||||
|
||||
use Illuminate\View\Component;
|
||||
|
||||
/**
|
||||
* Render wowhead tooltip javascript
|
||||
*/
|
||||
class WowheadScript extends Component
|
||||
{
|
||||
public $options;
|
||||
|
||||
/**
|
||||
* Option keys.
|
||||
*/
|
||||
protected static $keys = [
|
||||
'iconSize',
|
||||
'colorLinks',
|
||||
'iconizeLinks',
|
||||
'renameLinks',
|
||||
'hide'
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the view / contents that represent the component.
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\View|\Closure|string
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
$this->options = collect(config('wowhead'))
|
||||
->only(self::$keys)
|
||||
->filter(function ($value, $key) {
|
||||
return is_array($value) ? count($value) : $value !== null;
|
||||
});
|
||||
|
||||
return <<<'html'
|
||||
<script>const whTooltips=<?php echo json_encode($options) ?>;</script>
|
||||
<script src="https://wow.zamimg.com/widgets/power.js"></script>
|
||||
html;
|
||||
}
|
||||
}
|
||||
87
config/wowhead.php
Normal file
87
config/wowhead.php
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Default domain
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| What wowhead domain to use if not specified. The domain can control
|
||||
| versions (classic, ptr, retail) or localization. possible values
|
||||
| are: www, ptr, tbc, de, fr, ru, de.classic, ru.tbc etc.
|
||||
|
|
||||
*/
|
||||
|
||||
'domain' => 'tbc',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Rename links
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Set this to true to make wowhead replace the name for all wowhead links
|
||||
| on the page.
|
||||
|
|
||||
*/
|
||||
|
||||
'renameLinks' => false,
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Icon Size
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Configure the default size of the icons on the site. Available sizes:
|
||||
| tiny (the default), small, medium, and large.
|
||||
|
|
||||
*/
|
||||
|
||||
'iconSize' => 'small',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Iconize Links
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Set to true if icons should be added to all links. If you set this to
|
||||
| false, you can allow _some_ links on the site to have an icon by
|
||||
| setting the `icon-size=<value>` attribute on individual links.
|
||||
|
|
||||
*/
|
||||
|
||||
'iconizeLinks' => true,
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Color Links
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Set to true if wowhead should automatically set link text color
|
||||
| for all links on the page.
|
||||
|
|
||||
*/
|
||||
|
||||
'colorLinks' => false,
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Disabling components
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| You can disable some components in the tooltip by setting the component
|
||||
| in question to true.
|
||||
|
|
||||
*/
|
||||
|
||||
"hide" => [
|
||||
// "maxstack" => true,
|
||||
// "droppedby" => true,
|
||||
// "dropchance" => true,
|
||||
// "reagents" => true,
|
||||
// "ilvl" => true,
|
||||
// "extra" => true,
|
||||
// "sellprice" => true
|
||||
],
|
||||
|
||||
];
|
||||
3
resources/views/components/wowhead-link.blade.php
Normal file
3
resources/views/components/wowhead-link.blade.php
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
<x-link {{ $attributes->merge($component_attr) }}>
|
||||
{{ $slot }}
|
||||
</x-link>
|
||||
Reference in a new issue