-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCard.js
2337 lines (2050 loc) · 72.1 KB
/
Card.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @class Card
* @memberof module:TrelloEntities
* @param data (Object} key/value pairs of
* information, must at least contain "id",
* can basically just pass in response from Trello API
* @constructor
* @classdesc The Card class represents
* a Card in Trello. Not every Notification will
* have a card object associated with it because
* all Trellinator webhooks are registered at the
* board level (so for example a notification about
* the name of a list being updated won't have a
* card object associated with it).
*
* Cards are loaded from Boards or Lists, and must
* be created in a List.
*
* @example
* new Notification(posted).card().postComment("Hello world!");
* @example
* new Trellinator().board("Some Board").card(new RegExp("Find me.*"));
* @example
* new Notification(posted).board().list("ToDo").cards().first().moveToNextList();
* @example
* Card.create(new Trellinator().board("Some board").list("ToDo"),"Do it!");
* @xample
* Card.create(new Trellinator().board("Some board").list("ToDo"),{name: "Do it!"});
* @xample
* Card.findOrCreate(new Trellinator().board("Some board").list("ToDo"),"Do it!");
* @xample
* Card.findOrCreate(new Trellinator().board("Some board").list("ToDo"),{name: "Do it!"});
*/
var Card = function(data)
{
//allow Trello style IDs
if((prov = Trellinator.provider()) && (prov.name == "WeKan"))
data['_id'] = data['_id'] || data.id;
this.data = data;
this.notification_object = null;
this.checklist_list = null;
this.labels_list = null;
this.members_list = null;
this.current_list = null;
this.containing_board = null;
/**
* Returns the card ID
* @memberof module:TrelloEntities.Card
* @example
* new Notification(posted).card().id();
*/
this.id = function()
{
return Trellinator.standardId(this.data);
}
this.setNotification = function(notif)
{
this.notification_object = notif;
return this;
}
/**
* Return a Date object representing the
* creation date of this card
* @memberof module:TrelloEntities.Card
* @example
* new Notification(posted).card().whenCreated().toLocaleString();
*/
this.whenCreated = function()
{
if((prov = Trellinator.provider()) && (prov.name == "WeKan"))
{
if(!("createdAt" in this.data))
this.load();
return new Date(this.data.createdAt);
}
else
return new Date(1000*parseInt(this.id().substring(0,8),16));
}
/**
* Number of days excluding saturday and sunday
* since this card was created
* @memberof module:TrelloEntities.Card
* @example
* new Notification(posted).card().weekDaysSinceCreated().toLocaleString();
*/
this.weekDaysSinceCreated = function()
{
var created = this.whenCreated().addDays(1);
var ret = 0;
while(created < Trellinator.now())
{
if(created.isWeekDay())
ret++;
created.addDays(1);
}
return ret;
}
/**
* Return the notification (if any) that
* originated this card
* @memberof module:TrelloEntities.Card
* @example
* new Notification(posted).card().notification().replytoMember("Hai");
*/
this.notification = function()
{
return this.notification_object;
}
/**
* Removes any item from any checklist on
* this card matching the string or RegExp
* passed in
* @memberof module:TrelloEntities.Card
* @param name {string|RegExp} the text (exact or regex match)
* of the checklist item to remove
* @example
* new Notification(posted).card().removeChecklistItemByName(new RegExp("Milk.*"));
*/
this.removeChecklistItemByName = function(name)
{
this.checklists().each(function(list)
{
list.items().each(function(item)
{
if(TrelloApi.nameTest(name,item.name()))
item.remove();
});
});
this.checklist_list = null;
return this;
}
/**
* Return true if the due date on this
* card has been marked complete, or in the case
* of the WeKan API if a due date is set and in the past
* @memberof module:TrelloEntities.Card
* @example
* var card = new Notification(posted).card();
*
* if(card.dueComplete())
* card.moveToNextList();
*/
this.dueComplete = function()
{
if((prov = Trellinator.provider()) && (prov.name == "WeKan"))
{
if(!("dueAt" in this.data))
this.load();
if(this.data.dueAt && (new Date(this.data.dueAt).getTime() < new Date().getTime()))
return true;
else
return false;
}
else
{
if(typeof this.data.dueComplete == "undefined")
this.load();
return this.data.dueComplete;
}
}
/**
* Return the Board object that this card
* is on
* @memberof module:TrelloEntities.Card
* @example
* var card = new Notification(posted).card();
* card.board().card(card.name()).each(function(loop)
* {
* if(loop.id() != card.id())
* loop.postComments("Twinsies with "+card.link());
* }
*/
this.board = function()
{
var ret = null;
if(this.containing_board)
ret = this.containing_board;
else if(this.current_list)
ret = this.current_list.board();
else if((prov = Trellinator.provider()) && (prov.name == "WeKan"))
throw new InvalidRequestException('Cannot load a card with the WeKan API in the absence of a list/board');
else
{
if(!("idBoard" in this.data) && !("board" in this.data))
this.load();
var data = (this.data.board) ? this.data.board:{id: this.data.idBoard};
ret = new Board(data);
this.containing_board = ret;
}
if(!ret)
throw new InvalidDataException("Board not found for card: "+this.id());
return ret;
}
this.setContainingBoard = function(board)
{
this.containing_board = board;
return this;
}
/**
* Return a list of comments from this card
* @memberof module:TrelloEntities.Card
* @param limit {int} (optional) limit the number of comments
* returned, default limit is 20
* @example
* new Notification(posted).card().comments().each(function(comment)
* {
* Card.create(new Trellinator().board("Some Board").list("ToDo"),comment);
* });
*/
this.comments = function(limit)
{
if((prov = Trellinator.provider()) && (prov.name == "WeKan"))
{
return new IterableCollection(
WekanApi.get(
"boards/"+this.board().id()+"/cards/"+this.id()+"/comments"
)
).transform(function(elem)
{
return new Comment(elem).setContainingCard(this);;
}.bind(this));
}
else
{
if(!limit)
limit = 20;
return new IterableCollection(TrelloApi.get("cards/"+this.data.id+"/actions?filter=copyCommentCard,commentCard&limit="+limit))
.transform(function(elem)
{
return new Comment(elem).setContainingCard(this);
}.bind(this));
}
}
/**
* Post a comment to this card
* @memberof module:TrelloEntities.Card
* @param comment_text {string} the text to post
* @example
* var card = new Notification(posted).movedCard("Done");
*
* card.members().each(function(member)
* {
* card.postComment("@"+member.name()+" this card was moved to the Done list");
* });
*/
this.postComment = function(comment_text)
{
if(comment_text.length > 16384)
comment_text = comment_text.substring(0,16381)+"...";
if((prov = Trellinator.provider()) && (prov.name == "WeKan"))
{
WekanApi.post(
"boards/"+this.board().id()+"/cards/"+this.id()+"/comments",
{
authorId: WekanApi.login().id,
comment: comment_text
}
);
}
else
TrelloApi.post("cards/"+this.data.id+"/actions/comments?text="+encodeURIComponent(comment_text));
return this;
}
/**
* Return the date/time this card was moved
* into it's current list, in the case of the WeKan API it just
* returns the last activity date of any kind
* @memberof module:TrelloEntities.Card
* @example
* new Notification(posted).card().movedToList().toLocaleString();
*/
this.movedToList = function()
{
if((prov = Trellinator.provider()) && (prov.name == "WeKan"))
{
if(!("dateLastActivity" in this.data))
this.load();
return new Date(this.data.dateLastActivity);
}
else
{
var res = TrelloApi.get("cards/"+this.data.id+"/actions?filter=updateCard:idList&limit=1");
if(!res.length)
res = TrelloApi.get("cards/"+this.data.id+"/actions?filter=createCard&limit=1");
if(res.length)
var ret = new Date(res[0].date);
else
var ret = Trellinator.now();
this.moved_to_list_cache = ret;
return this.moved_to_list_cache;
}
}
/**
* Move the card to the next list in the same board
* or throw InvalidDataException if there is no next
* list
* @memberof module:TrelloEntities.Card
* @example
* new Notification(posted).card().moveToNextList();
*/
this.moveToNextList = function()
{
this.moveToList(
this.currentList()
.board()
.lists()
.itemAfter(this.currentList().id(),
function(test,elem)
{
return test == elem.id();
}),"top");
return this;
}
/**
* Set the position of the card in its list,
* currently only implemented for the WeKan API
* @memberof module:TrelloEntities.Card
* @param list {List} a list object to move the card to
* @param position {string|int} top, bottom or a number (defaults to bottom)
*/
this.setPosition = function(position)
{
if((prov = Trellinator.provider()) && (prov.name == "WeKan"))
WekanApi.put("boards/"+this.board().id()+"/lists/"+this.currentList().id()+"/cards/"+this.id(),{sort: position});
else
throw new InvalidRequestException("Card.setPosition implemented only for WeKan API currently");
}
/**
* Move a card to a given List
* @memberof module:TrelloEntities.Card
* @param list {List} a list object to move the card to
* @param position {string|int} top, bottom or a number (defaults to bottom)
* @example
* var to_list = new Trellinator().board("Other Board").list("ToDo");
* new Notification(posted).createdCard("Doing").moveToList(to_list,"top");
*/
this.moveToList = function(list,position)
{
if(!position)
position = "bottom";
if((prov = Trellinator.provider()) && (prov.name == "WeKan"))
{
if(list.board().id() != this.board().id())
{
list = this.board().findOrCreateList(list.name());
}
WekanApi.put(
"boards/"+this.board().id()+"/lists/"+this.currentList().id()+"/cards/"+this.id(),
{listId: list.id()}
)
this.current_list.card_list = null;
this.current_list = list;
}
else
{
TrelloApi.put("cards/"+this.data.id+"?idList="+list.data.id+"&idBoard="+list.board().data.id+"&pos="+position);
if(this.current_list)
{
this.current_list.card_list = null;
this.current_list = null;
}
this.current_list = null;
this.moved_to_list_cache = null;
list.card_list = null;
this.containing_board = null;
this.data.list = null;
}
this.moved_to_list_cache = null;
list.card_list = null;
this.containing_board = null;
return this;
}
/**
* Return the List this card is currently in
* @memberof module:TrelloEntities.Card
* @example
* new Notification(posted).archivedCard().currentList().archive();
*/
this.currentList = function()
{
var ret = null;
if(!("list" in this.data) && !this.current_list)
this.load();
if(this.current_list)
ret = this.current_list;
else if(this.data.list)
ret = new List(this.data.list);
if(!ret)
throw new InvalidDataException("Card is not in a list: "+this.id());
return ret;
}
this.setCurrentList = function(list)
{
this.current_list = list;
return this;
}
/**
* Return true if all checklists on a card
* are complete
* @memberof module:TrelloEntities.Card
* @see Notification.completedAllChecklists()
* @example
* new Trellinator().board("Some Board").list("Doing").cards().each(function(card)
* {
* if(card.allChecklistsComplete())
* card.moveToNextList();
* });
* @example
* //This is not part of this class, but a common use case you
* //should be aware of instead
* new Notification(posted).completedAllChecklists().moveToNextList();
*/
this.allChecklistsComplete = function()
{
var ret = true;
this.checklists().each(function(checklist)
{
checklist.items().each(function(item)
{
ret = item.isComplete();
}.bind(this));
}.bind(this));
return ret;
}
/**
* Return an IterableCollection of all cards linked as attachments
* @memberof module:TrelloEntities.Card
* @example
* new Notification(posted).card().cardsLinkedInAttachments().first().postComment("Hello from over here");
*/
this.cardsLinkedInAttachments = function()
{
return this.attachments(TrelloApi.cardLinkRegExp()).find(function(elem)
{
try
{
return new Card({link: elem.link()});
}
catch(e)
{
if(
(e.toString().indexOf("card not found") === 0) ||
(e.toString().indexOf("unauthorized card permission requested") === 0)
)
{
return false;
}
else
{
throw e;
}
}
});
}
/**
* Return an IterableCollection of all boards linked as attachments
* @memberof module:TrelloEntities.Card
* @example
* new Notification(posted).card().boardsLinkedInAttachments().first().list("ToDo").cards().each(function(card)
* {
* card.postComment("Ger er done!");
* });
*/
this.boardsLinkedInAttachments = function()
{
return this.attachments(TrelloApi.boardLinkRegExp()).find(function(elem)
{
return new Board({link: elem.link()});
});
}
/**
* Fetch the first attachment on the card. Name filtering
* not implemented yet
* @memberof module:TrelloEntities.Card
* @param name {string|RegExp} not yet implemented
* @example
* Trellinator.log(new Notification(posted).card().attachment());
*/
this.attachment = function(name)
{
return this.attachments(name).first();
}
/**
* Get all attachments on the card, filtering
* not implemented
* @memberof module:TrelloEntities.Card
* @param name {string|RegExp} name or RegExp, not
* yet implemented
* @example
* new Notification(posted).card().attachments().each(function(att)
* {
* Trellinator.log(att);
* });
*/
this.attachments = function(name)
{
if((prov = Trellinator.provider()) && (prov.name == "WeKan"))
{
if(!this.data.attachments)
{
try
{
var atts = this.checklist('Attachments');
this.data.attachments = atts.items().find(function(att)
{
if(parts = Trellinator.regex(/\[(.+?)\]\((.+?)\)/).exec(att.name()))
{
var text = parts[1];
var url = parts[2];
}
else
{
var text = att.name();
var url = att.name();
}
var totest = new Attachment(
{
id: att.id(),
text: text,
url: url
}
);
var ret = false;
if(
(name && TrelloApi.nameTest(name,totest.text())) ||
(name && TrelloApi.nameTest(name,totest.link())) ||
!name
)
ret = totest;
if(ret)
ret.setContainingCard(this);
return ret;
});
}
catch(e)
{
Notification.expectException(InvalidDataException,e);
this.data.attachments = new IterableCollection();
}
}
return this.data.attachments;
}
else
{
if(!("attachments" in this.data))
this.load();
return new IterableCollection(this.data.attachments).find(function(elem)
{
var totest = new Attachment(elem);
var ret = false;
if(name && TrelloApi.nameTest(name,totest.text()))
ret = new Attachment(elem);
else if(name && TrelloApi.nameTest(name,totest.link()))
ret = new Attachment(elem);
else if(!name)
ret = totest;
if(ret)
ret.setContainingCard(this);
return ret;
}.bind(this));
}
}
/**
* Return a link to this card
* @memberof module:TrelloEntities.Card
* @example
* var notif = new Notification(posted);
* notif.card().cardsLinkedInAttachments().first().checkItemByName(notif.card().link());
*/
this.link = function()
{
if((prov = Trellinator.provider()) && (prov.name == "WeKan"))
return this.board().link()+"/"+this.id();
else
return "https://trello.com/c/"+this.shortId();
}
/**
* Return the short ID of this card
* @memberof module:TrelloEntities.Card
* @example
* var notif = new Notification(posted);
* notif.card().shortId();
*/
this.shortId = function()
{
if((prov = Trellinator.provider()) && (prov.name == "WeKan"))
return this.id();
else
{
if(!("shortLink" in this.data))
this.load();
return this.data.shortLink;
}
}
/**
* Return a link to this card formatted so
* that it can be used in a checklist item name
* such that the link will work on both mobile and
* web/desktop apps
* @memberof module:TrelloEntities.Card
* @example
* var notif = new Notification(posted);
* notif.card().mobileFriendlyLink();
*/
this.mobileFriendlyLink = function()
{
return "["+this.name().replaceAll("@","AT").replaceAll(/[<>"]/,"")+"]("+this.link()+")";
}
/**
* Add a link attachment to this card
* @memberof module:TrelloEntities.Card
* @param data {string|Object} either a string that is a fully
* formed URL, or an object that contains at least either
* an attribute link or url, and optionally one of these as
* well as a name attribute
* @example
* var notif = new Notification(posted);
*
* var notif.card().addChecklist("Linked Cards",function(list)
* {
* notif.board().list("ToDo").cards().each(function(card)
* {
* list.addItem(card.link());
* card.attachLink(notif.card().link());
* });
* });
* @example
* new Notification(posted).card().attachLink({name: "A Popular Search Engine",url: "https://www.google.com/"});
* @example
* new Notification(posted).card().attachLink({name: "A Popular Search Engine",link: "https://www.google.com/"});
*/
this.attachLink = function(data)
{
if(data.url)
var link = data.url;
else if(typeof data.link == "string")
var link = data.link;
else
var link = data;
if((prov = Trellinator.provider()) && (prov.name == "WeKan"))
{
this.addChecklist("Attachments",function(cl)
{
if(WekanApi.cardLinkRegExp().test(link))
cl.addItem(new Card({link: link}).mobileFriendlyLink());
else
{
var name = data.name || link;
cl.addItem('['+name+']('+link+')');
}
}.bind(this));
}
else
{
var url = "cards/"+this.data.id+"/attachments?url="+encodeURIComponent(link);
if(data.name)
{
var maxlength = 256;
var ltrimmed_name = data.name.substr(data.name.length-maxlength);
url += "&name="+encodeURIComponent(ltrimmed_name);
}
TrelloApi.post(url);
}
if(this.data.attachments)
this.data.attachments = null;
return this;
}
/**
* Download a file from a URL to Google Drive, then add it
* as a link to the card. I can't see a good way to add a file
* directly by URL either from the internet or from Google Drive
* so this should be updated at some point.
* @memberof module:TrelloEntities.Card
* @param data {string|Object} either a string that is a fully
* formed URL, or an object that contains at least either
* an attribute link or url, and optionally one of these as
* well as a name attribute
* @example
* var notif = new Notification(posted);
* card.attachFile(notif.card().attachments().first().link());
*/
this.attachFile = function(data)
{
if(data.url)
var link = data.url;
else if(typeof data.link == "string")
var link = data.link;
else
var link = data;
var file = Trellinator.downloadFileToGoogleDrive(link);
this.attachLink({name: file.getName(),url: file.getUrl()});
}
/**
* Set the name of this card
* @memberof module:TrelloEntities.Card
* @param name {string} the new name for the card
* @example
* new Notification(posted).card().setName("UPDATED");
*/
this.setName = function(name)
{
if((prov = Trellinator.provider()) && (prov.name == "WeKan"))
WekanApi.put("boards/"+this.board().id()+"/lists/"+this.currentList().id()+"/cards/"+this.id(),{title: name});
else
TrelloApi.put("cards/"+this.data.id+"?name="+encodeURIComponent(name));
this.data.name = name;
this.data.text = name;
return this;
}
/**
* Return the name of this card (also sometimes
* called the title of the card)
* @memberof module:TrelloEntities.Card
* @example
* Trellinator.log(new Notification(posted).card().name());
*/
this.name = function()
{
if(
(typeof this.data.name === 'undefined') &&
(typeof this.data.text === 'undefined') &&
(typeof this.data.title === 'undefined')
)
this.load();
return this.data.text || this.data.name || this.data.title || "";
}
/**
* Add a member to this card
* @memberof module:TrelloEntities.Card
* @param member {Member} a Member object to add to the
* card
* @example
* var notif = new Notification();
* var created = notif.createdCard();
*
* notif.board().members().each(function(member)
* {
* notif.card().addMember(member);
* });
*/
this.addMember = function(member)
{
try
{
if((prov = Trellinator.provider()) && (prov.name == "WeKan"))
{
var unique_mems = {};
this.members().each(function(loop)
{
unique_mems[loop.id()] = 1;
});
unique_mems[member.id()] = 1;
WekanApi.put(
"boards/"+this.board().id()+"/lists/"+this.currentList().id()+"/cards/"+this.id(),
{
members: Object.keys(unique_mems)
}
);
}
else
TrelloApi.post("cards/"+this.data.id+"/idMembers?value="+member.data.id);
this.members_list = null;
}
catch(e)
{
Notification.expectException(InvalidRequestException,e);
if(e.toString().indexOf("member is already on the card") == -1)
throw e;
}
return this;
}
/**
* Return a Member of this card matching
* the given name/regex
* @memberof module:TrelloEntities.Card
* @param name {string|RegExp} a name or RegExp to match
* @example
* new Notification(posted).card().member("iaindooley");
*/
this.member = function(name)
{
return this.members(name).first();
}
/**
* Return an IterableCollection of Members on this card
* optionally filtered by name using a string or RegExp
* @memberof module:TrelloEntities.Card
* @param name {string|RegExp} a name or RegExp to filter by
* @example
* var card = new Notification(posted).archivedCard();
*
* card.members().each(function(member)
* {
* card.postComment("@"+member.name()+" this card was archived");
* });
*/
this.members = function(name)
{
if(!this.members_list)
{
if(!("members" in this.data))
this.load();
this.members_list = new IterableCollection(this.data.members).find(function(elem)
{
if((prov = Trellinator.provider()) && (prov.name == "WeKan") && elem)
elem = {id: elem};
if(elem)
return new Member(elem);
else
return false;
});
}
return this.members_list.findByName(name);
}
/**
* Return a Card if there is one linked in the
* description of this card
* @memberof module:TrelloEntities.Card
* @example
* new Notification(posted).archivedCard().cardLinkedInDescription().postComment("Your pal was archived");
*/
this.cardLinkedInDescription = function()
{
if(parts = TrelloApi.cardLinkRegExp().exec(this.description()))
{
if((prov = Trellinator.provider()) && (prov.name == "WeKan"))
var ret = new Card({link: parts[0]});
else
var ret = new Card({id: parts[1]});
}
else
throw new InvalidDataException("No card linked in description");
return ret;
}
/**
* Return the description of this card
* @memberof module:TrelloEntities.Card
* @example
* var card = new Notification(posted).changedCardName();
*
* card.setDescription(Trellinator.now().toLocaleString()+" updated to "+card.name()+"\n\n"+card.description());
*/
this.description = function()
{
if(
!("desc" in this.data) &&
!("description" in this.data)
)
this.load();
return this.data.desc || this.data.description || "";
}
/**
* Return a Label if it is on this card, or false
* if the label is not on the card
* @memberof module:TrelloEntities.Card
* @param name {string|RegExp} a string or RegExp to match
* the label name against
* @example
* //check if a due date was marked complete on a card with label starting with "Process"
* var added = new Notification(posted).addedLabel("Old");
*
* if(added.card().hasLabel("New"))
* added.card().postComment("Something old and something new");
*/
this.hasLabel = function(name)
{
try
{
return this.labels(name).first();
}
catch(e)
{
Notification.expectException(InvalidDataException,e);
return false;
}
}
/**
* Return a Label if it is on this card, or throw
* InvalidDataException if it isn't on the card
* @memberof module:TrelloEntities.Card
* @param name {string|RegExp} a string or RegExp to match