Helios-Core.st 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. Smalltalk current createPackage: 'Helios-Core'!
  2. Object subclass: #HLModel
  3. instanceVariableNames: 'announcer environment'
  4. package: 'Helios-Core'!
  5. !HLModel methodsFor: 'accessing'!
  6. announcer
  7. ^ announcer ifNil: [ announcer := Announcer new ]
  8. !
  9. environment
  10. ^ environment ifNil: [ self manager environment ]
  11. !
  12. environment: anEnvironment
  13. environment := anEnvironment
  14. !
  15. manager
  16. ^ HLManager current
  17. !
  18. systemAnnouncer
  19. ^ self environment systemAnnouncer
  20. ! !
  21. Widget subclass: #HLTab
  22. instanceVariableNames: 'widget label root'
  23. package: 'Helios-Core'!
  24. !HLTab methodsFor: 'accessing'!
  25. activate
  26. self manager activate: self
  27. !
  28. add
  29. self manager addTab: self
  30. !
  31. displayLabel
  32. ^ self label size > 20
  33. ifTrue: [ (self label first: 20), '...' ]
  34. ifFalse: [ self label ]
  35. !
  36. focus
  37. self widget canHaveFocus ifTrue: [
  38. self widget focus ]
  39. !
  40. label
  41. ^ label ifNil: [ '' ]
  42. !
  43. label: aString
  44. label := aString
  45. !
  46. manager
  47. ^ HLManager current
  48. !
  49. widget
  50. ^ widget
  51. !
  52. widget: aWidget
  53. widget := aWidget
  54. ! !
  55. !HLTab methodsFor: 'actions'!
  56. hide
  57. root ifNotNil: [ root asJQuery css: 'visibility' put: 'hidden' ]
  58. !
  59. registerBindings
  60. self widget registerBindings
  61. !
  62. remove
  63. root ifNotNil: [ root asJQuery remove ]
  64. !
  65. show
  66. root
  67. ifNil: [ self appendToJQuery: 'body' asJQuery ]
  68. ifNotNil: [ root asJQuery css: 'visibility' put: 'visible' ]
  69. ! !
  70. !HLTab methodsFor: 'rendering'!
  71. renderOn: html
  72. root := html div
  73. class: 'tab';
  74. yourself.
  75. self renderTab
  76. !
  77. renderTab
  78. root contents: [ :html |
  79. html div
  80. class: 'amber_box';
  81. with: [ self widget renderOn: html ] ]
  82. ! !
  83. !HLTab methodsFor: 'testing'!
  84. isActive
  85. ^ self manager activeTab = self
  86. ! !
  87. !HLTab class methodsFor: 'instance creation'!
  88. on: aWidget labelled: aString
  89. ^ self new
  90. widget: aWidget;
  91. label: aString;
  92. yourself
  93. ! !
  94. Widget subclass: #HLWidget
  95. instanceVariableNames: 'wrapper'
  96. package: 'Helios-Core'!
  97. !HLWidget methodsFor: 'accessing'!
  98. manager
  99. ^ HLManager current
  100. !
  101. wrapper
  102. ^ wrapper
  103. ! !
  104. !HLWidget methodsFor: 'actions'!
  105. alert: aString
  106. window alert: aString
  107. !
  108. confirm: aString
  109. ^ window confirm: aString
  110. !
  111. execute: aCommand
  112. HLManager current keyBinder
  113. activate;
  114. applyBinding: aCommand asBinding
  115. ! !
  116. !HLWidget methodsFor: 'keybindings'!
  117. registerBindings
  118. self registerBindingsOn: self manager keyBinder bindings
  119. !
  120. registerBindingsOn: aBindingGroup
  121. ! !
  122. !HLWidget methodsFor: 'rendering'!
  123. renderContentOn: html
  124. !
  125. renderOn: html
  126. wrapper := html div.
  127. [ :renderer | self renderContentOn: renderer ] appendToJQuery: wrapper asJQuery
  128. ! !
  129. !HLWidget methodsFor: 'testing'!
  130. canHaveFocus
  131. ^ false
  132. ! !
  133. !HLWidget methodsFor: 'updating'!
  134. refresh
  135. self wrapper ifNil: [ ^ self ].
  136. self wrapper asJQuery empty.
  137. [ :html | self renderContentOn: html ] appendToJQuery: self wrapper asJQuery
  138. ! !
  139. !HLWidget class methodsFor: 'accessing'!
  140. openAsTab
  141. self canBeOpenAsTab ifFalse: [ ^ self ].
  142. HLManager current addTab: (HLTab on: self new labelled: self tabLabel)
  143. !
  144. tabLabel
  145. ^ 'Tab'
  146. !
  147. tabPriority
  148. ^ 500
  149. ! !
  150. !HLWidget class methodsFor: 'testing'!
  151. canBeOpenAsTab
  152. ^ false
  153. ! !
  154. HLWidget subclass: #HLDebugger
  155. instanceVariableNames: ''
  156. package: 'Helios-Core'!
  157. HLWidget subclass: #HLFocusableWidget
  158. instanceVariableNames: ''
  159. package: 'Helios-Core'!
  160. !HLFocusableWidget methodsFor: 'accessing'!
  161. focusClass
  162. ^ 'focused'
  163. ! !
  164. !HLFocusableWidget methodsFor: 'events'!
  165. blur
  166. self wrapper asJQuery blur
  167. !
  168. focus
  169. self wrapper asJQuery focus
  170. !
  171. hasFocus
  172. ^ self wrapper notNil and: [ self wrapper asJQuery is: ':focus' ]
  173. ! !
  174. !HLFocusableWidget methodsFor: 'rendering'!
  175. renderContentOn: html
  176. !
  177. renderOn: html
  178. self registerBindings.
  179. wrapper := html div
  180. class: 'hl_widget';
  181. yourself.
  182. wrapper with: [ self renderContentOn: html ].
  183. wrapper
  184. at: 'tabindex' put: '0';
  185. onBlur: [ self wrapper asJQuery removeClass: self focusClass ];
  186. onFocus: [ self wrapper asJQuery addClass: self focusClass ]
  187. ! !
  188. !HLFocusableWidget methodsFor: 'testing'!
  189. canHaveFocus
  190. ^ true
  191. ! !
  192. HLFocusableWidget subclass: #HLListWidget
  193. instanceVariableNames: 'items selectedItem mapping'
  194. package: 'Helios-Core'!
  195. !HLListWidget methodsFor: 'accessing'!
  196. cssClassForItem: anObject
  197. ^ self selectedItem = anObject
  198. ifTrue: [ 'active' ]
  199. ifFalse: [ 'inactive' ]
  200. !
  201. iconForItem: anObject
  202. ^ ''
  203. !
  204. items
  205. ^ items ifNil: [ items := self defaultItems ]
  206. !
  207. items: aCollection
  208. items := aCollection
  209. !
  210. positionOf: aListItem
  211. <
  212. return aListItem.parent().children().get().indexOf(aListItem.get(0)) + 1
  213. >
  214. !
  215. selectedItem
  216. ^ selectedItem
  217. !
  218. selectedItem: anObject
  219. selectedItem := anObject
  220. ! !
  221. !HLListWidget methodsFor: 'actions'!
  222. activateFirstListItem
  223. self activateListItem: (window jQuery: ((wrapper asJQuery find: 'li.inactive') get: 0))
  224. !
  225. activateItem: anObject
  226. self activateListItem: (mapping
  227. at: anObject
  228. ifAbsent: [ ^ self ]) asJQuery
  229. !
  230. activateListItem: aListItem
  231. | item |
  232. (aListItem get: 0) ifNil: [ ^self ].
  233. aListItem parent children removeClass: 'active'.
  234. aListItem addClass: 'active'.
  235. self ensureVisible: aListItem.
  236. "Activate the corresponding item"
  237. item := (self items at: (aListItem attr: 'list-data') asNumber).
  238. self selectedItem == item ifFalse: [
  239. self selectItem: item ]
  240. !
  241. activateNextListItem
  242. self activateListItem: (window jQuery: '.focused .nav-pills .active') next.
  243. "select the first item if none is selected"
  244. (window jQuery: '.focused .nav-pills .active') get ifEmpty: [
  245. self activateFirstListItem ]
  246. !
  247. activatePreviousListItem
  248. self activateListItem: (window jQuery: '.focused .nav-pills .active') prev
  249. !
  250. ensureVisible: aListItem
  251. "Move the scrollbar to show the active element"
  252. | perent position |
  253. position := self positionOf: aListItem.
  254. parent := aListItem parent.
  255. aListItem position top < 0 ifTrue: [
  256. (parent get: 0) scrollTop: ((parent get: 0) scrollTop + aListItem position top - 10) ].
  257. aListItem position top + aListItem height > parent height ifTrue: [
  258. (parent get: 0) scrollTop: ((parent get: 0) scrollTop + aListItem height - (parent height - aListItem position top)) +10 ]
  259. !
  260. focus
  261. super focus.
  262. self items isEmpty ifFalse: [
  263. self selectedItem ifNil: [ self activateFirstListItem ] ]
  264. !
  265. refresh
  266. super refresh.
  267. self ensureVisible: (mapping
  268. at: self selectedItem
  269. ifAbsent: [ ^ self ]) asJQuery
  270. !
  271. selectItem: anObject
  272. self selectedItem: anObject
  273. ! !
  274. !HLListWidget methodsFor: 'defaults'!
  275. defaultItems
  276. ^ #()
  277. ! !
  278. !HLListWidget methodsFor: 'events'!
  279. setupKeyBindings
  280. "TODO: refactor this!!"
  281. | active interval delay repeatInterval |
  282. active := false.
  283. repeatInterval := 70.
  284. self wrapper asJQuery unbind: 'keydown'.
  285. self wrapper asJQuery keydown: [ :e |
  286. (e which = 38 and: [ active = false ]) ifTrue: [
  287. active := true.
  288. self activatePreviousListItem.
  289. delay := [
  290. interval := [ self activatePreviousListItem ]
  291. valueWithInterval: repeatInterval ]
  292. valueWithTimeout: 300 ].
  293. (e which = 40 and: [ active = false ]) ifTrue: [
  294. active := true.
  295. self activateNextListItem.
  296. delay := [
  297. interval := [ self activateNextListItem ]
  298. valueWithInterval: repeatInterval ]
  299. valueWithTimeout: 300 ] ].
  300. self wrapper asJQuery keyup: [ :e |
  301. active ifTrue: [
  302. active := false.
  303. interval ifNotNil: [ interval clearInterval ].
  304. delay ifNotNil: [ delay clearTimeout] ] ]
  305. ! !
  306. !HLListWidget methodsFor: 'initialization'!
  307. initialize
  308. super initialize.
  309. mapping := Dictionary new.
  310. ! !
  311. !HLListWidget methodsFor: 'private'!
  312. registerMappingFrom: anObject to: aTag
  313. mapping at: anObject put: aTag
  314. ! !
  315. !HLListWidget methodsFor: 'rendering'!
  316. renderButtonsOn: html
  317. !
  318. renderContentOn: html
  319. html ul
  320. class: 'nav nav-pills nav-stacked';
  321. with: [ self renderListOn: html ].
  322. html div class: 'pane_actions form-actions'; with: [
  323. self renderButtonsOn: html ].
  324. self setupKeyBindings
  325. !
  326. renderItem: anObject on: html
  327. | li |
  328. li := html li.
  329. self registerMappingFrom: anObject to: li.
  330. li
  331. class: (self cssClassForItem: anObject);
  332. at: 'list-data' put: (self items indexOf: anObject) asString;
  333. with: [
  334. html a
  335. with: [
  336. (html tag: 'i') class: (self iconForItem: anObject).
  337. self renderItemLabel: anObject on: html ];
  338. onClick: [
  339. self activateListItem: li asJQuery ] ]
  340. !
  341. renderItemLabel: anObject on: html
  342. html with: anObject asString
  343. !
  344. renderListOn: html
  345. mapping := Dictionary new.
  346. self items do: [ :each |
  347. self renderItem: each on: html ]
  348. ! !
  349. HLListWidget subclass: #HLNavigationListWidget
  350. instanceVariableNames: 'previous next'
  351. package: 'Helios-Core'!
  352. !HLNavigationListWidget methodsFor: 'accessing'!
  353. next
  354. ^ next
  355. !
  356. next: aWidget
  357. next := aWidget.
  358. aWidget previous = self ifFalse: [ aWidget previous: self ]
  359. !
  360. previous
  361. ^ previous
  362. !
  363. previous: aWidget
  364. previous := aWidget.
  365. aWidget next = self ifFalse: [ aWidget next: self ]
  366. ! !
  367. !HLNavigationListWidget methodsFor: 'actions'!
  368. nextFocus
  369. self next ifNotNil: [ self next focus ]
  370. !
  371. previousFocus
  372. self previous ifNotNil: [ self previous focus ]
  373. ! !
  374. !HLNavigationListWidget methodsFor: 'events'!
  375. setupKeyBindings
  376. super setupKeyBindings.
  377. self wrapper asJQuery keydown: [ :e |
  378. e which = 39 ifTrue: [
  379. self nextFocus ].
  380. e which = 37 ifTrue: [
  381. self previousFocus ] ]
  382. ! !
  383. HLWidget subclass: #HLManager
  384. instanceVariableNames: 'tabs activeTab keyBinder environment history'
  385. package: 'Helios-Core'!
  386. !HLManager methodsFor: 'accessing'!
  387. activeTab
  388. ^ activeTab
  389. !
  390. environment
  391. "The default environment used by all Helios objects"
  392. ^ environment ifNil: [ environment := self defaultEnvironment ]
  393. !
  394. environment: anEnvironment
  395. environment := anEnvironment
  396. !
  397. history
  398. ^ history ifNil: [ history := OrderedCollection new ]
  399. !
  400. history: aCollection
  401. history := aCollection
  402. !
  403. keyBinder
  404. ^ keyBinder ifNil: [ keyBinder := HLKeyBinder new ]
  405. !
  406. tabs
  407. ^ tabs ifNil: [ tabs := OrderedCollection new ]
  408. ! !
  409. !HLManager methodsFor: 'actions'!
  410. activate: aTab
  411. self keyBinder flushBindings.
  412. aTab registerBindings.
  413. activeTab := aTab.
  414. self
  415. refresh;
  416. addToHistory: aTab;
  417. show: aTab
  418. !
  419. addTab: aTab
  420. self tabs add: aTab.
  421. self activate: aTab
  422. !
  423. addToHistory: aTab
  424. self removeFromHistory: aTab.
  425. self history add: aTab
  426. !
  427. removeActiveTab
  428. self removeTab: self activeTab
  429. !
  430. removeFromHistory: aTab
  431. self history: (self history reject: [ :each | each == aTab ])
  432. !
  433. removeTab: aTab
  434. (self tabs includes: aTab) ifFalse: [ ^ self ].
  435. self removeFromHistory: aTab.
  436. self tabs remove: aTab.
  437. self keyBinder flushBindings.
  438. aTab remove.
  439. self refresh.
  440. self history ifNotEmpty: [
  441. self history last activate ]
  442. ! !
  443. !HLManager methodsFor: 'defaults'!
  444. defaultEnvironment
  445. "If helios is loaded from within a frame, answer the parent window environment"
  446. window parent ifNil: [ ^ Environment new ].
  447. ^ ((window parent at: 'smalltalk')
  448. at: 'Environment') new
  449. ! !
  450. !HLManager methodsFor: 'initialization'!
  451. initialize
  452. super initialize.
  453. self keyBinder setupEvents
  454. ! !
  455. !HLManager methodsFor: 'rendering'!
  456. refresh
  457. (window jQuery: '.navbar') remove.
  458. self appendToJQuery: 'body' asJQuery
  459. !
  460. renderAddOn: html
  461. html li
  462. class: 'dropdown';
  463. with: [
  464. html a
  465. class: 'dropdown-toggle';
  466. at: 'data-toggle' put: 'dropdown';
  467. with: [
  468. html with: 'Open...'.
  469. (html tag: 'b') class: 'caret' ].
  470. html ul
  471. class: 'dropdown-menu';
  472. with: [
  473. ((HLWidget withAllSubclasses
  474. select: [ :each | each canBeOpenAsTab ])
  475. sorted: [ :a :b | a tabPriority < b tabPriority ])
  476. do: [ :each |
  477. html li with: [
  478. html a
  479. with: each tabLabel;
  480. onClick: [ each openAsTab ] ] ] ] ]
  481. !
  482. renderContentOn: html
  483. html div
  484. class: 'navbar navbar-fixed-top';
  485. with: [ html div
  486. class: 'navbar-inner';
  487. with: [ self renderTabsOn: html ] ]
  488. !
  489. renderTabsOn: html
  490. html ul
  491. class: 'nav';
  492. with: [
  493. self tabs do: [ :each |
  494. html li
  495. class: (each isActive ifTrue: [ 'active' ] ifFalse: [ 'inactive' ]);
  496. with: [
  497. html a
  498. with: [
  499. ((html tag: 'i') class: 'icon-remove')
  500. onClick: [ self removeTab: each ].
  501. html with: each displayLabel ];
  502. onClick: [ each activate ] ] ].
  503. self renderAddOn: html ]
  504. !
  505. show: aTab
  506. self tabs do: [ :each | each hide ].
  507. aTab show; focus
  508. ! !
  509. HLManager class instanceVariableNames: 'current'!
  510. !HLManager class methodsFor: 'accessing'!
  511. current
  512. ^ current ifNil: [ current := self basicNew initialize ]
  513. ! !
  514. !HLManager class methodsFor: 'initialization'!
  515. initialize
  516. self current appendToJQuery: 'body' asJQuery
  517. ! !
  518. !HLManager class methodsFor: 'instance creation'!
  519. new
  520. "Use current instead"
  521. self shouldNotImplement
  522. ! !
  523. HLWidget subclass: #HLSUnit
  524. instanceVariableNames: ''
  525. package: 'Helios-Core'!
  526. !HLSUnit class methodsFor: 'accessing'!
  527. tabLabel
  528. ^ 'SUnit'
  529. !
  530. tabPriority
  531. ^ 1000
  532. ! !
  533. !HLSUnit class methodsFor: 'testing'!
  534. canBeOpenAsTab
  535. ^ true
  536. ! !
  537. HLWidget subclass: #HLTranscript
  538. instanceVariableNames: ''
  539. package: 'Helios-Core'!
  540. !HLTranscript class methodsFor: 'accessing'!
  541. tabLabel
  542. ^ 'Transcript'
  543. !
  544. tabPriority
  545. ^ 600
  546. ! !
  547. !HLTranscript class methodsFor: 'testing'!
  548. canBeOpenAsTab
  549. ^ true
  550. ! !