1
0

Canvas.st 16 KB

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