/
userTagger.js
1421 lines (1373 loc) · 60.8 KB
/
userTagger.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
modules['userTagger'] = {
moduleID: 'userTagger',
moduleName: 'User Tagger',
category: 'Users',
options: {
/*
defaultMark: {
type: 'text',
value: '_',
description: 'clickable mark for users with no tag'
},
*/
hardIgnore: {
type: 'boolean',
value: false,
description: 'When "hard ignore" is off, only post titles and comment text is hidden. When it is on, the entire post is hidden (or for comments, collapsed).'
},
colorUser: {
type: 'boolean',
value: true,
description: 'Color users based on cumulative upvotes / downvotes',
advanced: true
},
storeSourceLink: {
type: 'boolean',
value: true,
description: 'By default, store a link to the link/comment you tagged a user on',
advanced: true
},
useCommentsLinkAsSource: {
type: 'boolean',
value: true,
description: 'By default, store a link to the comments when tagging a user in a link post. Otherwise, the link (that the post refers to) will be used.',
advanced: true
},
hoverInfo: {
type: 'boolean',
value: true,
description: 'Show information on user (karma, how long they\'ve been a redditor) on hover.'
},
useQuickMessage: {
type: 'boolean',
value: true,
description: 'Open the quick message dialog when clicking on the "send message" button in hover info, instead of going straight to reddit\'s message page.',
dependsOn: 'hoverInfo'
},
hoverDelay: {
type: 'text',
value: 800,
description: 'Delay, in milliseconds, before hover tooltip loads. Default is 800.',
advanced: true,
dependsOn: 'hoverInfo'
},
fadeDelay: {
type: 'text',
value: 200,
description: 'Delay, in milliseconds, before hover tooltip fades away. Default is 200.',
advanced: true,
dependsOn: 'hoverInfo'
},
fadeSpeed: {
type: 'text',
value: 0.3,
description: 'Fade animation\'s speed. Default is 0.3, the range is 0-1. Setting the speed to 1 will disable the animation.',
advanced: true,
dependsOn: 'hoverInfo'
},
gildComments: {
type: 'boolean',
value: true,
description: 'When clicking the "give gold" button on the user hover info on a comment, give gold to the comment.',
advanced: true,
dependsOn: 'hoverInfo'
},
highlightButton: {
type: 'boolean',
value: true,
description: 'Show "highlight" button in user hover info, for distinguishing posts/comments from particular users.',
advanced: true,
dependsOn: 'hoverInfo'
},
highlightColor: {
type: 'color',
value: '#5544CC',
description: 'Color used to highlight a selected user, when "highlighted" from hover info.',
advanced: true,
dependsOn: 'highlightButton'
},
highlightColorHover: {
type: 'color',
value: '#6677AA',
description: 'Color used to highlight a selected user on hover.',
advanced: true,
dependsOn: 'highlightButton'
},
USDateFormat: {
type: 'boolean',
value: false,
description: 'Show date (redditor since...) in US format (i.e. 08-31-2010)',
advanced: true,
dependsOn: 'hoverInfo'
},
vwNumber: {
type: 'boolean',
value: true,
description: 'Show the number (i.e. [+6]) rather than [vw]',
advanced: true
},
vwTooltip: {
type: 'boolean',
value: true,
description: 'Show the vote weight tooltip on hover (i.e. "your votes for...")',
advanced: true
}
},
description: 'Adds a great deal of customization around users - tagging them, ignoring them, and more. You can manage tagged users on <a href="/r/Dashboard/#userTaggerContents">Manage User Tags</a>.',
isEnabled: function() {
return RESUtils.options.getModulePrefs(this.moduleID);
},
isMatchURL: function() {
return RESUtils.isMatchURL(this.moduleID);
},
include: [
'all'
],
beforeLoad: function() {
if ((this.isEnabled()) && (this.isMatchURL())) {
var css = '.comment .tagline { display: inline; }';
css += '#userTaggerToolTip { display: none; position: absolute; width: 334px; height: 248px; }';
css += '#userTaggerToolTip label { margin-top: 5px; clear: both; float: left; width: 110px; }';
css += '#userTaggerToolTip input[type=text], #userTaggerToolTip select { margin-top: 5px; float: left; width: 195px; border: 1px solid #c7c7c7; border-radius: 3px; margin-bottom: 6px; }';
css += '#userTaggerToolTip input[type=checkbox] { margin-top: 5px; float: left; }';
css += '#userTaggerToolTip input[type=submit] { cursor: pointer; position: absolute; right: 16px; bottom: 16px; padding: 3px 5px; font-size: 12px; color: #fff; border: 1px solid #636363; border-radius: 3px; background-color: #5cc410; } ';
css += '#userTaggerToolTip .toggleButton { margin-top: 5px; margin-bottom: 5px; }';
css += '#userTaggerClose { position: absolute; right: 7px; top: 7px; z-index: 11; }';
css += '.ignoredUserComment { color: #CACACA; padding: 3px; font-size: 10px; }';
css += '.ignoredUserPost { color: #CACACA; padding: 3px; font-size: 10px; }';
css += 'a.voteWeight { text-decoration: none; color: #369; }';
css += 'a.voteWeight:hover { text-decoration: none; }';
css += '#authorInfoToolTip { display: none; position: absolute; min-width: 450px; z-index: 10001; }';
css += '#authorInfoToolTip:before { content: ""; position: absolute; top: 10px; left: -26px; border-style: solid; border-width: 10px 29px 10px 0; border-color: transparent #c7c7c7; display: block; width: 0; z-index: 1; }';
css += '#authorInfoToolTip:after { content: ""; position: absolute; top: 10px; left: -24px; border-style: solid; border-width: 10px 29px 10px 0; border-color: transparent #f0f3fc; display: block; width: 0; z-index: 1; }';
css += '#authorInfoToolTip.right:before { content: ""; position: absolute; top: 10px; right: -26px; left: auto; border-style: solid; border-width: 10px 0 10px 29px; border-color: transparent #c7c7c7; display: block; width: 0; z-index: 1; }';
css += '#authorInfoToolTip.right:after { content: ""; position: absolute; top: 10px; right: -24px; left: auto; border-style: solid; border-width: 10px 0 10px 29px; border-color: transparent #f0f3fc; display: block; width: 0; z-index: 1; }';
css += '#authorInfoToolTip .authorFieldPair { clear: both; overflow: auto; margin-bottom: 12px; }';
css += '#authorInfoToolTip .authorLabel { float: left; width: 140px; }';
css += '#authorInfoToolTip .authorDetail { float: left; min-width: 240px; }';
css += '#authorInfoToolTip .authorDetail a { display: block; max-width: 25em; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; }';
css += '#authorInfoToolTip .blueButton { float: right; margin-left: 8px; margin-top: 12px; }';
css += '#authorInfoToolTip .redButton { float: right; margin-left: 8px; }';
css += '#benefits { width: 200px; margin-left: 0; }';
css += '#userTaggerToolTip #userTaggerVoteWeight { width: 30px; }';
css += '.RESUserTagImage { display: inline-block; width: 16px; height: 8px; background-image: url("https://s3.amazonaws.com/b.thumbs.redditmedia.com/tBwwK20XXxtpgudWx1L7bDXla-iotv-JA0jgA0Y-FVs.png"); background-repeat: no-repeat; background-position: -16px -137px; }';
css += '.userTagLink { display: inline-block; }';
css += '.hoverHelp { margin-left: 3px; cursor: pointer; color: #369; text-decoration: underline; }';
css += '.userTagLink.hasTag, #userTaggerPreview { display: inline-block; padding: 0 4px; border: 1px solid #c7c7c7; border-radius: 3px; }';
css += '#userTaggerPreview { float: left; height: 16px; margin-bottom: 10px; }';
css += '#userTaggerToolTip .toggleButton .toggleOn { background-color: #107ac4; color: #fff; }';
css += '#userTaggerToolTip .toggleButton.enabled .toggleOn { background-color: #ddd ; color: #636363; }';
css += '#userTaggerToolTip .toggleButton.enabled .toggleOff { background-color: #d02020; color: #fff; }';
css += '#userTaggerToolTip .toggleButton .toggleOff { background-color: #ddd; color: #636363; } ';
css += '#userTaggerTable th { -moz-user-select: none; -webkit-user-select: none; -o-user-select: none; user-select: none; }';
css += '#userTaggerTable .deleteIcon {color:#999; cursor:pointer; }#userTaggerTable .deleteIcon:hover {color:#333;}';
css += '.userTagged { margin-right: 5px; }';
RESUtils.addCSS(css);
}
},
go: function() {
if ((this.isEnabled()) && (this.isMatchURL())) {
this.usernameRE = /(?:u|user)\/([\w\-]+)/;
// Get user tag data...
var tags = RESStorage.getItem('RESmodules.userTagger.tags'),
casefix = RESStorage.getItem('RESmodules.userTagger.casefix');
this.tags = null;
if (typeof tags !== 'undefined') {
this.tags = safeJSON.parse(tags, 'RESmodules.userTagger.tags', true);
}
// check if we're using the old method of storing user tags... yuck!
if (this.tags === null) {
this.updateTagStorage();
}
if (!casefix) {
this.updateTagStorageCaseInsensitive();
}
// If we're on the dashboard, add a tab to it...
if (RESUtils.currentSubreddit('dashboard')) {
// add tab to dashboard
modules['dashboard'].addTab('userTaggerContents', 'My User Tags');
// populate the contents of the tab
var showDiv = $('<div class="show">Show:</div>');
var tagFilter = $('<select id="tagFilter"><option>tagged users</option><option>all users</option></select>');
$(showDiv).append(tagFilter);
$('#userTaggerContents').append(showDiv);
$('#tagFilter').change(function() {
modules['userTagger'].drawUserTagTable();
});
var tagsPerPage = parseInt(modules['dashboard'].options['tagsPerPage'].value, 10);
if (tagsPerPage) {
var controlWrapper = document.createElement('div');
controlWrapper.id = 'tagPageControls';
controlWrapper.className = 'RESGalleryControls';
controlWrapper.page = 1;
controlWrapper.pageCount = 1;
var leftButton = document.createElement('a');
leftButton.className = 'previous noKeyNav';
leftButton.addEventListener('click', function(e) {
if (controlWrapper.page === 1) {
controlWrapper.page = controlWrapper.pageCount;
} else {
controlWrapper.page -= 1;
}
modules['userTagger'].drawUserTagTable();
});
controlWrapper.appendChild(leftButton);
var posLabel = document.createElement('span');
posLabel.className = 'RESGalleryLabel';
posLabel.textContent = '1 of 2';
controlWrapper.appendChild(posLabel);
var rightButton = document.createElement('a');
rightButton.className = 'next noKeyNav';
rightButton.addEventListener('click', function(e) {
if (controlWrapper.page === controlWrapper.pageCount) {
controlWrapper.page = 1;
} else {
controlWrapper.page += 1;
}
modules['userTagger'].drawUserTagTable();
});
controlWrapper.appendChild(rightButton);
$('#userTaggerContents').append(controlWrapper);
}
var thisTable = $('<table id="userTaggerTable" />');
$(thisTable).append('<thead><tr><th sort="" class="active">Username <span class="sortAsc"></span></th><th sort="tag">Tag</th><th sort="ignore">Ignored</th><th sort="color">Color</th><th sort="votes">Vote Weight</th></tr></thead><tbody></tbody>');
$('#userTaggerContents').append(thisTable);
$('#userTaggerTable thead th').click(function(e) {
e.preventDefault();
var $this = $(this);
if ($this.hasClass('delete')) {
return false;
}
if ($this.hasClass('active')) {
$this.toggleClass('descending');
}
$this.addClass('active');
$this.siblings().removeClass('active').find('SPAN').remove();
$this.find('.sortAsc, .sortDesc').remove();
$this.append($(e.target).hasClass('descending') ?
'<span class="sortDesc" />' :
'<span class="sortAsc" />');
modules['userTagger'].drawUserTagTable($(e.target).attr('sort'), $(e.target).hasClass('descending'));
});
this.drawUserTagTable();
}
// set up an array to cache user data
this.authorInfoCache = [];
if (this.options.colorUser.value) {
this.attachVoteHandlers(document.body);
}
// add tooltip to document body...
this.userTaggerToolTip = RESUtils.createElementWithID('div', 'userTaggerToolTip', 'RESDialogSmall');
var thisHTML = '<h3>Tag User</h3><div id="userTaggerToolTipContents" class="RESDialogContents clear">';
thisHTML += '<form name="userTaggerForm" action=""><input type="hidden" id="userTaggerName" value="">';
thisHTML += '<label for="userTaggerTag">Tag</label> <input type="text" id="userTaggerTag" value="">';
thisHTML += '<div id="userTaggerClose" class="RESCloseButton">×</div>';
thisHTML += '<label for="userTaggerColor">Color</label> <select id="userTaggerColor">';
for (var color in this.bgToTextColorMap) {
var bgColor = (color === 'none') ? 'transparent' : color;
thisHTML += '<option style="background-color: ' + bgColor + '; color: ' + this.bgToTextColorMap[color] + ' !important;" value="' + color + '">' + color + '</option>';
}
thisHTML += '</select>';
thisHTML += '<label for="userTaggerPreview">Preview</label> <span id="userTaggerPreview"></span>';
thisHTML += '<label for="userTaggerIgnore">Ignore ' + modules['settingsNavigation'].makeUrlHashLink('userTagger', 'hardIgnore', ' ', 'gearIcon') + '</label>'; // <input type="checkbox" id="userTaggerIgnore" value="true">';
thisHTML += '<label for="userTaggerLink">Link<span class="hoverHelp" title="add a link for this user (shows up in hover pane)">?</span> <span class="userTaggerOpenLink">- <a href="#">Open</a></span></label> <input type="text" id="userTaggerLink" value="">';
thisHTML += '<label for="userTaggerVoteWeight" title="The sum of upvotes and downvotes you have given this redditor">Vote Weight<span class="hoverHelp" title="manually edit vote weight (sum of upvotes and downvotes) for this redditor">?</span></label> <input type="text" size="2" id="userTaggerVoteWeight" value="">';
thisHTML += '<div class="clear"></div><a href="/r/dashboard#userTaggerContents" target="_blank">See all tags</a><input type="submit" id="userTaggerSave" value="Save"></form></div>';
$(this.userTaggerToolTip).html(thisHTML);
var ignoreLabel = this.userTaggerToolTip.querySelector('label[for=userTaggerIgnore]');
RESUtils.insertAfter(ignoreLabel, RESUtils.toggleButton(null, 'userTaggerIgnore', false, 'no', 'yes'));
this.userTaggerTag = this.userTaggerToolTip.querySelector('#userTaggerTag');
this.userTaggerTag.addEventListener('keyup', modules['userTagger'].updateTagPreview, false);
this.userTaggerColor = this.userTaggerToolTip.querySelector('#userTaggerColor');
this.userTaggerColor.addEventListener('change', modules['userTagger'].updateTagPreview, false);
this.userTaggerPreview = this.userTaggerToolTip.querySelector('#userTaggerPreview');
var userTaggerLink = this.userTaggerToolTip.querySelector('#userTaggerLink');
userTaggerLink.addEventListener('keyup', function(e) {
modules['userTagger'].updateOpenLink(userTaggerLink);
}, false);
var userTaggerOpen = this.userTaggerToolTip.querySelector('.userTaggerOpenLink a');
userTaggerOpen.addEventListener('click', function(e) {
var links = userTaggerLink.value.split(/\s/);
for(var i = links.length - 1; i > 0; i--) {
BrowserStrategy.openLinkInNewTab(links[i]);
}
});
var userTaggerSave = this.userTaggerToolTip.querySelector('#userTaggerSave');
userTaggerSave.setAttribute('type', 'submit');
userTaggerSave.setAttribute('value', '✓ save tag');
userTaggerSave.addEventListener('click', function(e) {
e.preventDefault();
modules['userTagger'].saveTagForm();
}, false);
var userTaggerClose = this.userTaggerToolTip.querySelector('#userTaggerClose');
userTaggerClose.addEventListener('click', function(e) {
modules['userTagger'].closeUserTagPrompt();
}, false);
//this.userTaggerToolTip.appendChild(userTaggerSave);
this.userTaggerForm = this.userTaggerToolTip.querySelector('FORM');
this.userTaggerForm.addEventListener('submit', function(e) {
e.preventDefault();
modules['userTagger'].saveTagForm();
}, true);
document.body.appendChild(this.userTaggerToolTip);
if (this.options.hoverInfo.value) {
this.authorInfoToolTip = RESUtils.createElementWithID('div', 'authorInfoToolTip', 'RESDialogSmall');
this.authorInfoToolTipHeader = document.createElement('h3');
this.authorInfoToolTip.appendChild(this.authorInfoToolTipHeader);
this.authorInfoToolTipCloseButton = RESUtils.createElementWithID('div', 'authorInfoToolTipClose', 'RESCloseButton');
$(this.authorInfoToolTipCloseButton).text('×');
this.authorInfoToolTip.appendChild(this.authorInfoToolTipCloseButton);
this.authorInfoToolTipCloseButton.addEventListener('click', function(e) {
if (typeof modules['userTagger'].hideTimer !== 'undefined') {
clearTimeout(modules['userTagger'].hideTimer);
}
modules['userTagger'].hideAuthorInfo();
}, false);
this.authorInfoToolTipContents = RESUtils.createElementWithID('div', 'authorInfoToolTipContents', 'RESDialogContents');
this.authorInfoToolTip.appendChild(this.authorInfoToolTipContents);
this.authorInfoToolTip.addEventListener('mouseover', function(e) {
if (typeof modules['userTagger'].hideTimer !== 'undefined') {
clearTimeout(modules['userTagger'].hideTimer);
}
}, false);
this.authorInfoToolTip.addEventListener('mouseout', function(e) {
if (e.target.getAttribute('class') !== 'hoverAuthor') {
modules['userTagger'].hideTimer = setTimeout(function() {
modules['userTagger'].hideAuthorInfo();
}, modules['userTagger'].options.fadeDelay.value);
}
}, false);
document.body.appendChild(this.authorInfoToolTip);
}
document.getElementById('userTaggerTag').addEventListener('keydown', function(e) {
if (e.keyCode === 27) {
// close prompt.
modules['userTagger'].closeUserTagPrompt();
}
}, true);
//console.log('before applytags: ' + Date());
this.applyTags();
//console.log('after applytags: ' + Date());
if (RESUtils.pageType() === 'comments') {
RESUtils.watchForElement('newComments', modules['userTagger'].attachVoteHandlersToComment);
RESUtils.watchForElement('newComments', modules['userTagger'].applyTags);
} else {
RESUtils.watchForElement('siteTable', modules['userTagger'].attachVoteHandlers);
RESUtils.watchForElement('siteTable', modules['userTagger'].applyTags);
}
if (RESUtils.profileRegex.test(location.href)) {
var friendButton = document.querySelector('.titlebox .fancy-toggle-button');
if ((typeof friendButton !== 'undefined') && (friendButton !== null)) {
var firstAuthor = document.querySelector('a.author'),
thisFriendComment;
if ((typeof firstAuthor !== 'undefined') && (firstAuthor !== null)) {
thisFriendComment = firstAuthor.getAttribute('title');
thisFriendComment = (thisFriendComment !== null) ? thisFriendComment.substring(8, thisFriendComment.length - 1) : '';
} else {
thisFriendComment = '';
}
}
}
this.registerCommandLine();
}
},
registerCommandLine: function() {
modules['commandLine'].registerCommand('tag', 'tag [text] - tags author of currently selected link/comment as text',
function (command, val, match) {
var tagLink = getAuthorTagLink();
if (tagLink) {
var str = 'tag user ' + tagLink.getAttribute('username');
if (val) {
str += ' as: ' + val;
}
return str;
} else {
return 'can\'t set tag - no post/comment selected';
}
},
function (command, val, match, e) {
var tagLink = getAuthorTagLink();
if (tagLink) {
RESUtils.click(tagLink);
setTimeout(function() {
if (val !== '') {
document.getElementById('userTaggerTag').value = val;
}
}, 20);
} else {
return 'can\'t set tag - no post/comment selected';
}
}
);
function getAuthorTagLink() {
var searchArea ;
if (modules['keyboardNav'].keyboardLinks && typeof modules['keyboardNav'].activeIndex !== 'undefined') {
searchArea = modules['keyboardNav'].keyboardLinks[modules['keyboardNav'].activeIndex];
}
if (!searchArea) {
searchArea = document.body;
}
var tagLink = searchArea.querySelector('a.userTagLink');
return tagLink;
}
},
attachVoteHandlersToComment: function(comment) {
var obj = $(comment).closest('.thing')[0];
// Attach to this comment, but not its children
modules['userTagger'].attachVoteHandlers(obj, 1);
},
attachVoteHandlers: function(obj, maxPairs) {
if (!RESUtils.loggedInUser()) return;
voteButtons = obj.querySelectorAll('.midcol .arrow');
if (maxPairs) {
voteButtons = Array.prototype.slice.call(voteButtons, 0, maxPairs * 2);
}
this.voteStates = this.voteStates || [];
var pairNumBaseIndex = this.voteStates.length;
for (var i = 0, len = voteButtons.length; i < len; i++) {
// get current vote states so that when we listen, we check the delta...
// pairNum is just the index of the "pair" of vote arrows... it's i/2 with no remainder...
var pairNum = Math.floor(i / 2) + pairNumBaseIndex;
if (typeof this.voteStates[pairNum] === 'undefined') {
this.voteStates[pairNum] = 0;
}
if (voteButtons[i].classList.contains('upmod')) {
this.voteStates[pairNum] = 1;
} else if (voteButtons[i].classList.contains('downmod')) {
this.voteStates[pairNum] = -1;
}
voteButtons[i].setAttribute('pairNum', pairNum);
voteButtons[i].addEventListener('click', modules['userTagger'].handleVoteClick, true);
}
},
handleVoteClick: function(e) {
var tags = RESStorage.getItem('RESmodules.userTagger.tags');
if (typeof tags !== 'undefined') {
modules['userTagger'].tags = safeJSON.parse(tags, 'RESmodules.userTagger.tags', true);
}
if (e.target.getAttribute('onclick').indexOf('unvotable') === -1) {
var pairNum = e.target.getAttribute('pairNum');
if (pairNum) {
pairNum = parseInt(pairNum, 10);
}
var thisAuthorA = this.parentNode.nextSibling.querySelector('p.tagline a.author');
// if this is a post with a thumbnail, we need to adjust the query a bit...
if (thisAuthorA === null && this.parentNode.nextSibling.classList.contains('thumbnail')) {
thisAuthorA = this.parentNode.nextSibling.nextSibling.querySelector('p.tagline a.author');
}
if (thisAuthorA) {
var thisVWobj = this.parentNode.nextSibling.querySelector('.voteWeight');
if (!thisVWobj) {
thisVWobj = this.parentNode.parentNode.querySelector('.voteWeight');
}
// but what if no obj exists
var thisAuthor = thisAuthorA.textContent;
// Stop if you are voting on your own post/comment/etc.
if (thisAuthor === RESUtils.loggedInUser()) {
return;
}
thisAuthor = thisAuthor.toLowerCase();
var votes = 0;
if (typeof modules['userTagger'].tags[thisAuthor] !== 'undefined') {
if (typeof modules['userTagger'].tags[thisAuthor].votes !== 'undefined') {
votes = parseInt(modules['userTagger'].tags[thisAuthor].votes, 10);
}
} else {
modules['userTagger'].tags[thisAuthor] = {};
}
// there are 6 possibilities here:
// 1) no vote yet, click upmod
// 2) no vote yet, click downmod
// 3) already upmodded, undoing
// 4) already downmodded, undoing
// 5) upmodded before, switching to downmod
// 6) downmodded before, switching to upmod
var upOrDown = ((this.classList.contains('up')) || (this.classList.contains('upmod'))) ? 'up' : 'down';
// did they click the up arrow, or down arrow?
switch (upOrDown) {
case 'up':
// the class changes BEFORE the click event is triggered, so we have to look at them backwards.
// if the arrow now has class "up" instead of "upmod", then it was "upmod" before, which means
// we are undoing an upvote...
if (this.classList.contains('up')) {
// this is an undo of an upvote. subtract one from votes. We end on no vote.
votes--;
modules['userTagger'].voteStates[pairNum] = 0;
} else {
// They've upvoted... the question is, is it an upvote alone, or an an undo of a downvote?
// add one vote either way...
votes++;
// if it was previously downvoted, add another!
if (modules['userTagger'].voteStates[pairNum] === -1) {
votes++;
}
modules['userTagger'].voteStates[pairNum] = 1;
}
break;
case 'down':
// the class changes BEFORE the click event is triggered, so we have to look at them backwards.
// if the arrow now has class "up" instead of "upmod", then it was "upmod" before, which means
// we are undoing an downvote...
if (this.classList.contains('down')) {
// this is an undo of an downvote. subtract one from votes. We end on no vote.
votes++;
modules['userTagger'].voteStates[pairNum] = 0;
} else {
// They've downvoted... the question is, is it an downvote alone, or an an undo of an upvote?
// subtract one vote either way...
votes--;
// if it was previously upvoted, subtract another!
if (modules['userTagger'].voteStates[pairNum] === 1) {
votes--;
}
modules['userTagger'].voteStates[pairNum] = -1;
}
break;
}
/*
if ((hasClass(this, 'upmod')) || (hasClass(this, 'down'))) {
// upmod = upvote. down = undo of downvote.
votes = votes + 1;
} else if ((hasClass(this, 'downmod')) || (hasClass(this, 'up'))) {
// downmod = downvote. up = undo of downvote.
votes = votes - 1;
}
*/
modules['userTagger'].tags[thisAuthor].votes = votes;
RESStorage.setItem('RESmodules.userTagger.tags', JSON.stringify(modules['userTagger'].tags));
modules['userTagger'].colorUser(thisVWobj, thisAuthor, votes);
}
}
},
drawUserTagTable: function(sortMethod, descending) {
this.currentSortMethod = sortMethod || this.currentSortMethod;
this.descending = (typeof descending === 'undefined' || descending === null) ? this.descending : descending;
var taggedUsers = [];
var filterType = $('#tagFilter').val();
for (var i in this.tags) {
if (filterType === 'tagged users') {
if (typeof this.tags[i].tag !== 'undefined') {
taggedUsers.push(i);
}
} else {
taggedUsers.push(i);
}
}
switch (this.currentSortMethod) {
case 'tag':
taggedUsers.sort(function(a, b) {
var tagA = (typeof modules['userTagger'].tags[a].tag === 'undefined') ? 'zzzzz' : modules['userTagger'].tags[a].tag.toLowerCase();
var tagB = (typeof modules['userTagger'].tags[b].tag === 'undefined') ? 'zzzzz' : modules['userTagger'].tags[b].tag.toLowerCase();
return (tagA > tagB) ? 1 : (tagB > tagA) ? -1 : 0;
});
if (this.descending) {
taggedUsers.reverse();
}
break;
case 'ignore':
taggedUsers.sort(function(a, b) {
var tagA = (typeof modules['userTagger'].tags[a].ignore === 'undefined') ? 'z' : 'a';
var tagB = (typeof modules['userTagger'].tags[b].ignore === 'undefined') ? 'z' : 'a';
return (tagA > tagB) ? 1 : (tagB > tagA) ? -1 : 0;
});
if (this.descending) {
taggedUsers.reverse();
}
break;
case 'color':
taggedUsers.sort(function(a, b) {
var colorA = (typeof modules['userTagger'].tags[a].color === 'undefined') ? 'zzzzz' : modules['userTagger'].tags[a].color.toLowerCase();
var colorB = (typeof modules['userTagger'].tags[b].color === 'undefined') ? 'zzzzz' : modules['userTagger'].tags[b].color.toLowerCase();
return (colorA > colorB) ? 1 : (colorB > colorA) ? -1 : 0;
});
if (this.descending) {
taggedUsers.reverse();
}
break;
case 'votes':
taggedUsers.sort(function(a, b) {
var tagA = (typeof modules['userTagger'].tags[a].votes === 'undefined') ? 0 : modules['userTagger'].tags[a].votes;
var tagB = (typeof modules['userTagger'].tags[b].votes === 'undefined') ? 0 : modules['userTagger'].tags[b].votes;
return (tagA > tagB) ? 1 : (tagB > tagA) ? -1 : (a.toLowerCase() > b.toLowerCase());
});
if (this.descending) {
taggedUsers.reverse();
}
break;
default:
// sort users, ignoring case
taggedUsers.sort(function(a, b) {
return (a.toLowerCase() > b.toLowerCase()) ? 1 : (b.toLowerCase() > a.toLowerCase()) ? -1 : 0;
});
if (this.descending) {
taggedUsers.reverse();
}
break;
}
$('#userTaggerTable tbody').html('');
var tagsPerPage = parseInt(modules['dashboard'].options['tagsPerPage'].value, 10);
var count = taggedUsers.length;
var start = 0;
var end = count;
if (tagsPerPage) {
var tagControls = $('#tagPageControls');
var page = tagControls.prop('page');
var pages = Math.ceil(count / tagsPerPage);
page = Math.min(page, pages);
page = Math.max(page, 1);
tagControls.prop('page', page).prop('pageCount', pages);
tagControls.find('.RESGalleryLabel').text(page + ' of ' + pages);
start = tagsPerPage * (page - 1);
end = Math.min(count, tagsPerPage * page);
}
for (var i = start; i < end; i++) {
var thisUser = taggedUsers[i];
var thisTag = (typeof this.tags[thisUser].tag === 'undefined') ? '' : this.tags[thisUser].tag;
var thisVotes = (typeof this.tags[thisUser].votes === 'undefined') ? 0 : this.tags[thisUser].votes;
var thisColor = (typeof this.tags[thisUser].color === 'undefined') ? '' : this.tags[thisUser].color;
var thisIgnore = (typeof this.tags[thisUser].ignore === 'undefined') ? 'no' : 'yes';
var userTagLink = document.createElement('a');
if (thisTag === '') {
// thisTag = '<div class="RESUserTagImage"></div>';
userTagLink.setAttribute('class', 'userTagLink RESUserTagImage');
} else {
userTagLink.setAttribute('class', 'userTagLink hasTag');
}
$(userTagLink).text(thisTag);
if (thisColor) {
var bgColor = (thisColor === 'none') ? 'transparent' : thisColor;
userTagLink.setAttribute('style', 'background-color: ' + bgColor + '; color: ' + this.bgToTextColorMap[thisColor] + ' !important;');
}
userTagLink.setAttribute('username', thisUser);
userTagLink.setAttribute('title', 'set a tag');
userTagLink.setAttribute('href', '#');
userTagLink.addEventListener('click', function(e) {
e.preventDefault();
modules['userTagger'].openUserTagPrompt(e.target, this.getAttribute('username'));
}, true);
$('#userTaggerTable tbody').append('<tr><td><a class="author" href="/user/' + thisUser + '">' + thisUser + '</a> <span class="res-icon res-right deleteIcon" data-icon="" user="' + thisUser + '"></span></td><td id="tag_' + i + '"></td><td id="ignore_' + i + '">' + thisIgnore + '</td><td><span style="color: ' + thisColor + '">' + thisColor + '</span></td><td>' + thisVotes + '</td></tr>');
$('#tag_' + i).append(userTagLink);
}
$('#userTaggerTable tbody .deleteIcon').click(function(e) {
var thisUser = $(this).attr('user').toLowerCase();
var button = $(this);
alert('Are you sure you want to delete the tag for user: ' + thisUser + '?',
function(e){
delete modules['userTagger'].tags[thisUser];
RESStorage.setItem('RESmodules.userTagger.tags', JSON.stringify(modules['userTagger'].tags));
button.closest('tr').remove();
}
);
});
},
saveTagForm: function() {
var thisName = document.getElementById('userTaggerName').value;
var thisTag = document.getElementById('userTaggerTag').value;
var thisColor = document.getElementById('userTaggerColor').value;
var thisIgnore = document.getElementById('userTaggerIgnore').checked;
var thisLink = document.getElementById('userTaggerLink').value;
var thisVotes = parseInt(document.getElementById('userTaggerVoteWeight').value, 10);
if (isNaN(thisVotes)) {
thisVotes = 0;
}
modules['userTagger'].setUserTag(thisName, thisTag, thisColor, thisIgnore, thisLink, thisVotes);
},
bgToTextColorMap: {
'none': 'black',
'aqua': 'black',
'black': 'white',
'blue': 'white',
'fuchsia': 'white',
'pink': 'black',
'gray': 'white',
'green': 'white',
'lime': 'black',
'maroon': 'white',
'navy': 'white',
'olive': 'white',
'orange': 'white',
'purple': 'white',
'red': ' white',
'silver': 'black',
'teal': 'white',
'white': 'black',
'yellow': 'black'
},
openUserTagPrompt: function(obj, username) {
var thisXY = RESUtils.getXYpos(obj);
username = username.toLowerCase();
this.clickedTag = obj;
var thisH3 = document.querySelector('#userTaggerToolTip h3');
thisH3.textContent = 'Tag ' + username;
document.getElementById('userTaggerName').value = username;
if (typeof this.tags[username] !== 'undefined') {
if (typeof this.tags[username].link !== 'undefined') {
document.getElementById('userTaggerLink').value = this.tags[username].link;
} else {
document.getElementById('userTaggerLink').value = '';
}
if (typeof this.tags[username].tag !== 'undefined') {
document.getElementById('userTaggerTag').value = this.tags[username].tag;
} else {
document.getElementById('userTaggerTag').value = '';
if (typeof this.tags[username].link === 'undefined') {
// since we haven't yet set a tag or a link for this user, auto populate a link for the
// user based on where we are tagging from.
this.setLinkBasedOnTagLocation(obj);
}
}
if (typeof this.tags[username].ignore !== 'undefined') {
document.getElementById('userTaggerIgnore').checked = this.tags[username].ignore;
var thisToggle = document.getElementById('userTaggerIgnoreContainer');
if (this.tags[username].ignore) {
thisToggle.classList.add('enabled');
}
} else {
document.getElementById('userTaggerIgnore').checked = false;
}
if (typeof this.tags[username].votes !== 'undefined') {
document.getElementById('userTaggerVoteWeight').value = this.tags[username].votes;
} else {
document.getElementById('userTaggerVoteWeight').value = '';
}
if (typeof this.tags[username].color !== 'undefined') {
RESUtils.setSelectValue(document.getElementById('userTaggerColor'), this.tags[username].color);
} else {
document.getElementById('userTaggerColor').selectedIndex = 0;
}
} else {
document.getElementById('userTaggerTag').value = '';
document.getElementById('userTaggerIgnore').checked = false;
document.getElementById('userTaggerVoteWeight').value = '';
document.getElementById('userTaggerLink').value = '';
if (this.options.storeSourceLink.value) {
this.setLinkBasedOnTagLocation(obj);
}
document.getElementById('userTaggerColor').selectedIndex = 0;
}
this.userTaggerToolTip.setAttribute('style', 'display: block; top: ' + thisXY.y + 'px; left: ' + thisXY.x + 'px; z-index: 20;');
document.getElementById('userTaggerTag').focus();
modules['userTagger'].updateOpenLink(document.getElementById('userTaggerLink'));
modules['userTagger'].updateTagPreview();
return false;
},
setLinkBasedOnTagLocation: function(obj) {
var closestEntry = $(obj).closest('.entry'),
linkTitle = '';
if (!modules['userTagger'].options.useCommentsLinkAsSource.value) {
linkTitle = $(closestEntry).find('a.title');
}
// if we didn't find anything, try a new search (works on inbox)
if (!linkTitle.length) {
linkTitle = $(closestEntry).find('a.bylink');
}
if (linkTitle.length) {
document.getElementById('userTaggerLink').value = $(linkTitle).attr('href');
} else {
var permaLink = $(closestEntry).find('.flat-list.buttons li.first a');
if (permaLink.length) {
document.getElementById('userTaggerLink').value = $(permaLink).attr('href');
}
}
},
updateTagPreview: function() {
$(modules['userTagger'].userTaggerPreview).text(modules['userTagger'].userTaggerTag.value);
var bgcolor = modules['userTagger'].userTaggerColor[modules['userTagger'].userTaggerColor.selectedIndex].value;
modules['userTagger'].userTaggerPreview.style.backgroundColor = (bgcolor === 'none') ? 'transparent' : bgcolor;
modules['userTagger'].userTaggerPreview.style.color = modules['userTagger'].bgToTextColorMap[bgcolor];
},
updateOpenLink: function(userTaggerLink) {
if (userTaggerLink.value) {
var openLinkSpan = modules['userTagger'].userTaggerToolTip.querySelector('.userTaggerOpenLink');
openLinkSpan.style.display = 'inline';
openLinkSpan.getElementsByTagName('a')[0].href = userTaggerLink.value.split(/\s/)[0];
} else {
modules['userTagger'].userTaggerToolTip.querySelector('.userTaggerOpenLink').style.display = 'none';
}
},
closeUserTagPrompt: function() {
this.userTaggerToolTip.setAttribute('style', 'display: none');
if (modules['keyboardNav'].isEnabled()) {
var inputs = this.userTaggerToolTip.querySelectorAll('INPUT, BUTTON');
// remove focus from any input fields from the prompt so that keyboard navigation works again...
for (var i = 0, len = inputs.length; i < len; i++) {
inputs[i].blur();
}
}
},
setUserTag: function(username, tag, color, ignore, link, votes, noclick) {
username = username.toLowerCase();
if (((tag !== null) && (tag !== '')) || (ignore)) {
if (tag === '') {
tag = 'ignored';
}
if (typeof this.tags[username] === 'undefined') {
this.tags[username] = {};
}
this.tags[username].tag = tag;
this.tags[username].link = link;
this.tags[username].color = color;
var bgColor = (color === 'none') ? 'transparent' : color;
if (ignore) {
this.tags[username].ignore = true;
} else {
delete this.tags[username].ignore;
}
if (!noclick) {
this.clickedTag.setAttribute('class', 'userTagLink hasTag');
this.clickedTag.setAttribute('style', 'background-color: ' + bgColor + '; color: ' + this.bgToTextColorMap[color] + ' !important;');
$(this.clickedTag).text(tag);
}
} else {
if (typeof this.tags[username] !== 'undefined') {
delete this.tags[username].tag;
delete this.tags[username].color;
delete this.tags[username].link;
if (this.tags[username].tag === 'ignored') {
delete this.tags[username].tag;
}
delete this.tags[username].ignore;
}
if (!noclick) {
this.clickedTag.setAttribute('style', 'background-color: transparent');
this.clickedTag.setAttribute('class', 'userTagLink RESUserTagImage');
$(this.clickedTag).html('');
}
}
if (typeof this.tags[username] !== 'undefined') {
this.tags[username].votes = (isNaN(votes)) ? 0 : votes;
}
if (!noclick) {
var thisVW = this.clickedTag.parentNode.parentNode.querySelector('a.voteWeight');
if (thisVW) {
this.colorUser(thisVW, username, votes);
}
}
if (RESUtils.isEmpty(this.tags[username])) {
delete this.tags[username];
}
RESStorage.setItem('RESmodules.userTagger.tags', JSON.stringify(this.tags));
this.closeUserTagPrompt();
},
applyTags: function(ele) {
ele = ele || document;
var authors = ele.querySelectorAll('.noncollapsed a.author, p.tagline a.author, #friend-table span.user a, .sidecontentbox .author, div.md a[href^="/u/"]:not([href*="/m/"]), div.md a[href*="reddit.com/u/"]:not([href*="/m/"]), .usertable a.author, .usertable span.user a, div.wiki-page-content .author, .commentingAsUser');
RESUtils.forEachChunked(authors, 15, 1000, function(arrayElement, index, array) {
modules['userTagger'].applyTagToAuthor(arrayElement);
});
},
applyTagToAuthor: function(thisAuthorObj, noEdit) {
var userObject = [],
thisVotes = 0,
thisTag = null,
thisColor = null,
thisIgnore = null,
thisAuthor, thisPost, thisComment, test, noTag;
// var thisAuthorObj = this.authors[authorNum];
if ((thisAuthorObj) && (!(thisAuthorObj.classList.contains('userTagged'))) && (typeof thisAuthorObj !== 'undefined') && (thisAuthorObj !== null)) {
if (this.options.hoverInfo.value) {
// add event listener to hover, so we can grab user data on hover...
thisAuthorObj.addEventListener('mouseover', function(e) {
modules['userTagger'].showTimer = setTimeout(function() {
modules['userTagger'].showAuthorInfo(thisAuthorObj);
}, modules['userTagger'].options.hoverDelay.value);
}, false);
thisAuthorObj.addEventListener('mouseout', function(e) {
clearTimeout(modules['userTagger'].showTimer);
}, false);
thisAuthorObj.addEventListener('click', function(e) {
clearTimeout(modules['userTagger'].showTimer);
}, false);
}
if (thisAuthorObj.href) {
test = thisAuthorObj.href.match(this.usernameRE);
if (test) {
thisAuthor = test[1];
}
} else if (thisAuthorObj.getAttribute('data-user')) {
thisAuthor = thisAuthorObj.getAttribute('data-user');
}
// var thisAuthor = thisAuthorObj.text;
noTag = false;
if ((thisAuthor) && (thisAuthor.substr(0, 3) === '/u/')) {
noTag = true;
thisAuthor = thisAuthor.substr(3);
}
thisAuthor = thisAuthor.toLowerCase();
if (!noTag) {
thisAuthorObj.classList.add('userTagged');
if (typeof userObject[thisAuthor] === 'undefined') {
if (this.tags && this.tags[thisAuthor]) {
if (typeof this.tags[thisAuthor].votes !== 'undefined') {
thisVotes = parseInt(this.tags[thisAuthor].votes, 10);
}
if (typeof this.tags[thisAuthor].tag !== 'undefined') {
thisTag = this.tags[thisAuthor].tag;
}
if (typeof this.tags[thisAuthor].color !== 'undefined') {
thisColor = this.tags[thisAuthor].color;
}
if (typeof this.tags[thisAuthor].ignore !== 'undefined') {
thisIgnore = this.tags[thisAuthor].ignore;
}
}
userObject[thisAuthor] = {
tag: thisTag,
color: thisColor,
ignore: thisIgnore,
votes: thisVotes
};
}
var userTagFrag = document.createDocumentFragment();
if (thisTag || typeof noEdit === 'undefined' || !noEdit) {
var userTagLink = document.createElement('a');
if (!thisTag) {
thisTag = '';
userTagLink.setAttribute('class', 'userTagLink RESUserTagImage');
} else {
userTagLink.setAttribute('class', 'userTagLink hasTag');
}
$(userTagLink).text(thisTag);
if (thisColor) {
var bgColor = (thisColor === 'none') ? 'transparent' : thisColor;
userTagLink.setAttribute('style', 'background-color: ' + bgColor + '; color: ' + this.bgToTextColorMap[thisColor] + ' !important;');
}
if (typeof noEdit === 'undefined' || !noEdit) {
userTagLink.setAttribute('username', thisAuthor);
userTagLink.setAttribute('title', 'set a tag');
userTagLink.setAttribute('href', '#');
userTagLink.addEventListener('click', function(e) {
e.preventDefault();
modules['userTagger'].openUserTagPrompt(e.target, this.getAttribute('username'));
}, true);
}
var userTag = document.createElement('span');
userTag.classList.add('RESUserTag');
// var lp = document.createTextNode(' (');
// var rp = document.createTextNode(')');
userTag.appendChild(userTagLink);
// userTagFrag.appendChild(lp);
userTagFrag.appendChild(userTag);
// userTagFrag.appendChild(rp);
if (typeof noEdit === 'undefined' || !noEdit) {
if (this.options.colorUser.value) {
var userVoteFrag = document.createDocumentFragment();
var spacer = document.createTextNode(' ');
userVoteFrag.appendChild(spacer);
var userVoteWeight = document.createElement('a');
userVoteWeight.setAttribute('href', '#');
userVoteWeight.setAttribute('class', 'voteWeight');
$(userVoteWeight).text('[vw]');
userVoteWeight.addEventListener('click', function(e) {
e.preventDefault();
var theTag = this.parentNode.querySelector('.userTagLink');
modules['userTagger'].openUserTagPrompt(theTag, theTag.getAttribute('username'));
}, true);
this.colorUser(userVoteWeight, thisAuthor, userObject[thisAuthor].votes);
userVoteFrag.appendChild(userVoteWeight);
userTagFrag.appendChild(userVoteFrag);
}
}
RESUtils.insertAfter(thisAuthorObj, userTagFrag);