Changeset 3706:170617361020 for themes/ductile
- Timestamp:
- 02/16/18 16:01:59 (8 years ago)
- Branch:
- default
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
themes/ductile/ductile.js
r2566 r3706 1 /*global $ */ 2 'use strict'; 3 1 4 $(function() { 2 5 var create_name = function(text) { 3 6 4 5 7 // Convert text to lower case. 8 var name = text.toLowerCase(); 6 9 7 8 9 10 // Remove leading and trailing spaces, and any non-alphanumeric 11 // characters except for ampersands, spaces and dashes. 12 name = name.replace(/^\s+|\s+$|[^a-z0-9&\s-]/g, ''); 10 13 11 12 14 // Replace '&' with 'and'. 15 name = name.replace(/&/g, 'and'); 13 16 14 15 17 // Replaces spaces with dashes. 18 name = name.replace(/\s/g, '-'); 16 19 17 18 name = name.replace(/(-)+\1/g, "$1");20 // Squash any duplicate dashes. 21 name = name.replace(/(-)+\1/g, '$1'); 19 22 20 21 23 return name; 24 }; 22 25 23 26 var add_link = function() { 24 27 25 26 27 28 // Convert the h2 element text into a value that 29 // is safe to use in a name attribute. 30 var name = create_name($(this).text()); 28 31 29 30 31 32 // Create a name attribute in the following sibling 33 // to act as a fragment anchor. 34 $(this).next().attr('name', name); 32 35 33 34 35 36 37 38 39 40 36 // Replace the h2.toggle element with a link to the 37 // fragment anchor. Use the h2 text to create the 38 // link title attribute. 39 $(this).html( 40 '<a href="#' + name + '" title="Reveal ' + 41 $(this).text() + ' content">' + 42 $(this).html() + '</a>'); 43 }; 41 44 42 43 45 var toggle = function(event) { 46 event.preventDefault(); 44 47 45 46 47 48 49 50 48 // Toggle the 'expanded' class of the h2.toggle 49 // element, then apply the slideToggle effect 50 // to all siblings. 51 $(this).toggleClass('expanded'). 52 nextAll().slideToggle('fast'); 53 }; 51 54 52 53 54 55 55 var remove_focus = function() { 56 // Use the blur() method to remove focus. 57 $(this).blur(); 58 }; 56 59 57 $(document).ready (function() {58 60 $(document).ready(function() { 61 if ($(window).width() < 1024) { 59 62 60 61 $("#sidebar div div h2").addClass('toggle');63 // Set toggle class to each #sidebar h2 64 $('#sidebar div div h2').addClass('toggle'); 62 65 63 64 66 // Hide all h2.toggle siblings 67 $('#sidebar div div h2').nextAll().hide(); 65 68 66 67 69 // Add a link to each h2.toggle element. 70 $('h2.toggle').each(add_link); 68 71 69 70 72 // Add a click event handler to all h2.toggle elements. 73 $('h2.toggle').click(toggle); 71 74 72 73 74 75 }) 75 // Remove the focus from the link tag when accessed with a mouse. 76 $('h2.toggle a').mouseup(remove_focus); 77 } 78 }); 76 79 77 80 });
Note: See TracChangeset
for help on using the changeset viewer.