Skip to content

Commit

Permalink
UI/Button/Month: 42413, use month input (#8278)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhaagen authored Oct 29, 2024
1 parent e3c9e3b commit ff0d63b
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 173 deletions.
228 changes: 111 additions & 117 deletions components/ILIAS/UI/resources/js/Button/button.js
Original file line number Diff line number Diff line change
@@ -1,130 +1,124 @@
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*/

il = il || {};
il.UI = il.UI || {};
il.UI.button = il.UI.button || {};
(function($, il) {
il.UI.button = (function($) {

/* month button */
var initMonth = function (id) {
$("#" + id).find(".inline-picker").each(function(o) {
$(this).datetimepicker({
inline: true,
sideBySide: true,
viewMode: "months",
format: "MM/YYYY",
defaultDate: $(this).parent().data("defaultDate"),
locale: $(this).parent().data("lang")
}).on("dp.change", function (ev) {
var i, d, months = [];
var d = new Date(ev.date);
var m = d.getMonth() + 1;
m = ("00" + m).substring(m.toString().length);

for (i = 1; i <= 12; i++) {
months.push(il.Language.txt("month_" + (("00" + i).substring(i.toString().length)) + "_short"));
}
(function ($, il) {
il.UI.button = (function ($) {
/* month button */
const initMonth = function (id) {
const btn = document.querySelector(`#${id} > input`);
btn.addEventListener('change', (e) => {
const value = e.srcElement.value.split('-').reverse().join('-');
const id = e.srcElement.closest('.il-btn-month').getAttribute('id');
$(`#${id}`).trigger('il.ui.button.month.changed', [id, value]);
});
};

$("#" + id + " span.il-current-month").html(months[d.getMonth()] + " " + d.getFullYear());
$("#" + id).trigger("il.ui.button.month.changed", [id, m + "-" + d.getFullYear()]);
});
});
};
/* toggle button */
const handleToggleClick = function (event, id, on_url, off_url, signals) {
const b = $(`#${id}`);
const pressed = b.attr('aria-pressed');
for (let i = 0; i < signals.length; i++) {
const s = signals[i];
if (s.event === 'click'
|| (pressed === 'true' && s.event === 'toggle_on')
|| (pressed !== 'true' && s.event === 'toggle_off')
) {
$(b).trigger(s.signal_id, {
id: s.signal_id,
event: s.event,
triggerer: b,
options: s.options,
});
}
}

/* toggle button */
var handleToggleClick = function (event, id, on_url, off_url, signals) {
var b = $("#" + id);
var pressed = b.attr("aria-pressed");
for (var i = 0; i < signals.length; i++) {
var s = signals[i];
if (s.event === "click" ||
(pressed === "true" && s.event === "toggle_on") ||
(pressed !== "true" && s.event === "toggle_off")
) {
$(b).trigger(s.signal_id, {
'id' : s.signal_id,
'event' : s.event,
'triggerer' : b,
'options' : s.options});
}
}
if (pressed === 'true' && on_url !== '') {
window.location = on_url;
}

if (pressed === "true" && on_url !== '') {
window.location = on_url;
}
if (pressed !== 'true' && off_url !== '') {
window.location = off_url;
}

if (pressed !== "true" && off_url !== '') {
window.location = off_url;
}
return false;
};

//console.log('handleToggelClick: ' + id);
return false;
};
const activateLoadingAnimation = function (id) {
const $button = $(`#${id}`);
$button.addClass('il-btn-with-loading-animation');
$button.addClass('disabled');
return $button;
};

var activateLoadingAnimation = function(id){
console.log('#'+id);
var $button = $('#'+id);
$button.addClass('il-btn-with-loading-animation');
$button.addClass('disabled');
return $button;
};
const deactivateLoadingAnimation = function (id) {
const $button = $(`#${id}`);
$button.removeClass('il-btn-with-loading-animation');
$button.removeClass('disabled');
return $button;
};

var deactivateLoadingAnimation = function(id){
var $button = $('#'+id);
$button.removeClass('il-btn-with-loading-animation');
$button.removeClass('disabled');
return $button;
};

return {
initMonth: initMonth,
handleToggleClick: handleToggleClick,
activateLoadingAnimation: activateLoadingAnimation,
deactivateLoadingAnimation: deactivateLoadingAnimation
};
})($);
})($, il);
return {
initMonth,
handleToggleClick,
activateLoadingAnimation,
deactivateLoadingAnimation,
};
}($));
}($, il));

// toggle init
document.addEventListener("DOMContentLoaded", function() {

document.querySelectorAll(".il-toggle-button:not(.unavailable)").forEach(button => {
const refreshLabels = (b, toggle = false) => {
let on = b.classList.contains("on");
if (toggle) {
on = !on;
}
if (b.querySelectorAll(".il-toggle-label-off, .il-toggle-label-on").length > 0) {
b.querySelectorAll(".il-toggle-label-off, .il-toggle-label-on").forEach(l => {
l.style.display = "none";
});
if (on) {
b.setAttribute('aria-pressed', true);
b.classList.add("on");
b.classList.remove("off");
b.querySelector(".il-toggle-label-on").style.display = "";
} else {
b.setAttribute('aria-pressed', false);
b.classList.add("off");
b.classList.remove("on");
b.querySelector(".il-toggle-label-off").style.display = "";
}
} else {
if (on) {
b.setAttribute('aria-pressed', true);
b.classList.add("on");
b.classList.remove("off");
} else {
b.setAttribute('aria-pressed', false);
b.classList.add("off");
b.classList.remove("on");
}
}
}
refreshLabels(button);
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('.il-toggle-button:not(.unavailable)').forEach((button) => {
const refreshLabels = (b, toggle = false) => {
let on = b.classList.contains('on');
if (toggle) {
on = !on;
}
if (b.querySelectorAll('.il-toggle-label-off, .il-toggle-label-on').length > 0) {
b.querySelectorAll('.il-toggle-label-off, .il-toggle-label-on').forEach((l) => {
l.style.display = 'none';
});
if (on) {
b.setAttribute('aria-pressed', true);
b.classList.add('on');
b.classList.remove('off');
b.querySelector('.il-toggle-label-on').style.display = '';
} else {
b.setAttribute('aria-pressed', false);
b.classList.add('off');
b.classList.remove('on');
b.querySelector('.il-toggle-label-off').style.display = '';
}
} else if (on) {
b.setAttribute('aria-pressed', true);
b.classList.add('on');
b.classList.remove('off');
} else {
b.setAttribute('aria-pressed', false);
b.classList.add('off');
b.classList.remove('on');
}
};
refreshLabels(button);

button.addEventListener("click", e => {
const b = e.currentTarget;
refreshLabels(b, true);
});
});
});
button.addEventListener('click', (e) => {
const b = e.currentTarget;
refreshLabels(b, true);
});
});
});
2 changes: 1 addition & 1 deletion components/ILIAS/UI/resources/js/Dropdown/dist/dropdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected function renderButton(Component\Button\Button $component, RendererInte

$tpl->setVariable("LABEL", $component->getLabel());
$symbol = $component->getSymbol();
if($symbol !== null) {
if ($symbol !== null) {
if ($component->getLabel() !== '') {
$symbol = $symbol->withLabel('');
}
Expand Down Expand Up @@ -284,32 +284,16 @@ protected function maybeRenderId(Component\JavaScriptBindable $component, Templa

protected function renderMonth(Component\Button\Month $component): string
{
$def = $component->getDefault();

for ($i = 1; $i <= 12; $i++) {
$this->toJS(array("month_" . str_pad((string) $i, 2, "0", STR_PAD_LEFT) . "_short"));
}

$tpl = $this->getTemplate("tpl.month.html", true, true);

$month = explode("-", $def);
$tpl->setVariable("DEFAULT_LABEL", $this->txt("month_" . str_pad($month[0], 2, "0", STR_PAD_LEFT) . "_short") . " " . $month[1]);
$tpl->setVariable("DEF_DATE", $month[0] . "/1/" . $month[1]);
// see https://github.com/moment/moment/tree/develop/locale
$lang_key = in_array($this->getLangKey(), array("ar", "bg", "cs", "da", "de", "el", "en", "es", "et", "fa", "fr", "hu", "it",
"ja", "ka", "lt", "nl", "pl", "pt", "ro", "ru", "sk", "sq", "sr", "tr", "uk", "vi", "zh"))
? $this->getLangKey()
: "en";
if ($lang_key == "zh") {
$lang_key = "zh-cn";
}
$tpl->setVariable("LANG", $lang_key);

$component = $component->withAdditionalOnLoadCode(fn($id) => "il.UI.button.initMonth('$id');");
$id = $this->bindJavaScript($component);

$tpl->setVariable("ID", $id);

$def = $component->getDefault();
$value = implode('-', array_reverse(explode("-", $def)));
$tpl->setVariable("DEFAULT", $value);

return $tpl->get();
}

Expand Down
16 changes: 0 additions & 16 deletions components/ILIAS/UI/src/examples/Button/Month/base.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace ILIAS\UI\examples\Button\Month;
Expand Down
10 changes: 2 additions & 8 deletions components/ILIAS/UI/src/templates/default/Button/tpl.month.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
<div id="{ID}" class="btn-group il-btn-month">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<span class="il-current-month">{DEFAULT_LABEL}</span>
<span class="caret"></span>
</button>
<div class="dropdown-menu" data-default-date="{DEF_DATE}" data-lang="{LANG}">
<div class="inline-picker"></div>
</div>
</div>
<input type="month" class="btn btn-default" value="{DEFAULT}" />
</div>
17 changes: 7 additions & 10 deletions components/ILIAS/UI/tests/Component/Button/ButtonMonthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,13 @@ public function testRender(): void
$html = $r->render($c);

$expected_html = <<<EOT
<div id="id_1" class="btn-group il-btn-month">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<span class="il-current-month">month_02_short 2017</span>
<span class="caret"></span>
</button>
<div class="dropdown-menu" data-default-date="02/1/2017" data-lang="en">
<div class="inline-picker"></div>
</div>
</div>
<div id="id_1" class="btn-group il-btn-month">
<input type="month" class="btn btn-default" value="2017-02" />
</div>
EOT;
$this->assertHTMLEquals("<div>" . $expected_html . "</div>", "<div>" . $html . "</div>");
$this->assertEquals(
$this->brutallyTrimHTML($expected_html),
$this->brutallyTrimHTML($html)
);
}
}

0 comments on commit ff0d63b

Please sign in to comment.