1
0

JQuery.st 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. !JQuery methodsFor: 'css'!
  45. cssAt: aString
  46. <return self['@jquery'].css(aString)>
  47. !
  48. cssAt: aString put: anotherString
  49. <self['@jquery'].css(aString, anotherString)>
  50. !
  51. addClass: aString
  52. "Adds the specified class(es) to each of the set of matched elements."
  53. self call: 'addClass' withArgument: aString
  54. !
  55. removeClass: aString
  56. "Remove a single class, multiple classes, or all classes from each element in the set of matched elements."
  57. self call: 'removeClass' withArgument: aString
  58. !
  59. toggleClass: aString
  60. "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."
  61. self call: 'toggleClass' withArgument: aString
  62. !
  63. height
  64. "Get the current computed height for the first element in the set of matched elements."
  65. ^self call: 'height'
  66. !
  67. height: anInteger
  68. self call: 'height' withArgument: anInteger
  69. !
  70. width: anInteger
  71. self call: 'width' withArgument: anInteger
  72. !
  73. width
  74. "Get the current computed width for the first element in the set of matched elements."
  75. ^self call: 'width'
  76. !
  77. innerHeight
  78. "Get the current computed height for the first element in the set of matched elements, including padding but not border."
  79. ^self call: 'innerHeight'
  80. !
  81. innerWidth
  82. "Get the current computed width for the first element in the set of matched elements, including padding but not border."
  83. ^self call: 'innerWidth'
  84. !
  85. outerHeight
  86. "Get the current computed height for the first element in the set of matched elements, including padding, border, and optionally margin."
  87. ^self call: 'outerHeight'
  88. !
  89. outerWidth
  90. "Get the current computed width for the first element in the set of matched elements, including padding and border."
  91. ^self call: 'outerWidth'
  92. !
  93. top
  94. "Get the current y coordinate of the first element in the set of matched elements, relative to the offset parent."
  95. ^(self call: 'position') basicAt: 'top'
  96. !
  97. left
  98. "Get the current x coordinate of the first element in the set of matched elements, relative to the offset parent."
  99. ^(self call: 'position') basicAt: 'left'
  100. !
  101. offsetLeft
  102. "Get the current coordinates of the first element in the set of matched elements, relative to the document."
  103. ^(self call: 'offset') basicAt: 'left'
  104. !
  105. offsetTop
  106. "Get the current coordinates of the first element in the set of matched elements, relative to the document."
  107. ^(self call: 'offset') basicAt: 'top'
  108. !
  109. scrollLeft
  110. "Get the current horizontal position of the scroll bar for the first element in the set of matched elements."
  111. ^self call: 'scrollLeft'
  112. !
  113. scrollTop
  114. "Get the current vertical position of the scroll bar for the first element in the set of matched elements."
  115. ^self call: 'scrollTop'
  116. !
  117. scrollLeft: anInteger
  118. self call: 'scrollLeft' withArgument: anInteger
  119. !
  120. scrollTop: anInteger
  121. self call: 'scrollTop' withArgument: anInteger
  122. ! !
  123. !JQuery methodsFor: 'enumerating'!
  124. do: aBlock
  125. self elementsDo: [:anElement| aBlock value: (JQuery fromElement: anElement)]
  126. ! !
  127. !JQuery methodsFor: 'events'!
  128. focus
  129. self call: 'focus'
  130. !
  131. show
  132. self call: 'show'
  133. !
  134. hide
  135. self call: 'hide'
  136. !
  137. remove
  138. self call: 'remove'
  139. !
  140. on: anEventString do: aBlock
  141. "Attach aBlock for anEventString on the element"
  142. <self['@jquery'].bind(anEventString, function(e){aBlock(e, self)})>
  143. !
  144. removeEvents: aString
  145. "Unbind all handlers attached to the event aString"
  146. self call: 'unbind' withArgument: aString
  147. ! !
  148. !JQuery methodsFor: 'initialization'!
  149. initializeWithJQueryObject: anObject
  150. jquery := anObject
  151. ! !
  152. !JQuery methodsFor: 'private'!
  153. call: aString
  154. <return self['@jquery'][aString]()>
  155. !
  156. call: aString withArgument: anObject
  157. <return self['@jquery'][aString](anObject)>
  158. !
  159. elementsDo: aBlock
  160. "Iterate over a jQuery object, executing a function for each matched element."
  161. <self['@jquery'].each(function(index, element){aBlock(element, self)})>
  162. ! !
  163. !JQuery methodsFor: 'testing'!
  164. hasClass: aString
  165. "Determine whether any of the matched elements are assigned the given class."
  166. ^self call: 'hasClass' withArgument: aString
  167. ! !
  168. !JQuery class methodsFor: 'instance creation'!
  169. fromString: aString
  170. | newJQuery |
  171. <newJQuery = jQuery(String(aString))>.
  172. ^self from: newJQuery
  173. !
  174. from: anObject
  175. ^self new
  176. initializeWithJQueryObject: anObject;
  177. yourself
  178. !
  179. window
  180. <return self._from_(jQuery(window))>
  181. !
  182. body
  183. <return self._from_(jQuery('body'))>
  184. !
  185. document
  186. <return self._from_(jQuery(document))>
  187. !
  188. fromElement: anElement
  189. | newJQuery |
  190. <newJQuery = jQuery(anElement)>.
  191. ^self from: newJQuery
  192. !
  193. documentReady: aBlock
  194. <jQuery(document).ready(aBlock)>
  195. ! !
  196. Object subclass: #Ajax
  197. instanceVariableNames: 'settings'
  198. category: 'JQuery'!
  199. !Ajax commentStamp!
  200. instance variable names:
  201. - settings A set of key/value pairs that configure the Ajax request. All settings are optional.
  202. Full list of settings options at http://api.jquery.com/jQuery.ajax/!
  203. !Ajax methodsFor: 'accessing'!
  204. at: aKey
  205. ^settings at: aKey ifAbsent: [nil]
  206. !
  207. at: aKey put: aValue
  208. settings at: aKey put: aValue
  209. !
  210. url
  211. ^self at: 'url'
  212. !
  213. url: aString
  214. self at: 'url' put: aString
  215. !
  216. onSuccessDo: aBlock
  217. "Set action to execute when Ajax request is successful. Pass received data as block argument"
  218. self at: 'success' put: aBlock
  219. ! !
  220. !Ajax methodsFor: 'actions'!
  221. send
  222. <jQuery.ajax(self['@settings'])>
  223. ! !
  224. !Ajax methodsFor: 'initialization'!
  225. initialize
  226. super initialize.
  227. settings := Dictionary new
  228. ! !
  229. !Ajax class methodsFor: 'instance creation'!
  230. url: aString
  231. ^self new
  232. url: aString;
  233. yourself
  234. ! !
  235. !BlockClosure methodsFor: '*JQuery'!
  236. appendToJQuery: aJQuery
  237. self value: (HTMLCanvas onJQuery: aJQuery)
  238. ! !
  239. !String methodsFor: '*JQuery'!
  240. asJQuery
  241. ^JQuery fromString: self
  242. !
  243. appendToJQuery: aJQuery
  244. <aJQuery._appendElement_(String(self))>
  245. ! !
  246. !HTMLCanvas methodsFor: '*JQuery'!
  247. appendToJQuery: aJQuery
  248. aJQuery appendElement: root element
  249. ! !