Skip to content

Commit

Permalink
✨ Sort the items in the session and task indicator menus
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Oct 15, 2023
1 parent 882db08 commit 5b3a076
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Add `Alchemy#isProcessRunning(pid)` method
* Complete Task system implementation
* Added task menu item to Janeway + added actions to it & the session menu
* Sort the items in the `session` and `task` indicator menus

## 1.3.16 (2023-10-05)

Expand Down
16 changes: 11 additions & 5 deletions lib/class/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var Session = Function.inherits('Alchemy.Base', function ClientSession(conduit)
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 1.1.0
* @version 1.1.0
* @version 1.3.17
*
* @type {Number}
*/
Expand All @@ -75,6 +75,10 @@ Session.enforceProperty(function request_count(amount) {

this.last_activity_date = new Date();

if (this.menu_item) {
this.menu_item.setWeight(this.last_activity_date.getTime());
}

return amount;
});

Expand Down Expand Up @@ -187,12 +191,14 @@ Session.setMethod(function createMenuItem(conduit) {

browser += ': ' + conduit.ip;

this.menu_item = alchemy.Janeway.session_menu.addItem(browser, () => {
let options = {
title : browser,
weight : this.last_activity_date.getTime(),
};

this.menu_item = alchemy.Janeway.session_menu.addItem(options, () => {
console.log('Clicked on session', this, this.id);
});

// Seems to fix render issues:
alchemy.Janeway.session_menu.reorderItems();
});

/**
Expand Down
39 changes: 32 additions & 7 deletions lib/class/task_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,33 @@ Service.setMethod(function createJanewayTaskMenu() {
return;
}

let task_menu = alchemy.Janeway.addIndicator('⌚︎');
let task_menu = alchemy.Janeway.addIndicator('⌚︎', {
sorter: function sortItems(a, b) {

let result = 0;

if (!a.task_schedule) {
// Things without schedules are titles
result = -10;
} else if (!b.task_schedule) {
// Things without schedules are titles
result = 10;
} else if (a.task_schedule.is_running) {
// If it's running, it should be at the top
result = -5;
} else if (a.task_schedule.has_ended) {
// If it has ended, it should be at the bottom
result = -3;
} else if (!a.task_schedule.next_scheduled_date) {
// If it has no scheduled date, it should be at the bottom
result = 1;
} else {
result = a.task_schedule.next_scheduled_date - b.task_schedule.next_scheduled_date;
}

return result;
}
});

if (!task_menu.addItem) {
return task_menu.remove();
Expand Down Expand Up @@ -487,6 +513,7 @@ class TaskSchedule {
next_scheduled_date = null;
timer_id = null;
is_running = false;
has_ended = false;
task_instance = null;
history_document = null;
janeway_menu_item = null;
Expand Down Expand Up @@ -656,8 +683,7 @@ class TaskSchedule {
let title = this.task_document?.title || this.task_schedules.task_constructor.type_name;
title += ' @ ' + scheduled_date.format('Y-m-d H:i:s');
this.janeway_menu_item = alchemy.task_service.janeway_task_menu.addItem(title, () => this.clickedJanewayMenuItem());
// Seems to fix render issues:
alchemy.task_service.janeway_task_menu.reorderItems();
this.janeway_menu_item.task_schedule = this;
}

// Calculate the timeout that should be set
Expand Down Expand Up @@ -775,10 +801,8 @@ class TaskSchedule {
if (alchemy.task_service.janeway_task_menu) {
janeway_title = this.task_document?.title || this.task_schedules.task_constructor.type_name;
let title = 'Currently running: ' + janeway_title;

janeway_item = alchemy.task_service.janeway_task_menu.addItem(title, () => this.clickedJanewayMenuItem());
// Seems to fix render issues:
alchemy.task_service.janeway_task_menu.reorderItems();
janeway_item.task_schedule = this;
}
} catch (err) {
alchemy.registerError(err);
Expand All @@ -798,12 +822,13 @@ class TaskSchedule {
this.next_scheduled_date = null;
this.task_instance = null;
this.is_running = false;
this.has_ended = true;

this.calculateNextScheduledDate();

if (janeway_item) {
let title = 'Finished: ' + janeway_title;
janeway_item.box.setContent(title);
janeway_item.setContent(title);
setTimeout(() => janeway_item.remove(), 5000);
}
}
Expand Down
11 changes: 9 additions & 2 deletions lib/init/alchemy.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ global.Alchemy = Function.inherits('Informer', 'Alchemy', function Alchemy() {
this.startJaneway();
}
} catch (err) {
log.warn('Failed to start Janeway:', err);
if (typeof log != 'undefined') {
log.warn('Failed to start Janeway:', err);
} else {
console.warn('Failed to start Janeway:', err);
}
}
});

Expand Down Expand Up @@ -496,7 +500,10 @@ Alchemy.setMethod(function startJaneway(options) {
return session_menu.remove();
}

session_menu.addItem('Current active browser sessions:', () => {
session_menu.addItem({
title : 'Current active browser sessions:',
weight : Infinity,
}, () => {
console.log('All sessions:', alchemy.sessions);
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"index.js"
],
"optionalDependencies": {
"janeway" : "~0.4.0",
"janeway" : "~0.4.1",
"less" : "~4.2.0",
"sass" : "~1.68.0",
"sass-embedded" : "~1.67.0",
Expand Down

0 comments on commit 5b3a076

Please sign in to comment.