Canvas.st 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. Smalltalk current createPackage: 'Canvas' properties: #{}!
  2. Object subclass: #HTMLCanvas
  3. instanceVariableNames: 'root'
  4. category: 'Canvas'!
  5. !HTMLCanvas methodsFor: 'accessing'!
  6. root: aTagBrush
  7. root := aTagBrush
  8. !
  9. root
  10. ^root
  11. ! !
  12. !HTMLCanvas methodsFor: 'adding'!
  13. with: anObject
  14. ^self root with: anObject
  15. ! !
  16. !HTMLCanvas methodsFor: 'initialization'!
  17. initialize
  18. super initialize.
  19. root ifNil: [root := TagBrush fromString: 'div' canvas: self]
  20. !
  21. initializeFromJQuery: aJQuery
  22. root := TagBrush fromJQuery: aJQuery canvas: self
  23. ! !
  24. !HTMLCanvas methodsFor: 'tags'!
  25. newTag: aString
  26. ^TagBrush fromString: aString canvas: self
  27. !
  28. tag: aString
  29. ^root addBrush: (self newTag: aString)
  30. !
  31. h1
  32. ^self tag: 'h1'
  33. !
  34. h2
  35. ^self tag: 'h2'
  36. !
  37. h3
  38. ^self tag: 'h3'
  39. !
  40. h4
  41. ^self tag: 'h4'
  42. !
  43. h5
  44. ^self tag: 'h5'
  45. !
  46. h6
  47. ^self tag: 'h6'
  48. !
  49. p
  50. ^self tag: 'p'
  51. !
  52. div
  53. ^self tag: 'div'
  54. !
  55. span
  56. ^self tag: 'span'
  57. !
  58. img
  59. ^self tag: 'img'
  60. !
  61. ul
  62. ^self tag: 'ul'
  63. !
  64. ol
  65. ^self tag: 'ol'
  66. !
  67. li
  68. ^self tag: 'li'
  69. !
  70. table
  71. ^self tag: 'table'
  72. !
  73. tr
  74. ^self tag: 'tr'
  75. !
  76. td
  77. ^self tag: 'td'
  78. !
  79. th
  80. ^self tag: 'th'
  81. !
  82. form
  83. ^self tag: 'form'
  84. !
  85. input
  86. ^self tag: 'input'
  87. !
  88. button
  89. ^self tag: 'button'
  90. !
  91. select
  92. ^self tag: 'select'
  93. !
  94. option
  95. ^self tag: 'option'
  96. !
  97. textarea
  98. ^self tag: 'textarea'
  99. !
  100. a
  101. ^self tag: 'a'
  102. !
  103. canvas
  104. ^self tag: 'canvas'
  105. !
  106. pre
  107. ^self tag: 'pre'
  108. !
  109. code
  110. ^self tag: 'code'
  111. !
  112. br
  113. ^self tag: 'br'
  114. !
  115. script
  116. ^self tag: 'script'
  117. !
  118. link
  119. ^self tag: 'link'
  120. !
  121. style
  122. ^self tag: 'style'
  123. !
  124. p: anObject
  125. ^self p with: anObject
  126. !
  127. h1: anObject
  128. ^self h1 with: anObject
  129. !
  130. iframe
  131. ^self tag: 'iframe'
  132. !
  133. iframe: aString
  134. ^self iframe src: aString
  135. !
  136. h2: anObject
  137. ^ self h2 with: anObject
  138. !
  139. h3: anObject
  140. ^self h3 with: anObject
  141. !
  142. h4: anObject
  143. ^self h4 with: anObject
  144. !
  145. h5: anObject
  146. ^self h5 with: anObject
  147. !
  148. h6: anObject
  149. ^self h6 with: anObject
  150. !
  151. img: aString
  152. ^self img src: aString
  153. !
  154. ol: anObject
  155. ^self ol with: anObject
  156. !
  157. li: anObject
  158. ^self li with: anObject
  159. !
  160. ul: anObject
  161. ^self ul with: anObject
  162. !
  163. span: anObject
  164. ^self span with: anObject
  165. !
  166. style: aString
  167. ^ self style with: aString; yourself
  168. !
  169. articles
  170. ^self tag: 'articles'
  171. !
  172. audio
  173. ^self tag: 'audio'
  174. !
  175. fieldset
  176. ^self tag: 'fieldset'
  177. !
  178. footer
  179. ^self tag: 'footer'
  180. !
  181. header
  182. ^self tag: 'header'
  183. !
  184. hr
  185. ^self tag: 'hr'
  186. !
  187. section
  188. ^self tag: 'section'
  189. !
  190. tbody
  191. ^self tag: 'tbody'
  192. !
  193. tfoot
  194. ^self tag: 'tfoot'
  195. !
  196. thead
  197. ^self tag: 'thead'
  198. !
  199. video
  200. ^self tag: 'video'
  201. ! !
  202. !HTMLCanvas class methodsFor: 'instance creation'!
  203. onJQuery: aJQuery
  204. ^self basicNew
  205. initializeFromJQuery: aJQuery;
  206. initialize;
  207. yourself
  208. ! !
  209. Object subclass: #TagBrush
  210. instanceVariableNames: 'canvas element'
  211. category: 'Canvas'!
  212. !TagBrush methodsFor: 'accessing'!
  213. element
  214. ^element
  215. ! !
  216. !TagBrush methodsFor: 'adding'!
  217. contents: anObject
  218. self
  219. empty;
  220. append: anObject
  221. !
  222. addBrush: aTagBrush
  223. self appendChild: aTagBrush element.
  224. ^aTagBrush
  225. !
  226. with: anObject
  227. self append: anObject
  228. !
  229. append: anObject
  230. anObject appendToBrush: self
  231. !
  232. appendToBrush: aTagBrush
  233. aTagBrush addBrush: self
  234. !
  235. appendBlock: aBlock
  236. | root |
  237. root := canvas root.
  238. canvas root: self.
  239. aBlock value: canvas.
  240. canvas root: root
  241. !
  242. appendChild: anElement
  243. "In IE7 and IE8 appendChild fails on several node types. So we need to check"
  244. <var element=self['@element'];
  245. if (null == element.canHaveChildren || element.canHaveChildren) {
  246. element.appendChild(anElement);
  247. } else {
  248. element.text = String(element.text) + anElement.innerHTML;
  249. } >
  250. !
  251. appendString: aString
  252. self appendChild: (self createTextNodeFor: aString)
  253. !
  254. empty
  255. self asJQuery empty
  256. ! !
  257. !TagBrush methodsFor: 'attributes'!
  258. at: aString put: aValue
  259. <self['@element'].setAttribute(aString, aValue)>
  260. !
  261. removeAt: aString
  262. <self['@element'].removeAttribute(aString)>
  263. !
  264. class: aString
  265. <self['@element'].className = aString>
  266. !
  267. id: aString
  268. self at: 'id' put: aString
  269. !
  270. src: aString
  271. self at: 'src' put: aString
  272. !
  273. href: aString
  274. self at: 'href' put: aString
  275. !
  276. title: aString
  277. self at: 'title' put: aString
  278. !
  279. style: aString
  280. self at: 'style' put: aString
  281. !
  282. type: aString
  283. self at: 'type' put: aString
  284. !
  285. media: aString
  286. self at: 'media' put: aString
  287. !
  288. rel: aString
  289. self at: 'rel' put: aString
  290. !
  291. width: aString
  292. self at: 'width' put: aString
  293. !
  294. height: aString
  295. self at: 'height' put: aString
  296. !
  297. value: aString
  298. self at: 'value' put: aString
  299. ! !
  300. !TagBrush methodsFor: 'converting'!
  301. asJQuery
  302. ^window jQuery: self element
  303. ! !
  304. !TagBrush methodsFor: 'events'!
  305. onKeyDown: aBlock
  306. self asJQuery bind: 'keydown' do: aBlock
  307. !
  308. onKeyPress: aBlock
  309. self asJQuery bind: 'keypress' do: aBlock
  310. !
  311. onKeyUp: aBlock
  312. self asJQuery bind: 'keyup' do: aBlock
  313. !
  314. onFocus: aBlock
  315. self asJQuery bind: 'focus' do: aBlock
  316. !
  317. onBlur: aBlock
  318. self asJQuery bind: 'blur' do: aBlock
  319. !
  320. onChange: aBlock
  321. self asJQuery bind: 'change' do: aBlock
  322. !
  323. onClick: aBlock
  324. self asJQuery bind: 'click' do: aBlock
  325. ! !
  326. !TagBrush methodsFor: 'initialization'!
  327. initializeFromString: aString canvas: aCanvas
  328. element := self createElementFor: aString.
  329. canvas := aCanvas
  330. !
  331. initializeFromJQuery: aJQuery canvas: aCanvas
  332. element := aJQuery get: 0.
  333. canvas := aCanvas
  334. ! !
  335. !TagBrush methodsFor: 'private'!
  336. createElementFor: aString
  337. <return document.createElement(String(aString))>
  338. !
  339. createTextNodeFor: aString
  340. <return document.createTextNode(String(aString))>
  341. ! !
  342. !TagBrush class methodsFor: 'instance creation'!
  343. fromString: aString canvas: aCanvas
  344. ^self new
  345. initializeFromString: aString canvas: aCanvas;
  346. yourself
  347. !
  348. fromJQuery: aJQuery canvas: aCanvas
  349. ^self new
  350. initializeFromJQuery: aJQuery canvas: aCanvas;
  351. yourself
  352. ! !
  353. Object subclass: #Widget
  354. instanceVariableNames: ''
  355. category: 'Canvas'!
  356. !Widget methodsFor: 'adding'!
  357. appendToBrush: aTagBrush
  358. self appendToJQuery: aTagBrush asJQuery
  359. !
  360. appendToJQuery: aJQuery
  361. self renderOn: (HTMLCanvas onJQuery: aJQuery)
  362. ! !
  363. !Widget methodsFor: 'rendering'!
  364. renderOn: html
  365. self
  366. ! !
  367. !Object methodsFor: '*Canvas'!
  368. appendToJQuery: aJQuery
  369. aJQuery append: self asString
  370. !
  371. appendToBrush: aTagBrush
  372. aTagBrush append: self asString
  373. ! !
  374. !BlockClosure methodsFor: '*Canvas'!
  375. appendToJQuery: aJQuery
  376. self value: (HTMLCanvas onJQuery: aJQuery)
  377. !
  378. appendToBrush: aTagBrush
  379. aTagBrush appendBlock: self
  380. ! !
  381. !String methodsFor: '*Canvas'!
  382. asJQuery
  383. <return jQuery(String(self))>
  384. !
  385. appendToJQuery: aJQuery
  386. aJQuery append: self
  387. !
  388. appendToBrush: aTagBrush
  389. aTagBrush appendString: self
  390. ! !