2
0

Canvas.st 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. <return smalltalk.JQuery._from_(jQuery(self['@element']))>
  215. !
  216. asJQueryDo: aBlock
  217. aBlock value: self asJQuery
  218. ! !
  219. !TagBrush methodsFor: 'events'!
  220. onKeyDown: aBlock
  221. self asJQuery on: 'keydown' do: aBlock
  222. !
  223. onKeyPress: aBlock
  224. self asJQuery on: 'keypress' do: aBlock
  225. !
  226. onKeyUp: aBlock
  227. self asJQuery on: 'keyup' do: aBlock
  228. !
  229. onFocus: aBlock
  230. self asJQuery on: 'focus' do: aBlock
  231. !
  232. onBlur: aBlock
  233. self asJQuery on: 'blur' do: aBlock
  234. !
  235. onChange: aBlock
  236. self asJQuery on: 'change' do: aBlock
  237. !
  238. onClick: aBlock
  239. self asJQuery on: 'click' do: aBlock
  240. ! !
  241. !TagBrush methodsFor: 'initialization'!
  242. initializeFromString: aString canvas: aCanvas
  243. element := self createElementFor: aString.
  244. canvas := aCanvas
  245. !
  246. initializeFromJQuery: aJQuery canvas: aCanvas
  247. element := aJQuery jquery get: 0.
  248. canvas := aCanvas
  249. ! !
  250. !TagBrush methodsFor: 'private'!
  251. createElementFor: aString
  252. <return document.createElement(String(aString))>
  253. !
  254. createTextNodeFor: aString
  255. <return document.createTextNode(String(aString))>
  256. ! !
  257. !TagBrush class methodsFor: 'instance creation'!
  258. fromString: aString canvas: aCanvas
  259. ^self new
  260. initializeFromString: aString canvas: aCanvas;
  261. yourself
  262. !
  263. fromJQuery: aJQuery canvas: aCanvas
  264. ^self new
  265. initializeFromJQuery: aJQuery canvas: aCanvas;
  266. yourself
  267. ! !
  268. Object subclass: #Widget
  269. instanceVariableNames: ''
  270. category: 'Canvas'!
  271. !Widget methodsFor: 'actions'!
  272. alert: aString
  273. <alert(aString)>
  274. !
  275. confirm: aString
  276. <return window.confirm(aString)>
  277. !
  278. prompt: aString
  279. ^self prompt: aString default: ''
  280. !
  281. prompt: aString default: anotherString
  282. <return window.prompt(aString, anotherString)>
  283. ! !
  284. !Widget methodsFor: 'adding'!
  285. appendToBrush: aTagBrush
  286. self appendToJQuery: aTagBrush asJQuery
  287. !
  288. appendToJQuery: aJQuery
  289. self renderOn: (HTMLCanvas onJQuery: aJQuery)
  290. ! !
  291. !Widget methodsFor: 'rendering'!
  292. renderOn: html
  293. self
  294. ! !
  295. !Object methodsFor: '*Canvas'!
  296. appendToBrush: aTagBrush
  297. aTagBrush append: self asString
  298. ! !
  299. !BlockClosure methodsFor: '*Canvas'!
  300. appendToBrush: aTagBrush
  301. aTagBrush appendBlock: self
  302. ! !
  303. !String methodsFor: '*Canvas'!
  304. appendToBrush: aTagBrush
  305. aTagBrush appendString: self
  306. ! !