1
0

Canvas.st 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. !HTMLCanvas class methodsFor: 'instance creation'!
  124. onJQuery: aJQuery
  125. ^self basicNew
  126. initializeFromJQuery: aJQuery;
  127. initialize;
  128. yourself
  129. ! !
  130. Object subclass: #TagBrush
  131. instanceVariableNames: 'canvas element'
  132. category: 'Canvas'!
  133. !TagBrush methodsFor: 'accessing'!
  134. element
  135. ^element
  136. ! !
  137. !TagBrush methodsFor: 'adding'!
  138. contents: anObject
  139. self
  140. empty;
  141. append: anObject
  142. !
  143. addBrush: aTagBrush
  144. self appendChild: aTagBrush element.
  145. ^aTagBrush
  146. !
  147. with: anObject
  148. self append: anObject
  149. !
  150. append: anObject
  151. anObject appendToBrush: self
  152. !
  153. appendToBrush: aTagBrush
  154. aTagBrush addBrush: self
  155. !
  156. appendBlock: aBlock
  157. | root |
  158. root := canvas root.
  159. canvas root: self.
  160. aBlock value: canvas.
  161. canvas root: root
  162. !
  163. appendChild: anElement
  164. "In IE7 and IE8 appendChild fails on several node types. So we need to check"
  165. <var element=self['@element'];
  166. if (null == element.canHaveChildren || element.canHaveChildren) {
  167. element.appendChild(anElement);
  168. } else {
  169. element.text = String(element.text) + anElement.innerHTML;
  170. } >
  171. !
  172. appendString: aString
  173. self appendChild: (self createTextNodeFor: aString)
  174. !
  175. empty
  176. self asJQuery empty
  177. ! !
  178. !TagBrush methodsFor: 'attributes'!
  179. at: aString put: aValue
  180. <self['@element'].setAttribute(aString, aValue)>
  181. !
  182. removeAt: aString
  183. <self['@element'].removeAttribute(aString)>
  184. !
  185. class: aString
  186. self at: 'class' put: aString
  187. !
  188. id: aString
  189. self at: 'id' put: aString
  190. !
  191. src: aString
  192. self at: 'src' put: aString
  193. !
  194. href: aString
  195. self at: 'href' put: aString
  196. !
  197. title: aString
  198. self at: 'title' put: aString
  199. !
  200. style: aString
  201. self at: 'style' put: aString
  202. !
  203. type: aString
  204. self at: 'type' put: aString
  205. !
  206. media: aString
  207. self at: 'media' put: aString
  208. !
  209. rel: aString
  210. self at: 'rel' put: aString
  211. ! !
  212. !TagBrush methodsFor: 'converting'!
  213. asJQuery
  214. ^window jQuery: self element
  215. ! !
  216. !TagBrush methodsFor: 'events'!
  217. onKeyDown: aBlock
  218. self asJQuery bind: 'keydown' do: aBlock
  219. !
  220. onKeyPress: aBlock
  221. self asJQuery bind: 'keypress' do: aBlock
  222. !
  223. onKeyUp: aBlock
  224. self asJQuery bind: 'keyup' do: aBlock
  225. !
  226. onFocus: aBlock
  227. self asJQuery bind: 'focus' do: aBlock
  228. !
  229. onBlur: aBlock
  230. self asJQuery bind: 'blur' do: aBlock
  231. !
  232. onChange: aBlock
  233. self asJQuery bind: 'change' do: aBlock
  234. !
  235. onClick: aBlock
  236. self asJQuery bind: 'click' do: aBlock
  237. ! !
  238. !TagBrush methodsFor: 'initialization'!
  239. initializeFromString: aString canvas: aCanvas
  240. element := self createElementFor: aString.
  241. canvas := aCanvas
  242. !
  243. initializeFromJQuery: aJQuery canvas: aCanvas
  244. element := aJQuery get: 0.
  245. canvas := aCanvas
  246. ! !
  247. !TagBrush methodsFor: 'private'!
  248. createElementFor: aString
  249. <return document.createElement(String(aString))>
  250. !
  251. createTextNodeFor: aString
  252. <return document.createTextNode(String(aString))>
  253. ! !
  254. !TagBrush class methodsFor: 'instance creation'!
  255. fromString: aString canvas: aCanvas
  256. ^self new
  257. initializeFromString: aString canvas: aCanvas;
  258. yourself
  259. !
  260. fromJQuery: aJQuery canvas: aCanvas
  261. ^self new
  262. initializeFromJQuery: aJQuery canvas: aCanvas;
  263. yourself
  264. ! !
  265. Object subclass: #Widget
  266. instanceVariableNames: ''
  267. category: 'Canvas'!
  268. !Widget methodsFor: 'actions'!
  269. alert: aString
  270. <alert(aString)>
  271. !
  272. confirm: aString
  273. <return window.confirm(aString)>
  274. !
  275. prompt: aString
  276. ^self prompt: aString default: ''
  277. !
  278. prompt: aString default: anotherString
  279. <return window.prompt(aString, anotherString)>
  280. ! !
  281. !Widget methodsFor: 'adding'!
  282. appendToBrush: aTagBrush
  283. self appendToJQuery: aTagBrush asJQuery
  284. !
  285. appendToJQuery: aJQuery
  286. self renderOn: (HTMLCanvas onJQuery: aJQuery)
  287. ! !
  288. !Widget methodsFor: 'rendering'!
  289. renderOn: html
  290. self
  291. ! !
  292. !Object methodsFor: '*Canvas'!
  293. appendToJQuery: aJQuery
  294. aJQuery append: self asString
  295. !
  296. appendToBrush: aTagBrush
  297. aTagBrush append: self asString
  298. ! !
  299. !BlockClosure methodsFor: '*Canvas'!
  300. appendToJQuery: aJQuery
  301. self value: (HTMLCanvas onJQuery: aJQuery)
  302. !
  303. appendToBrush: aTagBrush
  304. aTagBrush appendBlock: self
  305. ! !
  306. !String methodsFor: '*Canvas'!
  307. asJQuery
  308. <return jQuery(String(self))>
  309. !
  310. appendToJQuery: aJQuery
  311. aJQuery append: self
  312. !
  313. appendToBrush: aTagBrush
  314. aTagBrush appendString: self
  315. ! !