Project

General

Profile

Download (2.21 KB) Statistics
| Branch: | Tag: | Revision:
1

    
2
/**
3
 * Attaches add more fields behaviour to any form.
4
 * Dynamic adding of fields requires:
5
 *   - a button to click to produce more fields (id="field-name-more")
6
 *   - a wrapper div around the set of fields to be duplicated (id="field-name-wrapper")
7
 *     additional fields will be prepended to the end of this fieldset
8
 *   - a hidden element counter with the current number of fields (id="field-name-count")
9
 *   - field-name should be replaced by a unique identifier for your field
10
 *   - a callback function which produces an additional field
11
 */
12
Drupal.linkAutoAttach = function() {
13
  $('input.more-email-addresses').each(function() {
14
    var uri = this.value;
15
    // Extract the base name from the id (my-text-field-url -> my-text-field).
16
    var base = this.id.substring(0, this.id.length - 9);
17
    var button = base + '-more';
18
    var wrapper = base + '-wrapper';
19
    var counter = base + '-count';
20
    var link = new Drupal.jslink(uri, button, wrapper, counter);
21
  });
22

    
23
  // set radioCheckbox group, assuming that jquery plugin for radiocheckboxes is loaded
24
  $.radioCheckboxGroup("", ".preferred-email");
25

    
26
}
27

    
28
/**
29
 * JS jslink object.
30
 */
31
Drupal.jslink = function(uri, button, wrapper, counter) {
32
  this.button = '#'+ button;
33
  this.wrapper = '#'+ wrapper;
34
  this.counter = '#'+ counter;
35
  Drupal.redirectFormButton(uri, $(this.button).get(0), this);
36
}
37

    
38
/**
39
 * Handler for the form redirection submission.
40
 */
41
Drupal.jslink.prototype.onsubmit = function() {
42
  // Increment count
43
  var count = parseInt($(this.counter).val());
44
  $(this.counter).val(count + 1);
45
}
46

    
47
/**
48
 * Handler for the form redirection completion.
49
 */
50
Drupal.jslink.prototype.oncomplete = function(data) {
51
  // Avoid unnecessary scrolling
52
  Drupal.freezeHeight();
53

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

    
58
  // Append to form and update behaviour
59
  $(div).hide();
60
  $(this.wrapper).append(div);
61
  $(div).slideDown('fast');
62
  Drupal.linkAutoAttach();
63

    
64
  Drupal.unfreezeHeight();
65
}
66

    
67
/**
68
 * Handler for the form redirection error.
69
 */
70
Drupal.jslink.prototype.onerror = function(error) {
71
  alert('An error occurred:\n\n'+ error);
72
}
73

    
74
// Global killswitch
75
if (Drupal.jsEnabled) {
76
  $(document).ready(Drupal.linkAutoAttach);
77
}
(22-22/47)