1
0

Canvas.st 6.3 KB

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