DashNav.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. appEvents.emit('show-dash-search');
  52. };
  53. onClose = () => {
  54. if (this.props.editview) {
  55. this.props.updateLocation({
  56. query: { editview: null },
  57. partial: true,
  58. });
  59. } else {
  60. this.props.updateLocation({
  61. query: { panelId: null, edit: null, fullscreen: null, tab: null },
  62. partial: true,
  63. });
  64. }
  65. };
  66. onToggleTVMode = () => {
  67. appEvents.emit('toggle-kiosk-mode');
  68. };
  69. onSave = () => {
  70. const { $injector } = this.props;
  71. const dashboardSrv = $injector.get('dashboardSrv');
  72. dashboardSrv.saveDashboard();
  73. };
  74. onOpenSettings = () => {
  75. this.props.updateLocation({
  76. query: { editview: 'settings' },
  77. partial: true,
  78. });
  79. };
  80. onStarDashboard = () => {
  81. const { dashboard, $injector } = this.props;
  82. const dashboardSrv = $injector.get('dashboardSrv');
  83. dashboardSrv.starDashboard(dashboard.id, dashboard.meta.isStarred).then(newState => {
  84. dashboard.meta.isStarred = newState;
  85. this.forceUpdate();
  86. });
  87. };
  88. onPlaylistPrev = () => {
  89. this.playlistSrv.prev();
  90. };
  91. onPlaylistNext = () => {
  92. this.playlistSrv.next();
  93. };
  94. onPlaylistStop = () => {
  95. this.playlistSrv.stop();
  96. this.forceUpdate();
  97. };
  98. onOpenShare = () => {
  99. const $rootScope = this.props.$injector.get('$rootScope');
  100. const modalScope = $rootScope.$new();
  101. modalScope.tabIndex = 0;
  102. modalScope.dashboard = this.props.dashboard;
  103. appEvents.emit('show-modal', {
  104. src: 'public/app/features/dashboard/components/ShareModal/template.html',
  105. scope: modalScope,
  106. });
  107. };
  108. renderDashboardTitleSearchButton() {
  109. const { dashboard } = this.props;
  110. const folderTitle = dashboard.meta.folderTitle;
  111. const haveFolder = dashboard.meta.folderId > 0;
  112. return (
  113. <>
  114. <div>
  115. <a className="navbar-page-btn" onClick={this.onOpenSearch}>
  116. {!this.isInFullscreenOrSettings && <i className="gicon gicon-dashboard" />}
  117. {haveFolder && <span className="navbar-page-btn--folder">{folderTitle} / </span>}
  118. {dashboard.title}
  119. <i className="fa fa-caret-down" />
  120. </a>
  121. </div>
  122. {this.isSettings && <span className="navbar-settings-title">&nbsp;/ Settings</span>}
  123. <div className="navbar__spacer" />
  124. </>
  125. );
  126. }
  127. get isInFullscreenOrSettings() {
  128. return this.props.editview || this.props.isFullscreen;
  129. }
  130. get isSettings() {
  131. return this.props.editview;
  132. }
  133. renderBackButton() {
  134. return (
  135. <div className="navbar-edit">
  136. <Tooltip content="Go back (Esc)">
  137. <button className="navbar-edit__back-btn" onClick={this.onClose}>
  138. <i className="fa fa-arrow-left" />
  139. </button>
  140. </Tooltip>
  141. </div>
  142. );
  143. }
  144. render() {
  145. const { dashboard, onAddPanel, location } = this.props;
  146. const { canStar, canSave, canShare, showSettings, isStarred } = dashboard.meta;
  147. const { snapshot } = dashboard;
  148. const snapshotUrl = snapshot && snapshot.originalUrl;
  149. return (
  150. <div className="navbar">
  151. {this.isInFullscreenOrSettings && this.renderBackButton()}
  152. {this.renderDashboardTitleSearchButton()}
  153. {this.playlistSrv.isPlaying && (
  154. <div className="navbar-buttons navbar-buttons--playlist">
  155. <DashNavButton
  156. tooltip="Go to previous dashboard"
  157. classSuffix="tight"
  158. icon="fa fa-step-backward"
  159. onClick={this.onPlaylistPrev}
  160. />
  161. <DashNavButton
  162. tooltip="Stop playlist"
  163. classSuffix="tight"
  164. icon="fa fa-stop"
  165. onClick={this.onPlaylistStop}
  166. />
  167. <DashNavButton
  168. tooltip="Go to next dashboard"
  169. classSuffix="tight"
  170. icon="fa fa-forward"
  171. onClick={this.onPlaylistNext}
  172. />
  173. </div>
  174. )}
  175. <div className="navbar-buttons navbar-buttons--actions">
  176. {canSave && (
  177. <DashNavButton
  178. tooltip="Add panel"
  179. classSuffix="add-panel"
  180. icon="gicon gicon-add-panel"
  181. onClick={onAddPanel}
  182. />
  183. )}
  184. {canStar && (
  185. <DashNavButton
  186. tooltip="Mark as favorite"
  187. classSuffix="star"
  188. icon={`${isStarred ? 'fa fa-star' : 'fa fa-star-o'}`}
  189. onClick={this.onStarDashboard}
  190. />
  191. )}
  192. {canShare && (
  193. <DashNavButton
  194. tooltip="Share dashboard"
  195. classSuffix="share"
  196. icon="fa fa-share-square-o"
  197. onClick={this.onOpenShare}
  198. />
  199. )}
  200. {canSave && (
  201. <DashNavButton tooltip="Save dashboard" classSuffix="save" icon="fa fa-save" onClick={this.onSave} />
  202. )}
  203. {snapshotUrl && (
  204. <DashNavButton
  205. tooltip="Open original dashboard"
  206. classSuffix="snapshot-origin"
  207. icon="gicon gicon-link"
  208. href={snapshotUrl}
  209. />
  210. )}
  211. {showSettings && (
  212. <DashNavButton
  213. tooltip="Dashboard settings"
  214. classSuffix="settings"
  215. icon="gicon gicon-cog"
  216. onClick={this.onOpenSettings}
  217. />
  218. )}
  219. </div>
  220. <div className="navbar-buttons navbar-buttons--tv">
  221. <DashNavButton
  222. tooltip="Cycle view mode"
  223. classSuffix="tv"
  224. icon="fa fa-desktop"
  225. onClick={this.onToggleTVMode}
  226. />
  227. </div>
  228. {!dashboard.timepicker.hidden && (
  229. <div className="navbar-buttons">
  230. <div className="gf-timepicker-nav" ref={element => (this.timePickerEl = element)} />
  231. <DashNavTimeControls dashboard={dashboard} location={location} updateLocation={updateLocation} />
  232. </div>
  233. )}
  234. </div>
  235. );
  236. }
  237. }
  238. const mapStateToProps = (state: StoreState) => ({
  239. location: state.location,
  240. });
  241. const mapDispatchToProps = {
  242. updateLocation,
  243. };
  244. export default connect(
  245. mapStateToProps,
  246. mapDispatchToProps
  247. )(DashNav);