[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/adm/style/ -> permissions.js (source)

   1  /**
   2  * Hide and show all checkboxes
   3  * status = true (show boxes), false (hide boxes)
   4  */
   5  function display_checkboxes(status) {
   6      var form = document.getElementById('set-permissions');
   7      var cb = document.getElementsByTagName('input');
   8      var display;
   9  
  10      //show
  11      if (status) {
  12          display = 'inline';
  13      }
  14      //hide
  15      else {
  16          display = 'none';
  17      }
  18  
  19      for (var i = 0; i < cb.length; i++ ) {
  20          if (cb[i].className === 'permissions-checkbox') {
  21              cb[i].style.display = display;
  22          }
  23      }
  24  }
  25  
  26  /**
  27  * Change opacity of element
  28  * e = element
  29  * value = 0 (hidden) till 10 (fully visible)
  30  */
  31  function set_opacity(e, value) {
  32      e.style.opacity = value/10;
  33  
  34      //IE opacity currently turned off, because of its astronomical stupidity
  35      //e.style.filter = 'alpha(opacity=' + value*10 + ')';
  36  }
  37  
  38  /**
  39  * Reset the opacity and checkboxes
  40  * block_id = id of the element that needs to be toggled
  41  */
  42  function toggle_opacity(block_id) {
  43      var cb = document.getElementById('checkbox' + block_id);
  44      var fs = document.getElementById('perm' + block_id);
  45  
  46      if (cb.checked) {
  47          set_opacity(fs, 5);
  48      } else {
  49          set_opacity(fs, 10);
  50      }
  51  }
  52  
  53  /**
  54  * Reset the opacity and checkboxes
  55  * value = 0 (checked) and 1 (unchecked)
  56  * except_id = id of the element not to hide
  57  */
  58  function reset_opacity(status, except_id) {
  59      var perm = document.getElementById('set-permissions');
  60      var fs = perm.getElementsByTagName('fieldset');
  61      var opacity = 5;
  62  
  63      if (status) {
  64          opacity = 10;
  65      }
  66  
  67      for (var i = 0; i < fs.length; i++ ) {
  68          if (fs[i].className !== 'quick') {
  69              set_opacity(fs[i], opacity);
  70          }
  71      }
  72  
  73      if (typeof(except_id) !== 'undefined') {
  74          set_opacity(document.getElementById('perm' + except_id), 10);
  75      }
  76  
  77      //reset checkboxes too
  78      marklist('set-permissions', 'inherit', !status);
  79  }
  80  
  81  /**
  82  * Check whether we have a full radiobutton row of true
  83  * index = offset for the row of inputs (0 == first row, 1 == second, 2 == third),
  84  * rb = array of radiobuttons
  85  */
  86  function get_radio_status(index, rb) {
  87      for (var i = index; i < rb.length; i = i + 3 ) {
  88          if (rb[i].checked !== true) {
  89              if (i > index) {
  90                  //at least one is true, but not all (custom)
  91                  return 2;
  92              }
  93              //first one is not true
  94              return 0;
  95          }
  96      }
  97  
  98      // all radiobuttons true
  99      return 1;
 100  }
 101  
 102  /**
 103  * Set tab colours
 104  * id = panel the tab needs to be set for,
 105  * init = initialising on open,
 106  * quick = If no calculation needed, this contains the colour
 107  */
 108  function set_colours(id, init, quick) {
 109      var table = document.getElementById('table' + id);
 110      var tab = document.getElementById('tab' + id);
 111  
 112      if (typeof(quick) !== 'undefined') {
 113          tab.className = 'permissions-preset-' + quick + ' activetab';
 114          return;
 115      }
 116  
 117      var rb = table.getElementsByTagName('input');
 118      var colour = 'custom';
 119  
 120      var status = get_radio_status(0, rb);
 121  
 122      if (status === 1) {
 123          colour = 'yes';
 124      } else if (status === 0) {
 125          // We move on to No
 126          status = get_radio_status(1, rb);
 127  
 128          if (status === 1) {
 129              colour = 'no';
 130          } else if (status === 0) {
 131              // We move on to Never
 132              status = get_radio_status(2, rb);
 133  
 134              if (status === 1) {
 135                  colour = 'never';
 136              }
 137          }
 138      }
 139  
 140      if (init) {
 141          tab.className = 'permissions-preset-' + colour;
 142      } else {
 143          tab.className = 'permissions-preset-' + colour + ' activetab';
 144      }
 145  }
 146  
 147  /**
 148  * Initialise advanced tab colours on first load
 149  * block_id = block that is opened
 150  */
 151  function init_colours(block_id) {
 152      var block = document.getElementById('advanced' + block_id);
 153      var panels = block.getElementsByTagName('div');
 154      var tab = document.getElementById('tab' + id);
 155  
 156      for (var i = 0; i < panels.length; i++) {
 157          if (panels[i].className === 'permissions-panel') {
 158              set_colours(panels[i].id.replace(/options/, ''), true);
 159          }
 160      }
 161  
 162      tab.className = tab.className + ' activetab';
 163  }
 164  
 165  /**
 166  * Show/hide option panels
 167  * value = suffix for ID to show
 168  * adv = we are opening advanced permissions
 169  * view = called from view permissions
 170  */
 171  function swap_options(pmask, fmask, cat, adv, view) {
 172      id = pmask + fmask + cat;
 173      active_option = active_pmask + active_fmask + active_cat;
 174  
 175      var    old_tab = document.getElementById('tab' + active_option);
 176      var new_tab = document.getElementById('tab' + id);
 177      var adv_block = document.getElementById('advanced' + pmask + fmask);
 178  
 179      if (adv_block.style.display === 'block' && adv === true) {
 180          phpbb.toggleDisplay('advanced' + pmask + fmask, -1);
 181          reset_opacity(1);
 182          display_checkboxes(false);
 183          return;
 184      }
 185  
 186      // no need to set anything if we are clicking on the same tab again
 187      if (new_tab === old_tab && !adv) {
 188          return;
 189      }
 190  
 191      // init colours
 192      if (adv && (pmask + fmask) !== (active_pmask + active_fmask)) {
 193          init_colours(pmask + fmask);
 194          display_checkboxes(true);
 195          reset_opacity(1);
 196      } else if (adv) {
 197          //Checkbox might have been clicked, but we need full visibility
 198          display_checkboxes(true);
 199          reset_opacity(1);
 200      }
 201  
 202      // set active tab
 203      old_tab.className = old_tab.className.replace(/\ activetab/g, '');
 204      new_tab.className = new_tab.className + ' activetab';
 205  
 206      if (id === active_option && adv !== true) {
 207          return;
 208      }
 209  
 210      phpbb.toggleDisplay('options' + active_option, -1);
 211  
 212      //hiding and showing the checkbox
 213      if (document.getElementById('checkbox' + active_pmask + active_fmask)) {
 214          phpbb.toggleDisplay('checkbox' + pmask + fmask, -1);
 215  
 216          if ((pmask + fmask) !== (active_pmask + active_fmask)) {
 217              document.getElementById('checkbox' + active_pmask + active_fmask).style.display = 'inline';
 218          }
 219      }
 220  
 221      if (!view) {
 222          phpbb.toggleDisplay('advanced' + active_pmask + active_fmask, -1);
 223      }
 224  
 225      if (!view) {
 226          phpbb.toggleDisplay('advanced' + pmask + fmask, 1);
 227      }
 228      phpbb.toggleDisplay('options' + id, 1);
 229  
 230      active_pmask = pmask;
 231      active_fmask = fmask;
 232      active_cat = cat;
 233  }
 234  
 235  /**
 236  * Mark all radio buttons in one panel
 237  * id = table ID container, s = status ['y'/'u'/'n']
 238  */
 239  function mark_options(id, s) {
 240      var t = document.getElementById(id);
 241  
 242      if (!t) {
 243          return;
 244      }
 245  
 246      var rb = t.getElementsByTagName('input');
 247  
 248      for (var r = 0; r < rb.length; r++) {
 249          if (rb[r].id.substr(rb[r].id.length-1) === s) {
 250              rb[r].checked = true;
 251          }
 252      }
 253  }
 254  
 255  function mark_one_option(id, field_name, s) {
 256      var t = document.getElementById(id);
 257  
 258      if (!t) {
 259          return;
 260      }
 261  
 262      var rb = t.getElementsByTagName('input');
 263  
 264      for (var r = 0; r < rb.length; r++) {
 265          if (rb[r].id.substr(rb[r].id.length-field_name.length-3, field_name.length) === field_name && rb[r].id.substr(rb[r].id.length-1) === s) {
 266              rb[r].checked = true;
 267          }
 268      }
 269  }
 270  
 271  /**
 272   * (Re)set the permission role dropdown.
 273   *
 274   * Try and match the set permissions to an existing role.
 275   * Otherwise reset the dropdown to "Select a role.."
 276   *
 277   * @param {string}    id        The fieldset identifier
 278   * @returns {void}
 279   */
 280  function reset_role(id) {
 281      var t = document.getElementById(id);
 282  
 283      if (!t) {
 284          return;
 285      }
 286  
 287      // Before resetting the role dropdown, try and match any permission role
 288      var parent = t.parentNode,
 289          roleId = match_role_settings(id.replace('role', 'perm')),
 290          text = no_role_assigned,
 291          index = 0;
 292  
 293      // If a role permissions was matched, grab that option's value and index
 294      if (roleId) {
 295          for (var i = 0; i < t.options.length; i++) {
 296              if (parseInt(t.options[i].value, 10) === roleId) {
 297                  text = t.options[i].text;
 298                  index = i;
 299                  break;
 300              }
 301          }
 302      }
 303  
 304      // Update the select's value and selected index
 305      t.value = roleId;
 306      t.options[index].selected = true;
 307  
 308      // Update the dropdown trigger to show the new value
 309      parent.querySelector('span.dropdown-trigger').innerText = text;
 310      parent.querySelector('input[data-name^=role]').value = roleId;
 311  }
 312  
 313  /**
 314  * Load role and set options accordingly
 315  */
 316  function set_role_settings(role_id, target_id) {
 317      var settings = role_options[role_id];
 318  
 319      if (!settings) {
 320          return;
 321      }
 322  
 323      // Mark all options to no (unset) first...
 324      mark_options(target_id, 'u');
 325  
 326      for (var r in settings) {
 327          mark_one_option(target_id, r, (settings[r] === 1) ? 'y' : 'n');
 328      }
 329  }
 330  
 331  /**
 332   * Match the set permissions against the available roles.
 333   *
 334   * @param {string}    id        The parent fieldset identifier
 335   * @return {number}            The permission role identifier
 336   */
 337  function match_role_settings(id) {
 338      var fieldset = document.getElementById(id),
 339          radios = fieldset.getElementsByTagName('input'),
 340          set = {};
 341  
 342      // Iterate over all the radio buttons
 343      for (var i = 0; i < radios.length; i++) {
 344          var matches = radios[i].id.match(/setting\[\d+]\[\d+]\[([a-z_]+)]/);
 345  
 346          // Make sure the name attribute matches, the radio is checked and it is not the "No" (-1) value.
 347          if (matches !== null && radios[i].checked && radios[i].value !== '-1') {
 348              set[matches[1]] = parseInt(radios[i].value, 10);
 349          }
 350      }
 351  
 352      // Sort and stringify the 'set permissions' object
 353      set = sort_and_stringify(set);
 354  
 355      // Iterate over the available role options and return the first match
 356      for (var r in role_options)
 357      {
 358          if (sort_and_stringify(role_options[r]) === set) {
 359              return parseInt(r, 10);
 360          }
 361      }
 362  
 363      return 0;
 364  }
 365  
 366  /**
 367   * Sort and stringify an Object so it can be easily compared against another object.
 368   *
 369   * @param {object}    obj        The object to sort (by key) and stringify
 370   * @return {string}            The sorted object as a string
 371   */
 372  function sort_and_stringify(obj) {
 373      return JSON.stringify(Object.keys(obj).sort().reduce(function (result, key) {
 374          result[key] = obj[key];
 375          return result;
 376      }, {}));
 377  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1