JQuery.st 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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: 'effects'!
  128. fadeIn
  129. self call: 'fadeIn'
  130. !
  131. slideDown
  132. self call: 'slideDown'
  133. !
  134. fadeInSlow
  135. self call: 'fadeIn' withArgument: 'slow'
  136. !
  137. fadeOut
  138. self call: 'fadeOut'
  139. !
  140. fadeOutSlow
  141. self call: 'fadeOut' withArgument: 'slow'
  142. !
  143. slideUp
  144. self call: 'slideUp'
  145. ! !
  146. !JQuery methodsFor: 'enumerating'!
  147. do: aBlock
  148. self elementsDo: [:anElement| aBlock value: (JQuery fromElement: anElement)]
  149. ! !
  150. !JQuery methodsFor: 'events'!
  151. focus
  152. self call: 'focus'
  153. !
  154. show
  155. self call: 'show'
  156. !
  157. hide
  158. self call: 'hide'
  159. !
  160. remove
  161. self call: 'remove'
  162. !
  163. on: anEventString do: aBlock
  164. "Attach aBlock for anEventString on the element"
  165. <self['@jquery'].bind(anEventString, function(e){aBlock(e, self)})>
  166. !
  167. removeEvents: aString
  168. "Unbind all handlers attached to the event aString"
  169. self call: 'unbind' withArgument: aString
  170. !
  171. onLoadDo: aBlock
  172. "Bind an event handler to the 'load' JavaScript event."
  173. self call: 'load' withArgument: aBlock
  174. ! !
  175. !JQuery methodsFor: 'initialization'!
  176. initializeWithJQueryObject: anObject
  177. jquery := anObject
  178. ! !
  179. !JQuery methodsFor: 'private'!
  180. call: aString
  181. <return self['@jquery'][aString]()>
  182. !
  183. call: aString withArgument: anObject
  184. <return self['@jquery'][aString](anObject)>
  185. !
  186. elementsDo: aBlock
  187. "Iterate over a jQuery object, executing a function for each matched element."
  188. <self['@jquery'].each(function(index, element){aBlock(element, self)})>
  189. ! !
  190. !JQuery methodsFor: 'testing'!
  191. hasClass: aString
  192. "Determine whether any of the matched elements are assigned the given class."
  193. ^self call: 'hasClass' withArgument: aString
  194. ! !
  195. !JQuery methodsFor: 'traversing'!
  196. find: aSelector
  197. "Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element."
  198. ^ self call: 'find' withArgument: aSelector
  199. ! !
  200. !JQuery class methodsFor: 'instance creation'!
  201. fromString: aString
  202. | newJQuery |
  203. <newJQuery = jQuery(String(aString))>.
  204. ^self from: newJQuery
  205. !
  206. from: anObject
  207. ^self new
  208. initializeWithJQueryObject: anObject;
  209. yourself
  210. !
  211. window
  212. <return self._from_(jQuery(window))>
  213. !
  214. body
  215. <return self._from_(jQuery('body'))>
  216. !
  217. document
  218. <return self._from_(jQuery(document))>
  219. !
  220. fromElement: anElement
  221. | newJQuery |
  222. <newJQuery = jQuery(anElement)>.
  223. ^self from: newJQuery
  224. !
  225. documentReady: aBlock
  226. <jQuery(document).ready(aBlock)>
  227. ! !
  228. Object subclass: #Ajax
  229. instanceVariableNames: 'settings'
  230. category: 'JQuery'!
  231. !Ajax commentStamp!
  232. instance variable names:
  233. - settings A set of key/value pairs that configure the Ajax request. All settings are optional.
  234. Full list of settings options at http://api.jquery.com/jQuery.ajax/!
  235. !Ajax methodsFor: 'accessing'!
  236. at: aKey
  237. ^settings at: aKey ifAbsent: [nil]
  238. !
  239. at: aKey put: aValue
  240. settings at: aKey put: aValue
  241. !
  242. url
  243. ^self at: 'url'
  244. !
  245. url: aString
  246. self at: 'url' put: aString
  247. ! !
  248. !Ajax methodsFor: 'actions'!
  249. send
  250. <jQuery.ajax(self['@settings'])>
  251. ! !
  252. !Ajax methodsFor: 'callbacks'!
  253. onSuccessDo: aBlock
  254. "Set action to execute when Ajax request is successful. Pass received data as block argument. Block arguments: data, textStatus, jqXHR"
  255. self at: 'success' put: aBlock
  256. !
  257. onCompleteDo: aBlock
  258. "A block to be called when the request finishes (after success and error callbacks are executed). Block arguments: jqXHR, textStatus"
  259. self at: 'complete' put: aBlock
  260. !
  261. onErrorDo: aBlock
  262. "A block to be called if the request fails.Block arguments: jqXHR, textStatus, errorThrown"
  263. self at: 'error' put: aBlock
  264. ! !
  265. !Ajax methodsFor: 'initialization'!
  266. initialize
  267. super initialize.
  268. settings := Dictionary new
  269. ! !
  270. !Ajax class methodsFor: 'instance creation'!
  271. url: aString
  272. ^self new
  273. url: aString;
  274. yourself
  275. ! !
  276. !BlockClosure methodsFor: '*JQuery'!
  277. appendToJQuery: aJQuery
  278. self value: (HTMLCanvas onJQuery: aJQuery)
  279. ! !
  280. !String methodsFor: '*JQuery'!
  281. asJQuery
  282. ^JQuery fromString: self
  283. !
  284. appendToJQuery: aJQuery
  285. <aJQuery._appendElement_(String(self))>
  286. ! !
  287. !HTMLCanvas methodsFor: '*JQuery'!
  288. appendToJQuery: aJQuery
  289. aJQuery appendElement: root element
  290. ! !