2
0

Canvas.st 18 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. Smalltalk current createPackage: 'Canvas'!
  2. Object subclass: #BrowserInterface
  3. instanceVariableNames: ''
  4. package: 'Canvas'!
  5. !BrowserInterface commentStamp!
  6. I am platform interface class that tries to use window and jQuery; that is, one for browser environment.
  7. ## API
  8. self isAvailable. "true if window and jQuery exist".
  9. self alert: 'Hey, there is a problem'.
  10. self confirm: 'Affirmative?'.
  11. self prompt: 'Your name:'.
  12. self ajax: #{
  13. 'url' -> '/patch.js'. 'type' -> 'GET'. dataType->'script'
  14. }.!
  15. !BrowserInterface methodsFor: 'actions'!
  16. ajax: anObject
  17. ^ jQuery ajax: anObject
  18. !
  19. alert: aString
  20. ^ window alert: aString
  21. !
  22. confirm: aString
  23. ^ window confirm: aString
  24. !
  25. prompt: aString
  26. ^ window prompt: aString
  27. ! !
  28. !BrowserInterface methodsFor: 'testing'!
  29. isAvailable
  30. <return typeof window !!== "undefined" && typeof jQuery !!== "undefined">
  31. ! !
  32. BrowserInterface class instanceVariableNames: 'uiWorker ajaxWorker'!
  33. Object subclass: #HTMLCanvas
  34. instanceVariableNames: 'root'
  35. package: 'Canvas'!
  36. !HTMLCanvas commentStamp!
  37. I am a canvas for building HTML.
  38. I provide the `#tag:` method to create a `TagBrush` (wrapping a DOM element) and convenience methods in the `tags` protocol.
  39. ## API
  40. My instances are used as the argument of the `#renderOn:` method of `Widget` objects.
  41. The `#with:` method is used to compose HTML, nesting tags. `#with:` can take a `TagBrush`, a `String`, a `BlockClosure` or a `Widget` as argument.
  42. ## Usage example:
  43. aCanvas a
  44. with: [ aCanvas span with: 'click me' ];
  45. onClick: [ window alert: 'clicked!!' ]!
  46. !HTMLCanvas methodsFor: 'accessing'!
  47. root
  48. ^ root
  49. !
  50. root: aTagBrush
  51. root := aTagBrush
  52. !
  53. snippet: anElement
  54. "Adds clone of anElement, finds [data-snippet=""*""] subelement
  55. and returns TagBrush as if that subelement was just added.
  56. Rarely needed to use directly, use `html foo` dynamically installed method
  57. for a snippet named foo."
  58. | clone caret |
  59. clone := anElement asJQuery clone.
  60. self with: (TagBrush fromJQuery: clone canvas: self).
  61. caret := clone find: '[data-snippet="*"]'.
  62. caret toArray isEmpty ifTrue: [ caret := clone ].
  63. ^ TagBrush fromJQuery: (caret removeAttr: 'data-snippet') canvas: self
  64. ! !
  65. !HTMLCanvas methodsFor: 'adding'!
  66. entity: aString
  67. "Adds a character representing html entity, eg.
  68. html entity: 'copy'
  69. adds a copyright sign.
  70. If a name does not represent valid HTML entity, error is raised."
  71. | result |
  72. result := ('<span />' asJQuery html: '&', aString, ';') text.
  73. result size = 1 ifFalse: [ self error: 'Not an HTML entity: ', aString ].
  74. self with: result
  75. !
  76. with: anObject
  77. ^ self root with: anObject
  78. ! !
  79. !HTMLCanvas methodsFor: 'initialization'!
  80. initialize
  81. super initialize.
  82. root ifNil: [ root := TagBrush fromString: 'div' canvas: self ]
  83. !
  84. initializeFromJQuery: aJQuery
  85. root := TagBrush fromJQuery: aJQuery canvas: self
  86. ! !
  87. !HTMLCanvas methodsFor: 'tags'!
  88. a
  89. ^ self tag: 'a'
  90. !
  91. abbr
  92. ^ self tag: 'abbr'
  93. !
  94. address
  95. ^ self tag: 'address'
  96. !
  97. area
  98. ^ self tag: 'area'
  99. !
  100. article
  101. ^ self tag: 'article'
  102. !
  103. aside
  104. ^ self tag: 'aside'
  105. !
  106. audio
  107. ^ self tag: 'audio'
  108. !
  109. base
  110. ^ self tag: 'base'
  111. !
  112. blockquote
  113. ^ self tag: 'blockquote'
  114. !
  115. body
  116. ^ self tag: 'body'
  117. !
  118. br
  119. ^ self tag: 'br'
  120. !
  121. button
  122. ^ self tag: 'button'
  123. !
  124. canvas
  125. ^ self tag: 'canvas'
  126. !
  127. caption
  128. ^ self tag: 'caption'
  129. !
  130. cite
  131. ^ self tag: 'cite'
  132. !
  133. code
  134. ^ self tag: 'code'
  135. !
  136. col
  137. ^ self tag: 'col'
  138. !
  139. colgroup
  140. ^ self tag: 'colgroup'
  141. !
  142. command
  143. ^ self tag: 'command'
  144. !
  145. datalist
  146. ^ self tag: 'datalist'
  147. !
  148. dd
  149. ^ self tag: 'dd'
  150. !
  151. del
  152. ^ self tag: 'del'
  153. !
  154. details
  155. ^ self tag: 'details'
  156. !
  157. div
  158. ^ self tag: 'div'
  159. !
  160. div: aBlock
  161. ^ self div with: aBlock
  162. !
  163. dl
  164. ^ self tag: 'dl'
  165. !
  166. dt
  167. ^ self tag: 'dt'
  168. !
  169. em
  170. ^ self tag: 'em'
  171. !
  172. embed
  173. ^ self tag: 'embed'
  174. !
  175. fieldset
  176. ^ self tag: 'fieldset'
  177. !
  178. figcaption
  179. ^ self tag: 'figcaption'
  180. !
  181. figure
  182. ^ self tag: 'figure'
  183. !
  184. footer
  185. ^ self tag: 'footer'
  186. !
  187. form
  188. ^ self tag: 'form'
  189. !
  190. h1
  191. ^ self tag: 'h1'
  192. !
  193. h1: anObject
  194. ^ self h1 with: anObject
  195. !
  196. h2
  197. ^ self tag: 'h2'
  198. !
  199. h2: anObject
  200. ^ self h2 with: anObject
  201. !
  202. h3
  203. ^ self tag: 'h3'
  204. !
  205. h3: anObject
  206. ^ self h3 with: anObject
  207. !
  208. h4
  209. ^ self tag: 'h4'
  210. !
  211. h4: anObject
  212. ^ self h4 with: anObject
  213. !
  214. h5
  215. ^ self tag: 'h5'
  216. !
  217. h5: anObject
  218. ^ self h5 with: anObject
  219. !
  220. h6
  221. ^ self tag: 'h6'
  222. !
  223. h6: anObject
  224. ^ self h6 with: anObject
  225. !
  226. head
  227. ^ self tag: 'head'
  228. !
  229. header
  230. ^ self tag: 'header'
  231. !
  232. hgroup
  233. ^ self tag: 'hgroup'
  234. !
  235. hr
  236. ^ self tag: 'hr'
  237. !
  238. html
  239. ^ self tag: 'html'
  240. !
  241. iframe
  242. ^ self tag: 'iframe'
  243. !
  244. iframe: aString
  245. ^ self iframe src: aString
  246. !
  247. img
  248. ^ self tag: 'img'
  249. !
  250. img: aString
  251. ^ self img src: aString
  252. !
  253. input
  254. ^ self tag: 'input'
  255. !
  256. label
  257. ^ self tag: 'label'
  258. !
  259. legend
  260. ^ self tag: 'legend'
  261. !
  262. li
  263. ^ self tag: 'li'
  264. !
  265. li: anObject
  266. ^ self li with: anObject
  267. !
  268. link
  269. ^ self tag: 'link'
  270. !
  271. map
  272. ^ self tag: 'map'
  273. !
  274. mark
  275. ^ self tag: 'mark'
  276. !
  277. menu
  278. ^ self tag: 'menu'
  279. !
  280. meta
  281. ^ self tag: 'meta'
  282. !
  283. nav
  284. ^ self tag: 'nav'
  285. !
  286. newTag: aString
  287. ^ TagBrush fromString: aString canvas: self
  288. !
  289. noscript
  290. ^ self tag: 'noscript'
  291. !
  292. object
  293. ^ self tag: 'object'
  294. !
  295. ol
  296. ^ self tag: 'ol'
  297. !
  298. ol: anObject
  299. ^ self ol with: anObject
  300. !
  301. optgroup
  302. ^ self tag: 'optgroup'
  303. !
  304. option
  305. ^ self tag: 'option'
  306. !
  307. output
  308. ^ self tag: 'output'
  309. !
  310. p
  311. ^ self tag: 'p'
  312. !
  313. p: anObject
  314. ^ self p with: anObject
  315. !
  316. param
  317. ^ self tag: 'param'
  318. !
  319. pre
  320. ^ self tag: 'pre'
  321. !
  322. progress
  323. ^ self tag: 'progress'
  324. !
  325. script
  326. ^ self tag: 'script'
  327. !
  328. section
  329. ^ self tag: 'section'
  330. !
  331. select
  332. ^ self tag: 'select'
  333. !
  334. small
  335. ^ self tag: 'small'
  336. !
  337. source
  338. ^ self tag: 'source'
  339. !
  340. span
  341. ^ self tag: 'span'
  342. !
  343. span: anObject
  344. ^ self span with: anObject
  345. !
  346. strong
  347. ^ self tag: 'strong'
  348. !
  349. strong: anObject
  350. ^ self strong with: anObject
  351. !
  352. style
  353. ^ root addBrush: (StyleTag canvas: self)
  354. !
  355. style: aString
  356. ^ self style with: aString; yourself
  357. !
  358. sub
  359. ^ self tag: 'sub'
  360. !
  361. summary
  362. ^ self tag: 'summary'
  363. !
  364. sup
  365. ^ self tag: 'sup'
  366. !
  367. table
  368. ^ self tag: 'table'
  369. !
  370. tag: aString
  371. ^ root addBrush: (self newTag: aString)
  372. !
  373. tbody
  374. ^ self tag: 'tbody'
  375. !
  376. td
  377. ^ self tag: 'td'
  378. !
  379. textarea
  380. ^ self tag: 'textarea'
  381. !
  382. tfoot
  383. ^ self tag: 'tfoot'
  384. !
  385. th
  386. ^ self tag: 'th'
  387. !
  388. thead
  389. ^ self tag: 'thead'
  390. !
  391. time
  392. ^ self tag: 'time'
  393. !
  394. title
  395. ^ self tag: 'title'
  396. !
  397. tr
  398. ^ self tag: 'tr'
  399. !
  400. ul
  401. ^ self tag: 'ul'
  402. !
  403. ul: anObject
  404. ^ self ul with: anObject
  405. !
  406. video
  407. ^ self tag: 'video'
  408. ! !
  409. !HTMLCanvas class methodsFor: 'instance creation'!
  410. browserVersion
  411. ^ (jQuery at: #browser) version
  412. !
  413. isMSIE
  414. ^ ((jQuery at: #browser) at: #msie) notNil
  415. !
  416. isMozilla
  417. ^ ((jQuery at: #browser) at: #mozilla) notNil
  418. !
  419. isOpera
  420. ^ ((jQuery at: #browser) at: #opera) notNil
  421. !
  422. isWebkit
  423. ^ ((jQuery at: #browser) at: #webkit) notNil
  424. !
  425. onJQuery: aJQuery
  426. ^ self basicNew
  427. initializeFromJQuery: aJQuery;
  428. initialize;
  429. yourself
  430. ! !
  431. Object subclass: #HTMLSnippet
  432. instanceVariableNames: 'snippets'
  433. package: 'Canvas'!
  434. !HTMLSnippet commentStamp!
  435. My sole instance is the registry of html snippets.
  436. `HTMLSnippet current` is the public singleton instance.
  437. On startup, it scans the document for any html elements
  438. with `'data-snippet="foo"'` attribute and takes them off the document,
  439. remembering them in the store under the specified name.
  440. It also install method #foo into HTMLCanvas dynamically.
  441. Every html snippet should mark a 'caret', a place where contents
  442. can be inserted, by 'data-snippet="*"' (a special name for caret).
  443. For example:
  444. `<li data-snippet='menuelement' class='...'><a data-snippet='*'></a></li>`
  445. defines a list element with a link inside; the link itself is marked as a caret.
  446. You can later issue
  447. `html menuelement href: '/foo'; with: 'A foo'`
  448. to insert the whole snippet and directly manipulate the caret, so it renders:
  449. `<li class='...'><a href='/foo'>A foo</a></li>`
  450. For a self-careting tags (not very useful, but you do not need to fill class etc.
  451. you can use
  452. `<div class='lots of classes' attr1='one' attr2='two' data-snippet='*bar'></div>`
  453. and in code later do:
  454. `html bar with: [ xxx ]`
  455. to render
  456. `<div class='lots of classes' attr1='one' attr2='two'>...added by xxx...</div>`!
  457. !HTMLSnippet methodsFor: 'accessing'!
  458. snippetAt: aString
  459. ^ self snippets at: aString
  460. !
  461. snippets
  462. ^ snippets ifNil: [ snippets := #{} ]
  463. ! !
  464. !HTMLSnippet methodsFor: 'initialization'!
  465. initializeFromJQuery: aJQuery
  466. "Finds and takes out all snippets out of aJQuery.
  467. Installs it into self."
  468. (self snippetsFromJQuery: aJQuery) do: [ :each |
  469. self installSnippetFromJQuery: each asJQuery ]
  470. ! !
  471. !HTMLSnippet methodsFor: 'method generation'!
  472. snippetAt: aString compile: anElement
  473. "Method generation for the snippet.
  474. The selector is aString, the method block uses anElement"
  475. ClassBuilder new
  476. installMethod: ([ :htmlReceiver | htmlReceiver snippet: anElement ]
  477. currySelf asCompiledMethod: aString)
  478. forClass: HTMLCanvas
  479. category: '**snippets'
  480. ! !
  481. !HTMLSnippet methodsFor: 'private'!
  482. snippetsFromJQuery: aJQuery
  483. ^ (aJQuery find: '[data-snippet]') toArray
  484. ! !
  485. !HTMLSnippet methodsFor: 'snippet installation'!
  486. installSnippetFromJQuery: element
  487. | name |
  488. name := element attr: 'data-snippet'.
  489. name = '*' ifFalse: [
  490. ('^\*' asRegexp test: name)
  491. ifTrue: [
  492. name := name allButFirst.
  493. element attr: 'data-snippet' put: '*' ]
  494. ifFalse: [
  495. element removeAttr: 'data-snippet' ].
  496. self snippetAt: name install: (element detach get: 0) ]
  497. !
  498. snippetAt: aString install: anElement
  499. self snippets at: aString put: anElement.
  500. self snippetAt: aString compile: anElement
  501. ! !
  502. HTMLSnippet class instanceVariableNames: 'current'!
  503. !HTMLSnippet class methodsFor: 'initialization'!
  504. ensureCurrent
  505. current ifNil: [
  506. current := super new
  507. initializeFromJQuery: document asJQuery;
  508. yourself ]
  509. !
  510. initialize
  511. super initialize.
  512. self isDOMAvailable ifTrue: [
  513. self ensureCurrent ]
  514. ! !
  515. !HTMLSnippet class methodsFor: 'instance creation'!
  516. current
  517. ^ current
  518. !
  519. isDOMAvailable
  520. < return typeof document !!== 'undefined' >
  521. !
  522. new
  523. self shouldNotImplement
  524. ! !
  525. Object subclass: #TagBrush
  526. instanceVariableNames: 'canvas element'
  527. package: 'Canvas'!
  528. !TagBrush commentStamp!
  529. I am a brush for building a single DOM element (which I hold onto).
  530. All tags but `<style>` are instances of me (see the `StyleBrush` class).
  531. ## API
  532. 1. Nesting
  533. Use `#with:` to nest tags. `#with:` can take aString, `TagBrush` instance, a `Widget` or block closure as parameter.
  534. Example: `aTag with: aString with: aCanvas div`
  535. 2. Events
  536. The `events` protocol contains all methods related to events (delegating event handling to jQuery).
  537. Example: `aTag onClick: [ window alert: 'clicked' ]`
  538. 3. Attributes
  539. The `attribute` protocol contains methods for attribute manipulation (delegating to jQuery too).
  540. Example: `aTag at: 'value' put: 'hello world'`
  541. 4. Raw access and jQuery
  542. The `#element` method can be used to access to JavaScript DOM element object.
  543. Example: `aTag element cssStyle`
  544. Use `#asJQuery` to access to the receiver converted into a jQuery object.
  545. Example: `aTag asJQuery css: 'color' value: 'red'`!
  546. !TagBrush methodsFor: 'accessing'!
  547. element
  548. ^ element
  549. ! !
  550. !TagBrush methodsFor: 'adding'!
  551. addBrush: aTagBrush
  552. self appendChild: aTagBrush element.
  553. ^ aTagBrush
  554. !
  555. append: anObject
  556. anObject appendToBrush: self
  557. !
  558. appendBlock: aBlock
  559. | root |
  560. root := canvas root.
  561. canvas root: self.
  562. aBlock value: canvas.
  563. canvas root: root
  564. !
  565. appendChild: anElement
  566. "In IE7 and IE8 appendChild fails on several node types. So we need to check"
  567. <var element=self['@element'];
  568. if (null == element.canHaveChildren || element.canHaveChildren) {
  569. element.appendChild(anElement);
  570. } else {
  571. element.text = String(element.text) + anElement.innerHTML;
  572. } >
  573. !
  574. appendString: aString
  575. self appendChild: (self createTextNodeFor: aString)
  576. !
  577. appendToBrush: aTagBrush
  578. aTagBrush addBrush: self
  579. !
  580. contents: anObject
  581. self
  582. empty;
  583. append: anObject
  584. !
  585. empty
  586. self asJQuery empty
  587. !
  588. with: anObject
  589. self append: anObject
  590. ! !
  591. !TagBrush methodsFor: 'attributes'!
  592. accesskey: aString
  593. self at: 'accesskey' put: aString
  594. !
  595. action: aString
  596. self at: 'action' put: aString
  597. !
  598. align: aString
  599. self at: 'align' put: aString
  600. !
  601. alt: aString
  602. self at: 'alt' put: aString
  603. !
  604. at: aString
  605. ^ self at: aString ifAbsent: [ Collection new errorNotFound ]
  606. !
  607. at: aString ifAbsent: aBlock
  608. <return self['@element'].hasAttribute(aString) ? self['@element'].getAttribute(aString) : aBlock._value()>
  609. !
  610. at: aString put: aValue
  611. <self['@element'].setAttribute(aString, aValue)>
  612. !
  613. class: aString
  614. <self['@element'].className = aString>
  615. !
  616. cols: aString
  617. self at: 'cols' put: aString
  618. !
  619. contenteditable: aString
  620. self at: 'contenteditable' put: aString
  621. !
  622. contextmenu: aString
  623. self at: 'contextmenu' put: aString
  624. !
  625. draggable: aString
  626. self at: 'draggable' put: aString
  627. !
  628. for: aString
  629. self at: 'for' put: aString
  630. !
  631. height: aString
  632. self at: 'height' put: aString
  633. !
  634. hidden
  635. self at: 'hidden' put: 'hidden'
  636. !
  637. href: aString
  638. self at: 'href' put: aString
  639. !
  640. id: aString
  641. self at: 'id' put: aString
  642. !
  643. media: aString
  644. self at: 'media' put: aString
  645. !
  646. method: aString
  647. self at: 'method' put: aString
  648. !
  649. name: aString
  650. self at: 'name' put: aString
  651. !
  652. placeholder: aString
  653. self at: 'placeholder' put: aString
  654. !
  655. rel: aString
  656. self at: 'rel' put: aString
  657. !
  658. removeAt: aString
  659. <self['@element'].removeAttribute(aString)>
  660. !
  661. rows: aString
  662. self at: 'rows' put: aString
  663. !
  664. src: aString
  665. self at: 'src' put: aString
  666. !
  667. style: aString
  668. self at: 'style' put: aString
  669. !
  670. tabindex: aNumber
  671. self at: 'tabindex' put: aNumber
  672. !
  673. target: aString
  674. self at: 'target' put: aString
  675. !
  676. title: aString
  677. self at: 'title' put: aString
  678. !
  679. type: aString
  680. self at: 'type' put: aString
  681. !
  682. valign: aString
  683. self at: 'valign' put: aString
  684. !
  685. value: aString
  686. self at: 'value' put: aString
  687. !
  688. width: aString
  689. self at: 'width' put: aString
  690. ! !
  691. !TagBrush methodsFor: 'converting'!
  692. asJQuery
  693. ^ self element asJQuery
  694. ! !
  695. !TagBrush methodsFor: 'events'!
  696. onBlur: aBlock
  697. self asJQuery bind: 'blur' do: aBlock
  698. !
  699. onChange: aBlock
  700. self asJQuery bind: 'change' do: aBlock
  701. !
  702. onClick: aBlock
  703. self asJQuery bind: 'click' do: aBlock
  704. !
  705. onDblClick: aBlock
  706. self asJQuery bind: 'dblclick' do: aBlock
  707. !
  708. onFocus: aBlock
  709. self asJQuery bind: 'focus' do: aBlock
  710. !
  711. onFocusIn: aBlock
  712. self asJQuery bind: 'focusin' do: aBlock
  713. !
  714. onFocusOut: aBlock
  715. self asJQuery bind: 'focusout' do: aBlock
  716. !
  717. onHover: aBlock
  718. self asJQuery bind: 'hover' do: aBlock
  719. !
  720. onKeyDown: aBlock
  721. self asJQuery bind: 'keydown' do: aBlock
  722. !
  723. onKeyPress: aBlock
  724. self asJQuery bind: 'keypress' do: aBlock
  725. !
  726. onKeyUp: aBlock
  727. self asJQuery bind: 'keyup' do: aBlock
  728. !
  729. onMouseDown: aBlock
  730. self asJQuery bind: 'mousedown' do: aBlock
  731. !
  732. onMouseEnter: aBlock
  733. self asJQuery bind: 'mouseenter' do: aBlock
  734. !
  735. onMouseLeave: aBlock
  736. self asJQuery bind: 'mouseleave' do: aBlock
  737. !
  738. onMouseMove: aBlock
  739. self asJQuery bind: 'mousemove' do: aBlock
  740. !
  741. onMouseOut: aBlock
  742. self asJQuery bind: 'mouseout' do: aBlock
  743. !
  744. onMouseOver: aBlock
  745. self asJQuery bind: 'mouseover' do: aBlock
  746. !
  747. onMouseUp: aBlock
  748. self asJQuery bind: 'mouseup' do: aBlock
  749. !
  750. onSelect: aBlock
  751. self asJQuery bind: 'select' do: aBlock
  752. !
  753. onSubmit: aBlock
  754. self asJQuery bind: 'submit' do: aBlock
  755. !
  756. onUnload: aBlock
  757. self asJQuery bind: 'unload' do: aBlock
  758. ! !
  759. !TagBrush methodsFor: 'initialization'!
  760. initializeFromJQuery: aJQuery canvas: aCanvas
  761. element := aJQuery get: 0.
  762. canvas := aCanvas
  763. !
  764. initializeFromString: aString canvas: aCanvas
  765. element := self createElementFor: aString.
  766. canvas := aCanvas
  767. ! !
  768. !TagBrush methodsFor: 'private'!
  769. appendDocumentFragment: anElement
  770. <var element=self['@element'].appendChild(anElement["@element"])>
  771. !
  772. createElementFor: aString
  773. <return document.createElement(String(aString))>
  774. !
  775. createTextNodeFor: aString
  776. <return document.createTextNode(String(aString))>
  777. ! !
  778. !TagBrush class methodsFor: 'instance creation'!
  779. fromJQuery: aJQuery canvas: aCanvas
  780. ^ self new
  781. initializeFromJQuery: aJQuery canvas: aCanvas;
  782. yourself
  783. !
  784. fromString: aString canvas: aCanvas
  785. ^ self new
  786. initializeFromString: aString canvas: aCanvas;
  787. yourself
  788. ! !
  789. TagBrush subclass: #StyleTag
  790. instanceVariableNames: ''
  791. package: 'Canvas'!
  792. !StyleTag commentStamp!
  793. I'm a `<style>` tag use to inline CSS or load a stylesheet.
  794. ## Motivation
  795. The need for a specific class comes from Internet Explorer compatibility issues.!
  796. !StyleTag methodsFor: 'adding'!
  797. with: aString
  798. HTMLCanvas isMSIE
  799. ifTrue: [ self element styleSheet cssText: aString ]
  800. ifFalse: [ super with: aString ].
  801. ! !
  802. !StyleTag class methodsFor: 'instance creation'!
  803. canvas: aCanvas
  804. ^ self new
  805. initializeFromString: 'style' canvas: aCanvas;
  806. yourself
  807. ! !
  808. InterfacingObject subclass: #Widget
  809. instanceVariableNames: ''
  810. package: 'Canvas'!
  811. !Widget commentStamp!
  812. I am a presenter building HTML. Subclasses are typically reusable components.
  813. ## API
  814. Use `#renderContentOn:` to build HTML. (See `HTMLCanvas` and `TagBrush` classes for more about building HTML).
  815. To add a widget to the page, the convenience method `#appendToJQuery:` is very useful.
  816. Exemple:
  817. Counter new appendToJQuery: 'body' asJQuery!
  818. !Widget methodsFor: 'adding'!
  819. appendToBrush: aTagBrush
  820. self appendToJQuery: aTagBrush asJQuery
  821. !
  822. appendToJQuery: aJQuery
  823. self renderOn: (HTMLCanvas onJQuery: aJQuery)
  824. ! !
  825. !Widget methodsFor: 'rendering'!
  826. renderOn: html
  827. self
  828. ! !
  829. !Widget class methodsFor: 'helios'!
  830. heliosClass
  831. ^ 'widget'
  832. ! !
  833. !Object methodsFor: '*Canvas'!
  834. appendToBrush: aTagBrush
  835. aTagBrush append: self asString
  836. !
  837. appendToJQuery: aJQuery
  838. aJQuery append: self asString
  839. ! !
  840. !BlockClosure methodsFor: '*Canvas'!
  841. appendToBrush: aTagBrush
  842. aTagBrush appendBlock: self
  843. !
  844. appendToJQuery: aJQuery
  845. self value: (HTMLCanvas onJQuery: aJQuery)
  846. ! !
  847. !CharacterArray methodsFor: '*Canvas'!
  848. asSnippet
  849. ^ HTMLSnippet current snippetAt: self asString
  850. ! !
  851. !String methodsFor: '*Canvas'!
  852. appendToBrush: aTagBrush
  853. aTagBrush appendString: self
  854. !
  855. appendToJQuery: aJQuery
  856. aJQuery append: self
  857. !
  858. asJQuery
  859. <return jQuery(String(self))>
  860. ! !
  861. !JSObjectProxy methodsFor: '*Canvas'!
  862. asJQuery
  863. <return jQuery(self['@jsObject'])>
  864. ! !