DashNav.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // Libaries
  2. import React, { PureComponent } from 'react';
  3. import { connect } from 'react-redux';
  4. // Utils & Services
  5. import { AngularComponent, getAngularLoader } from 'app/core/services/AngularLoader';
  6. import { appEvents } from 'app/core/app_events';
  7. import { PlaylistSrv } from 'app/features/playlist/playlist_srv';
  8. // Components
  9. import { DashNavButton } from './DashNavButton';
  10. import { DashNavTimeControls } from './DashNavTimeControls';
  11. import { Tooltip } from '@grafana/ui';
  12. // State
  13. import { updateLocation } from 'app/core/actions';
  14. // Types
  15. import { DashboardModel } from '../../state';
  16. import { StoreState } from 'app/types';
  17. export interface OwnProps {
  18. dashboard: DashboardModel;
  19. editview: string;
  20. isEditing: boolean;
  21. isFullscreen: boolean;
  22. $injector: any;
  23. updateLocation: typeof updateLocation;
  24. onAddPanel: () => void;
  25. }
  26. export interface StateProps {
  27. location: any;
  28. }
  29. type Props = StateProps & OwnProps;
  30. export class DashNav extends PureComponent<Props> {
  31. timePickerEl: HTMLElement;
  32. timepickerCmp: AngularComponent;
  33. playlistSrv: PlaylistSrv;
  34. constructor(props: Props) {
  35. super(props);
  36. this.playlistSrv = this.props.$injector.get('playlistSrv');
  37. }
  38. componentDidMount() {
  39. const loader = getAngularLoader();
  40. const template =
  41. '<gf-time-picker class="gf-timepicker-nav" dashboard="dashboard" ng-if="!dashboard.timepicker.hidden" />';
  42. const scopeProps = { dashboard: this.props.dashboard };
  43. this.timepickerCmp = loader.load(this.timePickerEl, scopeProps, template);
  44. }
  45. componentWillUnmount() {
  46. if (this.timepickerCmp) {
  47. this.timepickerCmp.destroy();
  48. }
  49. }
  50. onOpenSearch = () => {
  51. const { dashboard } = this.props;
  52. const haveFolder = dashboard.meta.folderId > 0;
  53. appEvents.emit(
  54. 'show-dash-search',
  55. haveFolder
  56. ? {
  57. query: 'folder:current',
  58. }
  59. : null
  60. );
  61. };
  62. onClose = () => {
  63. if (this.props.editview) {
  64. this.props.updateLocation({
  65. query: { editview: null },
  66. partial: true,
  67. });
  68. } else {
  69. this.props.updateLocation({
  70. query: { panelId: null, edit: null, fullscreen: null, tab: null },
  71. partial: true,
  72. });
  73. }
  74. };
  75. onToggleTVMode = () => {
  76. appEvents.emit('toggle-kiosk-mode');
  77. };
  78. onSave = () => {
  79. const { $injector } = this.props;
  80. const dashboardSrv = $injector.get('dashboardSrv');
  81. dashboardSrv.saveDashboard();
  82. };
  83. onOpenSettings = () => {
  84. this.props.updateLocation({
  85. query: { editview: 'settings' },
  86. partial: true,
  87. });
  88. };
  89. onStarDashboard = () => {
  90. const { dashboard, $injector } = this.props;
  91. const dashboardSrv = $injector.get('dashboardSrv');
  92. dashboardSrv.starDashboard(dashboard.id, dashboard.meta.isStarred).then(newState => {
  93. dashboard.meta.isStarred = newState;
  94. this.forceUpdate();
  95. });
  96. };
  97. onPlaylistPrev = () => {
  98. this.playlistSrv.prev();
  99. };
  100. onPlaylistNext = () => {
  101. this.playlistSrv.next();
  102. };
  103. onPlaylistStop = () => {
  104. this.playlistSrv.stop();
  105. this.forceUpdate();
  106. };
  107. onOpenShare = () => {
  108. const $rootScope = this.props.$injector.get('$rootScope');
  109. const modalScope = $rootScope.$new();
  110. modalScope.tabIndex = 0;
  111. modalScope.dashboard = this.props.dashboard;
  112. appEvents.emit('show-modal', {
  113. src: 'public/app/features/dashboard/components/ShareModal/template.html',
  114. scope: modalScope,
  115. });
  116. };
  117. renderDashboardTitleSearchButton() {
  118. const { dashboard } = this.props;
  119. const folderTitle = dashboard.meta.folderTitle;
  120. const haveFolder = dashboard.meta.folderId > 0;
  121. return (
  122. <>
  123. <div>
  124. <a className="navbar-page-btn" onClick={this.onOpenSearch}>
  125. {!this.isInFullscreenOrSettings && <i className="gicon gicon-dashboard" />}
  126. {haveFolder && <span className="navbar-page-btn--folder">{folderTitle} / </span>}
  127. {dashboard.title} <i className="fa fa-caret-down" />
  128. </a>
  129. </div>
  130. {this.isSettings && <span className="navbar-settings-title">&nbsp;/ Settings</span>}
  131. <div className="navbar__spacer" />
  132. </>
  133. );
  134. }
  135. get isInFullscreenOrSettings() {
  136. return this.props.editview || this.props.isFullscreen;
  137. }
  138. get isSettings() {
  139. return this.props.editview;
  140. }
  141. renderBackButton() {
  142. return (
  143. <div className="navbar-edit">
  144. <Tooltip content="Go back (Esc)">
  145. <button className="navbar-edit__back-btn" onClick={this.onClose}>
  146. <i className="fa fa-arrow-left" />
  147. </button>
  148. </Tooltip>
  149. </div>
  150. );
  151. }
  152. render() {
  153. const { dashboard, onAddPanel, location } = this.props;
  154. const { canStar, canSave, canShare, showSettings, isStarred } = dashboard.meta;
  155. const { snapshot } = dashboard;
  156. const snapshotUrl = snapshot && snapshot.originalUrl;
  157. return (
  158. <div className="navbar">
  159. {this.isInFullscreenOrSettings && this.renderBackButton()}
  160. {this.renderDashboardTitleSearchButton()}
  161. {this.playlistSrv.isPlaying && (
  162. <div className="navbar-buttons navbar-buttons--playlist">
  163. <DashNavButton
  164. tooltip="Go to previous dashboard"
  165. classSuffix="tight"
  166. icon="fa fa-step-backward"
  167. onClick={this.onPlaylistPrev}
  168. />
  169. <DashNavButton
  170. tooltip="Stop playlist"
  171. classSuffix="tight"
  172. icon="fa fa-stop"
  173. onClick={this.onPlaylistStop}
  174. />
  175. <DashNavButton
  176. tooltip="Go to next dashboard"
  177. classSuffix="tight"
  178. icon="fa fa-forward"
  179. onClick={this.onPlaylistNext}
  180. />
  181. </div>
  182. )}
  183. <div className="navbar-buttons navbar-buttons--actions">
  184. {canSave && (
  185. <DashNavButton
  186. tooltip="Add panel"
  187. classSuffix="add-panel"
  188. icon="gicon gicon-add-panel"
  189. onClick={onAddPanel}
  190. />
  191. )}
  192. {canStar && (
  193. <DashNavButton
  194. tooltip="Mark as favorite"
  195. classSuffix="star"
  196. icon={`${isStarred ? 'fa fa-star' : 'fa fa-star-o'}`}
  197. onClick={this.onStarDashboard}
  198. />
  199. )}
  200. {canShare && (
  201. <DashNavButton
  202. tooltip="Share dashboard"
  203. classSuffix="share"
  204. icon="fa fa-share-square-o"
  205. onClick={this.onOpenShare}
  206. />
  207. )}
  208. {canSave && (
  209. <DashNavButton tooltip="Save dashboard" classSuffix="save" icon="fa fa-save" onClick={this.onSave} />
  210. )}
  211. {snapshotUrl && (
  212. <DashNavButton
  213. tooltip="Open original dashboard"
  214. classSuffix="snapshot-origin"
  215. icon="gicon gicon-link"
  216. href={snapshotUrl}
  217. />
  218. )}
  219. {showSettings && (
  220. <DashNavButton
  221. tooltip="Dashboard settings"
  222. classSuffix="settings"
  223. icon="gicon gicon-cog"
  224. onClick={this.onOpenSettings}
  225. />
  226. )}
  227. </div>
  228. <div className="navbar-buttons navbar-buttons--tv">
  229. <DashNavButton
  230. tooltip="Cycle view mode"
  231. classSuffix="tv"
  232. icon="fa fa-desktop"
  233. onClick={this.onToggleTVMode}
  234. />
  235. </div>
  236. {!dashboard.timepicker.hidden && (
  237. <div className="navbar-buttons">
  238. <div className="gf-timepicker-nav" ref={element => (this.timePickerEl = element)} />
  239. <DashNavTimeControls dashboard={dashboard} location={location} updateLocation={updateLocation} />
  240. </div>
  241. )}
  242. </div>
  243. );
  244. }
  245. }
  246. const mapStateToProps = (state: StoreState) => ({
  247. location: state.location,
  248. });
  249. const mapDispatchToProps = {
  250. updateLocation,
  251. };
  252. export default connect(
  253. mapStateToProps,
  254. mapDispatchToProps
  255. )(DashNav);