inspector.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. Message
  5. } from 'phosphor-messaging';
  6. import {
  7. Panel
  8. } from 'phosphor-panel';
  9. import {
  10. ISignal, Signal, clearSignalData
  11. } from 'phosphor-signaling';
  12. import {
  13. TabPanel
  14. } from 'phosphor-tabs';
  15. import {
  16. Widget
  17. } from 'phosphor-widget';
  18. import {
  19. NotebookToolbar, ToolbarButton
  20. } from '../notebook/notebook/toolbar';
  21. /**
  22. * The class name added to inspector panels.
  23. */
  24. const PANEL_CLASS = 'jp-Inspector';
  25. /**
  26. * The class name added to inspector child item widgets.
  27. */
  28. const ITEM_CLASS = 'jp-InspectorItem';
  29. /**
  30. * The class name added to inspector child item widgets' content.
  31. */
  32. const CONTENT_CLASS = 'jp-InspectorItem-content';
  33. /**
  34. * The history clear button class name.
  35. */
  36. const CLEAR_CLASS = 'jp-InspectorItem-clear';
  37. /**
  38. * The back button class name.
  39. */
  40. const BACK_CLASS = 'jp-InspectorItem-back';
  41. /**
  42. * The forward button class name.
  43. */
  44. const FORWARD_CLASS = 'jp-InspectorItem-forward';
  45. /**
  46. * The orientation toggle bottom button class name.
  47. */
  48. const BOTTOM_TOGGLE_CLASS = 'jp-InspectorItem-bottom';
  49. /**
  50. * The orientation toggle right button class name.
  51. */
  52. const RIGHT_TOGGLE_CLASS = 'jp-InspectorItem-right';
  53. /**
  54. * A panel which contains a set of inspectors.
  55. */
  56. export
  57. class Inspector extends TabPanel {
  58. /**
  59. * Construct an inspector.
  60. */
  61. constructor(options: Inspector.IOptions) {
  62. super();
  63. this.addClass(PANEL_CLASS);
  64. // Create console inspector widgets and add them to the inspectors panel.
  65. (options.items || []).forEach(value => {
  66. let widget = value.widget || new InspectorItem();
  67. widget.orientation = this._orientation as Inspector.Orientation;
  68. widget.orientationToggled.connect(() => {
  69. this.orientation = 'vertical' ? 'horizontal' : 'vertical';
  70. });
  71. widget.rank = value.rank;
  72. widget.remember = !!value.remember;
  73. widget.title.closable = false;
  74. widget.title.text = value.name;
  75. if (value.className) {
  76. widget.addClass(value.className);
  77. }
  78. this._items[value.type] = widget;
  79. this.addChild(widget);
  80. });
  81. }
  82. /**
  83. * Set the orientation of the inspector panel.
  84. */
  85. get orientation(): Inspector.Orientation {
  86. return this._orientation;
  87. }
  88. set orientation(orientation: Inspector.Orientation) {
  89. if (this._orientation === orientation) {
  90. return;
  91. }
  92. this._orientation = orientation;
  93. Object.keys(this._items).forEach(i => {
  94. this._items[i].orientation = orientation;
  95. });
  96. }
  97. /**
  98. * Set the reference to the semantic parent of the inspector panel.
  99. */
  100. get reference(): Inspector.IInspectable {
  101. return this._reference;
  102. }
  103. set reference(reference: Inspector.IInspectable) {
  104. if (this._reference === reference) {
  105. return;
  106. }
  107. // Disconnect old signal handler.
  108. if (this.reference) {
  109. this._reference.inspected.disconnect(this.onInspectorUpdate, this);
  110. }
  111. // Clear the inspector child items (but maintain history).
  112. Object.keys(this._items).forEach(i => this._items[i].content = null);
  113. this._reference = reference;
  114. // Connect new signal handler.
  115. if (this.reference) {
  116. this._reference.inspected.connect(this.onInspectorUpdate, this);
  117. }
  118. }
  119. /**
  120. * Dispose of the resources held by the widget.
  121. */
  122. dispose(): void {
  123. if (this.isDisposed) {
  124. return;
  125. }
  126. // Dispose the inspector child items.
  127. Object.keys(this._items).forEach(i => this._items[i].dispose());
  128. this._items = null;
  129. // Disconnect from reference.
  130. this.reference = null;
  131. super.dispose();
  132. }
  133. /**
  134. * Handle inspector update signals.
  135. */
  136. protected onInspectorUpdate(sender: any, args: Inspector.IInspectorUpdate): void {
  137. let widget = this._items[args.type];
  138. if (!widget) {
  139. return;
  140. }
  141. // Update the content of the inspector widget.
  142. widget.content = args.content;
  143. let items = this._items;
  144. // If any inspector with a higher rank has content, do not change focus.
  145. if (args.content) {
  146. for (let type in items) {
  147. let inspector = this._items[type];
  148. if (inspector.rank < widget.rank && inspector.content) {
  149. return;
  150. }
  151. }
  152. this.currentWidget = widget;
  153. return;
  154. }
  155. // If the inspector was emptied, show the next best ranked inspector.
  156. let lowest = Infinity;
  157. widget = null;
  158. for (let type in items) {
  159. let inspector = this._items[type];
  160. if (inspector.rank < lowest && inspector.content) {
  161. lowest = inspector.rank;
  162. widget = inspector;
  163. }
  164. }
  165. if (widget) {
  166. this.currentWidget = widget;
  167. }
  168. }
  169. private _items: { [type: string]: InspectorItem } = Object.create(null);
  170. private _orientation: Inspector.Orientation = 'horizontal';
  171. private _reference: Inspector.IInspectable = null;
  172. }
  173. /**
  174. * A namespace for Inspector statics.
  175. */
  176. export
  177. namespace Inspector {
  178. /**
  179. * The orientation options of an inspector panel.
  180. */
  181. export
  182. type Orientation = 'horizontal' | 'vertical';
  183. /**
  184. * The definition of an inspector.
  185. */
  186. export
  187. interface IInspectable {
  188. /**
  189. * A signal emitted when an inspector value is generated.
  190. */
  191. inspected: ISignal<any, IInspectorUpdate>;
  192. }
  193. /**
  194. * An update value for code inspectors.
  195. */
  196. export
  197. interface IInspectorUpdate {
  198. /**
  199. * The content being sent to the inspector for display.
  200. */
  201. content: Widget;
  202. /**
  203. * The type of the inspector being updated.
  204. */
  205. type: string;
  206. }
  207. /**
  208. * The definition of a child item of an inspector panel.
  209. */
  210. export
  211. interface IInspectorItem {
  212. /**
  213. * The optional class name added to the inspector child widget.
  214. */
  215. className?: string;
  216. /**
  217. * The display name of the inspector child.
  218. */
  219. name: string;
  220. /**
  221. * The rank order of display priority for inspector updates. A lower rank
  222. * denotes a higher display priority.
  223. */
  224. rank: number;
  225. /**
  226. * A flag that indicates whether the inspector remembers history.
  227. *
  228. * The default value is `false`.
  229. */
  230. remember?: boolean;
  231. /**
  232. * The type of the inspector.
  233. */
  234. type: string;
  235. /**
  236. * The optional console inspector widget instance.
  237. */
  238. widget?: InspectorItem;
  239. }
  240. /**
  241. * The initialization options for a console panel.
  242. */
  243. export
  244. interface IOptions {
  245. /**
  246. * The list of available child inspectors items for code introspection.
  247. *
  248. * #### Notes
  249. * The order of items in the inspectors array is the order in which they
  250. * will be rendered in the inspectors tab panel.
  251. */
  252. items?: IInspectorItem[];
  253. /**
  254. * The orientation of the inspector panel.
  255. *
  256. * The default value is `'horizontal'`.
  257. */
  258. orientation?: Orientation;
  259. }
  260. }
  261. /**
  262. * A code inspector child widget.
  263. */
  264. export
  265. class InspectorItem extends Panel {
  266. /**
  267. * Construct an inspector widget.
  268. */
  269. constructor() {
  270. super();
  271. this.addClass(ITEM_CLASS);
  272. this.update();
  273. }
  274. /**
  275. * The text of the inspector.
  276. */
  277. get content(): Widget {
  278. return this._content;
  279. }
  280. set content(newValue: Widget) {
  281. if (newValue === this._content) {
  282. return;
  283. }
  284. if (this._content) {
  285. if (this._remember) {
  286. this._content.hide();
  287. } else {
  288. this._content.dispose();
  289. }
  290. }
  291. this._content = newValue;
  292. if (this._content) {
  293. this._content.addClass(CONTENT_CLASS);
  294. this.addChild(this._content);
  295. if (this.remember) {
  296. this._history.push(newValue);
  297. this._index++;
  298. }
  299. }
  300. }
  301. /**
  302. * The display orientation of the inspector widget.
  303. */
  304. get orientation(): Inspector.Orientation {
  305. return this._orientation;
  306. }
  307. set orientation(newValue: Inspector.Orientation) {
  308. if (newValue === this._orientation) {
  309. return;
  310. }
  311. this._orientation = newValue;
  312. this.update();
  313. }
  314. /**
  315. * A signal emitted when an inspector widget's orientation is toggled.
  316. */
  317. get orientationToggled(): ISignal<InspectorItem, void> {
  318. return Private.orientationToggledSignal.bind(this);
  319. }
  320. /**
  321. * A flag that indicates whether the inspector remembers history.
  322. */
  323. get remember(): boolean {
  324. return this._remember;
  325. }
  326. set remember(newValue: boolean) {
  327. if (newValue === this._remember) {
  328. return;
  329. }
  330. this._clear();
  331. this._remember = newValue;
  332. if (!this.remember) {
  333. this._history = null;
  334. }
  335. this.update();
  336. }
  337. /**
  338. * The display rank of the inspector.
  339. */
  340. get rank(): number {
  341. return this._rank;
  342. }
  343. set rank(newValue: number) {
  344. this._rank = newValue;
  345. }
  346. /**
  347. * Dispose of the resources held by the widget.
  348. */
  349. dispose(): void {
  350. if (this.isDisposed) {
  351. return;
  352. }
  353. clearSignalData(this);
  354. if (this._history) {
  355. this._history.forEach(widget => widget.dispose());
  356. this._history = null;
  357. }
  358. if (this._toolbar) {
  359. this._toolbar.dispose();
  360. }
  361. super.dispose();
  362. }
  363. /**
  364. * Handle `update_request` messages.
  365. */
  366. protected onUpdateRequest(msg: Message): void {
  367. if (this._toolbar) {
  368. this._toolbar.dispose();
  369. }
  370. this._toolbar = this._createToolbar();
  371. this.insertChild(0, this._toolbar);
  372. }
  373. /**
  374. * Navigate back in history.
  375. */
  376. private _back(): void {
  377. if (this._history.length) {
  378. this._navigateTo(Math.max(this._index - 1, 0));
  379. }
  380. }
  381. /**
  382. * Clear history.
  383. */
  384. private _clear(): void {
  385. if (this._history) {
  386. this._history.forEach(widget => widget.dispose());
  387. }
  388. this._history = [];
  389. this._index = -1;
  390. }
  391. /**
  392. * Navigate forward in history.
  393. */
  394. private _forward(): void {
  395. if (this._history.length) {
  396. this._navigateTo(Math.min(this._index + 1, this._history.length - 1));
  397. }
  398. }
  399. /**
  400. * Create a history toolbar.
  401. */
  402. private _createToolbar(): NotebookToolbar {
  403. let toolbar = new NotebookToolbar();
  404. let toggle = new ToolbarButton({
  405. className: this.orientation === 'vertical' ? RIGHT_TOGGLE_CLASS
  406. : BOTTOM_TOGGLE_CLASS,
  407. onClick: () => this.orientationToggled.emit(void 0),
  408. tooltip: 'Toggle the inspector orientation.'
  409. });
  410. toolbar.add('toggle', toggle);
  411. if (!this._remember) {
  412. return toolbar;
  413. }
  414. let clear = new ToolbarButton({
  415. className: CLEAR_CLASS,
  416. onClick: () => this._clear(),
  417. tooltip: 'Clear history.'
  418. });
  419. toolbar.add('clear', clear);
  420. let back = new ToolbarButton({
  421. className: BACK_CLASS,
  422. onClick: () => this._back(),
  423. tooltip: 'Navigate back in history.'
  424. });
  425. toolbar.add('back', back);
  426. let forward = new ToolbarButton({
  427. className: FORWARD_CLASS,
  428. onClick: () => this._forward(),
  429. tooltip: 'Navigate forward in history.'
  430. });
  431. toolbar.add('forward', forward);
  432. return toolbar;
  433. }
  434. /**
  435. * Navigate to a known index in history.
  436. */
  437. private _navigateTo(index: number): void {
  438. if (this._content) {
  439. this._content.hide();
  440. }
  441. this._content = this._history[index];
  442. this._index = index;
  443. this._content.show();
  444. }
  445. private _content: Widget = null;
  446. private _history: Widget[] = null;
  447. private _index: number = -1;
  448. private _orientation: Inspector.Orientation = 'horizontal';
  449. private _rank: number = Infinity;
  450. private _remember: boolean = false;
  451. private _toolbar: NotebookToolbar = null;
  452. }
  453. /**
  454. * A namespace for inspector private data.
  455. */
  456. namespace Private {
  457. /**
  458. * A signal emitted when an inspector's orientation is toggled.
  459. */
  460. export
  461. const orientationToggledSignal = new Signal<InspectorItem, void>();
  462. }