Canvas.st 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. Smalltalk current createPackage: 'Canvas' properties: #{}!
  2. Object subclass: #HTMLCanvas
  3. instanceVariableNames: 'root'
  4. package: 'Canvas'!
  5. !HTMLCanvas methodsFor: 'accessing'!
  6. root
  7. ^root
  8. !
  9. root: aTagBrush
  10. root := aTagBrush
  11. ! !
  12. !HTMLCanvas methodsFor: 'adding'!
  13. with: anObject
  14. ^self root with: anObject
  15. ! !
  16. !HTMLCanvas methodsFor: 'initialization'!
  17. initialize
  18. super initialize.
  19. root ifNil: [root := TagBrush fromString: 'div' canvas: self]
  20. !
  21. initializeFromJQuery: aJQuery
  22. root := TagBrush fromJQuery: aJQuery canvas: self
  23. ! !
  24. !HTMLCanvas methodsFor: 'tags'!
  25. a
  26. ^self tag: 'a'
  27. !
  28. abbr
  29. ^self tag: 'abbr'
  30. !
  31. address
  32. ^self tag: 'address'
  33. !
  34. area
  35. ^self tag: 'area'
  36. !
  37. article
  38. ^self tag: 'article'
  39. !
  40. aside
  41. ^self tag: 'aside'
  42. !
  43. audio
  44. ^self tag: 'audio'
  45. !
  46. base
  47. ^self tag: 'base'
  48. !
  49. blockquote
  50. ^self tag: 'blockquote'
  51. !
  52. body
  53. ^self tag: 'body'
  54. !
  55. br
  56. ^self tag: 'br'
  57. !
  58. button
  59. ^self tag: 'button'
  60. !
  61. canvas
  62. ^self tag: 'canvas'
  63. !
  64. caption
  65. ^self tag: 'caption'
  66. !
  67. cite
  68. ^self tag: 'cite'
  69. !
  70. code
  71. ^self tag: 'code'
  72. !
  73. col
  74. ^self tag: 'col'
  75. !
  76. colgroup
  77. ^self tag: 'colgroup'
  78. !
  79. command
  80. ^self tag: 'command'
  81. !
  82. datalist
  83. ^self tag: 'datalist'
  84. !
  85. dd
  86. ^self tag: 'dd'
  87. !
  88. del
  89. ^self tag: 'del'
  90. !
  91. details
  92. ^self tag: 'details'
  93. !
  94. div
  95. ^self tag: 'div'
  96. !
  97. div: aBlock
  98. ^self div with: aBlock
  99. !
  100. dl
  101. ^self tag: 'dl'
  102. !
  103. dt
  104. ^self tag: 'dt'
  105. !
  106. em
  107. ^self tag: 'em'
  108. !
  109. embed
  110. ^self tag: 'embed'
  111. !
  112. fieldset
  113. ^self tag: 'fieldset'
  114. !
  115. figcaption
  116. ^self tag: 'figcaption'
  117. !
  118. figure
  119. ^self tag: 'figure'
  120. !
  121. footer
  122. ^self tag: 'footer'
  123. !
  124. form
  125. ^self tag: 'form'
  126. !
  127. h1
  128. ^self tag: 'h1'
  129. !
  130. h1: anObject
  131. ^self h1 with: anObject
  132. !
  133. h2
  134. ^self tag: 'h2'
  135. !
  136. h2: anObject
  137. ^ self h2 with: anObject
  138. !
  139. h3
  140. ^self tag: 'h3'
  141. !
  142. h3: anObject
  143. ^self h3 with: anObject
  144. !
  145. h4
  146. ^self tag: 'h4'
  147. !
  148. h4: anObject
  149. ^self h4 with: anObject
  150. !
  151. h5
  152. ^self tag: 'h5'
  153. !
  154. h5: anObject
  155. ^self h5 with: anObject
  156. !
  157. h6
  158. ^self tag: 'h6'
  159. !
  160. h6: anObject
  161. ^self h6 with: anObject
  162. !
  163. head
  164. ^self tag: 'head'
  165. !
  166. header
  167. ^self tag: 'header'
  168. !
  169. hgroup
  170. ^self tag: 'hgroup'
  171. !
  172. hr
  173. ^self tag: 'hr'
  174. !
  175. html
  176. ^self tag: 'html'
  177. !
  178. iframe
  179. ^self tag: 'iframe'
  180. !
  181. iframe: aString
  182. ^self iframe src: aString
  183. !
  184. img
  185. ^self tag: 'img'
  186. !
  187. img: aString
  188. ^self img src: aString
  189. !
  190. input
  191. ^self tag: 'input'
  192. !
  193. label
  194. ^self tag: 'label'
  195. !
  196. legend
  197. ^self tag: 'legend'
  198. !
  199. li
  200. ^self tag: 'li'
  201. !
  202. li: anObject
  203. ^self li with: anObject
  204. !
  205. link
  206. ^self tag: 'link'
  207. !
  208. map
  209. ^self tag: 'map'
  210. !
  211. mark
  212. ^self tag: 'mark'
  213. !
  214. menu
  215. ^self tag: 'menu'
  216. !
  217. meta
  218. ^self tag: 'meta'
  219. !
  220. nav
  221. ^self tag: 'nav'
  222. !
  223. newTag: aString
  224. ^TagBrush fromString: aString canvas: self
  225. !
  226. noscript
  227. ^self tag: 'noscript'
  228. !
  229. object
  230. ^self tag: 'object'
  231. !
  232. ol
  233. ^self tag: 'ol'
  234. !
  235. ol: anObject
  236. ^self ol with: anObject
  237. !
  238. optgroup
  239. ^self tag: 'optgroup'
  240. !
  241. option
  242. ^self tag: 'option'
  243. !
  244. output
  245. ^self tag: 'output'
  246. !
  247. p
  248. ^self tag: 'p'
  249. !
  250. p: anObject
  251. ^self p with: anObject
  252. !
  253. param
  254. ^self tag: 'param'
  255. !
  256. pre
  257. ^self tag: 'pre'
  258. !
  259. progress
  260. ^self tag: 'progress'
  261. !
  262. script
  263. ^self tag: 'script'
  264. !
  265. section
  266. ^self tag: 'section'
  267. !
  268. select
  269. ^self tag: 'select'
  270. !
  271. small
  272. ^self tag: 'small'
  273. !
  274. source
  275. ^self tag: 'source'
  276. !
  277. span
  278. ^self tag: 'span'
  279. !
  280. span: anObject
  281. ^self span with: anObject
  282. !
  283. strong
  284. ^self tag: 'strong'
  285. !
  286. strong: anObject
  287. ^self strong with: anObject
  288. !
  289. style
  290. ^ root addBrush: (StyleTag canvas: self)
  291. !
  292. style: aString
  293. ^ self style with: aString; yourself
  294. !
  295. sub
  296. ^self tag: 'sub'
  297. !
  298. summary
  299. ^self tag: 'summary'
  300. !
  301. sup
  302. ^self tag: 'sup'
  303. !
  304. table
  305. ^self tag: 'table'
  306. !
  307. tag: aString
  308. ^root addBrush: (self newTag: aString)
  309. !
  310. tbody
  311. ^self tag: 'tbody'
  312. !
  313. td
  314. ^self tag: 'td'
  315. !
  316. textarea
  317. ^self tag: 'textarea'
  318. !
  319. tfoot
  320. ^self tag: 'tfoot'
  321. !
  322. th
  323. ^self tag: 'th'
  324. !
  325. thead
  326. ^self tag: 'thead'
  327. !
  328. time
  329. ^self tag: 'time'
  330. !
  331. title
  332. ^self tag: 'title'
  333. !
  334. tr
  335. ^self tag: 'tr'
  336. !
  337. ul
  338. ^self tag: 'ul'
  339. !
  340. ul: anObject
  341. ^self ul with: anObject
  342. !
  343. video
  344. ^self tag: 'video'
  345. ! !
  346. !HTMLCanvas class methodsFor: 'instance creation'!
  347. browserVersion
  348. ^(jQuery at: #browser) version
  349. !
  350. isMSIE
  351. ^((jQuery at: #browser) at: #msie) notNil
  352. !
  353. isMozilla
  354. ^((jQuery at: #browser) at: #mozilla) notNil
  355. !
  356. isOpera
  357. ^((jQuery at: #browser) at: #opera) notNil
  358. !
  359. isWebkit
  360. ^((jQuery at: #browser) at: #webkit) notNil
  361. !
  362. onJQuery: aJQuery
  363. ^self basicNew
  364. initializeFromJQuery: aJQuery;
  365. initialize;
  366. yourself
  367. ! !
  368. Object subclass: #TagBrush
  369. instanceVariableNames: 'canvas element'
  370. package: 'Canvas'!
  371. !TagBrush methodsFor: 'accessing'!
  372. element
  373. ^element
  374. ! !
  375. !TagBrush methodsFor: 'adding'!
  376. addBrush: aTagBrush
  377. self appendChild: aTagBrush element.
  378. ^aTagBrush
  379. !
  380. append: anObject
  381. anObject appendToBrush: self
  382. !
  383. appendBlock: aBlock
  384. | root |
  385. root := canvas root.
  386. canvas root: self.
  387. aBlock value: canvas.
  388. canvas root: root
  389. !
  390. appendChild: anElement
  391. "In IE7 and IE8 appendChild fails on several node types. So we need to check"
  392. <var element=self['@element'];
  393. if (null == element.canHaveChildren || element.canHaveChildren) {
  394. element.appendChild(anElement);
  395. } else {
  396. element.text = String(element.text) + anElement.innerHTML;
  397. } >
  398. !
  399. appendString: aString
  400. self appendChild: (self createTextNodeFor: aString)
  401. !
  402. appendToBrush: aTagBrush
  403. aTagBrush addBrush: self
  404. !
  405. contents: anObject
  406. self
  407. empty;
  408. append: anObject
  409. !
  410. empty
  411. self asJQuery empty
  412. !
  413. with: anObject
  414. self append: anObject
  415. ! !
  416. !TagBrush methodsFor: 'attributes'!
  417. accesskey: aString
  418. self at: 'accesskey' put: aString
  419. !
  420. action: aString
  421. self at: 'action' put: aString
  422. !
  423. align: aString
  424. self at: 'align' put: aString
  425. !
  426. alt: aString
  427. self at: 'alt' put: aString
  428. !
  429. at: aString put: aValue
  430. <self['@element'].setAttribute(aString, aValue)>
  431. !
  432. class: aString
  433. <self['@element'].className = aString>
  434. !
  435. cols: aString
  436. self at: 'cols' put: aString
  437. !
  438. contenteditable: aString
  439. self at: 'contenteditable' put: aString
  440. !
  441. contextmenu: aString
  442. self at: 'contextmenu' put: aString
  443. !
  444. draggable: aString
  445. self at: 'draggable' put: aString
  446. !
  447. for: aString
  448. self at: 'for' put: aString
  449. !
  450. height: aString
  451. self at: 'height' put: aString
  452. !
  453. hidden
  454. self at: 'hidden' put: 'hidden'
  455. !
  456. href: aString
  457. self at: 'href' put: aString
  458. !
  459. id: aString
  460. self at: 'id' put: aString
  461. !
  462. media: aString
  463. self at: 'media' put: aString
  464. !
  465. method: aString
  466. self at: 'method' put: aString
  467. !
  468. name: aString
  469. self at: 'name' put: aString
  470. !
  471. placeholder: aString
  472. self at: 'placeholder' put: aString
  473. !
  474. rel: aString
  475. self at: 'rel' put: aString
  476. !
  477. removeAt: aString
  478. <self['@element'].removeAttribute(aString)>
  479. !
  480. rows: aString
  481. self at: 'rows' put: aString
  482. !
  483. src: aString
  484. self at: 'src' put: aString
  485. !
  486. style: aString
  487. self at: 'style' put: aString
  488. !
  489. tabindex: aNumber
  490. self at: 'tabindex' put: aNumber
  491. !
  492. target: aString
  493. self at: 'target' put: aString
  494. !
  495. title: aString
  496. self at: 'title' put: aString
  497. !
  498. type: aString
  499. self at: 'type' put: aString
  500. !
  501. valign: aString
  502. self at: 'valign' put: aString
  503. !
  504. value: aString
  505. self at: 'value' put: aString
  506. !
  507. width: aString
  508. self at: 'width' put: aString
  509. ! !
  510. !TagBrush methodsFor: 'converting'!
  511. asJQuery
  512. ^window jQuery: self element
  513. ! !
  514. !TagBrush methodsFor: 'events'!
  515. onBlur: aBlock
  516. self asJQuery bind: 'blur' do: aBlock
  517. !
  518. onChange: aBlock
  519. self asJQuery bind: 'change' do: aBlock
  520. !
  521. onClick: aBlock
  522. self asJQuery bind: 'click' do: aBlock
  523. !
  524. onDblClick: aBlock
  525. self asJQuery bind: 'dblclick' do: aBlock
  526. !
  527. onFocus: aBlock
  528. self asJQuery bind: 'focus' do: aBlock
  529. !
  530. onFocusIn: aBlock
  531. self asJQuery bind: 'focusin' do: aBlock
  532. !
  533. onFocusOut: aBlock
  534. self asJQuery bind: 'focusout' do: aBlock
  535. !
  536. onHover: aBlock
  537. self asJQuery bind: 'hover' do: aBlock
  538. !
  539. onKeyDown: aBlock
  540. self asJQuery bind: 'keydown' do: aBlock
  541. !
  542. onKeyPress: aBlock
  543. self asJQuery bind: 'keypress' do: aBlock
  544. !
  545. onKeyUp: aBlock
  546. self asJQuery bind: 'keyup' do: aBlock
  547. !
  548. onMouseDown: aBlock
  549. self asJQuery bind: 'mousedown' do: aBlock
  550. !
  551. onMouseEnter: aBlock
  552. self asJQuery bind: 'mouseenter' do: aBlock
  553. !
  554. onMouseLeave: aBlock
  555. self asJQuery bind: 'mouseleave' do: aBlock
  556. !
  557. onMouseMove: aBlock
  558. self asJQuery bind: 'mousemove' do: aBlock
  559. !
  560. onMouseOut: aBlock
  561. self asJQuery bind: 'mouseout' do: aBlock
  562. !
  563. onMouseOver: aBlock
  564. self asJQuery bind: 'mouseover' do: aBlock
  565. !
  566. onMouseUp: aBlock
  567. self asJQuery bind: 'mouseup' do: aBlock
  568. !
  569. onSelect: aBlock
  570. self asJQuery bind: 'select' do: aBlock
  571. !
  572. onSubmit: aBlock
  573. self asJQuery bind: 'submit' do: aBlock
  574. !
  575. onUnload: aBlock
  576. self asJQuery bind: 'unload' do: aBlock
  577. ! !
  578. !TagBrush methodsFor: 'initialization'!
  579. initializeFromJQuery: aJQuery canvas: aCanvas
  580. element := aJQuery get: 0.
  581. canvas := aCanvas
  582. !
  583. initializeFromString: aString canvas: aCanvas
  584. element := self createElementFor: aString.
  585. canvas := aCanvas
  586. ! !
  587. !TagBrush methodsFor: 'private'!
  588. createElementFor: aString
  589. <return document.createElement(String(aString))>
  590. !
  591. createTextNodeFor: aString
  592. <return document.createTextNode(String(aString))>
  593. ! !
  594. !TagBrush class methodsFor: 'instance creation'!
  595. fromJQuery: aJQuery canvas: aCanvas
  596. ^self new
  597. initializeFromJQuery: aJQuery canvas: aCanvas;
  598. yourself
  599. !
  600. fromString: aString canvas: aCanvas
  601. ^self new
  602. initializeFromString: aString canvas: aCanvas;
  603. yourself
  604. ! !
  605. TagBrush subclass: #StyleTag
  606. instanceVariableNames: 'canvas element'
  607. package: 'Canvas'!
  608. !StyleTag commentStamp!
  609. I'm a <style> tag use to inline CSS or load a stylesheet.
  610. For inlining handle IE compatibility problems.!
  611. !StyleTag methodsFor: 'adding'!
  612. with: aString
  613. HTMLCanvas isMSIE
  614. ifTrue: [self element styleSheet cssText: aString ]
  615. ifFalse: [super with: aString ].
  616. ! !
  617. !StyleTag class methodsFor: 'instance creation'!
  618. canvas: aCanvas
  619. ^self new
  620. initializeFromString: 'style' canvas: aCanvas;
  621. yourself
  622. ! !
  623. Object subclass: #Widget
  624. instanceVariableNames: ''
  625. package: 'Canvas'!
  626. !Widget methodsFor: 'adding'!
  627. appendToBrush: aTagBrush
  628. self appendToJQuery: aTagBrush asJQuery
  629. !
  630. appendToJQuery: aJQuery
  631. self renderOn: (HTMLCanvas onJQuery: aJQuery)
  632. ! !
  633. !Widget methodsFor: 'rendering'!
  634. renderOn: html
  635. self
  636. ! !
  637. !Object methodsFor: '*Canvas'!
  638. appendToBrush: aTagBrush
  639. aTagBrush append: self asString
  640. !
  641. appendToJQuery: aJQuery
  642. aJQuery append: self asString
  643. ! !
  644. !BlockClosure methodsFor: '*Canvas'!
  645. appendToBrush: aTagBrush
  646. aTagBrush appendBlock: self
  647. !
  648. appendToJQuery: aJQuery
  649. self value: (HTMLCanvas onJQuery: aJQuery)
  650. ! !
  651. !String methodsFor: '*Canvas'!
  652. appendToBrush: aTagBrush
  653. aTagBrush appendString: self
  654. !
  655. appendToJQuery: aJQuery
  656. aJQuery append: self
  657. !
  658. asJQuery
  659. <return jQuery(String(self))>
  660. ! !