Helios-Core.st 13 KB

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