model.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. 'use strict';
  4. import {
  5. IChangedArgs, Property
  6. } from 'phosphor-properties';
  7. import {
  8. ISignal, Signal
  9. } from 'phosphor-signaling';
  10. import {
  11. IEditorModel, IEditorOptions, EditorModel
  12. } from '../editor';
  13. /**
  14. * The model for an input area.
  15. */
  16. export
  17. interface IInputAreaModel {
  18. /**
  19. * A signal emitted when state of the input area changes.
  20. */
  21. stateChanged: ISignal<IInputAreaModel, IChangedArgs<any>>;
  22. /**
  23. * The text editor model.
  24. */
  25. textEditor: IEditorModel;
  26. /**
  27. * Whether the input area should be collapsed (hidden) or expanded.
  28. */
  29. collapsed: boolean;
  30. /**
  31. * The prompt text to display for the input area.
  32. */
  33. prompt: string;
  34. /**
  35. * The dirty state of the input cell.
  36. */
  37. dirty: boolean;
  38. /**
  39. * The read only state of the input cell.
  40. */
  41. readOnly: boolean;
  42. }
  43. /**
  44. * The options for creating an input area.
  45. */
  46. export
  47. interface IInputAreaOptions extends IEditorOptions { }
  48. /**
  49. * An implementation of an input area model.
  50. */
  51. export
  52. class InputAreaModel implements IInputAreaModel {
  53. /**
  54. * Construct a new input area model.
  55. */
  56. constructor(options?: IInputAreaOptions) {
  57. this._editor = new EditorModel(options);
  58. this._editor.stateChanged.connect(this.onEditorChanged, this);
  59. }
  60. /**
  61. * A signal emitted when the state of the model changes.
  62. */
  63. get stateChanged() {
  64. return InputAreaModelPrivate.stateChangedSignal.bind(this);
  65. }
  66. /**
  67. * Get whether the input area should be collapsed or displayed.
  68. */
  69. get collapsed() {
  70. return InputAreaModelPrivate.collapsedProperty.get(this);
  71. }
  72. /**
  73. * Set whether the input area should be collapsed or displayed.
  74. */
  75. set collapsed(value: boolean) {
  76. InputAreaModelPrivate.collapsedProperty.set(this, value);
  77. }
  78. /**
  79. * Get the prompt text.
  80. */
  81. get prompt() {
  82. return InputAreaModelPrivate.promptProperty.get(this);
  83. }
  84. /**
  85. * Set the prompt text.
  86. */
  87. set prompt(value: string) {
  88. InputAreaModelPrivate.promptProperty.set(this, value);
  89. }
  90. /**
  91. * Get the text editor Model.
  92. *
  93. * #### Notes
  94. * This is a read-only property.
  95. */
  96. get textEditor(): EditorModel {
  97. return this._editor;
  98. }
  99. /**
  100. * Get the dirty state.
  101. *
  102. * #### Notest
  103. * This is a delegate to the dirty state of the [textEditor].
  104. */
  105. get dirty(): boolean {
  106. return this.textEditor.dirty;
  107. }
  108. /**
  109. * Set the dirty state.
  110. *
  111. * #### Notest
  112. * This is a delegate to the dirty state of the [textEditor].
  113. */
  114. set dirty(value: boolean) {
  115. this.textEditor.dirty = value;
  116. }
  117. /**
  118. * Get the read only state.
  119. */
  120. get readOnly(): boolean {
  121. return this.textEditor.readOnly;
  122. }
  123. /**
  124. * Set the read only state.
  125. */
  126. set readOnly(value: boolean) {
  127. this.textEditor.readOnly = value;
  128. }
  129. /**
  130. * Handle changes to the editor state.
  131. */
  132. protected onEditorChanged(editor: EditorModel, args: IChangedArgs<any>): void {
  133. if (args.name === 'dirty') {
  134. // Re-emit dirty state changes from the editor.
  135. this.stateChanged.emit(args);
  136. }
  137. }
  138. private _editor: EditorModel = null;
  139. }
  140. /**
  141. * The namespace for the `InputAreaModel` class private data.
  142. */
  143. namespace InputAreaModelPrivate {
  144. /**
  145. * A signal emitted when the state of the model changes.
  146. */
  147. export
  148. const stateChangedSignal = new Signal<InputAreaModel, IChangedArgs<any>>();
  149. /**
  150. * A property descriptor which determines whether the input area is collapsed or displayed.
  151. */
  152. export
  153. const collapsedProperty = new Property<InputAreaModel, boolean>({
  154. name: 'collapsed',
  155. notify: stateChangedSignal,
  156. });
  157. /**
  158. * A property descriptor containing the prompt.
  159. */
  160. export
  161. const promptProperty = new Property<InputAreaModel, string>({
  162. name: 'prompt',
  163. notify: stateChangedSignal,
  164. });
  165. }