forked from BeIntrepid/Trello-Card-Dependencies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
387 lines (297 loc) · 10.5 KB
/
app.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
var TrelloInvisDepApp = function(){
this.baseTrelloUrl = 'https://api.trello.com/1/';
this.trelloKey = '&key=30ba6112a9c864cb0ef59ee7f62478d7&token=';
this.boardShortlink = window.location.search.match(/boardShortLink\=(.+)&/)[1];
this.trelloToken = window.location.search.match(/trelloToken\=(.+)/)[1];
Trello.setToken(this.trelloToken);
this.board = 'boards/' + this.boardShortlink;
this.nodeW = 100;
this.nodeH = 100;
this.w = 800;
this.h = 600;
//Bad
cssClone = false;
this.dependent = null,
this.dependency = null;
};
TrelloInvisDepApp.prototype = function(){
var loadDataFromTrello = function()
{
var cardApiUrl = this.baseTrelloUrl + this.board +'/cards?fields=name,shortLink,idList,desc' + this.trelloKey + this.trelloToken;
var listApiUrl = this.baseTrelloUrl + this.board +'/lists?fields=name,shortLink,idList' + this.trelloKey + this.trelloToken;
return $.when($.ajax({url : cardApiUrl}),$.ajax({url : listApiUrl}));
};
var setupChildCommunication = function()
{
// Create IE + others compatible event handler
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
var promise = new $.Deferred();
// Listen to message from child window
eventer(messageEvent,function(e) {
if(e.data.type !== undefined)
{
switch(e.data.type)
{
case 'css':
{
if(cssClone === true) {break;}
var links = (e.data.links);
Enumerable.From(links)
.ForEach(function(e){
$('#customStyle').before('<link rel="stylesheet" href="'+ e +'" />');
});
cssClone = true;
}break;
case 'cards':
{
//BAD !
cardViews = $('<p></p>');
Enumerable.From(e.data.cards).ForEach(function(c){
cardViews.append($(c));
});
promise.resolve();
}break;
case 'dependencyAdded':
case 'dependencyRemoved':
{
this.updateDataFromTrello();
}break;
}
}
}.bind(this),false);
return promise;
};
var updateDataFromTrello = function()
{
this.loadDataFromTrello().done(function(cardResult,listResult){
var cards = cardResult;
var lists = listResult;
var dataset = new TrelloTransformer().buildDependencyOrientatedDataSet(cards,lists);
this.invis.updateGraph(this.settings,dataset);
}.bind(this));
}
var init = function()
{
$.when(this.loadDataFromTrello(),this.setupChildCommunication())
.done(function(results){
$('.loadingMessage').hide();
var cards = results[0];
var lists = results[1];
var dataset = new TrelloTransformer().buildDependencyOrientatedDataSet(cards,lists);
this.invis = new InVis();
var markerHtml = '<marker id="markerArrow" markerWidth="30" markerHeight="13" refx="2" refy="7" orient="auto"> <path d="M25,7 L2,13 L8,7 L2,2"></path> </marker>';
//var markerHtml = '<marker id="markerArrow" markerWidth="30" markerHeight="30" refX="15" refY="15" orient="auto"> <path d="M30,15 L0,30 L5,15 L2,0"></path> </marker>';
//var markerHtml = '<marker id="markerCircle" markerWidth="8" markerHeight="8" refx="5" refy="5"> <circle cx="5" cy="5" r="3" style="stroke: none; fill:#000000;"/></marker>';
setInterval(function(){
var endX = 30;
var ma = $('#markerArrow');
if($('#markerArrow').length < 0) {return;}
var newX = parseInt( ma[0].getAttribute('refX')) - 1;
if(newX <= -endX) {newX = endX-1;}
ma[0].setAttribute('refX',newX);
},10);
this.settings = buildSettings(markerHtml);
this.invis.create(this.settings,dataset);
$('#removeDependencyButton').click(function(){this.removeDependencyClick(this.settings,dataset);}.bind(this));
$('#addDependencyButton').click(function(){this.addDependencyClicked(this.settings,dataset);}.bind(this));
$('#cancelDependencyButton').click(function(){this.resetDependencyFlow();}.bind(this));
$('#removeAllDependencies').click(function(){this.removeAllDependencies(this.settings,dataset);}.bind(this));
}.bind(this));
};
var resetDependencyFlow = function (){
$('#dependency').text('Add dependency');
$('#cancelDependencyButton').hide();
$('#addDependencyButton').show();
$('#removeDependencyButton').show();
$('#dependency').text('');
$('#dependant').text('');
this.dependency = null,this.dependent = null;
$(this.settings.svgElement[0]).unbind('mousedown',removeDependencyMouseDown);
$(this.settings.svgElement[0]).unbind('mousedown',addDependencyMouseDown);
};
var removeAllDependencies = function(settings,dataset){
var dependent = null, dependency = null;
var data = this.invis.data;
this.invis.updateGraph(this.settings,{nodes: data.nodes, edges : []});
};
var removeDependencyMouseDown = function removeDepMouseDown(e)
{
me = e.data;
if(me.dependency == null)
{
var dependency = getCardDataFromTarget(e.target);
var dependencies = new TrelloTransformer().getDependenciesForCard(dependency.shortLink,me.invis.data.nodes);
me.dependency = dependency;
// If there's just one dependency then remove that
if(dependencies.length == 1)
{
var dependShortLink = dependencies[0].shortLink;
var dependent = Enumerable.From(me.invis.data.nodes).Single(function(d){return d.shortLink == dependShortLink});
removeDependency(me.dependency,dependent);
me.resetDependencyFlow(true);
return;
}
$('#dependant').text('Click on the dependent');
return;
}
if(dependent == null)
{
var dependent = getCardDataFromTarget(e.target);
removeDependency(me.dependency,dependent);
me.resetDependencyFlow(true);
return;
}
};
var removeDependencyClick = function(settings,dataset){
this.resetDependencyFlow();
$('#addDependencyButton').hide();
$('#removeDependencyButton').hide();
$('#cancelDependencyButton').show();
$('#dependant').text('click on the dependency to remove');
var dependent = null, dependency = null;
$(settings.svgElement[0]).mousedown(this,this.removeDependencyMouseDown);
};
var getCardDataFromTarget = function(target)
{
var cardObject = $(target).parents('foreignObject').first();
return dependency = d3.select(cardObject[0]).data()[0];
};
var addDependencyMouseDown = function dependencyMouseDown(e){
$('#removeDependencyButton').hide();
me = e.data;
if(me.dependency == null)
{
me.dependency = getCardDataFromTarget(e.target);
$('#dependency').text(dependency.name);
$('#dependant').text('click on the dependent');
}
else if(me.dependent === null)
{
me.dependent = getCardDataFromTarget(e.target);
$('#dependant').text(me.dependent.name);
createNewDependency(me.dependency,me.dependent);
me.resetDependencyFlow(true);
me.dependency = null,me.dependent = null;
}
};
var addDependencyClicked = function(settings,dataset){
this.resetDependencyFlow(true);
$('#addDependencyButton').hide();
$('#removeDependencyButton').hide();
$('#cancelDependencyButton').show();
$('#dependency').text('click on the dependency');
$(settings.svgElement[0]).mousedown(this,this.addDependencyMouseDown);
};
var createNewDependency = function(dependency,dependent)
{
window.parent.postMessage({type:'addDependency',
dependency : dependency,
dependent : dependent},'*');
}
var removeDependency = function(dependency,dependent)
{
window.parent.postMessage({type:'removeDependency',
dependency : dependency,
dependent : dependent},'*');
}
//var updateLinksBasedOn
var buildSettings = function(markerHtml){
var settings = new VisSettings();
settings.svgElement = d3.select("body").append("svg");
//.attr('viewBox','0 0 1920 1024')
//.attr('perserveAspectRatio','xMinYMid');
settings.svgElement.append('defs')
.html(markerHtml);
settings.svgHeight = $(document).height();
settings.svgWidth = $(document).width();
settings.forceSettings.linkDistance = function(d,i){
switch(d.source.nodeType)
{
case 'Card':{
return 150;
break;}
case 'List':{
return 100;
break;}
case 'Anchor':{
return 180;
break;}
}
};
var buildTemplate = function(templateName){
var template = $('#templates #'+templateName+' > div').clone();
return template;
};
var convertTemplateToHtml = function(t){
return $('<p><p/>').append(t).html();
}
settings.nodeSettings.buildNode = function(d){
if(d.nodeType == 'Card')
{
var findCard = function(name){
return cardViews.find(".list-card").filter(":has(a:contains('"+name+"'))");
}
//Find in parent
var card = findCard(d.name);
if(card.length === 0)
{
var storyPointsMatch = d.name.match(/(\(|\[).+(\)|\])(.+)/);
if(storyPointsMatch !== null)
{
var nameWithoutStoryPoints = storyPointsMatch[3];
card = findCard(nameWithoutStoryPoints);
}
else
{
storyPointsMatch = d.name.match(/(.+)\W\(\?\)/)
if(storyPointsMatch !== null)
{
nameWithoutStoryPoints = storyPointsMatch[1]
card = findCard(nameWithoutStoryPoints);
}
}
}
card.addClass(d.state);
//return convertTemplateToHtml($template);
return convertTemplateToHtml(card[0].outerHTML);
}
if(d.nodeType == 'List')
{
var template = buildTemplate('listTemplate');
template.find('.name').text(d.name);
return convertTemplateToHtml(template);
}
if(d.nodeType == 'Anchor')
{
var template = buildTemplate('anchorTemplate');
template.find('.name').text(d.name);
return convertTemplateToHtml(template);
}
};
return settings;
};
return {
init:init,
loadDataFromTrello:loadDataFromTrello,
setupChildCommunication : setupChildCommunication,
removeDependencyClick : removeDependencyClick,
removeAllDependencies : removeAllDependencies,
updateDataFromTrello : updateDataFromTrello,
addDependencyMouseDown : addDependencyMouseDown,
addDependencyClicked : addDependencyClicked,
removeDependencyMouseDown : removeDependencyMouseDown,
resetDependencyFlow : resetDependencyFlow
};
}();
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-46145442-5']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
var app = new TrelloInvisDepApp();
app.init();