Cleanup and modernize JS

This commit is contained in:
Peter Pfeufer 2021-05-09 06:43:58 +00:00 committed by Ariel Rin
parent 630400fee4
commit bd2d19f867
13 changed files with 270 additions and 251 deletions

View File

@ -44,4 +44,5 @@
{% block extra_script %}
$(document).ready(function () {
$("[rel=tooltip]").tooltip();
});
{% endblock extra_script %}

View File

@ -46,4 +46,5 @@
{% block extra_script %}
$(document).ready(function () {
$("[rel=tooltip]").tooltip();
});
{% endblock extra_script %}

View File

@ -83,7 +83,7 @@
{% block extra_script %}
$.fn.dataTable.moment = function(format, locale) {
var types = $.fn.dataTable.ext.type;
let types = $.fn.dataTable.ext.type;
// Add type detection
types.detect.unshift(function(d) {

View File

@ -34,7 +34,6 @@
{% endblock %}
{% block extra_script %}
$('#id_start').datetimepicker({
setlocale: '{{ LANGUAGE_CODE }}',
{% if NIGHT_MODE %}
@ -46,5 +45,4 @@
format: 'Y-m-d H:i',
minDate: 0
});
{% endblock extra_script %}

View File

@ -41,9 +41,10 @@
{% include 'bundles/moment-js.html' with locale=True %}
<script src="{% static 'js/timers.js' %}"></script>
<script type="application/javascript">
// Data
var timers = [
let timers = [
{% for op in optimer %}
{
'id': {{ op.id }},
@ -52,67 +53,66 @@
},
{% endfor %}
];
</script>
<script type="application/javascript">
timedUpdate();
setAllLocalTimes();
// Start timed updates
setInterval(timedUpdate, 1000);
function timedUpdate() {
updateClock();
updateAllTimers();
}
function updateAllTimers () {
var l = timers.length;
for (var i=0; i < l; ++i) {
if (timers[i].expired) continue;
updateTimer(timers[i]);
}
}
/**
* Update a timer
* @param timer Timer information
* @param timer.start Date of the timer
* @param timer.id Id number of the timer
* @param timer.expired
*/
function updateTimer(timer) {
let updateTimer = function (timer) {
if (timer.start.isAfter(Date.now())) {
var duration = moment.duration(timer.start - moment(), 'milliseconds');
let duration = moment.duration(timer.start - moment(), 'milliseconds');
document.getElementById("countdown" + timer.id).innerHTML = getDurationString(duration);
} else {
timer.expired = true;
document.getElementById("countdown" + timer.id).innerHTML = "";
}
}
};
let updateAllTimers = function () {
let l = timers.length;
/**
* Set all local time fields
*/
function setAllLocalTimes() {
var l = timers.length;
for (var i=0; i < l; ++i) {
setLocalTime(timers[i]);
}
if (timers[i].expired) continue;
updateTimer(timers[i]);
}
};
/**
* Set the local time info for the timer
* @param timer Timer information
* @param timer.start Date of the timer
* @param timer.id Id number of the timer
*/
function setLocalTime(timer) {
let setLocalTime = function (timer) {
document.getElementById("localtime" + timer.id).innerHTML = timer.start.format("ddd @ LT");
}
};
function updateClock() {
document.getElementById("current-time").innerHTML = getCurrentEveTimeString();
/**
* Set all local time fields
*/
let setAllLocalTimes = function () {
let l = timers.length;
for (var i=0; i < l; ++i) {
setLocalTime(timers[i]);
}
};
let updateClock = function () {
document.getElementById("current-time").innerHTML = getCurrentEveTimeString();
};
let timedUpdate = function () {
updateClock();
updateAllTimers();
};
// Set initial values
setAllLocalTimes();
timedUpdate();
// Start timed updates
setInterval(timedUpdate, 1000);
</script>
{% endblock content %}

View File

@ -56,8 +56,9 @@
{% block extra_script %}
$(document).ready(function() {
var groupColumn = 0;
var table = $('#tab_permissions_audit').DataTable({
let groupColumn = 0;
$('#tab_permissions_audit').DataTable({
columnDefs: [
{ "visible": false, "targets": groupColumn }
],
@ -73,9 +74,9 @@
bootstrap: true
},
drawCallback: function ( settings ) {
var api = this.api();
var rows = api.rows( {page:'current'} ).nodes();
var last=null;
let api = this.api();
let rows = api.rows( {page:'current'} ).nodes();
let last = null;
api.column(groupColumn, {page:'current'} ).data().each( function ( group, i ) {
if ( last !== group ) {

View File

@ -89,8 +89,9 @@
{% block extra_script %}
$(document).ready(function() {
var groupColumn = 0;
var table = $('#tab_permissions_overview').DataTable({
let groupColumn = 0;
$('#tab_permissions_overview').DataTable({
columnDefs: [
{ "visible": false, "targets": groupColumn }
],
@ -108,9 +109,9 @@
bootstrap: true
},
drawCallback: function ( settings ) {
var api = this.api();
var rows = api.rows( {page:'current'} ).nodes();
var last=null;
let api = this.api();
let rows = api.rows( {page:'current'} ).nodes();
let last = null;
api.column(groupColumn, {page:'current'} ).data().each( function ( group, i ) {
if ( last !== group ) {

View File

@ -43,7 +43,6 @@
{% endblock %}
{% block extra_script %}
$('#id_fleet_time').datetimepicker({
setlocale: '{{ LANGUAGE_CODE }}',
{% if NIGHT_MODE %}
@ -55,5 +54,4 @@
format: 'Y-m-d H:i',
minDate: 0
});
{% endblock extra_script %}

View File

@ -211,38 +211,8 @@ ESC to cancel{% endblocktrans %}"id="blah"></i></th>
$.fn.editable.defaults.showbuttons = false;
$.fn.editable.defaults.highlight = "#AAFF80";
$('.srp').editable({
display: function(value, response) {
return false;
},
success: function(response, newValue) {
newValue = parseInt(newValue);
newvalue = newValue.toLocaleString() + " ISK";
$(this).html(newvalue.bold());
},
validate: function(value) {
if (value === null || value === '') {
return 'Empty values not allowed';
}
}
});
$('.srp').on('hidden', function(e, reason){
if(reason === 'save' || reason === 'nochange') {
var $next = $(this).closest('tr').next().find('.editable');
setTimeout(function() {
$next.editable('show');
}, 400);
}
});
});
$(document).ready(function(){
$("[rel=tooltip]").tooltip({ placement: 'top'});
});
$.fn.dataTable.moment = function(format, locale) {
var types = $.fn.dataTable.ext.type;
let types = $.fn.dataTable.ext.type;
// Add type detection
types.detect.unshift(function(d) {
@ -256,23 +226,51 @@ ESC to cancel{% endblocktrans %}"id="blah"></i></th>
return moment(d, format, locale, true).unix();
};
};
$(document).ready( function(){
$.fn.dataTable.moment('YYYY-MMM-D, HH:mm');
$('.srp').editable({
display: function(value, response) {
return false;
},
success: function(response, newValue) {
newValue = parseInt(newValue);
let newValueOutput = newValue.toLocaleString() + " ISK";
$(this).html(newValueOutput.bold());
},
validate: function(value) {
if (value === null || value === '') {
return 'Empty values not allowed';
}
}
});
$('.srp').on('hidden', function(e, reason){
if(reason === 'save' || reason === 'nochange') {
let $next = $(this).closest('tr').next().find('.editable');
setTimeout(function() {
$next.editable('show');
}, 400);
}
});
$('table.srplist').DataTable({
"order": [[ 6, "asc" ]],
"paging": false,
"columnDefs": [{
"columnDefs": [
{
"targets": [1, 8],
"orderable": false
},
{
"targets": [4, 5],
"type": "num"
}]
});
}
]
});
// tooltip
$("[rel=tooltip]").tooltip({ placement: 'top'});
});
{% endblock extra_script %}

View File

@ -1,3 +1,5 @@
/* global notificationUPdateSettings */
/*
This script refreshed the unread notification count in the top menu
on a regular basis so to keep the user apprised about newly arrived
@ -6,70 +8,67 @@
The refresh rate can be changes via the Django setting NOTIFICATIONS_REFRESH_TIME.
See documentation for details.
*/
$(function () {
var elem = document.getElementById("dataExport");
var notificationsListViewUrl = elem.getAttribute("data-notificationsListViewUrl");
var notificationsRefreshTime = elem.getAttribute("data-notificationsRefreshTime");
var userNotificationsCountViewUrl = elem.getAttribute(
"data-userNotificationsCountViewUrl"
);
'use strict';
let notificationsListViewUrl = notificationUPdateSettings.notificationsListViewUrl;
let notificationsRefreshTime = notificationUPdateSettings.notificationsRefreshTime;
let userNotificationsCountViewUrl = notificationUPdateSettings.userNotificationsCountViewUrl;
// update the notification unread count in the top menu
function update_notifications() {
let updateNotifications = function () {
$.getJSON(userNotificationsCountViewUrl, function (data, status) {
if (status == 'success') {
var innerHtml = "";
var unread_count = data.unread_count;
if (unread_count > 0) {
if (status === 'success') {
let innerHtml = '';
let unreadCount = data.unread_count;
if (unreadCount > 0) {
innerHtml = (
`Notifications <span class="badge">${unread_count}</span>`
)
`Notifications <span class="badge">${unreadCount}</span>`
);
} else {
innerHtml = '<i class="far fa-bell"></i>';
}
else {
innerHtml = '<i class="far fa-bell"></i>'
}
$("#menu_item_notifications").html(
$('#menu_item_notifications').html(
`<a href="${notificationsListViewUrl}">${innerHtml}</a>`
);
}
else {
} else {
console.error(
`Failed to load HTMl to render notifications item. Error: `
`${xhr.status}': '${xhr.statusText}`
`Failed to load HTMl to render notifications item. Error: ${xhr.status}': '${xhr.statusText}`
);
}
});
}
};
var myInterval;
let myInterval;
// activate automatic refreshing every x seconds
function activate_refreshing() {
let activateRefreshing = function () {
if (notificationsRefreshTime > 0) {
myInterval = setInterval(
update_notifications, notificationsRefreshTime * 1000
updateNotifications, notificationsRefreshTime * 1000
);
}
}
};
// deactivate automatic refreshing
function deactivate_refreshing() {
let deactivateRefreshing = function () {
if ((notificationsRefreshTime > 0) && (typeof myInterval !== 'undefined')) {
clearInterval(myInterval)
}
clearInterval(myInterval);
}
};
// refreshing only happens on active browser tab
$(document).on({
'show': function () {
activate_refreshing()
activateRefreshing();
},
'hide': function () {
deactivate_refreshing()
deactivateRefreshing();
}
});
// Initial start of refreshing on script loading
activate_refreshing()
activateRefreshing();
});

View File

@ -1,23 +1,49 @@
/* global moment */
/**
* Get a duration string like countdown.js
* e.g. "1y 2d 3h 4m 5s"
* @param duration moment.duration
*
* @param duration
* @returns {string}
*/
function getDurationString(duration) {
var out = "";
let getDurationString = function (duration) {
'use strict';
let out = '';
if (duration.years()) {
out += duration.years() + 'y ';
}
if (duration.months()) {
out += duration.months() + 'm ';
}
if (duration.days()) {
out += duration.days() + 'd ';
}
return out + duration.hours() + "h " + duration.minutes() + "m " + duration.seconds() + "s";
return out + duration.hours() + 'h ' + duration.minutes() + 'm ' + duration.seconds() + 's';
};
/**
* returns the current eve time as a formatted string
*
* condition:
* only if moment.js is loaded before,
* if not this function returns an empty string to avoid JS errors from happening.
*
* @returns {string}
*/
let getCurrentEveTimeString = function () {
'use strict';
let returnValue = '';
if (window.moment) {
returnValue = moment().utc().format('dddd LL HH:mm:ss');
}
function getCurrentEveTimeString() {
return moment().utc().format('dddd LL HH:mm:ss')
}
return returnValue;
};

View File

@ -46,16 +46,17 @@
</div>
</div>
{% endif %}
<!-- share data with JS part -->
<div
id="dataExport"
data-notificationsListViewUrl="{% url 'notifications:list' %}"
data-notificationsRefreshTime="{% notifications_refresh_time %}"
data-userNotificationsCountViewUrl="{% url 'notifications:user_notifications_count' request.user.pk %}"
>
</div>
{% include 'bundles/bootstrap-js.html' %}
{% include 'bundles/jquery-visibility-js.html' %}
<script type="application/javascript">
let notificationUPdateSettings = {
notificationsListViewUrl: "{% url 'notifications:list' %}",
notificationsRefreshTime: "{% notifications_refresh_time %}",
userNotificationsCountViewUrl: "{% url 'notifications:user_notifications_count' request.user.pk %}"
};
</script>
<script src="{% static 'js/refresh_notifications.js' %}"></script>
{% block extra_javascript %}

View File

@ -526,9 +526,7 @@
{% include 'bundles/moment-js.html' with locale=True %}
<script src="{% static 'js/timers.js' %}"></script>
<script type="application/javascript">
var locale = "{{ LANGUAGE_CODE }}";
var timers = [
let timers = [
{% for timer in timers %}
{
'id': {{ timer.id }},
@ -545,67 +543,64 @@
{% endfor %}
];
moment.locale(locale);
/**
* Update a timer
* @param timer Timer information
*/
let updateTimer = function (timer) {
if (timer.targetDate.isAfter(Date.now())) {
let duration = moment.duration(timer.targetDate - moment(), 'milliseconds');
// Set initial values
setAllLocalTimes();
timedUpdate();
document.getElementById("countdown" + timer.id).innerHTML = getDurationString(duration);
} else {
timer.expired = true;
// Start timed updates
setInterval(timedUpdate, 1000);
document.getElementById("countdown" + timer.id).innerHTML = "";
}
};
let updateAllTimers = function () {
let l = timers.length;
for (var i=0; i < l; ++i) {
if (timers[i].expired) continue;
updateTimer(timers[i]);
}
};
function timedUpdate() {
updateClock();
updateAllTimers();
}
function updateAllTimers () {
var l = timers.length;
for (var i=0; i < l; ++i) {
if (timers[i].expired) continue;
updateTimer(timers[i]);
}
}
/**
* Update a timer
* Set the local time info for the timer
* @param timer Timer information
* @param timer.targetDate Date of the timer
* @param timer.id Id number of the timer
* @param timer.expired
*/
function updateTimer(timer) {
if (timer.targetDate.isAfter(Date.now())) {
duration = moment.duration(timer.targetDate - moment(), 'milliseconds');
document.getElementById("countdown" + timer.id).innerHTML = getDurationString(duration);
} else {
timer.expired = true;
document.getElementById("countdown" + timer.id).innerHTML = "";
}
}
let setLocalTime = function (timer) {
document.getElementById("localtime" + timer.id).innerHTML = timer.targetDate.format("ddd @ LT");
};
/**
* Set all local time fields
*/
function setAllLocalTimes() {
var l = timers.length;
let setAllLocalTimes = function () {
let l = timers.length;
for (var i=0; i < l; ++i) {
setLocalTime(timers[i]);
}
}
};
/**
* Set the local time info for the timer
* @param timer Timer information
* @param timer.targetDate Date of the timer
* @param timer.id Id number of the timer
*/
function setLocalTime(timer) {
document.getElementById("localtime" + timer.id).innerHTML = timer.targetDate.format("ddd @ LT");
}
function updateClock() {
let updateClock = function () {
document.getElementById("current-time").innerHTML = getCurrentEveTimeString();
}
};
// Set initial values
setAllLocalTimes();
timedUpdate();
// Start timed updates
setInterval(timedUpdate, 1000);
</script>
{% endblock content %}