Hello Flapjax team and thanks for your work !
I find myself constantly re-implementing tabs widgets at work, so I
made a generic one with jQuery. Then I thought I'd try with Flapjax
(something similar to
http://andreaslagerkvist.com/jquery/super-simple-tabs/).
At first I didn't know how to get started, until I realized that the
css classes I was changing through callbacks in my jQuery code, really
are time varying values. I really liked how Flapjax let me express how
each tab state is dependent on the state of the others:
tabSelectedB = mergeE(
tabClickE[i],
notE(mergeE.apply(this, otherTabsClickE))
).startsWith(i == 0 ? true : false);
The logic is explicit: a tab is selected if it's been clicked of if
another has not been clicked
Expressing values dependencies this way is a different way of thinking
about problems, higher level, I find this pretty exciting...
On another note, I think adding such a "Tab" or "Navigation" example
to the the Flapjax website demos would be a good idea. This is the
kind of widget you constantly need to deal with, and potential
adopters might relate to it. Yet it is simple enough to be easily
understood by Flapjax beginners. (The form validation demo is also a
good example of "real-world" widget)
This is my first piece of Flapjax code so it's probably ugly and
suboptimal, thanks in advance for any comments or suggestions.
The code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>untitled</title>
<style type="text/css" media="screen">
a.selected { background:red; }
div { display:none; }
div.selected { display:block; }
</style>
<script type="text/javascript" src="../flapjax-2.0.1.min.js"></
script>
<script type="text/javascript">
function loader() {
// each tab and each associated content has a class which is a
behavior value
var tabsContainer = document.getElementById('tabs');
var tabs = tabsContainer.getElementsByTagName('a');
var nbTabs = tabs.length;
var tabClickE = [], otherTabsClickE = []; // tabs click events
var tabSelectedB, tabClassB, contentId, otherTabs; // local loop
vars
for(var i=0; i < nbTabs; i++){
// event for the current tab
tabClickE[i] = extractEventE(tabs[i],'click');
// all other tabs
otherTabs = filter(function(elt){ return elt !== tabs[i] ?
true : false ; }, tabs);
// other tabs click events
otherTabsClickE = map(function(elt) { return extractEventE
(elt,'click'); }, otherTabs);
// a tab is selected, if event is true or other tabs events
are false
// we convert the event into a behavior to specify a starting
state (startsWith): the first tab is selected
tabSelectedB = mergeE(
tabClickE[i],
notE(mergeE.apply(this, otherTabsClickE))
).startsWith(i == 0 ? true : false);
// tabClassB is a behavior equals to "selected" when a tab is
selected
tabClassB = liftB(function(selected){ return selected ?
"selected" : ""; }, tabSelectedB);
// we insert the behavior value as a css classes
insertValueB(tabClassB, tabs[i], 'className');
contentId = tabs[i].href.match(/#(.*)/)[1];
insertValueB(tabClassB, contentId, 'className');
}
}
</script>
</head>
<body onload="loader()">
<ul id="tabs">
<li><a href="#tab-content-1">Content 1</a></li>
<li><a href="#tab-content-2">Content 2</a></li>
<li><a href="#tab-content-3">Content 3</a></li>
</ul>
<div id="tab-content-1"><p>1. Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam </p>
</div>
<div id="tab-content-2"><p>2. Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum. </p>
</div>
<div id="tab-content-3"><p>3. Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur
sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum. </p>
</div>
</body>
</html>
T25