Skip to content

Commit b57ecc4

Browse files
authored
DEV: Refactor and improvements
* converts some items to flexbox and removes old caret styles * code refactor The component now uses template literals for better readability. This commit also minimizes the dropdown library * adds scrollable menu feature on mobile * code formatting and placeholder fix * removes unwanted styles
1 parent 1a048e0 commit b57ecc4

File tree

4 files changed

+149
-267
lines changed

4 files changed

+149
-267
lines changed

common/common.scss

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ $divider-color: null !default;
6969

7070
.wrap {
7171
display: flex;
72-
justify-content: flex-start;
72+
align-items: center;
7373

7474
@if $Invert_position == "true" {
7575
justify-content: flex-end;
@@ -83,6 +83,9 @@ $divider-color: null !default;
8383
font-size: $font-up-1;
8484
line-height: $line-height-medium;
8585
transition: all 0.15s;
86+
display: flex;
87+
align-items: center;
88+
white-space: nowrap;
8689

8790
&:first-of-type {
8891
margin-left: -12px;
@@ -117,24 +120,16 @@ $divider-color: null !default;
117120
}
118121
}
119122

120-
.fa-caret-down {
121-
margin-right: 0;
122-
margin-left: 5px;
123+
.d-icon-caret-down {
123124
transform: rotate(-90deg);
124125
transition: transform ease 0.15s;
125126

126127
.rtl & {
127-
margin-left: 0;
128-
margin-right: 5px;
129128
transform: rotate(90deg);
130129
}
131-
132-
@if $Show_caret == "false" {
133-
display: none;
134-
}
135130
}
136131

137-
.d-dropdown-open .fa-caret-down {
132+
.d-dropdown-open .d-icon-caret-down {
138133
transform: rotate(0);
139134
}
140135
}
@@ -163,7 +158,8 @@ $divider-color: null !default;
163158
}
164159

165160
.d-dropdown-menu li > a {
166-
display: block;
161+
display: flex;
162+
align-items: center;
167163
color: $submenu-item-color;
168164
text-decoration: none;
169165
line-height: 18px;

common/header.html

Lines changed: 92 additions & 223 deletions
Original file line numberDiff line numberDiff line change
@@ -8,250 +8,119 @@
88
*
99
* @license: MIT license: http://opensource.org/licenses/MIT
1010
*/
11-
if (jQuery)
12-
(function($) {
13-
$.extend($.fn, {
14-
dDropdown: function(method, data) {
15-
switch (method) {
16-
case "show":
17-
show(null, $(this));
18-
return $(this);
19-
case "hide":
20-
hide();
21-
return $(this);
22-
case "attach":
23-
return $(this).attr("data-d-dropdown", data);
24-
case "detach":
25-
hide();
26-
return $(this).removeAttr("data-d-dropdown");
27-
case "disable":
28-
return $(this).addClass("d-dropdown-disabled");
29-
case "enable":
30-
hide();
31-
return $(this).removeClass("d-dropdown-disabled");
32-
}
33-
}
34-
});
35-
36-
function show(event, object) {
37-
var trigger = event ? $(this) : object,
38-
dDropdown = $(trigger.attr("data-d-dropdown")),
39-
isOpen = trigger.hasClass("d-dropdown-open");
40-
41-
if (event) {
42-
if ($(event.target).hasClass("d-dropdown-ignore")) return;
43-
44-
event.preventDefault();
45-
event.stopPropagation();
46-
} else {
47-
if (
48-
trigger !== object.target &&
49-
$(object.target).hasClass("d-dropdown-ignore")
50-
)
51-
return;
52-
}
53-
hide();
54-
55-
if (isOpen || trigger.hasClass("d-dropdown-disabled")) return;
56-
57-
trigger.addClass("d-dropdown-open");
58-
dDropdown.data("d-dropdown-trigger", trigger).show();
59-
60-
position();
61-
62-
dDropdown.trigger("show", {
63-
dDropdown: dDropdown,
64-
trigger: trigger
65-
});
66-
}
67-
68-
function hide(event) {
69-
var targetGroup = event
70-
? $(event.target)
71-
.parents()
72-
.addBack()
73-
: null;
74-
75-
if (targetGroup && targetGroup.is(".d-dropdown")) {
76-
if (targetGroup.is(".d-dropdown-menu")) {
77-
if (!targetGroup.is("A")) return;
78-
} else {
79-
return;
80-
}
81-
}
82-
83-
$(document)
84-
.find(".d-dropdown:visible")
85-
.each(function() {
86-
var dDropdown = $(this);
87-
dDropdown
88-
.hide()
89-
.removeData("d-dropdown-trigger")
90-
.trigger("hide", { dDropdown: dDropdown });
91-
});
92-
93-
$(document)
94-
.find(".d-dropdown-open")
95-
.removeClass("d-dropdown-open");
96-
}
97-
98-
function position() {
99-
var dDropdown = $(".d-dropdown:visible").eq(0),
100-
trigger = dDropdown.data("d-dropdown-trigger"),
101-
hOffset = trigger
102-
? parseInt(trigger.attr("data-horizontal-offset") || 0, 10)
103-
: null,
104-
vOffset = trigger
105-
? parseInt(trigger.attr("data-vertical-offset") || 0, 10)
106-
: null;
107-
108-
if (dDropdown.length === 0 || !trigger) return;
109-
110-
if (dDropdown.hasClass("d-dropdown-relative")) {
111-
dDropdown.css({
112-
left: dDropdown.hasClass("d-dropdown-anchor-right")
113-
? trigger.position().left -
114-
(dDropdown.outerWidth(true) - trigger.outerWidth(true)) -
115-
parseInt(trigger.css("margin-right"), 10) +
116-
hOffset
117-
: trigger.position().left +
118-
parseInt(trigger.css("margin-left"), 10) +
119-
hOffset,
120-
top:
121-
trigger.position().top +
122-
trigger.outerHeight(true) -
123-
parseInt(trigger.css("margin-top"), 10) +
124-
vOffset
125-
});
126-
} else {
127-
dDropdown.css({
128-
left: dDropdown.hasClass("d-dropdown-anchor-right")
129-
? trigger.offset().left -
130-
(dDropdown.outerWidth() - trigger.outerWidth()) +
131-
hOffset
132-
: trigger.offset().left + hOffset,
133-
top: trigger.offset().top + trigger.outerHeight() + vOffset
134-
});
135-
}
136-
}
137-
138-
$(document).on("click.d-dropdown", "[data-d-dropdown]", show);
139-
$(document).on("click.d-dropdown", hide);
140-
$(document).on("scroll", hide);
141-
$(window).on("resize", position);
142-
})(jQuery);
11+
jQuery&&function(n){function o(d,t){var o=d?n(this):t,r=n(o.attr("data-d-dropdown")),e=o.hasClass("d-dropdown-open");if(d){if(n(d.target).hasClass("d-dropdown-ignore"))return;d.preventDefault(),d.stopPropagation()}else if(o!==t.target&&n(t.target).hasClass("d-dropdown-ignore"))return;s(),e||o.hasClass("d-dropdown-disabled")||(o.addClass("d-dropdown-open"),r.data("d-dropdown-trigger",o).show(),a(),r.trigger("show",{dDropdown:r,trigger:o}))}function s(d){var t=d?n(d.target).parents().addBack():null;if(t&&t.is(".d-dropdown")){if(!t.is(".d-dropdown-menu"))return;if(!t.is("A"))return}n(document).find(".d-dropdown:visible").each(function(){var d=n(this);d.hide().removeData("d-dropdown-trigger").trigger("hide",{dDropdown:d})}),n(document).find(".d-dropdown-open").removeClass("d-dropdown-open")}function a(){var d=n(".d-dropdown:visible").eq(0),t=d.data("d-dropdown-trigger"),o=t?parseInt(t.attr("data-horizontal-offset")||0,10):null,r=t?parseInt(t.attr("data-vertical-offset")||0,10):null;0!==d.length&&t&&(d.hasClass("d-dropdown-relative")?d.css({left:d.hasClass("d-dropdown-anchor-right")?t.position().left-(d.outerWidth(!0)-t.outerWidth(!0))-parseInt(t.css("margin-right"),10)+o:t.position().left+parseInt(t.css("margin-left"),10)+o,top:t.position().top+t.outerHeight(!0)-parseInt(t.css("margin-top"),10)+r}):d.css({left:d.hasClass("d-dropdown-anchor-right")?t.offset().left-(d.outerWidth()-t.outerWidth())+o:t.offset().left+o,top:t.offset().top+t.outerHeight()+r}))}n.extend(n.fn,{dDropdown:function(d,t){switch(d){case"show":return o(null,n(this)),n(this);case"hide":return s(),n(this);case"attach":return n(this).attr("data-d-dropdown",t);case"detach":return s(),n(this).removeAttr("data-d-dropdown");case"disable":return n(this).addClass("d-dropdown-disabled");case"enable":return s(),n(this).removeClass("d-dropdown-disabled")}}}),n(document).on("click.d-dropdown","[data-d-dropdown]",o),n(document).on("click.d-dropdown",s),n(document).on("scroll",s),n('.top-menu .wrap').on("scroll",s),n(window).on("resize",a)}(jQuery);
14312
</script>
14413

14514

14615
<script type="text/discourse-plugin" version="0.8.23">
147-
const { iconHTML } = require("discourse-common/lib/icon-library");
148-
149-
let sec = "",
150-
seg = "",
151-
rawMain = "",
152-
mainUl = "";
15316

154-
sec = $.map(settings.Menu_items.split("|"), $.trim);
155-
seg = $.map(settings.Submenu_items.split("|"), $.trim);
156-
rtl = settings.RTL_support;
17+
function cleanUp(item) {
18+
return item.replace(/\s+/g, "-").toLowerCase();
19+
}
15720

158-
if (rtl) {
159-
rtlClass = "d-dropdown-anchor-right";
160-
} else {
161-
rtlClass = "";
21+
function exists(item) {
22+
return (
23+
item &&
24+
item.length &&
25+
item !== "null" &&
26+
item !== "none" &&
27+
item !== "None" &&
28+
item !== " " &&
29+
item !== ""
30+
);
16231
}
16332

33+
const { iconHTML } = require("discourse-common/lib/icon-library"),
34+
rtlClass = settings.RTL_support ? "d-dropdown-anchor-right" : "",
35+
caret = settings.Show_caret
36+
? iconHTML("caret-down", { class: "header-menu-caret" })
37+
: "";
38+
39+
let sec = $.map(settings.Menu_items.split("|"), $.trim),
40+
seg = $.map(settings.Submenu_items.split("|"), $.trim),
41+
rawMain = "",
42+
mainUl = "";
43+
16444
$.each(sec, function() {
165-
var sec = $.map(this.split(","), $.trim);
166-
var icon = sec[1].length && sec[1] != 'none' ? iconHTML(sec[1]) : '';
167-
rawMain +=
168-
'<a data-d-dropdown="#' +
169-
sec[0] +
170-
'" class="' +
171-
sec[3] +
172-
'" title="' +
173-
sec[2] +
174-
'">' +
175-
icon +
176-
sec[0] +
177-
iconHTML("caret-down") +
178-
'</a><div id="' +
179-
sec[0] +
180-
'" class="d-dropdown ' +
181-
rtlClass +
182-
'"><ul class="d-dropdown-menu"></ul></div>';
45+
const sec = $.map(this.split(","), $.trim),
46+
text = sec[0],
47+
trigger = cleanUp(sec[0]),
48+
title = sec[2],
49+
viewClass = sec[3],
50+
icon = exists(sec[1])
51+
? iconHTML(cleanUp(sec[1]), { class: "header-menu-icon" })
52+
: "";
53+
54+
rawMain += `<a
55+
data-d-dropdown="#${trigger}"
56+
class="${viewClass} ${trigger}"
57+
title="${title}">
58+
${icon}
59+
${text}
60+
${caret}
61+
</a>
62+
<div id="${trigger}" class="d-dropdown ${rtlClass}">
63+
<ul class="d-dropdown-menu"></ul>
64+
</div>`;
18365
});
18466

18567
$("#top-menu .wrap").html(rawMain);
18668

18769
$.each(sec, function() {
188-
var sec = $.map(this.split(","), $.trim);
189-
mainUl = $("#" + sec[0] + " ul").html();
70+
let sec = $.map(this.split(","), $.trim),
71+
mainUl = "";
19072

19173
$.each(seg, function() {
192-
var seg = $.map(this.split(","), $.trim),
193-
itemClass = seg[1].replace(/\s+/g, "-").toLowerCase();
194-
195-
if (sec[0] == seg[0]) {
196-
if (seg[1] == "divider") {
197-
mainUl += "<li class='divider'></li>";
198-
return $("#" + sec[0] + " ul").html(mainUl);
199-
}
200-
201-
if (seg[2] == null || seg[2] == " ") {
202-
seg[2] = "";
203-
}
204-
205-
var icon = seg[2].length && seg[2] != 'none' ? iconHTML(seg[2]) : '';
206-
207-
if (seg[4] == "blank") {
208-
seg[4] = "_blank";
209-
itemClass += " blank ";
210-
} else {
211-
seg[4] = "_self";
74+
const seg = $.map(this.split(","), $.trim),
75+
parentMenu = cleanUp(sec[0]),
76+
parentName = cleanUp(seg[0]),
77+
text = seg[1],
78+
itemClass =
79+
seg[4] === "blank" ? `${cleanUp(seg[1])} blank` : cleanUp(seg[1]),
80+
href = seg[3],
81+
target = seg[4] === "blank" ? "_blank" : "_self",
82+
title = exists(seg[5]) ? seg[5] : "",
83+
icon = exists(seg[2])
84+
? iconHTML(cleanUp(seg[2]), { class: "header-menu-icon" })
85+
: "";
86+
87+
if (parentName === parentMenu) {
88+
if (text === "divider") {
89+
mainUl += `<li class='divider'></li>`;
90+
return $(`#${parentMenu} ul`).html(mainUl);
21291
}
21392

214-
if (seg[5] == null || seg[5] == " ") {
215-
seg[5] = "";
216-
}
217-
218-
mainUl +=
219-
'<li class="submenu-item ' +
220-
itemClass +
221-
'"><a target="' +
222-
seg[4] +
223-
'" title="' +
224-
seg[5] +
225-
'" class="submenu-link" href="' +
226-
seg[3] +
227-
'">' +
228-
icon +
229-
seg[1] +
230-
"</li>";
231-
$("#" + sec[0] + " ul").html(mainUl);
93+
mainUl += `<li class="submenu-item ${itemClass}">
94+
<a
95+
target="${target}"
96+
title="${title}"
97+
class="submenu-link"
98+
href="${href}">
99+
${icon}
100+
${text}
101+
</a>
102+
</li>`;
103+
$(`#${parentMenu} ul`).html(mainUl);
232104
}
233105
});
234106
});
235107

236-
$(function() {
237-
$(".d-dropdown-menu li:not(.blank) a").click(function() {
238-
require("discourse/lib/url").default.routeTo(
239-
$(event.currentTarget).attr("href")
240-
);
241-
$(document)
242-
.find(".d-dropdown:visible")
243-
.each(function() {
244-
var dDropdown = $(this);
245-
dDropdown
246-
.hide()
247-
.removeData("d-dropdown-trigger")
248-
.trigger("hide", { dDropdown: dDropdown });
249-
});
250-
$(document)
251-
.find(".d-dropdown-open")
252-
.removeClass("d-dropdown-open");
253-
return false;
254-
});
108+
$(".d-dropdown-menu li:not(.blank) a").click(function() {
109+
require("discourse/lib/url").default.routeTo(
110+
$(event.currentTarget).attr("href")
111+
);
112+
$(document)
113+
.find(".d-dropdown:visible")
114+
.each(function() {
115+
var dDropdown = $(this);
116+
dDropdown
117+
.hide()
118+
.removeData("d-dropdown-trigger")
119+
.trigger("hide", { dDropdown: dDropdown });
120+
});
121+
$(document)
122+
.find(".d-dropdown-open")
123+
.removeClass("d-dropdown-open");
124+
return false;
255125
});
256-
257126
</script>

0 commit comments

Comments
 (0)