Permalink
Browse files

add in dom/class functions and more insertion functions

Signed-off-by: Chris Granger <ibdknox@gmail.com>
  • Loading branch information...
1 parent 37fdec6 commit dd08f21bdccc3f61935017841dd5cea05f79c5d2 @ibdknox committed Oct 6, 2011
Showing with 54 additions and 1 deletion.
  1. +54 −0 src/pinot/dom.cljs
  2. +0 −1 src/pinot/events.cljs
View
@@ -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]
@@ -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
;; ********************************************
@@ -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
@@ -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))))

0 comments on commit dd08f21

Please sign in to comment.