Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
154 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ pom.xml.asc | |
/targets/ | ||
/target/ | ||
.lein-deps-sum | ||
.idea | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,67 @@ | ||
(ns widje.core | ||
"See widje.macros for documenation." | ||
(:require [jayq.core :as jq])) | ||
"See widje.macros for widget-related stuff." | ||
(:require [jayq.core :as jq] | ||
[crate.binding :as binding] | ||
[widje.role :as role])) | ||
|
||
(defn instances [widget] | ||
;; General | ||
|
||
(defn render | ||
"Renders widget, inserts it into container and notifies listeners." | ||
[container widget & params] | ||
(let [node (apply widget params)] | ||
(jq/inner (jq/$ container) node) | ||
(jq/trigger (:notify node) :render))) | ||
|
||
;; Instances | ||
|
||
(defn instances | ||
"Returns all instances (root DOM elements) created by widget. | ||
Result may be map (for parametrized widgets) or vector." | ||
[widget] | ||
@(.-_instances widget)) | ||
|
||
(defn find-instance [widget param] | ||
(defn find-instance | ||
"Returns instance (root DOM element) created by widget with | ||
param passed as first argument. Works only for widgets with | ||
at least one argument." | ||
[widget param] | ||
((instances widget) param)) | ||
|
||
(defn render [container widget & params] | ||
"Renders widget, inserts it into container and notifies listeners." | ||
(let [node (apply widget params)] | ||
(jq/inner (jq/$ container) node) | ||
(jq/trigger (:notify node) :render))) | ||
;; Binding | ||
|
||
(declare atoms-binding) | ||
|
||
(def bound | ||
^{:doc "Returns value of func (or just value) bound to atm. | ||
func is a function of one argument: current value of atm." | ||
:arglists '([atm func?])} | ||
binding/bound) | ||
|
||
(defn bound* | ||
^{:doc "Returns value of func (or just value) bound to all provided atoms. | ||
func is a function of (count atm*) arguments: current values of atm(s)." | ||
:arglists '([[atm*] func?])} | ||
[atoms & [func]] | ||
(let [func (or func identity)] | ||
(atoms-binding. atoms func))) | ||
|
||
(def bound-coll | ||
^{:doc "Returns value of func (:as opts) bound to each element of collection atom." | ||
:arglists '(atm path? opts?)} | ||
binding/bound-coll) | ||
|
||
(def map-bound | ||
^{:doc "Returns value of as func bound to each element of map atom." | ||
:arglists '(as atm opts?)} | ||
binding/map-bound) | ||
|
||
;; Internals | ||
|
||
(deftype atoms-binding [atoms value-func] | ||
crate.binding/bindable | ||
(-value [this] (apply value-func (map deref atoms))) | ||
(-on-change [this func] | ||
(doseq [atm atoms] | ||
(add-watch atm (gensym "atom-binding") #(func (crate.binding/-value this)))))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
(ns widje.util | ||
(:require [jayq.core :as jq]) | ||
(:use-macros [widje.macros :only [defwidget]])) | ||
|
||
;; Events | ||
|
||
(defn evt->key | ||
"Maps event to readable key names" | ||
[e] | ||
(get { | ||
08 :backspace | ||
09 :tab | ||
13 :enter | ||
16 :shift | ||
17 :ctrl | ||
18 :alt | ||
19 :pause | ||
20 :capslock | ||
27 :esc | ||
32 :space | ||
33 :page-up | ||
34 :page-down | ||
35 :end | ||
36 :home | ||
37 :left-arrow | ||
38 :up-arrow | ||
39 :right-arrow | ||
40 :down-arrow | ||
45 :insert | ||
46 :delete | ||
} (.-keyCode e))) | ||
|
||
;; Checkbox | ||
|
||
;; sidenote: checkbox state querying and manipulation is | ||
;; definitely not well-designed in html/dom | ||
|
||
(defn checkbox-checked? | ||
"Returns boolean state of checkbox" | ||
[checkbox] | ||
(jq/is ($ checkbox) ":checked")) | ||
|
||
(defn check! [checkbox value] | ||
"Sets boolean state of checkbox" | ||
(if value | ||
(jq/attr ($ checkbox) "checked" true) | ||
(jq/remove-attr ($ checkbox) "checked"))) | ||
|
||
; Widget: checkbox bound to atom via val-fn | ||
(defwidget bound-checkbox [id classes atm val-fn] | ||
[:input.-checkbox {:id id | ||
:class (str classes " -checkbox") | ||
:type "checkbox"}] | ||
[checkbox] | ||
(check! checkbox (val-fn @atm)) | ||
(add-watch atm (gensym "bound-checkbox") | ||
#(check! checkbox (val-fn %4)))) |