[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

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

   1  /* global phpbb */
   2  
   3  (function($) {  // Avoid conflicts with other libraries
   4  
   5  'use strict';
   6  
   7  
   8  phpbb.prepareSendStats = function () {
   9      var $form = $('#acp_help_phpbb');
  10      var $dark = $('#darkenwrapper');
  11      var $loadingIndicator;
  12  
  13      $form.on('submit', function (event) {
  14          var $this = $(this),
  15              currentTime = Math.floor(new Date().getTime() / 1000),
  16              statsTime = parseInt($this.find('input[name=help_send_statistics_time]').val(), 10);
  17  
  18          event.preventDefault();
  19          $this.unbind('submit');
  20  
  21          // Skip ajax request if form is submitted too early or send stats
  22          // checkbox is not checked
  23          if (!$this.find('input[name=help_send_statistics]').is(':checked') ||
  24              statsTime > currentTime) {
  25              $form.find('input[type=submit]').click();
  26              setTimeout(function () {
  27                  $form.find('input[type=submit]').click();
  28              }, 300);
  29              return;
  30          }
  31  
  32          /**
  33           * Handler for AJAX errors
  34           */
  35  		function errorHandler(jqXHR, textStatus, errorThrown) {
  36              if (typeof console !== 'undefined' && console.log) {
  37                  console.log('AJAX error. status: ' + textStatus + ', message: ' + errorThrown);
  38              }
  39              phpbb.clearLoadingTimeout();
  40              var errorText = '';
  41  
  42              if (typeof errorThrown === 'string' && errorThrown.length > 0) {
  43                  errorText = errorThrown;
  44              } else {
  45                  errorText = $dark.attr('data-ajax-error-text-' + textStatus);
  46                  if (typeof errorText !== 'string' || !errorText.length) {
  47                      errorText = $dark.attr('data-ajax-error-text');
  48                  }
  49              }
  50              phpbb.alert($dark.attr('data-ajax-error-title'), errorText);
  51          }
  52  
  53          /**
  54           * This is a private function used to handle the callbacks, refreshes
  55           * and alert. It calls the callback, refreshes the page if necessary, and
  56           * displays an alert to the user and removes it after an amount of time.
  57           *
  58           * It cannot be called from outside this function, and is purely here to
  59           * avoid repetition of code.
  60           *
  61           * @param {object} res The object sent back by the server.
  62           */
  63  		function returnHandler(res) {
  64              phpbb.clearLoadingTimeout();
  65  
  66              // If a confirmation is not required, display an alert and call the
  67              // callbacks.
  68              $dark.fadeOut(phpbb.alertTime);
  69  
  70              if ($loadingIndicator) {
  71                  $loadingIndicator.fadeOut(phpbb.alertTime);
  72              }
  73  
  74              var $sendStatisticsSuccess = $('<input />', {
  75                  type: 'hidden',
  76                  name: 'send_statistics_response',
  77                  value: res
  78              });
  79              $sendStatisticsSuccess.appendTo('p.submit-buttons');
  80  
  81              // Finish actual form submission
  82              $form.find('input[type=submit]').click();
  83          }
  84  
  85          $loadingIndicator = phpbb.loadingIndicator();
  86  
  87          $.ajax({
  88              url: $this.attr('data-ajax-action').replace('&amp;', '&'),
  89              type: 'POST',
  90              data: 'systemdata=' + encodeURIComponent($this.find('input[name=systemdata]').val()),
  91              success: returnHandler,
  92              error: errorHandler,
  93              cache: false
  94          }).always(function() {
  95              if ($loadingIndicator && $loadingIndicator.is(':visible')) {
  96                  $loadingIndicator.fadeOut(phpbb.alertTime);
  97              }
  98          });
  99      });
 100  };
 101  
 102  /**
 103   * The following callbacks are for reording items. row_down
 104   * is triggered when an item is moved down, and row_up is triggered when
 105   * an item is moved up. It moves the row up or down, and deactivates /
 106   * activates any up / down icons that require it (the ones at the top or bottom).
 107   */
 108  phpbb.addAjaxCallback('row_down', function(res) {
 109      if (typeof res.success === 'undefined' || !res.success) {
 110          return;
 111      }
 112  
 113      var $firstTr = $(this).parents('tr'),
 114          $secondTr = $firstTr.next();
 115  
 116      $firstTr.insertAfter($secondTr);
 117  });
 118  
 119  phpbb.addAjaxCallback('row_up', function(res) {
 120      if (typeof res.success === 'undefined' || !res.success) {
 121          return;
 122      }
 123  
 124      var $secondTr = $(this).parents('tr'),
 125          $firstTr = $secondTr.prev();
 126  
 127      $secondTr.insertBefore($firstTr);
 128  });
 129  
 130  /**
 131   * This callback replaces activate links with deactivate links and vice versa.
 132   * It does this by replacing the text, and replacing all instances of "activate"
 133   * in the href with "deactivate", and vice versa.
 134   */
 135  phpbb.addAjaxCallback('activate_deactivate', function(res) {
 136      var $this = $(this),
 137          newHref = $this.attr('href');
 138  
 139      $this.text(res.text);
 140  
 141      if (newHref.indexOf('deactivate') !== -1) {
 142          newHref = newHref.replace('deactivate', 'activate');
 143      } else {
 144          newHref = newHref.replace('activate', 'deactivate');
 145      }
 146  
 147      $this.attr('href', newHref);
 148  });
 149  
 150  /**
 151   * The removes the parent row of the link or form that triggered the callback,
 152   * and is good for stuff like the removal of forums.
 153   */
 154  phpbb.addAjaxCallback('row_delete', function(res) {
 155      if (res.SUCCESS !== false) {
 156          $(this).parents('tr').remove();
 157      }
 158  });
 159  
 160  /**
 161   * Handler for submitting permissions form in chunks
 162   * This call will submit permissions forms in chunks of 5 fieldsets.
 163   */
 164  function submitPermissions() {
 165      var $form = $('form#set-permissions'),
 166          fieldsetList = $form.find('fieldset[id^=perm]'),
 167          formDataSets = [],
 168          dataSetIndex = 0,
 169          $submitAllButton = $form.find('input[type=submit][name^=action]')[0],
 170          $submitButton = $form.find('input[type=submit][data-clicked=true]')[0];
 171  
 172      // Set proper start values for handling refresh of page
 173      var permissionSubmitSize = 0,
 174          permissionRequestCount = 0,
 175          forumIds = [],
 176          permissionSubmitFailed = false;
 177  
 178      if ($submitAllButton !== $submitButton) {
 179          fieldsetList = $form.find('fieldset#' + $submitButton.closest('fieldset.permissions').id);
 180      }
 181  
 182      $.each(fieldsetList, function (key, value) {
 183          dataSetIndex = Math.floor(key / 5);
 184          var $fieldset = $('fieldset#' + value.id);
 185          if (key % 5 === 0) {
 186              formDataSets[dataSetIndex] = $fieldset.find('select:visible, input:not([data-name])').serialize();
 187          } else {
 188              formDataSets[dataSetIndex] += '&' + $fieldset.find('select:visible, input:not([data-name])').serialize();
 189          }
 190  
 191          // Find proper role value
 192          var roleInput = $fieldset.find('input[name^=role][data-name]');
 193          if (roleInput.val()) {
 194              formDataSets[dataSetIndex] += '&' + roleInput.attr('name') + '=' + roleInput.val();
 195          } else {
 196              formDataSets[dataSetIndex] += '&' + roleInput.attr('name') + '=' +
 197                  $fieldset.find('select[name="' + roleInput.attr('name') + '"]').val();
 198          }
 199      });
 200  
 201      permissionSubmitSize = formDataSets.length;
 202  
 203      // Add each forum ID to forum ID list to preserve selected forums
 204      $.each($form.find('input[type=hidden][name^=forum_id]'), function (key, value) {
 205          if (value.name.match(/^forum_id\[([0-9]+)\]$/)) {
 206              forumIds.push(value.value);
 207          }
 208      });
 209  
 210      /**
 211       * Handler for submitted permissions form chunk
 212       *
 213       * @param {object} res Object returned by AJAX call
 214       */
 215  	function handlePermissionReturn(res) {
 216          permissionRequestCount++;
 217          var $dark = $('#darkenwrapper');
 218  
 219          if (res.S_USER_WARNING) {
 220              phpbb.alert(res.MESSAGE_TITLE, res.MESSAGE_TEXT);
 221              permissionSubmitFailed = true;
 222          } else if (!permissionSubmitFailed && res.S_USER_NOTICE) {
 223              // Display success message at the end of submitting the form
 224              if (permissionRequestCount >= permissionSubmitSize) {
 225                  var $alert = phpbb.alert(res.MESSAGE_TITLE, res.MESSAGE_TEXT);
 226                  var $alertBoxLink = $alert.find('p.alert_text > a');
 227  
 228                  // Create form to submit instead of normal "Back to previous page" link
 229                  if ($alertBoxLink) {
 230                      // Remove forum_id[] from URL
 231                      $alertBoxLink.attr('href', $alertBoxLink.attr('href').replace(/(&forum_id\[\]=[0-9]+)/g, ''));
 232                      var previousPageForm = '<form action="' + $alertBoxLink.attr('href') + '" method="post">';
 233                      $.each(forumIds, function (key, value) {
 234                          previousPageForm += '<input type="text" name="forum_id[]" value="' + value + '" />';
 235                      });
 236                      previousPageForm += '</form>';
 237  
 238                      $alertBoxLink.on('click', function (e) {
 239                          var $previousPageForm = $(previousPageForm);
 240                          $('body').append($previousPageForm);
 241                          e.preventDefault();
 242                          $previousPageForm.submit();
 243                      });
 244                  }
 245  
 246                  // Do not allow closing alert
 247                  $dark.off('click');
 248                  $alert.find('.alert_close').hide();
 249  
 250                  if (typeof res.REFRESH_DATA !== 'undefined') {
 251                      setTimeout(function () {
 252                          // Create forum to submit using POST. This will prevent
 253                          // exceeding the maximum length of URLs
 254                          var form = '<form action="' + res.REFRESH_DATA.url.replace(/(&forum_id\[\]=[0-9]+)/g, '') + '" method="post">';
 255                          $.each(forumIds, function (key, value) {
 256                              form += '<input type="text" name="forum_id[]" value="' + value + '" />';
 257                          });
 258                          form += '</form>';
 259                          $form = $(form);
 260                          $('body').append($form);
 261  
 262                          // Hide the alert even if we refresh the page, in case the user
 263                          // presses the back button.
 264                          $dark.fadeOut(phpbb.alertTime, function () {
 265                              if (typeof $alert !== 'undefined') {
 266                                  $alert.hide();
 267                              }
 268                          });
 269  
 270                          // Submit form
 271                          $form.submit();
 272                      }, res.REFRESH_DATA.time * 1000); // Server specifies time in seconds
 273                  }
 274              }
 275          }
 276      }
 277  
 278      // Create AJAX request for each form data set
 279      $.each(formDataSets, function (key, formData) {
 280          $.ajax({
 281              url: $form.action,
 282              type: 'POST',
 283              data: formData + '&' + $submitButton.name + '=' + encodeURIComponent($submitButton.value) +
 284                  '&creation_time=' + $form.find('input[type=hidden][name=creation_time]')[0].value +
 285                  '&form_token=' + $form.find('input[type=hidden][name=form_token]')[0].value +
 286                  '&' + $form.children('input[type=hidden]').serialize() +
 287                  '&' + $form.find('input[type=checkbox][name^=inherit]').serialize(),
 288              success: handlePermissionReturn,
 289              error: handlePermissionReturn
 290          });
 291      });
 292  }
 293  
 294  $('[data-ajax]').each(function() {
 295      var $this = $(this),
 296          ajax = $this.attr('data-ajax');
 297  
 298      if (ajax !== 'false') {
 299          var fn = (ajax !== 'true') ? ajax : null;
 300          phpbb.ajaxify({
 301              selector: this,
 302              refresh: $this.attr('data-refresh') !== undefined,
 303              callback: fn
 304          });
 305      }
 306  });
 307  
 308  /**
 309  * Automatically resize textarea
 310  */
 311  $(function() {
 312      phpbb.resizeTextArea($('textarea:not(.no-auto-resize)'), {minHeight: 75});
 313  
 314      var $setPermissionsForm = $('form#set-permissions');
 315      if ($setPermissionsForm.length) {
 316          $setPermissionsForm.on('submit', function (e) {
 317              submitPermissions();
 318              e.preventDefault();
 319          });
 320          $setPermissionsForm.find('input[type=submit]').click(function() {
 321              $('input[type=submit]', $(this).parents($('form#set-permissions'))).removeAttr('data-clicked');
 322              $(this).attr('data-clicked', true);
 323          });
 324      }
 325  
 326      if ($('#acp_help_phpbb')) {
 327          phpbb.prepareSendStats();
 328      }
 329  });
 330  
 331  
 332  })(jQuery); // Avoid conflicts with other libraries


Generated: Sat Oct 26 11:28:42 2019 Cross-referenced by PHPXref 0.7.1