Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #3 from rrthomas/master

Generalize toggles to work with arbitrary elements
  • Loading branch information...
commit ad56cf8821164c4735d53b287596197d7306ce0a 2 parents 0b90dfa + 2dffa1e
Almog Melamed authored
Showing with 39 additions and 18 deletions.
  1. +14 −5 index.html
  2. +11 −6 verdict.coffee
  3. +14 −7 verdict.js
19 index.html
View
@@ -246,12 +246,21 @@
<div class='desc'>
<h2>Toggle</h2>
<div class='example'>
- <blockquote>Theodore Robert "Ted" Bundy was an American <span class='toggle' id='ted1'>serial killer</span>, <span id='ted2' class='toggle' >rapist</span>, <span id='ted3' class='toggle' >kidnapper</span>, and <span id='ted4' class='toggle' >necrophile</span> who assaulted and murdered numerous young women during the 1970s, and possibly earlier. </blockquote>
+ <blockquote>Theodore Robert "Ted" Bundy was an American <span class='toggle' id='ted1'><span>serial killer</span><span>serial entrepreneur</span></span> and <span id='ted2' class='toggle' ><span>rapist</span><span>rapper</span><span>flapper</span></span> who assaulted and murdered numerous young women during the 1970s, and possibly earlier. </blockquote>
<pre class='sh_javascript_dom'>
-$("#ted1").toggle_select(['serial killer', 'serial entrepreneur']);
-$("#ted2").toggle_select(['rapist', 'rapper', 'flapper']);
-$("#ted3").toggle_select(['kidnapper', 'babysitter']);
-$("#ted4").toggle_select(['necrophile', 'overall, a very decent man']);</pre>
+&lt;span class='toggle' id='ted1'&gt;
+ &lt;span&gt;serial killer&lt;/span&gt;
+ &lt;span&gt;serial entrepreneur&lt;/span&gt;
+&lt;/span&gt;
+
+&lt;span id='ted2' class='toggle' &gt;
+ &lt;span&gt;rapist&lt;/span&gt;
+ &lt;span&gt;rapper&lt;/span&gt;
+ &lt;span&gt;flapper&lt;/span&gt;
+&lt;/span&gt;
+
+$("#ted1").toggle_select();
+$("#ted2").toggle_select();</pre>
</div>
</div>
17 verdict.coffee
View
@@ -70,17 +70,22 @@
list of alternative contents
###
(($) ->
- $.fn.toggle_select = (o) ->
+ $.fn.toggle_select = () ->
# Add the event handlers
toggle = (elem) ->
+ children = elem.children()
+ for i in [1...children.length]
+ children[i].style.display = "none"
elem.click (e) ->
+ children = elem.children()
n = 0
- for n in [0..o.length - 1]
- if o[n] == elem.text()
- elem.text(o[(n + 1) % o.length])
- break
- elem.trigger('verdict_change', elem.text())
+ for i in [0...children.length]
+ if children[i].style.display != "none"
+ n = (i + 1) % children.length
+ children[i].style.display = "none"
+ children[n].style.removeProperty("display")
+ elem.trigger('verdict_change', elem)
@each ->
toggle $(@),
21 verdict.js
View
@@ -86,19 +86,26 @@
(function($) {
- return $.fn.toggle_select = function(o) {
+ return $.fn.toggle_select = function() {
var toggle;
toggle = function(elem) {
+ var children, i, _i, _ref;
+ children = elem.children();
+ for (i = _i = 1, _ref = children.length; 1 <= _ref ? _i < _ref : _i > _ref; i = 1 <= _ref ? ++_i : --_i) {
+ children[i].style.display = "none";
+ }
return elem.click(function(e) {
- var n, _i, _ref;
+ var i, n, _j, _ref1;
+ children = elem.children();
n = 0;
- for (n = _i = 0, _ref = o.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; n = 0 <= _ref ? ++_i : --_i) {
- if (o[n] === elem.text()) {
- elem.text(o[(n + 1) % o.length]);
- break;
+ for (i = _j = 0, _ref1 = children.length; 0 <= _ref1 ? _j < _ref1 : _j > _ref1; i = 0 <= _ref1 ? ++_j : --_j) {
+ if (children[i].style.display !== "none") {
+ n = (i + 1) % children.length;
}
+ children[i].style.display = "none";
}
- return elem.trigger('verdict_change', elem.text());
+ children[n].style.removeProperty("display");
+ return elem.trigger('verdict_change', elem);
});
};
return this.each(function() {
Please sign in to comment.
Something went wrong with that request. Please try again.