1
0

Canvas.st 17 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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 put: aValue
  605. <self['@element'].setAttribute(aString, aValue)>
  606. !
  607. class: aString
  608. <self['@element'].className = aString>
  609. !
  610. cols: aString
  611. self at: 'cols' put: aString
  612. !
  613. contenteditable: aString
  614. self at: 'contenteditable' put: aString
  615. !
  616. contextmenu: aString
  617. self at: 'contextmenu' put: aString
  618. !
  619. draggable: aString
  620. self at: 'draggable' put: aString
  621. !
  622. for: aString
  623. self at: 'for' put: aString
  624. !
  625. height: aString
  626. self at: 'height' put: aString
  627. !
  628. hidden
  629. self at: 'hidden' put: 'hidden'
  630. !
  631. href: aString
  632. self at: 'href' put: aString
  633. !
  634. id: aString
  635. self at: 'id' put: aString
  636. !
  637. media: aString
  638. self at: 'media' put: aString
  639. !
  640. method: aString
  641. self at: 'method' put: aString
  642. !
  643. name: aString
  644. self at: 'name' put: aString
  645. !
  646. placeholder: aString
  647. self at: 'placeholder' put: aString
  648. !
  649. rel: aString
  650. self at: 'rel' put: aString
  651. !
  652. removeAt: aString
  653. <self['@element'].removeAttribute(aString)>
  654. !
  655. rows: aString
  656. self at: 'rows' put: aString
  657. !
  658. src: aString
  659. self at: 'src' put: aString
  660. !
  661. style: aString
  662. self at: 'style' put: aString
  663. !
  664. tabindex: aNumber
  665. self at: 'tabindex' put: aNumber
  666. !
  667. target: aString
  668. self at: 'target' put: aString
  669. !
  670. title: aString
  671. self at: 'title' put: aString
  672. !
  673. type: aString
  674. self at: 'type' put: aString
  675. !
  676. valign: aString
  677. self at: 'valign' put: aString
  678. !
  679. value: aString
  680. self at: 'value' put: aString
  681. !
  682. width: aString
  683. self at: 'width' put: aString
  684. ! !
  685. !TagBrush methodsFor: 'converting'!
  686. asJQuery
  687. ^ self element asJQuery
  688. ! !
  689. !TagBrush methodsFor: 'events'!
  690. onBlur: aBlock
  691. self asJQuery bind: 'blur' do: aBlock
  692. !
  693. onChange: aBlock
  694. self asJQuery bind: 'change' do: aBlock
  695. !
  696. onClick: aBlock
  697. self asJQuery bind: 'click' do: aBlock
  698. !
  699. onDblClick: aBlock
  700. self asJQuery bind: 'dblclick' do: aBlock
  701. !
  702. onFocus: aBlock
  703. self asJQuery bind: 'focus' do: aBlock
  704. !
  705. onFocusIn: aBlock
  706. self asJQuery bind: 'focusin' do: aBlock
  707. !
  708. onFocusOut: aBlock
  709. self asJQuery bind: 'focusout' do: aBlock
  710. !
  711. onHover: aBlock
  712. self asJQuery bind: 'hover' do: aBlock
  713. !
  714. onKeyDown: aBlock
  715. self asJQuery bind: 'keydown' do: aBlock
  716. !
  717. onKeyPress: aBlock
  718. self asJQuery bind: 'keypress' do: aBlock
  719. !
  720. onKeyUp: aBlock
  721. self asJQuery bind: 'keyup' do: aBlock
  722. !
  723. onMouseDown: aBlock
  724. self asJQuery bind: 'mousedown' do: aBlock
  725. !
  726. onMouseEnter: aBlock
  727. self asJQuery bind: 'mouseenter' do: aBlock
  728. !
  729. onMouseLeave: aBlock
  730. self asJQuery bind: 'mouseleave' do: aBlock
  731. !
  732. onMouseMove: aBlock
  733. self asJQuery bind: 'mousemove' do: aBlock
  734. !
  735. onMouseOut: aBlock
  736. self asJQuery bind: 'mouseout' do: aBlock
  737. !
  738. onMouseOver: aBlock
  739. self asJQuery bind: 'mouseover' do: aBlock
  740. !
  741. onMouseUp: aBlock
  742. self asJQuery bind: 'mouseup' do: aBlock
  743. !
  744. onSelect: aBlock
  745. self asJQuery bind: 'select' do: aBlock
  746. !
  747. onSubmit: aBlock
  748. self asJQuery bind: 'submit' do: aBlock
  749. !
  750. onUnload: aBlock
  751. self asJQuery bind: 'unload' do: aBlock
  752. ! !
  753. !TagBrush methodsFor: 'initialization'!
  754. initializeFromJQuery: aJQuery canvas: aCanvas
  755. element := aJQuery get: 0.
  756. canvas := aCanvas
  757. !
  758. initializeFromString: aString canvas: aCanvas
  759. element := self createElementFor: aString.
  760. canvas := aCanvas
  761. ! !
  762. !TagBrush methodsFor: 'private'!
  763. appendDocumentFragment: anElement
  764. <var element=self['@element'].appendChild(anElement["@element"])>
  765. !
  766. createElementFor: aString
  767. <return document.createElement(String(aString))>
  768. !
  769. createTextNodeFor: aString
  770. <return document.createTextNode(String(aString))>
  771. ! !
  772. !TagBrush class methodsFor: 'instance creation'!
  773. fromJQuery: aJQuery canvas: aCanvas
  774. ^ self new
  775. initializeFromJQuery: aJQuery canvas: aCanvas;
  776. yourself
  777. !
  778. fromString: aString canvas: aCanvas
  779. ^ self new
  780. initializeFromString: aString canvas: aCanvas;
  781. yourself
  782. ! !
  783. TagBrush subclass: #StyleTag
  784. instanceVariableNames: ''
  785. package: 'Canvas'!
  786. !StyleTag commentStamp!
  787. I'm a `<style>` tag use to inline CSS or load a stylesheet.
  788. ## Motivation
  789. The need for a specific class comes from Internet Explorer compatibility issues.!
  790. !StyleTag methodsFor: 'adding'!
  791. with: aString
  792. HTMLCanvas isMSIE
  793. ifTrue: [ self element styleSheet cssText: aString ]
  794. ifFalse: [ super with: aString ].
  795. ! !
  796. !StyleTag class methodsFor: 'instance creation'!
  797. canvas: aCanvas
  798. ^ self new
  799. initializeFromString: 'style' canvas: aCanvas;
  800. yourself
  801. ! !
  802. InterfacingObject subclass: #Widget
  803. instanceVariableNames: ''
  804. package: 'Canvas'!
  805. !Widget commentStamp!
  806. I am a presenter building HTML. Subclasses are typically reusable components.
  807. ## API
  808. Use `#renderContentOn:` to build HTML. (See `HTMLCanvas` and `TagBrush` classes for more about building HTML).
  809. To add a widget to the page, the convenience method `#appendToJQuery:` is very useful.
  810. Exemple:
  811. Counter new appendToJQuery: 'body' asJQuery!
  812. !Widget methodsFor: 'adding'!
  813. appendToBrush: aTagBrush
  814. self appendToJQuery: aTagBrush asJQuery
  815. !
  816. appendToJQuery: aJQuery
  817. self renderOn: (HTMLCanvas onJQuery: aJQuery)
  818. ! !
  819. !Widget methodsFor: 'rendering'!
  820. renderOn: html
  821. self
  822. ! !
  823. !Widget class methodsFor: 'helios'!
  824. heliosClass
  825. ^ 'widget'
  826. ! !
  827. !Object methodsFor: '*Canvas'!
  828. appendToBrush: aTagBrush
  829. aTagBrush append: self asString
  830. !
  831. appendToJQuery: aJQuery
  832. aJQuery append: self asString
  833. ! !
  834. !BlockClosure methodsFor: '*Canvas'!
  835. appendToBrush: aTagBrush
  836. aTagBrush appendBlock: self
  837. !
  838. appendToJQuery: aJQuery
  839. self value: (HTMLCanvas onJQuery: aJQuery)
  840. ! !
  841. !CharacterArray methodsFor: '*Canvas'!
  842. asSnippet
  843. ^ HTMLSnippet current snippetAt: self asString
  844. ! !
  845. !String methodsFor: '*Canvas'!
  846. appendToBrush: aTagBrush
  847. aTagBrush appendString: self
  848. !
  849. appendToJQuery: aJQuery
  850. aJQuery append: self
  851. !
  852. asJQuery
  853. <return jQuery(String(self))>
  854. ! !
  855. !JSObjectProxy methodsFor: '*Canvas'!
  856. asJQuery
  857. <return jQuery(self['@jsObject'])>
  858. ! !