2
0

Canvas.st 18 KB

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