[ Index ]

PHP Cross Reference of MyBB 1.8.27

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                  common = '<ul><li>' + common.join('</li><li>') + '</li></ul>';
 219                  MyBB.prompt(lang.update_confirm.replace("{1}", common), {
 220                      buttons: [
 221                          { title: yes_confirm, value: true },
 222                          { title: no_confirm, value: false }
 223                      ],
 224                      submit: function (e, v, m, f) {
 225                          if (v == true) {
 226                              Post.form.append('<input type="hidden" class="temp_input" name="updateconfirmed" value="1" />');
 227                              Post.uploadAttachments('updateattachment');
 228                          }
 229                      }
 230                  });
 231              } else {
 232                  Post.uploadAttachments('newattachment');
 233              }
 234          }
 235          return false;
 236      },
 237  
 238      uploadAttachments: function (type) {
 239          if (use_xmlhttprequest != 1) {
 240              $("input[name='" + type + "']").trigger('click');
 241          } else {
 242              Post.form.append('<input type="hidden" class="temp_input" name="' + type + '" value="1" />');
 243              var formData = new FormData($(Post.form)[0]);
 244  
 245              $.ajax({
 246                  xhr: function () {
 247                      var x = $.ajaxSettings.xhr();
 248                      x.upload.addEventListener("progress", function (e) {
 249                          if (e.lengthComputable) {
 250                              var completed = parseFloat((e.loaded / e.total) * 100).toFixed(2);
 251                              $('#upload_bar').css('width', completed + '%');
 252                              Post.dropZone.find('div').text(completed + '%');
 253                              if (e.loaded === e.total) {
 254                                  $('#upload_bar').css('width', '0%');
 255                                  Post.dropZone.find('div').text(lang.drop_files);
 256                              }
 257                          }
 258                      }, false);
 259                      return x;
 260                  },
 261                  type: 'POST',
 262                  url: Post.form.attr('action') + '&ajax=1',
 263                  data: formData,
 264                  async: true,
 265                  cache: false,
 266                  contentType: false,
 267                  enctype: 'multipart/form-data',
 268                  processData: false,
 269                  success: function (data) {
 270                      if (data.hasOwnProperty("errors")) {
 271                          $.each(data.errors, function (i, message) {
 272                              $.jGrowl(message, { theme: 'jgrowl_error' });
 273                          });
 274                      }
 275                      // Append new attachment data
 276                      if (data.hasOwnProperty("success")) {
 277                          $.each(data.success, function (i, message) {
 278                              if ($('#attachment_' + message[0]).length) {
 279                                  $('#attachment_' + message[0]).remove();
 280                              }
 281                              Post.fileInput.parents().eq(2).append(data.template
 282                                  .replace(/\{1\}/g, message[0])
 283                                  .replace('{2}', message[1])
 284                                  .replace('{3}', message[2])
 285                                  .replace('{4}', message[3]))
 286                                  .find('.tcat>strong').text(data.usage);
 287                          });
 288                      }
 289  
 290                      Post.fileInput.val('');
 291                      Post.regenAttachbuttons();
 292                  }
 293              });
 294              $('.temp_input').remove();
 295          }
 296      },
 297  
 298      regenAttachbuttons: function () {
 299          var attachButton = $("input[name=newattachment], input[name=updateattachment]").eq(0).clone();
 300  
 301          if (Post.getAttachments().length) {
 302              if (!$('input[name=updateattachment]').length) {
 303                  var updateButton = attachButton.clone()
 304                      .prop('name', 'updateattachment')
 305                      .prop('value', lang.update_attachment)
 306                      .prop('tabindex', '12');
 307                  $("input[name='newattachment']").before(updateButton).before('&nbsp;');
 308              }
 309          } else {
 310              $('input[name=updateattachment]').remove();
 311          }
 312  
 313          if (Post.getAttachments().length < mybb_max_file_uploads) {
 314              if (!$('input[name=newattachment]').length) {
 315                  var newButton = attachButton.clone()
 316                      .prop('name', 'newattachment')
 317                      .prop('value', lang.add_attachment)
 318                      .prop('tabindex', '13');
 319                  $("input[name='updateattachment']").after(newButton).after('&nbsp;');
 320              }
 321          } else {
 322              $('input[name=newattachment]').remove();
 323          }
 324      },
 325  
 326      checkAttachments: function (e) {
 327          var submitter = ($.type(e) === 'undefined') ? '' : e.originalEvent.submitter.name;
 328          var file = Post.fileInput[0];
 329          if (!file) {
 330              return true;
 331          }
 332  
 333          if (!file.files.length && (submitter == 'newattachment' || submitter == 'updateattachment')) {
 334              $.jGrowl(lang.attachment_missing, { theme: 'jgrowl_error' });
 335              return false;
 336          }
 337  
 338          if (mybb_max_file_uploads != 0) {
 339              var common = Post.getCommonFiles().length;
 340              var moreAllowed = (mybb_max_file_uploads - (Post.getAttachments().length - common));
 341              if (moreAllowed < 0 || (!moreAllowed && file.files.length)) {
 342                  $.jGrowl(lang.error_maxattachpost.replace('{1}', mybb_max_file_uploads), { theme: 'jgrowl_error' });
 343                  file.value = '';
 344                  return false;
 345              } else if (file.files.length > moreAllowed) {
 346                  $.jGrowl(lang.attachment_max_allowed_files.replace('{1}', (moreAllowed - common)), { theme: 'jgrowl_error' });
 347                  file.value = '';
 348                  return false;
 349              }
 350          }
 351  
 352          if (file.files.length > php_max_file_uploads && php_max_file_uploads != 0) {
 353              $.jGrowl(lang.attachment_too_many_files.replace('{1}', php_max_file_uploads), { theme: 'jgrowl_error' });
 354              file.value = '';
 355              return false;
 356          }
 357  
 358          var totalSize = 0;
 359          Post.fileInput.each(function () {
 360              for (var i = 0; i < this.files.length; i++) {
 361                  totalSize += (this.files[i].size || this.files[i].fileSize);
 362              }
 363          });
 364  
 365          if (totalSize > php_max_upload_size && php_max_upload_size > 0) {
 366              var php_max_upload_size_pretty = Math.round(php_max_upload_size / 1e4) / 1e2;
 367              $.jGrowl(lang.attachment_too_big_upload.replace('{1}', php_max_upload_size_pretty), { theme: 'jgrowl_error' });
 368              file.value = '';
 369              return false;
 370          }
 371  
 372          return true;
 373      },
 374      fileInput: $(),
 375      dropZone: $(),
 376      form: $()
 377  };
 378  
 379  Post.init();


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