DashNav.tsx 7.5 KB

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