-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSortProjects.omnijs
More file actions
159 lines (138 loc) · 7.17 KB
/
SortProjects.omnijs
File metadata and controls
159 lines (138 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*{
"type": "action",
"targets": ["omnifocus"],
"author": "Josh Hughes",
"identifier": "com.josh-hughes.SortProjects",
"image": "arrow.up.arrow.down",
"version": "1.0",
"description": "Sort projects by availability, dates, and tag order",
"label": "Sort Projects",
"paletteLabel": "Sort Projects"
}*/
var _ = function() {
const today = new Date(),
sortNonSals = true;
var action = new PlugIn.Action(function() {
function sortActions(project) {
if (project instanceof Project) {
var tasks = project.task.children
if (tasks.length > 0) {
tasks.sort((firstAction, secondAction) => {
if (project.containsSingletonActions) {
// 1. By Status
let sortValue = sortByTaskStatus(firstAction, secondAction);
if (sortValue == 0) {
// 2. If Deferred, By Defer Date
if (firstAction.taskStatus == Task.Status.Blocked && firstAction.effectiveDeferDate && (today < firstAction.effectiveDeferDate)) {
let sortValue = sortByEffectiveDeferDate(firstAction, secondAction);
if (sortValue == 0) {
// 3. By Tag Order of First Tag
let sortValue = sortByTagOrder(firstAction, secondAction);
if (sortValue == 0) {
// 4. By Due Date
return sortByEffectiveDueDate(firstAction, secondAction);
}
return sortValue;
}
return sortValue;
}
// 2. If Completed, By Completion Date
if ((firstAction.taskStatus == Task.Status.Completed) || (firstAction.taskStatus == Task.Status.Dropped)) {
return sortByEffectiveCompletionOrDropDate(firstAction, secondAction);
}
// 2. If Not Blocked, Completed, or Dropped, By Tag Order of First Tag
let sortValue = sortByTagOrder(firstAction, secondAction);
if (sortValue == 0) {
// 3. By Due Date
return sortByEffectiveDueDate(firstAction, secondAction);
}
return sortValue;
}
return sortValue;
} else {
if (sortNonSals === true) {
// Move Completed/Dropped down
if ((firstAction.taskStatus == Task.Status.Completed || firstAction.taskStatus == Task.Status.Dropped) && (secondAction.taskStatus != Task.Status.Completed && secondAction.taskStatus != Task.Status.Dropped)) {
return 1;
} else if ((secondAction.taskStatus == Task.Status.Completed || secondAction.taskStatus == Task.Status.Dropped) && (firstAction.taskStatus != Task.Status.Completed && firstAction.taskStatus != Task.Status.Dropped)) {
return -1;
} else {
return sortByEffectiveCompletionOrDropDate(firstAction, secondAction);
}
}
}
});
moveTasks(tasks, project);
}
}
}
function sortByTagOrder(firstAction, secondAction) {
if (flattenedTags.indexOf(firstAction.tags[0]) < flattenedTags.indexOf(secondAction.tags[0])) { return -1; }
if (flattenedTags.indexOf(firstAction.tags[0]) > flattenedTags.indexOf(secondAction.tags[0])) { return 1; }
return 0;
}
function sortByEffectiveDeferDate(firstAction, secondAction) {
if (firstAction.effectiveDeferDate < secondAction.effectiveDeferDate) { return -1; }
if (firstAction.effectiveDeferDate > secondAction.effectiveDeferDate) { return 1; }
return 0;
}
function sortByEffectiveDueDate(firstAction, secondAction) {
if (firstAction.effectiveDueDate && !secondAction.effectiveDueDate) { return -1; }
if (!firstAction.effectiveDueDate && secondAction.effectiveDueDate) { return 1; }
if (firstAction.effectiveDueDate < secondAction.effectiveDueDate) { return -1; }
if (firstAction.effectiveDueDate > secondAction.effectiveDueDate) { return 1; }
return 0;
}
function sortByEffectiveCompletionOrDropDate(firstAction, secondAction) {
let firstDate = firstAction.effectiveDropDate,
secondDate = secondAction.effectiveDropDate;
if (firstDate === null) {
firstDate = firstAction.effectiveCompletedDate;
}
if (secondDate === null) {
secondDate = secondAction.effectiveCompletedDate;
}
if (firstDate < secondDate) { return -1; }
if (firstDate > secondDate) { return 1; }
return 0;
}
function sortByTaskStatus(firstAction, secondAction) {
firstActionStatus = getTaskStatusRank(firstAction);
secondActionStatus = getTaskStatusRank(secondAction);
if (firstActionStatus < secondActionStatus) { return -1; }
if (firstActionStatus > secondActionStatus) { return 1; }
return 0;
}
function getTaskStatusRank(action) {
switch (action.taskStatus) {
case Task.Status.Available:
case Task.Status.DueSoon:
case Task.Status.Overdue:
case Task.Status.Next:
return 1;
break;
case Task.Status.Blocked:
// Rank blocked actions with a future defer date higher
if (action.effectiveDeferDate && (today < action.effectiveDeferDate)) {
return 2;
} else {
return 3;
}
break;
case Task.Status.Completed:
case Task.Status.Dropped:
return 4;
break;
default:
return 5;
break;
}
}
library.apply(sortActions);
});
action.validate = function(){
return (projects.length > 0 || folders.length > 0);
};
return action;
}();
_;