Helios-Core.st 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. Smalltalk current createPackage: 'Helios-Core' properties: #{}!
  2. Widget subclass: #HLSourceArea
  3. instanceVariableNames: 'editor textarea div receiver onDoIt'
  4. package: 'Helios-Core'!
  5. !HLSourceArea methodsFor: 'accessing'!
  6. contents
  7. ^editor getValue
  8. !
  9. contents: aString
  10. editor setValue: aString
  11. !
  12. currentLine
  13. ^editor getLine: (editor getCursor line)
  14. !
  15. currentLineOrSelection
  16. ^editor somethingSelected
  17. ifFalse: [self currentLine]
  18. ifTrue: [self selection]
  19. !
  20. editor
  21. ^editor
  22. !
  23. onDoIt
  24. ^onDoIt
  25. !
  26. onDoIt: aBlock
  27. onDoIt := aBlock
  28. !
  29. receiver
  30. ^receiver ifNil: [DoIt new]
  31. !
  32. receiver: anObject
  33. receiver := anObject
  34. !
  35. selection
  36. ^editor getSelection
  37. !
  38. selectionEnd
  39. ^textarea element selectionEnd
  40. !
  41. selectionEnd: anInteger
  42. textarea element selectionEnd: anInteger
  43. !
  44. selectionStart
  45. ^textarea element selectionStart
  46. !
  47. selectionStart: anInteger
  48. textarea element selectionStart: anInteger
  49. !
  50. setEditorOn: aTextarea
  51. <self['@editor'] = CodeMirror.fromTextArea(aTextarea, {
  52. theme: 'amber',
  53. lineNumbers: true,
  54. enterMode: 'flat',
  55. matchBrackets: true,
  56. electricChars: false
  57. })>
  58. !
  59. val
  60. ^editor getValue
  61. !
  62. val: aString
  63. editor setValue: aString
  64. ! !
  65. !HLSourceArea methodsFor: 'actions'!
  66. clear
  67. self contents: ''
  68. !
  69. doIt
  70. | result |
  71. result := self eval: self currentLineOrSelection.
  72. self onDoIt ifNotNil: [self onDoIt value].
  73. ^result
  74. !
  75. eval: aString
  76. | compiler |
  77. compiler := Compiler new.
  78. [compiler parseExpression: aString] on: Error do: [:ex |
  79. ^window alert: ex messageText].
  80. ^(compiler eval: (compiler compile: 'doIt ^[', aString, '] value' forClass: DoIt)) fn applyTo: self receiver arguments: #()
  81. !
  82. fileIn
  83. Importer new import: self currentLineOrSelection readStream
  84. !
  85. handleKeyDown: anEvent
  86. <if(anEvent.ctrlKey) {
  87. if(anEvent.keyCode === 80) { //ctrl+p
  88. self._printIt();
  89. anEvent.preventDefault();
  90. return false;
  91. }
  92. if(anEvent.keyCode === 68) { //ctrl+d
  93. self._doIt();
  94. anEvent.preventDefault();
  95. return false;
  96. }
  97. if(anEvent.keyCode === 73) { //ctrl+i
  98. self._inspectIt();
  99. anEvent.preventDefault();
  100. return false;
  101. }
  102. }>
  103. !
  104. inspectIt
  105. self doIt inspect
  106. !
  107. print: aString
  108. | start stop |
  109. start := HashedCollection new.
  110. stop := HashedCollection new.
  111. start at: 'line' put: (editor getCursor: false) line.
  112. start at: 'ch' put: (editor getCursor: false) ch.
  113. stop at: 'line' put: (start at: 'line').
  114. stop at: 'ch' put: ((start at: 'ch') + aString size + 2).
  115. editor replaceSelection: (editor getSelection, ' ', aString, ' ').
  116. editor setCursor: (editor getCursor: true).
  117. editor setSelection: stop end: start
  118. !
  119. printIt
  120. self print: self doIt printString
  121. ! !
  122. !HLSourceArea methodsFor: 'events'!
  123. onKeyDown: aBlock
  124. div onKeyDown: aBlock
  125. !
  126. onKeyUp: aBlock
  127. div onKeyUp: aBlock
  128. ! !
  129. !HLSourceArea methodsFor: 'rendering'!
  130. renderOn: html
  131. div := html div class: 'source'.
  132. div with: [textarea := html textarea].
  133. self setEditorOn: textarea element.
  134. div onKeyDown: [:e | self handleKeyDown: e]
  135. ! !
  136. Object subclass: #HLTab
  137. instanceVariableNames: 'widget label'
  138. package: 'Helios-Core'!
  139. !HLTab methodsFor: 'accessing'!
  140. activate
  141. self manager activate: self
  142. !
  143. add
  144. self manager addTab: self
  145. !
  146. label
  147. ^ label ifNil: [ '' ]
  148. !
  149. label: aString
  150. label := aString
  151. !
  152. manager
  153. ^ HLManager current
  154. !
  155. widget
  156. ^ widget
  157. !
  158. widget: aWidget
  159. widget := aWidget
  160. ! !
  161. !HLTab methodsFor: 'testing'!
  162. isActive
  163. ^ self manager activeTab = self
  164. ! !
  165. !HLTab class methodsFor: 'instance creation'!
  166. on: aWidget labelled: aString
  167. ^ self new
  168. widget: aWidget;
  169. label: aString;
  170. yourself
  171. ! !
  172. Widget subclass: #HLWidget
  173. instanceVariableNames: 'rootDiv'
  174. package: 'Helios-Core'!
  175. !HLWidget methodsFor: 'accessing'!
  176. manager
  177. ^ HLManager current
  178. ! !
  179. !HLWidget methodsFor: 'announcements'!
  180. subscribeTo: anAnnouncer
  181. ! !
  182. !HLWidget methodsFor: 'keybindings'!
  183. registerBindings
  184. self registerBindingsOn: self manager keyBinder bindings
  185. !
  186. registerBindingsOn: aBindingGroup
  187. ! !
  188. !HLWidget methodsFor: 'rendering'!
  189. renderContentOn: html
  190. !
  191. renderOn: html
  192. self registerBindings.
  193. rootDiv := html div with: [
  194. self renderContentOn: html ]
  195. ! !
  196. !HLWidget methodsFor: 'updating'!
  197. refresh
  198. rootDiv ifNil: [ ^ self ].
  199. rootDiv asJQuery empty.
  200. [ :html | self renderContentOn: html ] appendToJQuery: rootDiv asJQuery
  201. ! !
  202. !HLWidget class methodsFor: 'accessing'!
  203. openAsTab
  204. HLManager current addTab: (HLTab on: self new labelled: self tabLabel)
  205. !
  206. tabLabel
  207. ^ 'Tab'
  208. !
  209. tabPriority
  210. ^ 500
  211. ! !
  212. !HLWidget class methodsFor: 'testing'!
  213. canBeOpenAsTab
  214. ^ false
  215. ! !
  216. HLWidget subclass: #HLDebugger
  217. instanceVariableNames: ''
  218. package: 'Helios-Core'!
  219. HLWidget subclass: #HLFocusableWidget
  220. instanceVariableNames: 'hiddenInput'
  221. package: 'Helios-Core'!
  222. !HLFocusableWidget methodsFor: 'accessing'!
  223. focusClass
  224. ^ 'focused'
  225. ! !
  226. !HLFocusableWidget methodsFor: 'events'!
  227. blur
  228. hiddenInput asJQuery blur
  229. !
  230. focus
  231. hiddenInput asJQuery focus
  232. !
  233. hasFocus
  234. ^ rootDiv notNil and: [ rootDiv asJQuery hasClass: self focusClass ]
  235. ! !
  236. !HLFocusableWidget methodsFor: 'rendering'!
  237. renderContentOn: html
  238. !
  239. renderHiddenInputOn: html
  240. hiddenInput := html input
  241. style: 'position: absolute; left: -100000px;';
  242. onBlur: [ rootDiv asJQuery removeClass: self focusClass ];
  243. onFocus: [ rootDiv asJQuery addClass: self focusClass ]
  244. !
  245. renderOn: html
  246. self registerBindings.
  247. self renderHiddenInputOn: html.
  248. rootDiv := html div
  249. class: 'hl_widget';
  250. onClick: [ hiddenInput asJQuery focus ];
  251. with: [
  252. self renderContentOn: html ]
  253. ! !
  254. HLFocusableWidget subclass: #HLListWidget
  255. instanceVariableNames: 'items selectedItem'
  256. package: 'Helios-Core'!
  257. !HLListWidget methodsFor: 'accessing'!
  258. cssClassForItem: anObject
  259. ^ self selectedItem = anObject
  260. ifTrue: [ 'active' ]
  261. ifFalse: [ 'inactive' ]
  262. !
  263. iconForItem: anObject
  264. ^ ''
  265. !
  266. items
  267. ^ items ifNil: [ #() ]
  268. !
  269. items: aCollection
  270. items := aCollection
  271. !
  272. selectedItem
  273. ^ selectedItem
  274. !
  275. selectedItem: anObject
  276. selectedItem := anObject
  277. ! !
  278. !HLListWidget methodsFor: 'actions'!
  279. activateListItem: aListItem
  280. | parent position |
  281. (aListItem get: 0) ifNil: [ ^self ].
  282. <position = aListItem.parent().children().get().indexOf(aListItem.get(0)) + 1>.
  283. parent := aListItem parent.
  284. parent children removeClass: 'active'.
  285. aListItem addClass: 'active'.
  286. "Move the scrollbar to show the active element"
  287. aListItem position top < 0 ifTrue: [
  288. (parent get: 0) scrollTop: ((parent get: 0) scrollTop + aListItem position top - 10) ].
  289. aListItem position top + aListItem height > parent height ifTrue: [
  290. (parent get: 0) scrollTop: ((parent get: 0) scrollTop + aListItem height - (parent height - aListItem position top)) +10 ].
  291. "Activate the corresponding item"
  292. self selectItem: (self items at: (aListItem attr: 'list-data') asNumber)
  293. !
  294. selectItem: anObject
  295. self selectedItem: anObject
  296. ! !
  297. !HLListWidget methodsFor: 'events'!
  298. setupKeyBindings
  299. hiddenInput asJQuery unbind: 'keydown'.
  300. hiddenInput asJQuery keydown: [ :e | | selected |
  301. selected := window jQuery: '.focused .nav-pills .active'.
  302. e which = 38 ifTrue: [
  303. self activateListItem: selected prev ].
  304. e which = 40 ifTrue: [
  305. self activateListItem: selected next ] ]
  306. ! !
  307. !HLListWidget methodsFor: 'rendering'!
  308. renderButtonsOn: html
  309. !
  310. renderContentOn: html
  311. html ul
  312. class: 'nav nav-pills nav-stacked';
  313. with: [ self renderListOn: html ].
  314. html div class: 'pane_actions form-actions'; with: [
  315. self renderButtonsOn: html ].
  316. self setupKeyBindings
  317. !
  318. renderItem: anObject on: html
  319. | li |
  320. li := html li.
  321. li
  322. class: (self cssClassForItem: anObject);
  323. at: 'list-data' put: (self items indexOf: anObject) asString;
  324. with: [
  325. html a
  326. with: [
  327. (html tag: 'i') class: (self iconForItem: anObject).
  328. self renderItemLabel: anObject on: html ];
  329. onClick: [
  330. self activateListItem: li asJQuery.
  331. "self selectItem: anObject" ] ]
  332. !
  333. renderItemLabel: anObject on: html
  334. html with: anObject asString
  335. !
  336. renderListOn: html
  337. self items do: [ :each |
  338. self renderItem: each on: html ]
  339. ! !
  340. HLListWidget subclass: #HLNavigationListWidget
  341. instanceVariableNames: 'previous next'
  342. package: 'Helios-Core'!
  343. !HLNavigationListWidget methodsFor: 'accessing'!
  344. next
  345. ^ next
  346. !
  347. next: aWidget
  348. next := aWidget.
  349. aWidget previous: self
  350. !
  351. previous
  352. ^ previous
  353. !
  354. previous: aWidget
  355. previous := aWidget
  356. ! !
  357. !HLNavigationListWidget methodsFor: 'actions'!
  358. nextFocus
  359. self next ifNotNil: [ self next focus ]
  360. !
  361. previousFocus
  362. self previous ifNotNil: [ self previous focus ]
  363. ! !
  364. !HLNavigationListWidget methodsFor: 'events'!
  365. setupKeyBindings
  366. super setupKeyBindings.
  367. hiddenInput asJQuery keydown: [ :e |
  368. e which = 39 ifTrue: [
  369. self nextFocus ].
  370. e which = 37 ifTrue: [
  371. self previousFocus ] ]
  372. ! !
  373. HLWidget subclass: #HLInspector
  374. instanceVariableNames: ''
  375. package: 'Helios-Core'!
  376. HLWidget subclass: #HLManager
  377. instanceVariableNames: 'tabs activeTab keyBinder'
  378. package: 'Helios-Core'!
  379. !HLManager methodsFor: 'accessing'!
  380. activeTab
  381. ^ activeTab
  382. !
  383. keyBinder
  384. ^ keyBinder ifNil: [ keyBinder := HLKeyBinder new ]
  385. !
  386. tabs
  387. ^ tabs ifNil: [ tabs := OrderedCollection new ]
  388. ! !
  389. !HLManager methodsFor: 'actions'!
  390. activate: aTab
  391. self keyBinder flushBindings.
  392. activeTab := aTab.
  393. self
  394. refresh;
  395. show: aTab
  396. !
  397. addTab: aTab
  398. self tabs add: aTab.
  399. self activate: aTab
  400. !
  401. removeTab: aTab
  402. "Todo: activate the previously activated tab. Keep a history of tabs selection"
  403. (self tabs includes: aTab) ifFalse: [ ^ self ].
  404. self tabs remove: aTab.
  405. self refresh
  406. ! !
  407. !HLManager methodsFor: 'initialization'!
  408. initialize
  409. super initialize.
  410. self keyBinder setupEvents
  411. ! !
  412. !HLManager methodsFor: 'rendering'!
  413. refresh
  414. (window jQuery: '.navbar') remove.
  415. (window jQuery: '#container') remove.
  416. self appendToJQuery: 'body' asJQuery
  417. !
  418. renderAddOn: html
  419. html li
  420. class: 'dropdown';
  421. with: [
  422. html a
  423. class: 'dropdown-toggle';
  424. at: 'data-toggle' put: 'dropdown';
  425. with: [
  426. html with: 'Open...'.
  427. (html tag: 'b') class: 'caret' ].
  428. html ul
  429. class: 'dropdown-menu';
  430. with: [
  431. ((HLWidget withAllSubclasses
  432. select: [ :each | each canBeOpenAsTab ])
  433. sorted: [ :a :b | a tabPriority < b tabPriority ])
  434. do: [ :each |
  435. html li with: [
  436. html a
  437. with: each tabLabel;
  438. onClick: [ each openAsTab ] ] ] ] ]
  439. !
  440. renderContentOn: html
  441. html div
  442. class: 'navbar navbar-fixed-top';
  443. with: [ html div
  444. class: 'navbar-inner';
  445. with: [ self renderTabsOn: html ] ].
  446. html div id: 'container'
  447. !
  448. renderTabsOn: html
  449. html ul
  450. class: 'nav';
  451. with: [
  452. self tabs do: [ :each |
  453. html li
  454. class: (each isActive ifTrue: [ 'active' ] ifFalse: [ 'inactive' ]);
  455. with: [
  456. html a
  457. with: [
  458. ((html tag: 'i') class: 'icon-remove-circle')
  459. onClick: [ self removeTab: each ].
  460. html with: each label ];
  461. onClick: [ each activate ] ] ].
  462. self renderAddOn: html ]
  463. !
  464. show: aTab
  465. (window jQuery: '#container') empty.
  466. aTab widget appendToJQuery: '#container' asJQuery
  467. ! !
  468. HLManager class instanceVariableNames: 'current'!
  469. !HLManager class methodsFor: 'accessing'!
  470. current
  471. ^ current ifNil: [ current := self basicNew initialize ]
  472. ! !
  473. !HLManager class methodsFor: 'initialization'!
  474. initialize
  475. self current appendToJQuery: 'body' asJQuery
  476. ! !
  477. !HLManager class methodsFor: 'instance creation'!
  478. new
  479. "Use current instead"
  480. self shouldNotImplement
  481. ! !
  482. HLWidget subclass: #HLSUnit
  483. instanceVariableNames: ''
  484. package: 'Helios-Core'!
  485. !HLSUnit class methodsFor: 'accessing'!
  486. tabLabel
  487. ^ 'SUnit'
  488. !
  489. tabPriority
  490. ^ 1000
  491. ! !
  492. !HLSUnit class methodsFor: 'testing'!
  493. canBeOpenAsTab
  494. ^ true
  495. ! !
  496. HLWidget subclass: #HLTranscript
  497. instanceVariableNames: ''
  498. package: 'Helios-Core'!
  499. !HLTranscript class methodsFor: 'accessing'!
  500. tabLabel
  501. ^ 'Transcript'
  502. !
  503. tabPriority
  504. ^ 600
  505. ! !
  506. !HLTranscript class methodsFor: 'testing'!
  507. canBeOpenAsTab
  508. ^ true
  509. ! !
  510. HLWidget subclass: #HLWorkspace
  511. instanceVariableNames: ''
  512. package: 'Helios-Core'!
  513. !HLWorkspace class methodsFor: 'accessing'!
  514. tabLabel
  515. ^ 'Workspace'
  516. !
  517. tabPriority
  518. ^ 10
  519. ! !
  520. !HLWorkspace class methodsFor: 'testing'!
  521. canBeOpenAsTab
  522. ^ true
  523. ! !