DashNav.tsx 8.1 KB

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