2
0

JQuery.st 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. Object subclass: #JQuery
  2. instanceVariableNames: 'jquery'
  3. category: 'JQuery'!
  4. !JQuery methodsFor: 'DOM insertion'!
  5. append: anObject
  6. "Append anObject at the end of the element."
  7. anObject appendToJQuery: self
  8. !
  9. appendElement: anElement
  10. "Append anElement at the end of the element.
  11. Dont't call this method directly, use #append: instead"
  12. self call: 'append' withArgument: anElement
  13. !
  14. appendToJQuery: aJQuery
  15. aJQuery appendElement: jquery
  16. !
  17. contents: anObject
  18. self empty.
  19. self append: anObject
  20. !
  21. empty
  22. ^self call: 'empty'
  23. ! !
  24. !JQuery methodsFor: 'accessing'!
  25. jquery
  26. ^jquery
  27. ! !
  28. !JQuery methodsFor: 'attributes'!
  29. removeAttribute: aString
  30. "Remove an attribute from each element in the set of matched elements."
  31. ^self call: 'removeAttribute' withArgument: aString
  32. !
  33. attr: aString
  34. "Get the value of an attribute for the first element in the set of matched elements."
  35. ^self call: 'attr' withArgument: aString
  36. !
  37. val
  38. "Get the current value of the first element in the set of matched elements."
  39. ^self call: 'val'
  40. !
  41. val: aString
  42. self call: 'val' withArgument: aString
  43. !
  44. attrAt: aString put: anotherString
  45. "Set the value of an attribute for the first element in the set of matched elements."
  46. <self['@jquery'].attr(aString, anotherString)>
  47. ! !
  48. !JQuery methodsFor: 'css'!
  49. cssAt: aString
  50. <return self['@jquery'].css(aString)>
  51. !
  52. cssAt: aString put: anotherString
  53. <self['@jquery'].css(aString, anotherString)>
  54. !
  55. addClass: aString
  56. "Adds the specified class(es) to each of the set of matched elements."
  57. self call: 'addClass' withArgument: aString
  58. !
  59. removeClass: aString
  60. "Remove a single class, multiple classes, or all classes from each element in the set of matched elements."
  61. self call: 'removeClass' withArgument: aString
  62. !
  63. toggleClass: aString
  64. "Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the switch argument."
  65. self call: 'toggleClass' withArgument: aString
  66. !
  67. height
  68. "Get the current computed height for the first element in the set of matched elements."
  69. ^self call: 'height'
  70. !
  71. height: anInteger
  72. self call: 'height' withArgument: anInteger
  73. !
  74. width: anInteger
  75. self call: 'width' withArgument: anInteger
  76. !
  77. width
  78. "Get the current computed width for the first element in the set of matched elements."
  79. ^self call: 'width'
  80. !
  81. innerHeight
  82. "Get the current computed height for the first element in the set of matched elements, including padding but not border."
  83. ^self call: 'innerHeight'
  84. !
  85. innerWidth
  86. "Get the current computed width for the first element in the set of matched elements, including padding but not border."
  87. ^self call: 'innerWidth'
  88. !
  89. outerHeight
  90. "Get the current computed height for the first element in the set of matched elements, including padding, border, and optionally margin."
  91. ^self call: 'outerHeight'
  92. !
  93. outerWidth
  94. "Get the current computed width for the first element in the set of matched elements, including padding and border."
  95. ^self call: 'outerWidth'
  96. !
  97. top
  98. "Get the current y coordinate of the first element in the set of matched elements, relative to the offset parent."
  99. ^(self call: 'position') basicAt: 'top'
  100. !
  101. left
  102. "Get the current x coordinate of the first element in the set of matched elements, relative to the offset parent."
  103. ^(self call: 'position') basicAt: 'left'
  104. !
  105. offsetLeft
  106. "Get the current coordinates of the first element in the set of matched elements, relative to the document."
  107. ^(self call: 'offset') basicAt: 'left'
  108. !
  109. offsetTop
  110. "Get the current coordinates of the first element in the set of matched elements, relative to the document."
  111. ^(self call: 'offset') basicAt: 'top'
  112. !
  113. scrollLeft
  114. "Get the current horizontal position of the scroll bar for the first element in the set of matched elements."
  115. ^self call: 'scrollLeft'
  116. !
  117. scrollTop
  118. "Get the current vertical position of the scroll bar for the first element in the set of matched elements."
  119. ^self call: 'scrollTop'
  120. !
  121. scrollLeft: anInteger
  122. self call: 'scrollLeft' withArgument: anInteger
  123. !
  124. scrollTop: anInteger
  125. self call: 'scrollTop' withArgument: anInteger
  126. ! !
  127. !JQuery methodsFor: 'enumerating'!
  128. do: aBlock
  129. self elementsDo: [:anElement| aBlock value: (JQuery fromElement: anElement)]
  130. ! !
  131. !JQuery methodsFor: 'events'!
  132. focus
  133. self call: 'focus'
  134. !
  135. show
  136. self call: 'show'
  137. !
  138. hide
  139. self call: 'hide'
  140. !
  141. remove
  142. self call: 'remove'
  143. !
  144. on: anEventString do: aBlock
  145. "Attach aBlock for anEventString on the element"
  146. <self['@jquery'].bind(anEventString, function(e){aBlock(e, self)})>
  147. !
  148. removeEvents: aString
  149. "Unbind all handlers attached to the event aString"
  150. self call: 'unbind' withArgument: aString
  151. ! !
  152. !JQuery methodsFor: 'initialization'!
  153. initializeWithJQueryObject: anObject
  154. jquery := anObject
  155. ! !
  156. !JQuery methodsFor: 'private'!
  157. call: aString
  158. <return self['@jquery'][aString]()>
  159. !
  160. call: aString withArgument: anObject
  161. <return self['@jquery'][aString](anObject)>
  162. !
  163. elementsDo: aBlock
  164. "Iterate over a jQuery object, executing a function for each matched element."
  165. <self['@jquery'].each(function(index, element){aBlock(element, self)})>
  166. ! !
  167. !JQuery methodsFor: 'testing'!
  168. hasClass: aString
  169. "Determine whether any of the matched elements are assigned the given class."
  170. ^self call: 'hasClass' withArgument: aString
  171. ! !
  172. !JQuery methodsFor: 'traversing'!
  173. find: aSelector
  174. "Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element."
  175. ^ self call: 'find' withArgument: aSelector
  176. ! !
  177. !JQuery class methodsFor: 'instance creation'!
  178. fromString: aString
  179. | newJQuery |
  180. <newJQuery = jQuery(String(aString))>.
  181. ^self from: newJQuery
  182. !
  183. from: anObject
  184. ^self new
  185. initializeWithJQueryObject: anObject;
  186. yourself
  187. !
  188. window
  189. <return self._from_(jQuery(window))>
  190. !
  191. body
  192. <return self._from_(jQuery('body'))>
  193. !
  194. document
  195. <return self._from_(jQuery(document))>
  196. !
  197. fromElement: anElement
  198. | newJQuery |
  199. <newJQuery = jQuery(anElement)>.
  200. ^self from: newJQuery
  201. !
  202. documentReady: aBlock
  203. <jQuery(document).ready(aBlock)>
  204. ! !
  205. Object subclass: #Ajax
  206. instanceVariableNames: 'settings'
  207. category: 'JQuery'!
  208. !Ajax commentStamp!
  209. instance variable names:
  210. - settings A set of key/value pairs that configure the Ajax request. All settings are optional.
  211. Full list of settings options at http://api.jquery.com/jQuery.ajax/!
  212. !Ajax methodsFor: 'accessing'!
  213. at: aKey
  214. ^settings at: aKey ifAbsent: [nil]
  215. !
  216. at: aKey put: aValue
  217. settings at: aKey put: aValue
  218. !
  219. url
  220. ^self at: 'url'
  221. !
  222. url: aString
  223. self at: 'url' put: aString
  224. ! !
  225. !Ajax methodsFor: 'actions'!
  226. send
  227. <jQuery.ajax(self['@settings'])>
  228. ! !
  229. !Ajax methodsFor: 'callbacks'!
  230. onSuccessDo: aBlock
  231. "Set action to execute when Ajax request is successful. Pass received data as block argument. Block arguments: data, textStatus, jqXHR"
  232. self at: 'success' put: aBlock
  233. !
  234. onCompleteDo: aBlock
  235. "A block to be called when the request finishes (after success and error callbacks are executed). Block arguments: jqXHR, textStatus"
  236. self at: 'complete' put: aBlock
  237. !
  238. onErrorDo: aBlock
  239. "A block to be called if the request fails.Block arguments: jqXHR, textStatus, errorThrown"
  240. self at: 'error' put: aBlock
  241. ! !
  242. !Ajax methodsFor: 'initialization'!
  243. initialize
  244. super initialize.
  245. settings := Dictionary new
  246. ! !
  247. !Ajax class methodsFor: 'instance creation'!
  248. url: aString
  249. ^self new
  250. url: aString;
  251. yourself
  252. ! !
  253. !BlockClosure methodsFor: '*JQuery'!
  254. appendToJQuery: aJQuery
  255. self value: (HTMLCanvas onJQuery: aJQuery)
  256. ! !
  257. !String methodsFor: '*JQuery'!
  258. asJQuery
  259. ^JQuery fromString: self
  260. !
  261. appendToJQuery: aJQuery
  262. <aJQuery._appendElement_(String(self))>
  263. ! !
  264. !HTMLCanvas methodsFor: '*JQuery'!
  265. appendToJQuery: aJQuery
  266. aJQuery appendElement: root element
  267. ! !