Skip to content

Commit

Permalink
Added new file for Javascript-centric utilities. Added collapsible se…
Browse files Browse the repository at this point in the history
…ctions convenience layer.
  • Loading branch information
skypher committed Apr 21, 2010
1 parent b169f16 commit 4a04ac5
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
53 changes: 53 additions & 0 deletions pub/scripts/weblocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,56 @@ function include_dom(script_filename) {
return false;
}

/* working with CSS classes */
function addClass(el,class){
if ((hasClass(el,class)) || (typeof el == 'undefined'))
return;
el.className += " " + class;
}

function removeClass(el,class){
if (typeof el=='undefined')
return;
if (el.getAttribute('class') === null)
return;

var classes = el.getAttribute('class').split(" ");
var result=[];

for (i=classes.length;i>=0;i--) {
if (classes[i] != class)
result.push(classes[i]);
}

el.setAttribute('class', result.join(" ")); /* FIXME: ie6/7 need className here */
}

function hasClass(el, class){
if ((el.className === null) || (typeof el == 'undefined'))
return false;

var classes = el.className.split(" ");

for (i=classes.length;i>=0;i--) {
if (classes[i] == class)
return true;
}

return false;
}

/* collapsible sections */
function toggleExpandCollapse (heading,container) {
if (hasClass(heading,"collapsed")) {
removeClass(heading,"collapsed");
removeClass(container,"collapsed");
addClass(heading,"expanded");
addClass(container,"expanded");
} else {
removeClass(heading,"expanded");
removeClass(container,"expanded");
addClass(heading,"collapsed");
addClass(container,"collapsed");
}
}

25 changes: 25 additions & 0 deletions pub/stylesheets/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,28 @@ div.empty p span.caption
font-family: Garamond, New York, serif;
}

.collapsible-heading {
cursor:default;
}

.collapsible-container.expanded {
display:block;
}

.collapsible-heading.expanded:after {
content:" (click to collapse)";
font-size:small;
color:gray;

}

.collapsible-container.collapsed {
display:none;
}

.collapsible-heading.collapsed:after {
content:" (click to expand)";
font-size:small;
color:gray;
}

37 changes: 37 additions & 0 deletions src/utils/javascript.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

(in-package :weblocks)

(export '(begin-section
end-section
begin-collapsible-section
end-collapsible-section))

(defun begin-section (title &key (container-class "") (container-id (gen-id)) (heading t)
heading-onclick heading-class)
"Begin a section (a DIV container) on the client.
The section is preceded by a heading 3 unless HEADING is NIL."
(when heading
(write
(with-html-to-string
(:h3 :class heading-class :onclick heading-onclick (esc title)))
:stream *weblocks-output-stream*
:escape nil))
(format *weblocks-output-stream* "<div id='~D' class='~A'>" container-id container-class))

(defun end-section ()
"End a client section."
(format *weblocks-output-stream* "</div>"))

(defun begin-collapsible-section (title)
"Begin a section that is expandable/collapsible via clicks on
its heading."
(let ((id (gen-id)))
(begin-section title
:container-id id
:heading-onclick (format nil "toggleExpandCollapse(this,document.getElementById(\"~D\"));" id)
:heading-class "collapsible-heading collapsed"
:container-class "collapsible-container collapsed")))

(defun end-collapsible-section ()
(end-section))

1 change: 1 addition & 0 deletions weblocks.asd
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
(:file "list")
(:file "uri")
(:file "html")
(:file "javascript")
(:file "isearch"
:depends-on ("html"))
(:file "menu"
Expand Down

0 comments on commit 4a04ac5

Please sign in to comment.