@@ -23,7 +23,8 @@ const self = new Module({
23
23
let config = TBCore . config ,
24
24
sortReasons = [ ] ,
25
25
subreddit ,
26
- postFlairTemplates ;
26
+ postFlairTemplates ,
27
+ userFlairTemplates ;
27
28
28
29
// With the following function we will create the UI when we need it.
29
30
// Create the window overlay.
@@ -265,6 +266,14 @@ const self = new Module({
265
266
<label><input type="checkbox" id="unbanuser">unban user</label>
266
267
<label><input type="checkbox" id="muteuser">mute user</label>
267
268
</div>
269
+ <div class="tb-macro-actions-row">
270
+ <h2 style="padding-right: 8px">User flair</h2>
271
+ <input type="hidden" class="tb-input" name="user-flair-text" id="userflair-text" value="">
272
+ <select name="user-flair-id" id="userflair-id-select" class="tb-action-button inline-button user-flair-picker">
273
+ <option value="Select user flair" disabled>Select user flair template</option>
274
+ <option value="">Don't touch</option>
275
+ </select>
276
+ </div>
268
277
</div>
269
278
<div class="tb-macro-context">
270
279
<h2>Which context</h2>
@@ -384,6 +393,7 @@ const self = new Module({
384
393
sortReasons = [ ] ;
385
394
subreddit = null ;
386
395
postFlairTemplates = null ;
396
+ userFlairTemplates = null ;
387
397
} ) ;
388
398
389
399
// now we can play around!
@@ -651,6 +661,27 @@ const self = new Module({
651
661
} ) ;
652
662
}
653
663
664
+ async function addUserFlairTemplatesToDropdown ( $dropdown , macroNum ) {
665
+ // Fetching the flair templates if not fetched already
666
+ if ( ! userFlairTemplates ) {
667
+ userFlairTemplates = await TBApi . apiOauthGET ( `/r/${ subreddit } /api/user_flair_v2` ) . then ( r => r . json ( ) ) ;
668
+ }
669
+
670
+ // We should only append user flair templates to the dropdown if they're not
671
+ // already there, otherwise they'll duplicate with every click of the edit icon.
672
+ if ( $dropdown [ 0 ] . childElementCount > 2 ) {
673
+ return ;
674
+ }
675
+ // Getting the current flair template for macro so we can set the `selected` attribute
676
+ // on one of the `<option>`s. When adding a new macro we don't have one
677
+ // selected yet, so this argument won't be provided.
678
+ const defaultOption = macroNum ? config . modMacros [ macroNum ] . userflair : '' ;
679
+
680
+ userFlairTemplates . forEach ( flair => {
681
+ $dropdown . append ( `<option value="${ flair . id } " ${ flair . id === defaultOption ? 'selected' : '' } >${ flair . text } </option>` ) ;
682
+ } ) ;
683
+ }
684
+
654
685
// With this function we'll fetch the removal reasons for editing
655
686
function removalReasonsContent ( ) {
656
687
if ( config . removalReasons && config . removalReasons . reasons . length > 0 ) {
@@ -802,6 +833,14 @@ const self = new Module({
802
833
<label><input type="checkbox" class="{{i}}-unbanuser" id="unbanuser">unban user</label>
803
834
<label><input type="checkbox" class="{{i}}-muteuser" id="muteuser">mute user</label>
804
835
</div>
836
+ <div class="tb-macro-actions-row">
837
+ <h2 style="padding-right: 8px;">User flair</h2>
838
+ <input type="hidden" name="user-flair-text" class="tb-input {{i}}-user-flair-text" id="userflair-text" value="">
839
+ <select name="user-flair-id" id="userflair-id-select" class="tb-action-button inline-button {{i}}-user-flair-picker">
840
+ <option value="Select user flair" disabled>Select user flair template</option>
841
+ <option value="">Don't touch</option>
842
+ </select>
843
+ </div>
805
844
</div>
806
845
<div class="tb-macro-context">
807
846
<h2>Which context</h2>
@@ -830,6 +869,8 @@ const self = new Module({
830
869
$ ( `.${ i } -banuser` ) . prop ( 'checked' , macro . ban ) ;
831
870
$ ( `.${ i } -unbanuser` ) . prop ( 'checked' , macro . unban ) ;
832
871
$ ( `.${ i } -muteuser` ) . prop ( 'checked' , macro . mute ) ;
872
+ $ ( `.${ i } -user-flair-picker` ) . val ( macro . userflair ) ;
873
+ $ ( `.${ i } -user-flair-text` ) . val ( macro . userflairtext ) ;
833
874
$ ( `.${ i } -removeitem` ) . prop ( 'checked' , macro . remove ) ;
834
875
$ ( `.${ i } -spamitem` ) . prop ( 'checked' , macro . spam ) ;
835
876
$ ( `.${ i } -approveitem` ) . prop ( 'checked' , macro . approve ) ;
@@ -1269,6 +1310,18 @@ const self = new Module({
1269
1310
$flairCSS . val ( flairTemplate . css_class ) ;
1270
1311
} ) ;
1271
1312
1313
+ // Watching for changes in the flair template dropdown and assigning the flair text and class
1314
+ $body . on ( 'change' , '#userflair-id-select' , function ( ) {
1315
+ const $this = $ ( this ) ;
1316
+ const selectedFlairID = $this . val ( ) ;
1317
+
1318
+ const $flairText = $this . parents ( '.tb-macro-actions' ) . find ( 'input.tb-input[name="user-flair-text"]' ) ;
1319
+
1320
+ const flairTemplate = userFlairTemplates . find ( flair => flair . id === selectedFlairID ) ;
1321
+
1322
+ $flairText . val ( flairTemplate ?. text ?? '' ) ;
1323
+ } ) ;
1324
+
1272
1325
const resetForm = ( ) => {
1273
1326
$body . find ( '#tb-add-removal-reason' ) . show ( ) ;
1274
1327
$body . find ( '#tb-add-removal-reason-form' ) . hide ( ) ;
@@ -1439,6 +1492,13 @@ const self = new Module({
1439
1492
const $this = $ ( this ) ;
1440
1493
1441
1494
$this . closest ( 'tr.mod-macro' ) . find ( '.mod-macro-label' ) . hide ( ) ;
1495
+
1496
+ // Getting the flair dropdown
1497
+ const macroNum = $this . attr ( 'data-macro' ) ;
1498
+ const $flairDropdown = $this . closest ( '.mod-macro' ) . find ( '#userflair-id-select' ) ;
1499
+
1500
+ addUserFlairTemplatesToDropdown ( $flairDropdown , macroNum ) ;
1501
+
1442
1502
$this . closest ( 'tr.mod-macro' ) . find ( '.mod-macro-edit' ) . show ( ) ;
1443
1503
} ) ;
1444
1504
@@ -1455,6 +1515,8 @@ const self = new Module({
1455
1515
$macroContent . find ( '#banuser' ) . prop ( 'checked' , macro . ban ) ;
1456
1516
$macroContent . find ( '#unbanuser' ) . prop ( 'checked' , macro . unban ) ;
1457
1517
$macroContent . find ( '#muteuser' ) . prop ( 'checked' , macro . mute ) ;
1518
+ $macroContent . find ( '#userflair-id-select' ) . val ( macro . userflair ) ;
1519
+ $macroContent . find ( '#userflair-text' ) . val ( macro . userflairtext ) ;
1458
1520
$macroContent . find ( '#removeitem' ) . prop ( 'checked' , macro . remove ) ;
1459
1521
$macroContent . find ( '#approveitem' ) . prop ( 'checked' , macro . approve ) ;
1460
1522
// saved as lockthread for legacy reasons
@@ -1480,6 +1542,8 @@ const self = new Module({
1480
1542
banuser = $macroContent . find ( '#banuser' ) . prop ( 'checked' ) ,
1481
1543
unbanuser = $macroContent . find ( '#unbanuser' ) . prop ( 'checked' ) ,
1482
1544
muteuser = $macroContent . find ( '#muteuser' ) . prop ( 'checked' ) ,
1545
+ flairuser = $macroContent . find ( '#userflair-id-select' ) . val ( ) ,
1546
+ flairusertext = $macroContent . find ( '#userflair-text' ) . val ( ) ,
1483
1547
removeitem = $macroContent . find ( '#removeitem' ) . prop ( 'checked' ) ,
1484
1548
approveitem = $macroContent . find ( '#approveitem' ) . prop ( 'checked' ) ,
1485
1549
lockitem = $macroContent . find ( '#lockitem' ) . prop ( 'checked' ) ,
@@ -1510,6 +1574,8 @@ const self = new Module({
1510
1574
macro . ban = banuser ;
1511
1575
macro . unban = unbanuser ;
1512
1576
macro . mute = muteuser ;
1577
+ macro . userflair = flairuser ;
1578
+ macro . userflairtext = flairusertext ;
1513
1579
macro . remove = removeitem ;
1514
1580
macro . approve = approveitem ;
1515
1581
// saved as lockthread for legacy reasons
@@ -1580,7 +1646,16 @@ const self = new Module({
1580
1646
1581
1647
// Adding a new macro
1582
1648
$body . on ( 'click' , '#tb-add-mod-macro' , function ( ) {
1583
- $ ( this ) . hide ( ) ;
1649
+ const $this = $ ( this ) ;
1650
+
1651
+ $this . hide ( ) ;
1652
+
1653
+ // Getting the flair dropdown
1654
+ const $addMacroForm = $ ( '#tb-add-mod-macro-form' ) ;
1655
+ const $flairDropdown = $addMacroForm . find ( 'select#userflair-id-select' ) ;
1656
+
1657
+ addUserFlairTemplatesToDropdown ( $flairDropdown ) ;
1658
+
1584
1659
$body . find ( '#tb-add-mod-macro-form' ) . show ( ) ;
1585
1660
} ) ;
1586
1661
@@ -1592,6 +1667,8 @@ const self = new Module({
1592
1667
banuser = $body . find ( '#banuser' ) . prop ( 'checked' ) ,
1593
1668
unbanuser = $body . find ( '#unbanuser' ) . prop ( 'checked' ) ,
1594
1669
muteuser = $body . find ( '#muteuser' ) . prop ( 'checked' ) ,
1670
+ flairuser = $body . find ( '#userflair-id-select' ) . val ( ) ,
1671
+ flairusertext = $body . find ( '#userflair-text' ) . val ( ) ,
1595
1672
removeitem = $body . find ( '#removeitem' ) . prop ( 'checked' ) ,
1596
1673
approveitem = $body . find ( '#approveitem' ) . prop ( 'checked' ) ,
1597
1674
spamitem = $body . find ( '#spamitem' ) . prop ( 'checked' ) ,
@@ -1621,6 +1698,8 @@ const self = new Module({
1621
1698
macro . ban = banuser ;
1622
1699
macro . unban = unbanuser ;
1623
1700
macro . mute = muteuser ;
1701
+ macro . userflair = flairuser ;
1702
+ macro . userflairtext = flairusertext ;
1624
1703
macro . remove = removeitem ;
1625
1704
macro . approve = approveitem ;
1626
1705
macro . spam = spamitem ;
@@ -1654,6 +1733,8 @@ const self = new Module({
1654
1733
$body . find ( '#banuser' ) . prop ( 'checked' , false ) ;
1655
1734
$body . find ( '#unbanuser' ) . prop ( 'checked' , false ) ;
1656
1735
$body . find ( '#muteuser' ) . prop ( 'checked' , false ) ;
1736
+ $body . find ( '#userflair-id-select' ) . val ( '' ) ;
1737
+ $body . find ( '#userflair-text' ) . val ( '' ) ;
1657
1738
$body . find ( '#removeitem' ) . prop ( 'checked' , false ) ;
1658
1739
$body . find ( '#approveitem' ) . prop ( 'checked' , false ) ;
1659
1740
$body . find ( '#spamitem' ) . prop ( 'checked' , false ) ;
@@ -1678,6 +1759,8 @@ const self = new Module({
1678
1759
$body . find ( '#banuser' ) . prop ( 'checked' , false ) ;
1679
1760
$body . find ( '#unbanuser' ) . prop ( 'checked' , false ) ;
1680
1761
$body . find ( '#muteuser' ) . prop ( 'checked' , false ) ;
1762
+ $body . find ( '#userflair-id-select' ) . val ( '' ) ;
1763
+ $body . find ( '#userflair-text' ) . val ( '' ) ;
1681
1764
$body . find ( '#removeitem' ) . prop ( 'checked' , false ) ;
1682
1765
$body . find ( '#approveitem' ) . prop ( 'checked' , false ) ;
1683
1766
$body . find ( '#lockitem' ) . prop ( 'checked' , false ) ;
0 commit comments