summaryrefslogtreecommitdiff
path: root/misc/upload.js
blob: d69f3b7656ca9a189ff6c65abd3c39e1d2e79e53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// $Id$

/**
 * Attaches the upload behaviour to the upload form.
 */
Drupal.behaviors.upload = function(context) {
  $('input.upload:not(.upload-processed)', context).addClass('upload-processed').each(function () {
    var uri = this.value;
    // Extract the base name from the id (edit-attach-url -> attach).
    var base = this.id.substring(5, this.id.length - 4);
    var button = base + '-button';
    var wrapper = base + '-wrapper';
    var hide = base + '-hide';
    var upload = new Drupal.jsUpload(uri, button, wrapper, hide);
    $(this).addClass('upload-processed');
  });
};

/**
 * JS upload object.
 */
Drupal.jsUpload = function(uri, button, wrapper, hide) {
  // Note: these elements are replaced after an upload, so we re-select them
  // everytime they are needed.
  this.button = '#'+ button;
  this.wrapper = '#'+ wrapper;
  this.hide = '#'+ hide;
  Drupal.redirectFormButton(uri, $(this.button).get(0), this);
};

/**
 * Handler for the form redirection submission.
 */
Drupal.jsUpload.prototype.onsubmit = function () {
  // Insert progressbar and stretch to take the same space.
  this.progress = new Drupal.progressBar('uploadprogress');
  this.progress.setProgress(-1, Drupal.t('Uploading file'));

  var hide = this.hide;
  var el = this.progress.element;
  var offset = $(hide).get(0).offsetHeight;
  $(el).css({
    width: '28em',
    height: offset +'px',
    paddingTop: '10px',
    display: 'none'
  });
  $(hide).css('position', 'absolute');

  $(hide).after(el);
  $(el).fadeIn('slow');
  $(hide).fadeOut('slow');
};

/**
 * Handler for the form redirection completion.
 */
Drupal.jsUpload.prototype.oncomplete = function (data) {
  // Remove old form
  Drupal.freezeHeight(); // Avoid unnecessary scrolling
  $(this.wrapper).html('');

  // Place HTML into temporary div
  var div = document.createElement('div');
  $(div).html(data);

  // If uploading the first attachment fade in everything
  if ($('tr', div).size() == 2) {
    // Replace form and re-attach behaviours
    $(div).hide();
    $(this.wrapper).append(div);
    $(div).fadeIn('slow');
  }
  // Else fade in only the last table row
  else {
    // Hide form and last table row
    $('table tr:last-of-type td', div).hide();

    // Note: workaround because jQuery's #id selector does not work outside of 'document'
    // Should be: $(this.hide, div).hide();
    var hide = this.hide;
    $('div', div).each(function() {
      if (('#'+ this.id) == hide) {
        this.style.display = 'none';
      }
    });

    // Replace form, fade in items and re-attach behaviour
    $(this.wrapper).append(div);
    $('table tr:last-of-type td', div).fadeIn('slow');
    $(this.hide, div).fadeIn('slow');
  }
  Drupal.attachBehaviors(div);
  Drupal.unfreezeHeight();
};

/**
 * Handler for the form redirection error.
 */
Drupal.jsUpload.prototype.onerror = function (error) {
  alert(Drupal.t('An error occurred:\n\n@error', { '@error': error }));
  // Remove progressbar
  $(this.progress.element).remove();
  this.progress = null;
  // Undo hide
  $(this.hide).css({
    position: 'static',
    left: '0px'
  });
};