| 1 |
/** |
| 2 |
* External dependencies |
| 3 |
*/ |
| 4 |
import classnames from 'classnames/dedupe'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Internal dependencies |
| 8 |
*/ |
| 9 |
import getPages from '../pages'; |
| 10 |
import Logo from '../assets/logo.svg'; |
| 11 |
|
| 12 |
/** |
| 13 |
* WordPress dependencies |
| 14 |
*/ |
| 15 |
const { Component, Fragment } = wp.element; |
| 16 |
|
| 17 |
const { __ } = wp.i18n; |
| 18 |
|
| 19 |
const $ = window.jQuery; |
| 20 |
|
| 21 |
export default class Container extends Component { |
| 22 |
constructor(props) { |
| 23 |
super(props); |
| 24 |
|
| 25 |
// get variable. |
| 26 |
const $_GET = []; |
| 27 |
window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, (a, name, value) => { |
| 28 |
$_GET[name] = value; |
| 29 |
}); |
| 30 |
|
| 31 |
const pages = getPages(); |
| 32 |
|
| 33 |
// Set the default states |
| 34 |
this.state = { |
| 35 |
pages, |
| 36 |
activePage: $_GET.sub_page ? $_GET.sub_page : Object.keys(pages)[0], |
| 37 |
}; |
| 38 |
|
| 39 |
this.updateAdminPageActiveLink = this.updateAdminPageActiveLink.bind(this); |
| 40 |
} |
| 41 |
|
| 42 |
componentDidMount() { |
| 43 |
this.updateAdminPageActiveLink(); |
| 44 |
} |
| 45 |
|
| 46 |
updateAdminPageActiveLink() { |
| 47 |
const { activePage } = this.state; |
| 48 |
|
| 49 |
// disable active link. |
| 50 |
$('.toplevel_page_ghostkit .current').removeClass('current'); |
| 51 |
|
| 52 |
// find new active link. |
| 53 |
let $link = $( |
| 54 |
`.toplevel_page_ghostkit [href="admin.php?page=ghostkit&sub_page=${activePage}"]` |
| 55 |
); |
| 56 |
|
| 57 |
if (!$link.length) { |
| 58 |
$link = $('.toplevel_page_ghostkit [href="admin.php?page=ghostkit"]'); |
| 59 |
} |
| 60 |
|
| 61 |
$link.parent().addClass('current'); |
| 62 |
|
| 63 |
// change address bar link |
| 64 |
if ($link.length) { |
| 65 |
window.history.pushState(document.title, document.title, $link.prop('href')); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
render() { |
| 70 |
const { pages, activePage } = this.state; |
| 71 |
|
| 72 |
const resultTabs = []; |
| 73 |
let resultContent = ''; |
| 74 |
|
| 75 |
Object.keys(pages).forEach((k) => { |
| 76 |
resultTabs.push( |
| 77 |
<li key={k}> |
| 78 |
{/* eslint-disable-next-line react/button-has-type */} |
| 79 |
<button |
| 80 |
className={classnames( |
| 81 |
'ghostkit-admin-tabs-button', |
| 82 |
activePage === k ? 'ghostkit-admin-tabs-button-active' : '' |
| 83 |
)} |
| 84 |
onClick={() => { |
| 85 |
this.setState( |
| 86 |
{ |
| 87 |
activePage: k, |
| 88 |
}, |
| 89 |
() => { |
| 90 |
this.updateAdminPageActiveLink(); |
| 91 |
} |
| 92 |
); |
| 93 |
}} |
| 94 |
> |
| 95 |
{pages[k].label} |
| 96 |
</button> |
| 97 |
</li> |
| 98 |
); |
| 99 |
}); |
| 100 |
|
| 101 |
if (activePage && pages[activePage]) { |
| 102 |
const NewBlock = pages[activePage].block; |
| 103 |
|
| 104 |
resultContent = ( |
| 105 |
<Fragment> |
| 106 |
<h2>{pages[activePage].label}</h2> |
| 107 |
<NewBlock |
| 108 |
data={this.props.data} |
| 109 |
settings={this.state.settings} |
| 110 |
updateSettings={this.updateSettings} |
| 111 |
/> |
| 112 |
</Fragment> |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
return ( |
| 117 |
<Fragment> |
| 118 |
<div className="ghostkit-admin-head"> |
| 119 |
<div className="ghostkit-admin-head-wrap"> |
| 120 |
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */} |
| 121 |
<a href="https://ghostkit.io/?utm_source=plugin&utm_medium=settings&utm_campaign=logo&utm_content=2.25.0"> |
| 122 |
<Logo /> |
| 123 |
</a> |
| 124 |
<h1>{__('Ghost Kit')}</h1> |
| 125 |
<ul className="ghostkit-admin-tabs">{resultTabs}</ul> |
| 126 |
</div> |
| 127 |
</div> |
| 128 |
<div className="ghostkit-admin-content">{resultContent}</div> |
| 129 |
</Fragment> |
| 130 |
); |
| 131 |
} |
| 132 |
} |
| 133 |
|