1
0

Helios-Core.st 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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: [
  39. root asJQuery hide ]
  40. !
  41. registerBindings
  42. self widget registerBindings
  43. !
  44. remove
  45. root ifNotNil: [ root asJQuery remove ]
  46. !
  47. show
  48. root
  49. ifNil: [ self appendToJQuery: 'body' asJQuery ]
  50. ifNotNil: [ root asJQuery show ]
  51. ! !
  52. !HLTab methodsFor: 'rendering'!
  53. renderOn: html
  54. root := html div
  55. class: 'tab';
  56. yourself.
  57. self renderTab
  58. !
  59. renderTab
  60. root contents: [ :html |
  61. html div
  62. class: 'amber_box';
  63. with: [ self widget renderOn: html ] ]
  64. ! !
  65. !HLTab methodsFor: 'testing'!
  66. isActive
  67. ^ self manager activeTab = self
  68. ! !
  69. !HLTab class methodsFor: 'instance creation'!
  70. on: aWidget labelled: aString
  71. ^ self new
  72. widget: aWidget;
  73. label: aString;
  74. yourself
  75. ! !
  76. Widget subclass: #HLWidget
  77. instanceVariableNames: 'wrapper'
  78. package: 'Helios-Core'!
  79. !HLWidget methodsFor: 'accessing'!
  80. manager
  81. ^ HLManager current
  82. !
  83. wrapper
  84. ^ wrapper
  85. ! !
  86. !HLWidget methodsFor: 'actions'!
  87. alert: aString
  88. window alert: aString
  89. !
  90. confirm: aString
  91. ^ window confirm: aString
  92. ! !
  93. !HLWidget methodsFor: 'keybindings'!
  94. registerBindings
  95. self registerBindingsOn: self manager keyBinder bindings
  96. !
  97. registerBindingsOn: aBindingGroup
  98. ! !
  99. !HLWidget methodsFor: 'rendering'!
  100. renderContentOn: html
  101. !
  102. renderOn: html
  103. wrapper := html div.
  104. [ :renderer | self renderContentOn: renderer ] appendToJQuery: wrapper asJQuery
  105. ! !
  106. !HLWidget methodsFor: 'testing'!
  107. canHaveFocus
  108. ^ false
  109. ! !
  110. !HLWidget methodsFor: 'updating'!
  111. refresh
  112. self wrapper ifNil: [ ^ self ].
  113. self wrapper asJQuery empty.
  114. [ :html | self renderContentOn: html ] appendToJQuery: self wrapper asJQuery
  115. ! !
  116. !HLWidget class methodsFor: 'accessing'!
  117. openAsTab
  118. self canBeOpenAsTab ifFalse: [ ^ self ].
  119. HLManager current addTab: (HLTab on: self new labelled: self tabLabel)
  120. !
  121. tabLabel
  122. ^ 'Tab'
  123. !
  124. tabPriority
  125. ^ 500
  126. ! !
  127. !HLWidget class methodsFor: 'testing'!
  128. canBeOpenAsTab
  129. ^ false
  130. ! !
  131. HLWidget subclass: #HLDebugger
  132. instanceVariableNames: ''
  133. package: 'Helios-Core'!
  134. HLWidget subclass: #HLFocusableWidget
  135. instanceVariableNames: 'hiddenInput'
  136. package: 'Helios-Core'!
  137. !HLFocusableWidget methodsFor: 'accessing'!
  138. focusClass
  139. ^ 'focused'
  140. ! !
  141. !HLFocusableWidget methodsFor: 'events'!
  142. blur
  143. hiddenInput asJQuery blur
  144. !
  145. focus
  146. hiddenInput asJQuery focus
  147. !
  148. hasFocus
  149. ^ self wrapper notNil and: [ self wrapper asJQuery hasClass: self focusClass ]
  150. ! !
  151. !HLFocusableWidget methodsFor: 'rendering'!
  152. renderContentOn: html
  153. !
  154. renderHiddenInputOn: html
  155. hiddenInput := html input
  156. style: 'position: absolute; left: -100000px;';
  157. onBlur: [ self wrapper asJQuery removeClass: self focusClass ];
  158. onFocus: [ self wrapper asJQuery addClass: self focusClass ]
  159. !
  160. renderOn: html
  161. self registerBindings.
  162. self renderHiddenInputOn: html.
  163. wrapper := html div
  164. class: 'hl_widget';
  165. onClick: [ hiddenInput asJQuery focus ];
  166. with: [
  167. self renderContentOn: html ]
  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') get: 0))
  205. !
  206. activateItem: anObject
  207. self activateListItem: (mapping
  208. at: anObject
  209. ifAbsent: [ ^ self ]) asJQuery
  210. !
  211. activateListItem: aListItem
  212. | parent position item |
  213. (aListItem get: 0) ifNil: [ ^self ].
  214. position := self positionOf: aListItem.
  215. parent := aListItem parent.
  216. parent children removeClass: 'active'.
  217. aListItem addClass: 'active'.
  218. "Move the scrollbar to show the active element"
  219. aListItem position top < 0 ifTrue: [
  220. (parent get: 0) scrollTop: ((parent get: 0) scrollTop + aListItem position top - 10) ].
  221. aListItem position top + aListItem height > parent height ifTrue: [
  222. (parent get: 0) scrollTop: ((parent get: 0) scrollTop + aListItem height - (parent height - aListItem position top)) +10 ].
  223. "Activate the corresponding item"
  224. item := (self items at: (aListItem attr: 'list-data') asNumber).
  225. self selectedItem == item ifFalse: [
  226. self selectItem: item ]
  227. !
  228. focus
  229. super focus.
  230. self items isEmpty ifFalse: [
  231. self selectedItem ifNil: [ self activateFirstListItem ] ]
  232. !
  233. selectItem: anObject
  234. self selectedItem: anObject
  235. ! !
  236. !HLListWidget methodsFor: 'defaults'!
  237. defaultItems
  238. ^ #()
  239. ! !
  240. !HLListWidget methodsFor: 'events'!
  241. setupKeyBindings
  242. "TODO: refactor"
  243. | active selected interval repeatInterval |
  244. active := false.
  245. repeatInterval := 60.
  246. hiddenInput asJQuery unbind: 'keydown'.
  247. hiddenInput asJQuery keydown: [ :e |
  248. (e which = 38 and: [ active = false ]) ifTrue: [
  249. active := true.
  250. interval := [
  251. selected := window jQuery: '.focused .nav-pills .active'.
  252. self activateListItem: selected prev ]
  253. valueWithInterval: repeatInterval ].
  254. (e which = 40 and: [ active = false ]) ifTrue: [
  255. active := true.
  256. "(next get: 0) ifNil: [ next := window jQuery: '.focused .nav-pills li:first-child' ]."
  257. interval := [
  258. selected := window jQuery: '.focused .nav-pills .active'.
  259. self activateListItem: selected next ]
  260. valueWithInterval: repeatInterval ] ].
  261. hiddenInput asJQuery keyup: [ :e |
  262. active ifTrue: [
  263. active := false.
  264. interval clearInterval ] ]
  265. ! !
  266. !HLListWidget methodsFor: 'initialization'!
  267. initialize
  268. super initialize.
  269. mapping := Dictionary new.
  270. ! !
  271. !HLListWidget methodsFor: 'private'!
  272. registerMappingFrom: anObject to: aTag
  273. mapping at: anObject put: aTag
  274. ! !
  275. !HLListWidget methodsFor: 'rendering'!
  276. renderButtonsOn: html
  277. !
  278. renderContentOn: html
  279. html ul
  280. class: 'nav nav-pills nav-stacked';
  281. with: [ self renderListOn: html ].
  282. html div class: 'pane_actions form-actions'; with: [
  283. self renderButtonsOn: html ].
  284. self setupKeyBindings
  285. !
  286. renderItem: anObject on: html
  287. | li |
  288. li := html li.
  289. self registerMappingFrom: anObject to: li.
  290. li
  291. class: (self cssClassForItem: anObject);
  292. at: 'list-data' put: (self items indexOf: anObject) asString;
  293. with: [
  294. html a
  295. with: [
  296. (html tag: 'i') class: (self iconForItem: anObject).
  297. self renderItemLabel: anObject on: html ];
  298. onClick: [
  299. self activateListItem: li asJQuery ] ]
  300. !
  301. renderItemLabel: anObject on: html
  302. html with: anObject asString
  303. !
  304. renderListOn: html
  305. mapping := Dictionary new.
  306. self items do: [ :each |
  307. self renderItem: each on: html ]
  308. ! !
  309. HLListWidget subclass: #HLNavigationListWidget
  310. instanceVariableNames: 'previous next'
  311. package: 'Helios-Core'!
  312. !HLNavigationListWidget methodsFor: 'accessing'!
  313. next
  314. ^ next
  315. !
  316. next: aWidget
  317. next := aWidget.
  318. aWidget previous = self ifFalse: [ aWidget previous: self ]
  319. !
  320. previous
  321. ^ previous
  322. !
  323. previous: aWidget
  324. previous := aWidget.
  325. aWidget next = self ifFalse: [ aWidget next: self ]
  326. ! !
  327. !HLNavigationListWidget methodsFor: 'actions'!
  328. nextFocus
  329. self next ifNotNil: [ self next focus ]
  330. !
  331. previousFocus
  332. self previous ifNotNil: [ self previous focus ]
  333. ! !
  334. !HLNavigationListWidget methodsFor: 'events'!
  335. setupKeyBindings
  336. super setupKeyBindings.
  337. hiddenInput asJQuery keydown: [ :e |
  338. e which = 39 ifTrue: [
  339. self nextFocus ].
  340. e which = 37 ifTrue: [
  341. self previousFocus ] ]
  342. ! !
  343. HLWidget subclass: #HLManager
  344. instanceVariableNames: 'tabs activeTab keyBinder environment history'
  345. package: 'Helios-Core'!
  346. !HLManager methodsFor: 'accessing'!
  347. activeTab
  348. ^ activeTab
  349. !
  350. environment
  351. "The default environment used by all Helios objects"
  352. ^ environment ifNil: [ environment := self defaultEnvironment ]
  353. !
  354. environment: anEnvironment
  355. environment := anEnvironment
  356. !
  357. history
  358. ^ history ifNil: [ history := OrderedCollection new ]
  359. !
  360. history: aCollection
  361. history := aCollection
  362. !
  363. keyBinder
  364. ^ keyBinder ifNil: [ keyBinder := HLKeyBinder new ]
  365. !
  366. tabs
  367. ^ tabs ifNil: [ tabs := OrderedCollection new ]
  368. ! !
  369. !HLManager methodsFor: 'actions'!
  370. activate: aTab
  371. self keyBinder flushBindings.
  372. aTab registerBindings.
  373. activeTab := aTab.
  374. self
  375. refresh;
  376. addToHistory: aTab;
  377. show: aTab
  378. !
  379. addTab: aTab
  380. self tabs add: aTab.
  381. self activate: aTab
  382. !
  383. addToHistory: aTab
  384. self removeFromHistory: aTab.
  385. self history add: aTab
  386. !
  387. removeActiveTab
  388. self removeTab: self activeTab
  389. !
  390. removeFromHistory: aTab
  391. self history: (self history reject: [ :each | each == aTab ])
  392. !
  393. removeTab: aTab
  394. (self tabs includes: aTab) ifFalse: [ ^ self ].
  395. self removeFromHistory: aTab.
  396. self tabs remove: aTab.
  397. self keyBinder flushBindings.
  398. aTab remove.
  399. self refresh.
  400. self history ifNotEmpty: [
  401. self history last activate ]
  402. ! !
  403. !HLManager methodsFor: 'defaults'!
  404. defaultEnvironment
  405. ^ HLLocalEnvironment new
  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. self appendToJQuery: 'body' asJQuery
  416. !
  417. renderAddOn: html
  418. html li
  419. class: 'dropdown';
  420. with: [
  421. html a
  422. class: 'dropdown-toggle';
  423. at: 'data-toggle' put: 'dropdown';
  424. with: [
  425. html with: 'Open...'.
  426. (html tag: 'b') class: 'caret' ].
  427. html ul
  428. class: 'dropdown-menu';
  429. with: [
  430. ((HLWidget withAllSubclasses
  431. select: [ :each | each canBeOpenAsTab ])
  432. sorted: [ :a :b | a tabPriority < b tabPriority ])
  433. do: [ :each |
  434. html li with: [
  435. html a
  436. with: each tabLabel;
  437. onClick: [ each openAsTab ] ] ] ] ]
  438. !
  439. renderContentOn: html
  440. html div
  441. class: 'navbar navbar-fixed-top';
  442. with: [ html div
  443. class: 'navbar-inner';
  444. with: [ self renderTabsOn: html ] ]
  445. !
  446. renderTabsOn: html
  447. html ul
  448. class: 'nav';
  449. with: [
  450. self tabs do: [ :each |
  451. html li
  452. class: (each isActive ifTrue: [ 'active' ] ifFalse: [ 'inactive' ]);
  453. with: [
  454. html a
  455. with: [
  456. ((html tag: 'i') class: 'icon-remove')
  457. onClick: [ self removeTab: each ].
  458. html with: each displayLabel ];
  459. onClick: [ each activate ] ] ].
  460. self renderAddOn: html ]
  461. !
  462. show: aTab
  463. self tabs do: [ :each | each hide ].
  464. aTab show; focus
  465. ! !
  466. HLManager class instanceVariableNames: 'current'!
  467. !HLManager class methodsFor: 'accessing'!
  468. current
  469. ^ current ifNil: [ current := self basicNew initialize ]
  470. ! !
  471. !HLManager class methodsFor: 'initialization'!
  472. initialize
  473. self current appendToJQuery: 'body' asJQuery
  474. ! !
  475. !HLManager class methodsFor: 'instance creation'!
  476. new
  477. "Use current instead"
  478. self shouldNotImplement
  479. ! !
  480. HLWidget subclass: #HLSUnit
  481. instanceVariableNames: ''
  482. package: 'Helios-Core'!
  483. !HLSUnit class methodsFor: 'accessing'!
  484. tabLabel
  485. ^ 'SUnit'
  486. !
  487. tabPriority
  488. ^ 1000
  489. ! !
  490. !HLSUnit class methodsFor: 'testing'!
  491. canBeOpenAsTab
  492. ^ true
  493. ! !
  494. HLWidget subclass: #HLTranscript
  495. instanceVariableNames: ''
  496. package: 'Helios-Core'!
  497. !HLTranscript class methodsFor: 'accessing'!
  498. tabLabel
  499. ^ 'Transcript'
  500. !
  501. tabPriority
  502. ^ 600
  503. ! !
  504. !HLTranscript class methodsFor: 'testing'!
  505. canBeOpenAsTab
  506. ^ true
  507. ! !
  508. !Package methodsFor: '*Helios-Core'!
  509. ajaxPutAt: aURL data: aString
  510. jQuery
  511. ajax: aURL
  512. options: #{ 'type' -> 'PUT'.
  513. 'data' -> aString.
  514. 'contentType' -> 'text/plain;charset=UTF-8'.
  515. 'error' -> [:xhr | window
  516. alert: 'Commiting ' , aURL , ' failed with reason: "' , (xhr responseText) , '"'] }
  517. !
  518. heliosCommit
  519. { Exporter -> (self commitPathJs, '/', self name, '.js').
  520. StrippedExporter -> (self commitPathJs, '/', self name, '.deploy.js').
  521. ChunkExporter -> (self commitPathSt, '/', self name, '.st')
  522. } do: [ :commitStrategy || fileContents |
  523. fileContents := (commitStrategy key new exportPackage: self name).
  524. self ajaxPutAt: commitStrategy value data: fileContents ]
  525. ! !