Dotclear

source: themes/ductile/ductile.js @ 3706:170617361020

Revision 3706:170617361020, 2.1 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

use strict and no more linter warnings/errors as far as possible

Line 
1/*global $ */
2'use strict';
3
4$(function() {
5  var create_name = function(text) {
6
7    // Convert text to lower case.
8    var name = text.toLowerCase();
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, '');
13
14    // Replace '&' with 'and'.
15    name = name.replace(/&/g, 'and');
16
17    // Replaces spaces with dashes.
18    name = name.replace(/\s/g, '-');
19
20    // Squash any duplicate dashes.
21    name = name.replace(/(-)+\1/g, '$1');
22
23    return name;
24  };
25
26  var add_link = function() {
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());
31
32    // Create a name attribute in the following sibling
33    // to act as a fragment anchor.
34    $(this).next().attr('name', name);
35
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  };
44
45  var toggle = function(event) {
46    event.preventDefault();
47
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  };
54
55  var remove_focus = function() {
56    // Use the blur() method to remove focus.
57    $(this).blur();
58  };
59
60  $(document).ready(function() {
61    if ($(window).width() < 1024) {
62
63      // Set toggle class to each #sidebar h2
64      $('#sidebar div div h2').addClass('toggle');
65
66      // Hide all h2.toggle siblings
67      $('#sidebar div div h2').nextAll().hide();
68
69      // Add a link to each h2.toggle element.
70      $('h2.toggle').each(add_link);
71
72      // Add a click event handler to all h2.toggle elements.
73      $('h2.toggle').click(toggle);
74
75      // Remove the focus from the link tag when accessed with a mouse.
76      $('h2.toggle a').mouseup(remove_focus);
77    }
78  });
79
80});
Note: See TracBrowser for help on using the repository browser.

Sites map