[ Index ]

PHP Cross Reference of MyBB 1.8.38

title

Body

[close]

/jscripts/ -> post.js (source)

   1  var Post = {
   2      init: function () {
   3          $(function () {
   4              Post.fileInput = $("input[name='attachments[]']");
   5              Post.dropZone = $('#dropzone');
   6              Post.dropZone.find('div').text(lang.drop_files);
   7              Post.form = Post.fileInput.parents('form');
   8  
   9              Post.form.on('submit', Post.checkAttachments);
  10              Post.fileInput.on('change', Post.addAttachments);
  11  
  12              Post.dropZone.on('drag', function (e) {
  13                  e.preventDefault();
  14              });
  15  
  16              Post.dropZone.on('dragstart',  function (e) {
  17                  e.preventDefault();
  18                  e.originalEvent.dataTransfer.setData('text/plain', '');
  19              });
  20  
  21              Post.dropZone.on('dragover dragenter', function (e) {
  22                  e.preventDefault();
  23                  $(this).addClass('activated').find('div').text(lang.upload_initiate);
  24              });
  25  
  26              Post.dropZone.on('dragleave dragend', function (e) {
  27                  e.preventDefault();
  28                  $(this).removeClass('activated').find('div').text(lang.drop_files);
  29              });
  30  
  31              Post.dropZone.on('click', function () {
  32                  Post.fileInput.trigger('click');
  33              });
  34  
  35              Post.dropZone.on('drop', function (e) {
  36                  e.preventDefault();
  37                  $(this).removeClass('activated');
  38                  var files = e.originalEvent.dataTransfer.files;
  39                  Post.fileInput.prop('files', files).trigger('change');
  40              });
  41  
  42              Post.fileInput.parents().eq(1).hide();
  43              Post.dropZone.parents().eq(1).show();
  44  
  45              // prevent SCEditor from inserting [img] with data URI
  46              var $message = document.querySelector('#message');
  47  
  48              if ($message !== null) {
  49                  new MutationObserver(function () {
  50                      // run once #message is hidden by SCEditor, and the MyBBEditor instance becomes available
  51  
  52                      if (typeof MyBBEditor !== 'undefined' && MyBBEditor !== null) {
  53                          MyBBEditor.bind('valuechanged', function () {
  54                              var oldValue = MyBBEditor.val();
  55                              var newValue = oldValue.replace(/\[img]data:[a-z/]+;base64,[A-Za-z0-9+\/]+={0,2}\[\/img]/, '');
  56  
  57                              if (oldValue !== newValue) {
  58                                  MyBBEditor.val(newValue);
  59                              }
  60                          });
  61                      }
  62                  }).observe($message, {attributes: true});
  63              }
  64          });
  65      },
  66  
  67      loadMultiQuoted: function () {
  68          if (use_xmlhttprequest == 1) {
  69              tid = document.input.tid.value;
  70  
  71              $.ajax({
  72                  url: 'xmlhttp.php?action=get_multiquoted&tid=' + tid,
  73                  type: 'get',
  74                  complete: function (request, status) {
  75                      Post.multiQuotedLoaded(request, status);
  76                  }
  77              });
  78  
  79              return false;
  80          } else {
  81              return true;
  82          }
  83      },
  84  
  85      loadMultiQuotedAll: function () {
  86          if (use_xmlhttprequest == 1) {
  87              $.ajax({
  88                  url: 'xmlhttp.php?action=get_multiquoted&load_all=1',
  89                  type: 'get',
  90                  complete: function (request, status) {
  91                      Post.multiQuotedLoaded(request, status);
  92                  }
  93              });
  94  
  95              return false;
  96          } else {
  97              return true;
  98          }
  99      },
 100  
 101      multiQuotedLoaded: function (request) {
 102          var json = JSON.parse(request.responseText);
 103          if (typeof response == 'object') {
 104              if (json.hasOwnProperty("errors")) {
 105                  $.each(json.errors, function (i, message) {
 106                      $.jGrowl(lang.post_fetch_error + ' ' + message, { theme: 'jgrowl_error' });
 107                  });
 108                  return false;
 109              }
 110          }
 111  
 112          var id = 'message';
 113          if (typeof MyBBEditor !== 'undefined' && MyBBEditor !== null) {
 114              MyBBEditor.insert(json.message);
 115          } else {
 116              if ($('#' + id).value) {
 117                  $('#' + id).value += "\n";
 118              }
 119              $('#' + id).val($('#' + id).val() + json.message);
 120          }
 121  
 122          $('#multiquote_unloaded').hide();
 123          document.input.quoted_ids.value = 'all';
 124      },
 125  
 126      clearMultiQuoted: function () {
 127          $('#multiquote_unloaded').hide();
 128          Cookie.unset('multiquote');
 129      },
 130  
 131      removeAttachment: function (aid) {
 132          MyBB.prompt(removeattach_confirm, {
 133              buttons: [
 134                  { title: yes_confirm, value: true },
 135                  { title: no_confirm, value: false }
 136              ],
 137              submit: function (e, v, m, f) {
 138                  if (v == true) {
 139                      Post.attachmentAction(aid, 'remove');
 140  
 141                      if (use_xmlhttprequest != 1) {
 142                          Post.form.append('<input type="submit" id="rem_submit" class="hidden" />');
 143                          $('#rem_submit').trigger('click');
 144                          return false;
 145                      }
 146  
 147                      $.ajax({
 148                          type: 'POST',
 149                          url: Post.form.attr('action') + '&ajax=1',
 150                          data: Post.form.serialize(),
 151                          success: function (data) {
 152                              if (data.hasOwnProperty("errors")) {
 153                                  $.each(data.errors, function (i, message) {
 154                                      $.jGrowl(message, { theme: 'jgrowl_error' });
 155                                  });
 156                                  return false;
 157                              } else if (data.success) {
 158                                  $('#attachment_' + aid).hide(500, function () {
 159                                      var instance = MyBBEditor;
 160                                      if (typeof MyBBEditor === 'undefined') {
 161                                          instance = $('#message').sceditor('instance');
 162                                      }
 163  
 164                                      if (instance.sourceMode()) {
 165                                          instance.setSourceEditorValue(instance.getSourceEditorValue(false).split('[attachment=' + aid + ']').join(''));
 166                                      } else {
 167                                          instance.setWysiwygEditorValue(instance.getWysiwygEditorValue(false).split('[attachment=' + aid + ']').join(''));
 168                                      }
 169  
 170                                      $(this).parent().find('.tcat>strong').text(data.usage);
 171                                      $(this).remove();
 172                                      Post.regenAttachbuttons();
 173                                  });
 174                              }
 175                              Post.attachmentAction('', '');
 176                          }
 177                      });
 178                  }
 179              }
 180          });
 181  
 182          return false;
 183      },
 184  
 185      attachmentAction: function (aid, action) {
 186          document.input.attachmentaid.value = aid;
 187          document.input.attachmentact.value = action;
 188      },
 189  
 190      getAttachments: function () {
 191          var attached = [];
 192          $('.attachment_filename').each(function () {
 193              attached.push($(this).text());
 194          });
 195          return attached;
 196      },
 197  
 198      getCommonFiles: function () {
 199          var files = Post.fileInput.prop('files');
 200          if (files.length) {
 201              var names = $.map(files, function (val) {
 202                  return val.name;
 203              });
 204              return $.grep(Post.getAttachments(), function (i) {
 205                  return $.inArray(i, names) > -1;
 206              });
 207          } else {
 208              return [];
 209          }
 210      },
 211  
 212      addAttachments: function () {
 213          Post.checkAttachments();
 214  
 215          if (Post.fileInput.prop('files').length) {
 216              var common = Post.getCommonFiles();
 217              if (common.length) {
 218                  var list = document.createElement('ul');
 219  
 220                  $.map(common, function (val) {
 221                      var e = document.createElement('li');
 222                      e.textContent = val;
 223                      list.append(e);
 224                  });
 225  
 226                  MyBB.prompt(lang.update_confirm.replace("{1}", list.outerHTML), {
 227                      buttons: [
 228                          { title: yes_confirm, value: true },
 229                          { title: no_confirm, value: false }
 230                      ],
 231                      submit: function (e, v, m, f) {
 232                          if (v == true) {
 233                              Post.form.append('<input type="hidden" class="temp_input" name="updateconfirmed" value="1" />');
 234                              Post.uploadAttachments('updateattachment');
 235                          }
 236                      }
 237                  });
 238              } else {
 239                  Post.uploadAttachments('newattachment');
 240              }
 241          }
 242          return false;
 243      },
 244  
 245      uploadAttachments: function (type) {
 246          if (use_xmlhttprequest != 1) {
 247              $("input[name='" + type + "']").trigger('click');
 248          } else {
 249              Post.form.append('<input type="hidden" class="temp_input" name="' + type + '" value="1" />');
 250              var formData = new FormData($(Post.form)[0]);
 251  
 252              $.ajax({
 253                  xhr: function () {
 254                      var x = $.ajaxSettings.xhr();
 255                      x.upload.addEventListener("progress", function (e) {
 256                          if (e.lengthComputable) {
 257                              var completed = parseFloat((e.loaded / e.total) * 100).toFixed(2);
 258                              $('#upload_bar').css('width', completed + '%');
 259                              Post.dropZone.find('div').text(completed + '%');
 260                              if (e.loaded === e.total) {
 261                                  $('#upload_bar').css('width', '0%');
 262                                  Post.dropZone.find('div').text(lang.drop_files);
 263                              }
 264                          }
 265                      }, false);
 266                      return x;
 267                  },
 268                  type: 'POST',
 269                  url: Post.form.attr('action') + '&ajax=1',
 270                  data: formData,
 271                  async: true,
 272                  cache: false,
 273                  contentType: false,
 274                  enctype: 'multipart/form-data',
 275                  processData: false,
 276                  success: function (data) {
 277                      if (data.hasOwnProperty("errors")) {
 278                          $.each(data.errors, function (i, message) {
 279                              $.jGrowl(message, { theme: 'jgrowl_error' });
 280                          });
 281                      }
 282                      // Append new attachment data
 283                      if (data.hasOwnProperty("success")) {
 284                          $.each(data.success, function (i, message) {
 285                              if ($('#attachment_' + message[0]).length) {
 286                                  $('#attachment_' + message[0]).remove();
 287                              }
 288                              Post.fileInput.parents().eq(2).append(data.template
 289                                  .replace(/\{1\}/g, message[0])
 290                                  .replace('{2}', message[1])
 291                                  .replace('{3}', message[2])
 292                                  .replace('{4}', message[3]))
 293                                  .find('.tcat>strong').text(data.usage);
 294                          });
 295                      }
 296  
 297                      Post.fileInput.val('');
 298                      Post.regenAttachbuttons();
 299                  }
 300              });
 301              $('.temp_input').remove();
 302          }
 303      },
 304  
 305      regenAttachbuttons: function () {
 306          var attachButton = $("input[name=newattachment], input[name=updateattachment]").eq(0).clone();
 307  
 308          if (Post.getAttachments().length) {
 309              if (!$('input[name=updateattachment]').length) {
 310                  var updateButton = attachButton.clone()
 311                      .prop('name', 'updateattachment')
 312                      .prop('value', lang.update_attachment)
 313                      .prop('tabindex', '12');
 314                  $("input[name='newattachment']").before(updateButton).before('&nbsp;');
 315              }
 316          } else {
 317              $('input[name=updateattachment]').remove();
 318          }
 319  
 320          if (Post.getAttachments().length < mybb_max_file_uploads) {
 321              if (!$('input[name=newattachment]').length) {
 322                  var newButton = attachButton.clone()
 323                      .prop('name', 'newattachment')
 324                      .prop('value', lang.add_attachment)
 325                      .prop('tabindex', '13');
 326                  $("input[name='updateattachment']").after(newButton).after('&nbsp;');
 327              }
 328          } else {
 329              $('input[name=newattachment]').remove();
 330          }
 331      },
 332  
 333      checkAttachments: function (e) {
 334          var submitter = ($.type(e) === 'undefined') ? '' : e.originalEvent.submitter.name;
 335          var file = Post.fileInput[0];
 336          if (!file) {
 337              return true;
 338          }
 339  
 340          if (!file.files.length && (submitter == 'newattachment' || submitter == 'updateattachment')) {
 341              $.jGrowl(lang.attachment_missing, { theme: 'jgrowl_error' });
 342              return false;
 343          }
 344  
 345          if (mybb_max_file_uploads != 0) {
 346              var common = Post.getCommonFiles().length;
 347              var moreAllowed = (mybb_max_file_uploads - (Post.getAttachments().length - common));
 348              if (moreAllowed < 0 || (!moreAllowed && file.files.length)) {
 349                  $.jGrowl(lang.error_maxattachpost.replace('{1}', mybb_max_file_uploads), { theme: 'jgrowl_error' });
 350                  file.value = '';
 351                  return false;
 352              } else if (file.files.length > moreAllowed) {
 353                  $.jGrowl(lang.attachment_max_allowed_files.replace('{1}', (moreAllowed - common)), { theme: 'jgrowl_error' });
 354                  file.value = '';
 355                  return false;
 356              }
 357          }
 358  
 359          if (file.files.length > php_max_file_uploads && php_max_file_uploads != 0) {
 360              $.jGrowl(lang.attachment_too_many_files.replace('{1}', php_max_file_uploads), { theme: 'jgrowl_error' });
 361              file.value = '';
 362              return false;
 363          }
 364  
 365          var totalSize = 0;
 366          Post.fileInput.each(function () {
 367              for (var i = 0; i < this.files.length; i++) {
 368                  totalSize += (this.files[i].size || this.files[i].fileSize);
 369              }
 370          });
 371  
 372          if (totalSize > php_max_upload_size && php_max_upload_size > 0) {
 373              var php_max_upload_size_pretty = Math.round(php_max_upload_size / 1e4) / 1e2;
 374              $.jGrowl(lang.attachment_too_big_upload.replace('{1}', php_max_upload_size_pretty), { theme: 'jgrowl_error' });
 375              file.value = '';
 376              return false;
 377          }
 378  
 379          return true;
 380      },
 381      fileInput: $(),
 382      dropZone: $(),
 383      form: $()
 384  };
 385  
 386  Post.init();


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref