Canvas.st 6.2 KB

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