Skip to content

Commit df2ea70

Browse files
committed
Issue #9: Refactor variable initiation to make it more readable, use is-active class to detect active menu item in order to select correct option in drop-down.
1 parent 82c926d commit df2ea70

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

sfsmallscreen.js

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@
3535

3636
// We need to clean up the menu from anything unnecessary.
3737
function refine(menu){
38-
var
39-
refined = menu.clone(),
38+
var refined = menu.clone();
4039
// Things that should not be in the small-screen menus.
41-
rm = refined.find('span.sf-sub-indicator'),
40+
var rm = refined.find('span.sf-sub-indicator');
4241
// This is a helper class for those who need to add extra markup that shouldn't exist
4342
// in the small-screen versions.
44-
rh = refined.find('.sf-smallscreen-remove'),
43+
var rh = refined.find('.sf-smallscreen-remove');
4544
// Mega-menus has to be removed too.
46-
mm = refined.find('ul.sf-multicolumn');
45+
var mm = refined.find('ul.sf-multicolumn');
4746
for (var a = 0; a < rh.length; a++){
4847
rh.eq(a).replaceWith(rh.eq(a).html());
4948
}
@@ -73,9 +72,8 @@
7372

7473
// Creating <option> elements out of the menu.
7574
function toSelect(menu, level){
76-
var
77-
items = '',
78-
childLI = $(menu).children('li');
75+
var items = '';
76+
var childLI = $(menu).children('li');
7977
for (var a = 0; a < childLI.length; a++){
8078
var list = childLI.eq(a), parent = list.children('a, span');
8179
for (var b = 0; b < parent.length; b++){
@@ -109,24 +107,22 @@
109107

110108
// Create the new version, hide the original.
111109
function convert(menu){
112-
var menuID = menu.attr('id'),
110+
var menuID = menu.attr('id');
113111
// Creating a refined version of the menu.
114-
refinedMenu = refine(menu);
112+
var refinedMenu = refine(menu);
115113
// Currently the plugin provides two reactions to small screens.
116114
// Converting the menu to a <select> element, and converting to an accordion version of the menu.
117115
if (options.type == 'accordion'){
118-
var
119-
toggleID = menuID + '-toggle',
120-
accordionID = menuID + '-accordion';
116+
var toggleID = menuID + '-toggle';
117+
var accordionID = menuID + '-accordion';
121118
// Making sure the accordion does not exist.
122119
if ($('#' + accordionID).length == 0){
123-
var
124120
// Getting the style class.
125-
styleClass = menu.attr('class').split(' ').filter(function(item){
121+
var styleClass = menu.attr('class').split(' ').filter(function(item){
126122
return item.indexOf('sf-style-') > -1 ? item : '';
127-
}),
123+
});
128124
// Creating the accordion.
129-
accordion = $(refinedMenu).attr('id', accordionID);
125+
var accordion = $(refinedMenu).attr('id', accordionID);
130126
// Removing unnecessary classes.
131127
accordion.removeClass('sf-horizontal sf-vertical sf-navbar sf-shadow sf-js-enabled');
132128
// Adding necessary classes.
@@ -155,11 +151,10 @@
155151
// Inserting the according and hiding the original menu.
156152
menu.before(toggle).before(accordion).hide();
157153

158-
var
159-
accordionElement = $('#' + accordionID),
154+
var accordionElement = $('#' + accordionID);
160155
// Deciding what should be used as accordion buttons.
161-
buttonElement = (options.accordionButton < 2) ? 'a.menuparent,span.nolink.menuparent' : 'a.sf-accordion-button',
162-
button = accordionElement.find(buttonElement);
156+
var buttonElement = (options.accordionButton < 2) ? 'a.menuparent,span.nolink.menuparent' : 'a.sf-accordion-button';
157+
var button = accordionElement.find(buttonElement);
163158

164159
// Attaching a click event to the toggle switch.
165160
$('#' + toggleID).on('click', function(e){
@@ -244,14 +239,14 @@
244239
// Class names modification.
245240
menuClone = menu.clone(), classes = (options.menuClasses) ? ((options.excludeClass_menu && menuClone.hasClass(options.excludeClass_menu)) ? menuClone.removeClass(options.excludeClass_menu).attr('class') : menuClone.attr('class')) : '',
246241
classes = (options.includeClass_menu && !menuClone.hasClass(options.includeClass_menu)) ? ((options.menuClasses) ? menuClone.addClass(options.includeClass_menu).attr('class') : options.includeClass_menu) : classes,
247-
classes = (classes) ? ' class="' + classes + '"' : '';
242+
classes = classes ? ' class="' + classes + '"' : '';
248243

249244
// Making sure the <select> element does not exist already.
250245
if ($('#' + menuID + '-select').length == 0){
251246
// Creating the <option> elements.
252-
var newMenu = toSelect(refinedMenu, 1),
247+
var newMenu = toSelect(refinedMenu, 1);
253248
// Creating the <select> element and assigning an ID and class name.
254-
selectList = $('<select ' + classes + ' id="' + menuID + '-select"/>')
249+
var selectList = $('<select ' + classes + ' id="' + menuID + '-select"/>')
255250
// Attaching the title and the items to the <select> element.
256251
.html('<option>' + options.title + '</option>' + newMenu)
257252
// Attaching an event then.
@@ -273,8 +268,7 @@
273268

274269
// Turn everything back to normal.
275270
function turnBack(menu){
276-
var
277-
id = '#' + menu.attr('id');
271+
var id = '#' + menu.attr('id');
278272
// Removing the small screen version.
279273
$(id + '-' + options.type).remove();
280274
// Removing the accordion toggle switch as well.
@@ -288,9 +282,8 @@
288282
// Return original object to support chaining.
289283
// Although this is unnecessary because of the way the module uses these plugins.
290284
for (var s = 0; s < this.length; s++){
291-
var
292-
menu = $(this).eq(s),
293-
mode = options.mode;
285+
var menu = $(this).eq(s);
286+
var mode = options.mode;
294287
// The rest is crystal clear, isn't it? :)
295288
if (menu.children('li').length == 0){
296289
// Skip empty menu which will not be visible and don't want to suddenly make it visible.

superfish.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* Dual licensed under the MIT and GPL licenses:
66
* http://www.opensource.org/licenses/mit-license.php
77
* http://www.gnu.org/licenses/gpl.html
8-
*
9-
* CHANGELOG: http://users.tpg.com.au/j_birch/plugins/superfish/changelog.txt
108
*/
119
/*
1210
* This is not the original jQuery Superfish plugin.

0 commit comments

Comments
 (0)