JQuery.st 8.3 KB

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