In JQuery I can do something like:
$("#p1").css("color","red").slideUp(2000).slideDown(2000);
:and it runs them in order, but when I try the following in Clojurescript JayQ:
(-> $("#p1")
(css "color" "red")
(slideUp 2000)
(slideDown 2000)
)
: then the methods do not run one after the other. Any ideas on how I can do this?
I can't speak to jayq, but if you are trying to interop directly with jQuery you need to use the dot syntax to interop:
(-> (js/$ "#p1")
(.css "color" "red")
(.slideUp 2000)
(.slideDown 2000))
The JayQ syntax is slightly different. This should be more accurate:
(-> ($ "#p1")
(css {:color "red"})
(slide-up 2000)
(slide-down 2000))
Related
I'm trying to write a WYSIWYG text editor that allows me to write text in the top window, and have the resulting scribble text rendered in the bottom window. I figured the way I would do this would be to call scribble myfile.rkt in the background periodically, and then render the resulting output to a bottom screen using render-html-to-text. I'm having some trouble getting the render to work. Based on the documentation I need
in : input-port?
dest : (is-a? html-text<%>)
load-img? : any/c
eval-rkt? : any/c
The relevant parts of my script look like this
#lang racket/gui
(require racket/os racket/runtime-path "init.rkt"
browser/htmltext)
(require browser)
(define f (new frame% [label (~a "Dremacs" "#" (gethostname))]
[width 960]
[height 540]))
(define new-cnv (new cnv% [parent f]))
(send t insert ";; This buffer is for text that is not saved, and for Lisp evaluation.
;; To create a file, visit it with C-x C-f and enter text in its buffer.")
(send new-cnv set-editor t)
(define html-renderer%
(interface (html-text<%>)))
(define viewer (new panel%
[parent f]
[min-width 300]
[min-height 300]))
(render-html-to-text (open-input-file "TODO.html") html-renderer% #t #t)
(send f show #t)
But I get the error
render-html-to-text: expects argument of type <html-text<%> object>; given: '(#<input-port:/home/diego/repos/dremacs/dremacs/TODO.html> #<interface:html-renderer%>)
context...:
/usr/share/racket/pkgs/drracket/browser/htmltext.rkt:61:0: render-html-to-text
"/home/diego/repos/dremacs/dremacs/editor.rkt": [running body]
temp37_0
And I'm not sure what I'm doing wrong. Could anyone help?
The html-renderer% is not created correctly.
Use html-text-mixin to turn a text% into something that handles html-text.
But ... don't expect too much from the html-renderer.
It's old and doesn't support stylesheets.
Back in the day it was used to display documentation for DrRacket.
I'd like to add an "autosave" feature to a Reagent form in a ClojureScript function. In Clojure, I usually make something out of a ScheduledExecutorService. Since that isn't available in ClojureScript, I came up with the following.
(def delay-handle (atom nil))
(defn clear-autosave-delay! []
(.clearTimeout js/window #delay-handle))
(defn start-autosave-delay!
[doc-save-fn delay-ms page-map-atom]
(reset! delay-handle (.setTimeout js/window doc-save-fn delay-ms page-map-atom)))
(defn change-watcher!
[doc-save-fn page-map-atom]
(let [delay (* 1000 (get-in #page-map-atom [:options :editor_autosave_interval]))]
(when (pos? delay)
(clear-autosave-delay!)
(start-autosave-delay! doc-save-fn delay page-map-atom))))
I put the change-watcher! function in the Reagent :on-change handlers for the input controls. Whenever a change occurs, it resets the delay. If the delay expires, the doc-save-fn is called to do the save.
It seems to work Ok, but isn't very "Clojuresque." Is there a more idiomatic way to write this?
Use debouncer for this issue. It's pretty simple and does same
(goog.functions.debounce auto-save-action 1000)
Is there something like arguments.callee of JavaScript for MoonScript?
Since Moonscript functions are defined as local func; func = function() end, they are all recursive. This will print 120:
recursive = (n) -> return n > 1 and n*recursive(n-1) or 1
print recursive 5
As far as I know, there is no arguments.calee alternative, but I haven't seen cases where I'd need it either. Even Mozilla's docs say "there are nearly no cases where the same result cannot be achieved with named function expressions" about arguments.callee.
When mousing into a .thumb element I'd like to change #full-image's src to the .thumb element's src. What I have that is not working:
(defaction change-src [selector src]
[selector] (ef/set-attr :src src))
(defaction thumb-hover []
[".thumb"] (events/listen :mouseenter
#(change-src "#full-image"
; Looks like the following needs to be replaced
; with some $(this).attr('src') equivalent.
(ef/from % (ef/get-attr :src)))))
Can anyone point me to where I can use this? Enfocus is built on domina which is built on closure libs which is built on plain js. I don't know which layer I should be looking for this or if that is even the idiomatic solution.
I got it working with .-currentTarget. See http://ckirkendall.github.io/enfocus-site/#doc-events and http://docs.closure-library.googlecode.com/git/class_goog_events_Event.html.
(defaction change-src [selector src]
[selector] (ef/set-attr :src src))
(defaction thumb-hover []
[".thumb"] (events/listen :mouseenter
#(change-src "#full-image"
(ef/from (.-currentTarget %)
(ef/get-attr :src)))))
there! I'm building an image-processing application in swing/clojure, and right now I need to develop an image panel in which I can click and compute data. Thanks to coobird, I now have a good idea on how to do it in Java, but I still don't get many issues on its integration with Clojure.
Let's take a look at how coobird suggested me doing. First, we should extend a class in Java. In clojure, we do this with the proxy macro, so we'd have something like this:
(def painting-panel
(proxy [JPanel] []))
The next step is to create the class constructor and set some variables.
I can define functions after the second argument of proxy, but how can I create the constructor? Is painting-panel the name of this class (therefore the name of the function I should create)?
How can I deal with class variables? Should I define them with a let, like I did?
Are this and super available for me to use, like I did below?
(def painting-panel
(let [background-image (Image.)
point-clicked (Point.)]
(proxy [JPanel] []
(paintComponent [g]
(do ((.paintComponent super) g)
(doto g
(.drawImage background-image 0 0 nil)
(.fillRect (.x point-clicked) (.y point-clicked) 1 1))))
(painting-panel []; constructor?
((.addMouseListener this)
(proxy [MouseAdapter] []
(mouseClicked [e]
(do
(def point-clicked (.getPoint e))
(.repaint this)))))))))
Suggestions and code corrections are also welcome!
Thank you!
proxy actually creates an instance tada! You don't need to create a constructor.
Yes, but consider using a clojure ref instead. Also using def like that on the second last line is nasty! it creates a global binding for point-clicked when your logic relies on the lexically scoped one created by let.
(proxy-super paintComponent g), and yes "this" is available
This works for me:
(let [click (ref nil)
panel (proxy [javax.swing.JPanel] []
(paintComponent [g]
(proxy-super paintComponent g)
(.drawImage g (.getImage
(javax.swing.ImageIcon. "play.png"))
0 0 (.getWidth this) (.getHeight this) nil)
(if #click
(.fillRect g (:x #click) (:y #click) 10 10))))]
(.addMouseListener panel
(proxy [java.awt.event.MouseAdapter] []
(mouseClicked [e]
(let [p (.getPoint e)]
(dosync (ref-set click {:x (.x p), :y (.y p)})))
(javax.swing.SwingUtilities/invokeLater #(.repaint panel)))))
(doto (javax.swing.JFrame.)
(.setContentPane panel)
(.setSize 200 200)
(.show)))