Canvas.st 18 KB

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