Canvas.st 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. !HTMLCanvas class methodsFor: 'instance creation'!
  170. onJQuery: aJQuery
  171. ^self basicNew
  172. initializeFromJQuery: aJQuery;
  173. initialize;
  174. yourself
  175. ! !
  176. Object subclass: #TagBrush
  177. instanceVariableNames: 'canvas element'
  178. category: 'Canvas'!
  179. !TagBrush methodsFor: 'accessing'!
  180. element
  181. ^element
  182. ! !
  183. !TagBrush methodsFor: 'adding'!
  184. contents: anObject
  185. self
  186. empty;
  187. append: anObject
  188. !
  189. addBrush: aTagBrush
  190. self appendChild: aTagBrush element.
  191. ^aTagBrush
  192. !
  193. with: anObject
  194. self append: anObject
  195. !
  196. append: anObject
  197. anObject appendToBrush: self
  198. !
  199. appendToBrush: aTagBrush
  200. aTagBrush addBrush: self
  201. !
  202. appendBlock: aBlock
  203. | root |
  204. root := canvas root.
  205. canvas root: self.
  206. aBlock value: canvas.
  207. canvas root: root
  208. !
  209. appendChild: anElement
  210. "In IE7 and IE8 appendChild fails on several node types. So we need to check"
  211. <var element=self['@element'];
  212. if (null == element.canHaveChildren || element.canHaveChildren) {
  213. element.appendChild(anElement);
  214. } else {
  215. element.text = String(element.text) + anElement.innerHTML;
  216. } >
  217. !
  218. appendString: aString
  219. self appendChild: (self createTextNodeFor: aString)
  220. !
  221. empty
  222. self asJQuery empty
  223. ! !
  224. !TagBrush methodsFor: 'attributes'!
  225. at: aString put: aValue
  226. <self['@element'].setAttribute(aString, aValue)>
  227. !
  228. removeAt: aString
  229. <self['@element'].removeAttribute(aString)>
  230. !
  231. class: aString
  232. self at: 'class' put: aString
  233. !
  234. id: aString
  235. self at: 'id' put: aString
  236. !
  237. src: aString
  238. self at: 'src' put: aString
  239. !
  240. href: aString
  241. self at: 'href' put: aString
  242. !
  243. title: aString
  244. self at: 'title' put: aString
  245. !
  246. style: aString
  247. self at: 'style' put: aString
  248. !
  249. type: aString
  250. self at: 'type' put: aString
  251. !
  252. media: aString
  253. self at: 'media' put: aString
  254. !
  255. rel: aString
  256. self at: 'rel' put: aString
  257. !
  258. width: aString
  259. self at: 'width' put: aString
  260. !
  261. height: aString
  262. self at: 'height' put: aString
  263. !
  264. value: aString
  265. self at: 'value' put: aString
  266. ! !
  267. !TagBrush methodsFor: 'converting'!
  268. asJQuery
  269. ^window jQuery: self element
  270. ! !
  271. !TagBrush methodsFor: 'events'!
  272. onKeyDown: aBlock
  273. self asJQuery bind: 'keydown' do: aBlock
  274. !
  275. onKeyPress: aBlock
  276. self asJQuery bind: 'keypress' do: aBlock
  277. !
  278. onKeyUp: aBlock
  279. self asJQuery bind: 'keyup' do: aBlock
  280. !
  281. onFocus: aBlock
  282. self asJQuery bind: 'focus' do: aBlock
  283. !
  284. onBlur: aBlock
  285. self asJQuery bind: 'blur' do: aBlock
  286. !
  287. onChange: aBlock
  288. self asJQuery bind: 'change' do: aBlock
  289. !
  290. onClick: aBlock
  291. self asJQuery bind: 'click' do: aBlock
  292. ! !
  293. !TagBrush methodsFor: 'initialization'!
  294. initializeFromString: aString canvas: aCanvas
  295. element := self createElementFor: aString.
  296. canvas := aCanvas
  297. !
  298. initializeFromJQuery: aJQuery canvas: aCanvas
  299. element := aJQuery get: 0.
  300. canvas := aCanvas
  301. ! !
  302. !TagBrush methodsFor: 'private'!
  303. createElementFor: aString
  304. <return document.createElement(String(aString))>
  305. !
  306. createTextNodeFor: aString
  307. <return document.createTextNode(String(aString))>
  308. ! !
  309. !TagBrush class methodsFor: 'instance creation'!
  310. fromString: aString canvas: aCanvas
  311. ^self new
  312. initializeFromString: aString canvas: aCanvas;
  313. yourself
  314. !
  315. fromJQuery: aJQuery canvas: aCanvas
  316. ^self new
  317. initializeFromJQuery: aJQuery canvas: aCanvas;
  318. yourself
  319. ! !
  320. Object subclass: #Widget
  321. instanceVariableNames: ''
  322. category: 'Canvas'!
  323. !Widget methodsFor: 'adding'!
  324. appendToBrush: aTagBrush
  325. self appendToJQuery: aTagBrush asJQuery
  326. !
  327. appendToJQuery: aJQuery
  328. self renderOn: (HTMLCanvas onJQuery: aJQuery)
  329. ! !
  330. !Widget methodsFor: 'rendering'!
  331. renderOn: html
  332. self
  333. ! !
  334. !Object methodsFor: '*Canvas'!
  335. appendToJQuery: aJQuery
  336. aJQuery append: self asString
  337. !
  338. appendToBrush: aTagBrush
  339. aTagBrush append: self asString
  340. ! !
  341. !BlockClosure methodsFor: '*Canvas'!
  342. appendToJQuery: aJQuery
  343. self value: (HTMLCanvas onJQuery: aJQuery)
  344. !
  345. appendToBrush: aTagBrush
  346. aTagBrush appendBlock: self
  347. ! !
  348. !String methodsFor: '*Canvas'!
  349. asJQuery
  350. <return jQuery(String(self))>
  351. !
  352. appendToJQuery: aJQuery
  353. aJQuery append: self
  354. !
  355. appendToBrush: aTagBrush
  356. aTagBrush appendString: self
  357. ! !