Skip to content

Commit

Permalink
add in dom/class functions and more insertion functions
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Granger <ibdknox@gmail.com>
  • Loading branch information
ibdknox committed Oct 6, 2011
1 parent 37fdec6 commit dd08f21
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
54 changes: 54 additions & 0 deletions src/pinot/dom.cljs
@@ -1,6 +1,8 @@
(ns pinot.dom
(:refer-clojure :exclude [replace empty])
(:require [goog.dom :as dom]
[goog.events :as events]
[goog.dom.classes :as gclass]
[goog.style :as gstyle]
[goog.dom.query :as query]
[goog.dom.forms :as forms]
Expand Down Expand Up @@ -57,6 +59,28 @@
elem)
(forms/getValue elem))))


(defn has-class? [elem cls]
(let [elem (if (coll? elem)
(first elem)
elem)]
(gclass/has elem cls)))

(defn add-class [elem cls]
(doseq [el (pclj/->coll elem)]
(gclass/add el cls))
elem)

(defn remove-class [elem cls]
(doseq [el (pclj/->coll elem)]
(gclass/remove el cls))
elem)

(defn toggle-class [elem cls]
(doseq [el (pclj/->coll elem)]
(gclass/toggle el cls))
elem)

;; ********************************************
;; Dom interaction functions
;; ********************************************
Expand Down Expand Up @@ -85,6 +109,36 @@
(doseq [elem (pclj/->coll elem)]
(dom/removeNode elem)))

(defn before [elem & [sibling]]
(doseq [el (pclj/->coll elem)
sibling (pclj/->coll sibling)]
(if sibling
(dom/insertSiblingBefore (dom-clone sibling) el)
(dom/getPreviousElementSibling el))))

(defn after [elem & [sibling]]
(doseq [el (pclj/->coll elem)
sibling (pclj/->coll sibling)]
(if sibling
(dom/insertSiblingAfter (dom-clone sibling) el)
(dom/getNextElementSibling el))))

(defn prepend [elem neue]
(doseq [el (pclj/->coll elem)]
(let [firstChild (dom/getFirstElementChild el)]
(if firstChild
(before firstChild neue)
(append el neue)))))

(defn replace [elem neue]
(doseq [el (pclj/->coll elem)]
(after el neue)
(unappend el)))

(defn empty [elem]
(doseq [el (pclj/->coll elem)]
(dom/removeChildren el)))

(defn nodelist->coll [nl]
;; The results are a nodelist, which looks like an array, but
;; isn't one. We have to turn it into a collection that we can
Expand Down
1 change: 0 additions & 1 deletion src/pinot/events.cljs
Expand Up @@ -45,7 +45,6 @@
body-elem (get-body)]
(doseq [el (pclj/->coll elem)]
(let [parsed (->target el)]
(pjs/log parsed)
(events/listen body-elem
event
(make-listener func parsed))))
Expand Down

0 comments on commit dd08f21

Please sign in to comment.