DashNav.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. <div className="navbar__spacer" />
  123. </>
  124. );
  125. }
  126. get isInFullscreenOrSettings() {
  127. return this.props.editview || this.props.isFullscreen;
  128. }
  129. renderBackButton() {
  130. return (
  131. <div className="navbar-edit">
  132. <Tooltip content="Go back (Esc)">
  133. <button className="navbar-edit__back-btn" onClick={this.onClose}>
  134. <i className="fa fa-arrow-left" />
  135. </button>
  136. </Tooltip>
  137. </div>
  138. );
  139. }
  140. render() {
  141. const { dashboard, onAddPanel, location } = this.props;
  142. const { canStar, canSave, canShare, showSettings, isStarred } = dashboard.meta;
  143. const { snapshot } = dashboard;
  144. const snapshotUrl = snapshot && snapshot.originalUrl;
  145. return (
  146. <div className="navbar">
  147. {this.isInFullscreenOrSettings && this.renderBackButton()}
  148. {this.renderDashboardTitleSearchButton()}
  149. {this.playlistSrv.isPlaying && (
  150. <div className="navbar-buttons navbar-buttons--playlist">
  151. <DashNavButton
  152. tooltip="Go to previous dashboard"
  153. classSuffix="tight"
  154. icon="fa fa-step-backward"
  155. onClick={this.onPlaylistPrev}
  156. />
  157. <DashNavButton
  158. tooltip="Stop playlist"
  159. classSuffix="tight"
  160. icon="fa fa-stop"
  161. onClick={this.onPlaylistStop}
  162. />
  163. <DashNavButton
  164. tooltip="Go to next dashboard"
  165. classSuffix="tight"
  166. icon="fa fa-forward"
  167. onClick={this.onPlaylistNext}
  168. />
  169. </div>
  170. )}
  171. <div className="navbar-buttons navbar-buttons--actions">
  172. {canSave && (
  173. <DashNavButton
  174. tooltip="Add panel"
  175. classSuffix="add-panel"
  176. icon="gicon gicon-add-panel"
  177. onClick={onAddPanel}
  178. />
  179. )}
  180. {canStar && (
  181. <DashNavButton
  182. tooltip="Mark as favorite"
  183. classSuffix="star"
  184. icon={`${isStarred ? 'fa fa-star' : 'fa fa-star-o'}`}
  185. onClick={this.onStarDashboard}
  186. />
  187. )}
  188. {canShare && (
  189. <DashNavButton
  190. tooltip="Share dashboard"
  191. classSuffix="share"
  192. icon="fa fa-share-square-o"
  193. onClick={this.onOpenShare}
  194. />
  195. )}
  196. {canSave && (
  197. <DashNavButton tooltip="Save dashboard" classSuffix="save" icon="fa fa-save" onClick={this.onSave} />
  198. )}
  199. {snapshotUrl && (
  200. <DashNavButton
  201. tooltip="Open original dashboard"
  202. classSuffix="snapshot-origin"
  203. icon="fa fa-link"
  204. href={snapshotUrl}
  205. />
  206. )}
  207. {showSettings && (
  208. <DashNavButton
  209. tooltip="Dashboard settings"
  210. classSuffix="settings"
  211. icon="fa fa-cog"
  212. onClick={this.onOpenSettings}
  213. />
  214. )}
  215. </div>
  216. <div className="navbar-buttons navbar-buttons--tv">
  217. <DashNavButton
  218. tooltip="Cycle view mode"
  219. classSuffix="tv"
  220. icon="fa fa-desktop"
  221. onClick={this.onToggleTVMode}
  222. />
  223. </div>
  224. {!dashboard.timepicker.hidden && (
  225. <div className="navbar-buttons">
  226. <DashNavTimeControls dashboard={dashboard} location={location} updateLocation={updateLocation} />
  227. <div className="gf-timepicker-nav" ref={element => (this.timePickerEl = element)} />
  228. </div>
  229. )}
  230. </div>
  231. );
  232. }
  233. }
  234. const mapStateToProps = (state: StoreState) => ({
  235. location: state.location,
  236. });
  237. const mapDispatchToProps = {
  238. updateLocation,
  239. };
  240. export default connect(
  241. mapStateToProps,
  242. mapDispatchToProps
  243. )(DashNav);