etonbones

Copy Proboards Labels

Mar 7th, 2020
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function () {
  2.     var forumLabels = {
  3.         labels: {},
  4.         insertionIndex: 0,
  5.         copy: function () {
  6.             var labels = this.labels = [];
  7.  
  8.             $('div.list.ui-autoform').children().each(function () {
  9.                 var name = $(this).find('input.tl-name-input').val();
  10.  
  11.                 if ('' == name)
  12.                     return;
  13.  
  14.                 labels.push({
  15.                     name: name,
  16.                     color: $(this).find('input.tl-color-input').val(),
  17.                     perms: {
  18.                         access: $(this).find('input.tl-permi' + 'ssions_accessible-input').val(),
  19.                         view: $(this).find('input.tl-permi' + 'ssions_viewable-input').val()
  20.                     }
  21.                 });
  22.             });
  23.             function unique(e) {
  24.                 for (var t = {}, i = [], n = 0, a = e.length; n < a; ++n)
  25.                     JSON.stringify(e[n], null, 0)in t || (i.push(e[n]), t[JSON.stringify(e[n], null, 0)] = 1);
  26.                 return i
  27.             }
  28.             labels = unique(labels);
  29.         },
  30.         paste: function () {
  31.             if (0 == this.labels.length)
  32.                 return;
  33.  
  34.             this.insertionIndex = 0;
  35.             this.insertNextLabels();
  36.         },
  37.  
  38.         insertNextLabels() {
  39.             var label = this.labels[this.insertionIndex++];
  40.             var list = $('.thread_label_div.ui-autoform .list.ui-autoform');
  41.             var newItem = list.find('.ui-autoform-new-item:last');
  42.  
  43.             if (this.labels.length > this.insertionIndex)
  44.                 list.one('DOMNodeInserted', '.ui-autoform-new-item', this.insertNextLabels.bind(this));
  45.  
  46.             $('#colorPicker').css({
  47.                 visibility: 'hidden'
  48.             });
  49.  
  50.             newItem
  51.             .find('input.tl-color-input')
  52.             .val(label.color)
  53.             .trigger('focus')
  54.             .trigger('change')
  55.             .trigger('blur')
  56.             .end()
  57.             .find('input.tl-permi' + 'ssions_accessible-input')
  58.             .val(label.perms.access)
  59.             .end()
  60.             .find('input.tl-permi' + 'ssions_viewable-input')
  61.             .val(label.perms.view)
  62.             .end()
  63.             .find('input.tl-name-input')
  64.             .val(label.name)
  65.             .trigger('change');
  66.  
  67.             $('#colorPicker').css({
  68.                 visibility: ''
  69.             });
  70.         }
  71.     };
  72.     (function () {
  73.         $(document).ajaxComplete(function (event, xhr, settings) {
  74.             if (settings.url == proboards.route('category_board_board_dialog')) {
  75.                 const buttonpane = $('#dialog_settings').nextAll('.ui-dialog-buttonpane').find('.ui-dialog-buttonset');
  76.                 function addPasteButton() {
  77.                     "undefined" !== typeof forumLabels.labels.length && forumLabels.labels.length && buttonpane.find('.ui-button.board-paste').length == 0 && $('<button>', {
  78.                         role: 'button',
  79.                         type: 'button',
  80.                         class: 'ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only board-paste',
  81.                         title: '[' + forumLabels.labels.map(x => {
  82.                             return x.name
  83.                         }).join('][') + ']'
  84.                     }).append('<span class="ui-button-text">Paste Board Labels</span>').click(function (e) {
  85.                         forumLabels.paste();
  86.                         e.preventDefault();
  87.                         return false;
  88.                     }).prependTo(buttonpane)
  89.                 }
  90.                 function addCopyButton() {
  91.                     $('.list.ui-autoform > .item').not('.ui-autoform-new-item').length && buttonpane.find('.ui-button.board-copy').length == 0 && $('<button>', {
  92.                         role: 'button',
  93.                         type: 'button',
  94.                         class: 'ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only board-copy'
  95.                     }).append('<span class="ui-button-text">Copy Board Labels</span>').click(function (e) {
  96.                         forumLabels.copy();
  97.                         e.preventDefault();
  98.                         $('.thread_label_div').prevAll('b').first().setStatus('Copied', {
  99.                             position: 'after'
  100.                         }),
  101.                         addPasteButton();
  102.                         return false;
  103.                     }).prependTo(buttonpane);
  104.                 }
  105.                 addCopyButton(),
  106.                 addPasteButton()
  107.             }
  108.         })
  109.     })();
  110. })()
  111.  
  112. /** CHANGELOG:
  113. 1. added copy and paste buttons
  114. 2. added dynamic paste button when data copied
  115. 3. added duplicate filtering during copy process
  116. **/
Add Comment
Please, Sign In to add comment