-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwtt.js
206 lines (172 loc) · 4.43 KB
/
wtt.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
window.WTT = {};
(function(WTT) {
// console.log(WTT);
var lists,
_totalTasks = {
completed: 0,
all: 0
},
_savedTasks = 0,
_includeCompleted;
WTT.getTrello = function(key) {
return $.getScript("https://api.trello.com/1/client.js?key=" + key, function(script, status) {
return WTT.auth();
})
}
WTT.auth = function() {
Trello.authorize({
type: 'popup',
name: 'Wunderlist→Trello',
scope: {
read: 'true',
write: 'true'
},
expiration: '1hour',
success: function authSuccess() {
alert("Authentication successful!");
},
error: function(e) {
alert("Auth failed :( bye")
}
});
}
WTT.toggleTasks = function() {
$(".outer-task").toggle();
};
WTT.export = function() {
if (!window.Trello) {
WTT.getTrello().then(function() {
alert("try again :)");
});
return;
}
var idBoard = $("#board-id").val();
_includeCompleted = $("[name=include-completed]")[0].checked;
_savedTasks = 0;
// get "real" idBoard
Trello.get("/boards/" + idBoard).then(function(data) {
idBoard = data.id;
var newLists = [];
lists.forEach(function(l, li) {
Trello.post("/lists/", {
name: l.title,
idBoard: idBoard,
}, (function(data) {
var l = this;
newLists.push(data.id);
l.tasks.forEach((function(t, ti) {
var data = this,
desc = "",
task;
if (t.completed && !_includeCompleted) {
return;
}
if (t.completed) {
desc += "_This task has already been \"Completed\" in Wunderlist_\n\n";
}
desc += "https://www.wunderlist.com/#/tasks/" + t.id;
task = {
name: t.title,
idList: data.id,
desc: desc,
};
// max 10 requests per s
window.setTimeout(_postTask.bind(null, task), li * ti * 100);
}).bind(data));
}).bind(l), function(resp) {
alert("Trello API error: ");
});
})
})
return false;
}
function _postTask(task, count) {
var count = parseInt("0" + count);
Trello.post("/cards/", task, function success() {
_savedTasks++;
var total = _includeCompleted ? _totalTasks.all : _totalTasks.all - _totalTasks.completed;
$(".info").show();
if (_savedTasks === total) {
$(".info").html("<h1>ALL DONE</h1>");
} else {
$(".info").html('<p>Added task <span class="info__task">' + task.name + '</span></p>');
}
}, function error(resp) {
if (resp.status !== 429 || count > 3) {
// error is not rate limit exceeded, so stop
alert("Failed to add task " + JSON.stringify(task) + ", aborting");
} else {
window.setTimeout(_postTask.bind(null, task, count + 1), 1000);
}
});
}
WTT.parseJSON = function() {
_jsonToLists($("#wunderlist-json").val());
}
function sortByUncompleted(lists) {
lists.forEach(function(l) {
l.tasks.sort(function(a, b) {
if (a.completed && !b.completed) {
return 1;
} else if (!a.completed && b.completed) {
return -1;
} else {
return 0;
}
})
})
}
function _jsonToLists(json) {
var obj = JSON.parse(json);
var lists_o = {}; // lists "indexed" by id
var lists_a = []; // lists as array
_totalTasks = {
completed: 0,
all: 0
};
obj.data.lists.forEach(function(l) {
l.tasks = {};
l.index = lists_a.length;
lists_o[l.id] = l;
l_a = JSON.parse(JSON.stringify(l));
l_a.tasks = [];
lists_a.push(l_a);
})
var tasks = {};
obj.data.tasks.forEach(function(t) {
t.subtasks = [];
lists_o[t.list_id].tasks[t.id] = t;
tasks[t.id] = t;
t.title = t.completed ? "✔ " + t.title : t.title;
lists_a[lists_o[t.list_id].index].tasks.push(t);
if (t.completed) _totalTasks.completed++;
_totalTasks.all++;
});
obj.data.subtasks.forEach(function(st) {
st.title = st.completed ? "✔ " + st.title : st.title;
tasks[st.task_id].subtasks.push(st);
})
sortByUncompleted(lists_a);
lists_a.forEach(function(l) {
var li = $("<li>").text(l.title)
var ul = $("<ul>").appendTo(li);
l.tasks.forEach(function(t) {
var t_li = $("<li>")
.text(t.title)
.addClass("task outer-task" + (t.completed ? " completed" : ""));
ul.append(t_li);
if (t.subtasks.length > 0) {
var st_ul = $("<ul>").appendTo(t_li);
t.subtasks.forEach(function(st) {
$("<li>")
.text(st.title)
.addClass("task subtask" + (st.completed ? " completed" : ""))
.appendTo(st_ul);
})
}
});
$("#wunderlist-lists").append(li);
});
return lists = lists_a;
}
})(window.WTT);