forked from danlec/Trello-Bookmarklet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trello_bookmarklet.js
executable file
·293 lines (252 loc) · 7.92 KB
/
trello_bookmarklet.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
(function(window){
var $;
/* This is run after we've connected to Trello and selected a list */
var run = function(Trello, idList) {
var name;
// Default description is the URL of the page we're looking at
var desc = location.href;
if(window.goBug) {
// We're looking at a FogBugz case
name = goBug.ixBug + ": " + goBug.sTitle
} else if ($("#issue_header_summary").length){
// We're looking at a JIRA case in an older JIRA installation
name = $("#key-val").text() + ": " + $("#issue_header_summary").text();
} else if ($("#jira").length){
// We're looking at a 5.1+ JIRA case
name = $("#key-val").text() + ": " + $("#summary-val").text();
} else if ($("#show_issue").length) {
// We're looking at a GitHub issue
name = $("#show_issue .number strong").text() + " " + $("#show_issue .discussion-topic-title").text();
} else if ($("#all_commit_comments").length) {
// We're looking at a GitHub commit
name = $(".js-current-repository").text().trim() + ": " + $(".commit .commit-title").text().trim();
} else if (jQuery('head meta[content=Redmine]').length) {
// We're looking at a redmine issue
name = $("#content h2:first").text().trim() + ": " + $("#content h3:first").text().trim();
} else if ($('#header h1').length) {
// We're looking at a RequestTracker (RT) ticket
name = $('#header h1').text().trim();
} else if ($('h1 .hP').length){
// we're looking at an email in Gmail
name = $('h1 .hP').text().trim();
}
else {
// use page title as card title, taking trello as a "read-later" tool
name = $.trim(document.title);
}
// Get any selected text
var selection;
if(window.getSelection) {
selection = ""+window.getSelection();
} else if(document.selection && document.selection.createRange) {
selection = document.selection.createRange().text;
}
// If they've selected text, add it to the name/desc of the card
if(selection) {
if(!name) {
name = selection;
} else {
desc += "\n\n" + selection;
}
}
name = name || 'Unknown page';
// Create the card
if(name) {
Trello.post("lists/" + idList + "/cards", {
name: name,
desc: desc
}, function(card){
// Display a little notification in the upper-left corner with a link to the card
// that was just created
var $cardLink = $("<a>")
.attr({
href: card.url,
target: "card"
})
.text("Created a Trello Card")
.css({
position: "absolute",
left: 0,
top: 0,
padding: "4px",
border: "1px solid #000",
background: "#fff",
"z-index": 1e3
})
.appendTo("body")
setTimeout(function(){
$cardLink.fadeOut(3000);
}, 5000)
})
}
}
var storage = window.localStorage;
if(!storage) {
return;
}
// Store/retrieve a value from local storage
var store = function(key, value){
if(arguments.length == 2){
return (storage[key] = value);
} else {
return storage[key];
}
};
// A fake "prompt" to get info from the user
var overlayPrompt = function(html, hasInput, callback){
var done = function(value){
$div.remove();
$overlay.remove();
callback(value);
};
// Cover the existing webpage with an overlay
var $overlay = $("<div>")
.css({
background: "#000",
opacity: .75,
"z-index": 1e4,
position: "absolute",
left: 0,
top: 0,
right: 0,
bottom: 0
})
.appendTo("body")
.click(function(){
done(null);
})
// Show a "popup"
var $div = $("<div>")
.css({
position: "absolute",
border: "1px solid #000",
padding: "16px",
width: 300,
top: 64,
left: ($(window).width() - 200) / 2,
background: "#fff",
"z-index": 1e5
})
.appendTo("body");
// Show the prompt
$("<div>").html(html).appendTo($div);
// Optionally show an input
var $input = $("<input>")
.css({
width: "100%",
"margin-top": "8px"
})
.appendTo($div)
.toggle(hasInput);
// Add an "OK" button
$("<div>")
.text("OK")
.css({
width: "100%",
"text-align": "center",
border: "1px solid #000",
background: "#eee",
"margin-top": "8px",
cursor: "pointer"
})
.appendTo($div)
.click(function(){
done($input.val());
});
return $div;
};
// Run several asyncronous functions in order
var waterfall = function(fxs){
var runNext = function(){
if(fxs.length){
fxs.shift().apply(null, Array.prototype.slice.call(arguments).concat([runNext]))
}
}
runNext();
}
// The ids of values we keep in localStorage
var appKeyName = "trelloAppKey";
var idListName = "trelloIdList";
waterfall([
// Load jQuery
function(next) {
if(window.jQuery) {
next(null);
} else {
var script = document.createElement("script");
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
script.onload = next;
document.getElementsByTagName("head")[0].appendChild(script);
}
},
// Get the user's App Key, either from local storage, or by prompting them to retrieve it
function(ev, next) {
$ = window.jQuery;
var appKey = store(appKeyName) || window[appKeyName];
if(appKey && appKey.length == 32) {
next(appKey);
}
else {
overlayPrompt("Please specify your Trello API Key (you'll only need to do this once per site)<br><br>You can get your API Key <a href='https://trello.com/1/appKey/generate' target='apikey'>here</a><br><br>", true, function(newAppKey){
if(newAppKey) {
next(newAppKey);
}
})
}
},
// Load the Trello script
function(appKey, next) { $.getScript("https://trello.com/1/client.js?key=" + appKey, next); },
// Authorize our application
function(a, b, c, next) {
store(appKeyName, Trello.key())
Trello.authorize({
interactive: false,
success: next,
error: function(){
overlayPrompt("You need to authorize Trello", false, function(){
Trello.authorize({
type: "popup",
expiration: "never",
scope: { read: true, write: true },
success: next
});
});
}
});
},
// Get the list to add cards to, either from local storage or by prompting the user
function(next) {
var idList = store(idListName) || window[idListName];
if(idList && idList.length == 24) {
next(idList);
} else {
Trello.get("members/me/boards", { fields: "name" }, function(boards){
$prompt = overlayPrompt('Which list should cards be sent to?<hr><div class="boards"></div>', false, function(){
idList = $prompt.find("input:checked").attr("id");
next(idList);
})
$.each(boards, function(ix, board){
$board = $("<div>").appendTo($prompt.find(".boards"))
Trello.get("boards/" + board.id + "/lists", function(lists){
$.each(lists, function(ix, list) {
var $div = $("<div>").appendTo($board);
idList = list.id;
$("<input type='radio'>").attr("id", idList).attr("name", "idList").appendTo($div);
$("<label>").text(board.name + " : " + list.name).attr("for", idList).appendTo($div);
});
})
});
});
}
},
// Store the idList for later
function(idList, next) {
if(idList) {
store(idListName, idList);
next(Trello, idList);
}
},
// Run the user portion
run
]);
})(window);